ATLAS Offline Software
Loading...
Searching...
No Matches
LookUpTable.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef LOOKUPTABLE_H
6#define LOOKUPTABLE_H
7
8#include <algorithm>
9#include <functional>
10#include <iostream>
11#include <iterator>
12#include <vector>
13
20
21template <class T, class C = std::less<T> > class LookUpTable {
22public:
23 typedef T val_type;
24 typedef std::vector<val_type> vec_type;
25 typedef std::pair<val_type, val_type> pair_type;
26 // typename Data::iterator DataIt;
27 typedef typename vec_type::iterator vec_type_it;
28 typedef typename vec_type::const_iterator vec_type_cit;
29
30 LookUpTable() = default;
31 ~LookUpTable() = default;
32
33 bool empty() const { return m_data.empty(); }
34
35 void setData(const vec_type& data);
36 const vec_type& getData() const { return m_data; }
37
38 vec_type_it begin() { return m_data.begin(); }
39 vec_type_it end() { return m_data.end(); }
40 vec_type_cit begin() const { return m_data.begin(); }
41 vec_type_cit end() const { return m_data.end(); }
42
44 pair_type find(const val_type& val) const;
45
46 template <class CC> pair_type find(const val_type& val, const CC& c) const {
47 vec_type_cit it = lower_bound(m_data.begin(), m_data.end(), val, c);
48 if (it == m_data.end())
49 return std::make_pair(*(it - 2), *(it - 1));
50 else if (it == m_data.begin())
51 return std::make_pair(*it, *(it + 1));
52 else
53 return std::make_pair(*(it - 1), *it);
54 }
55
56private:
58};
59
60template <class T, class C> inline void LookUpTable<T, C>::setData(const vec_type& data) {
61 m_data.assign(data.begin(), data.end());
62 sort(m_data.begin(), m_data.end(), C());
63}
64
65template <class T, class C> inline typename LookUpTable<T, C>::vec_type_cit LookUpTable<T, C>::lowerBound(const val_type& val) {
66 vec_type_cit it = lower_bound(m_data.begin(), m_data.end(), val, C());
67 if (it == m_data.end()) return m_data.end() - 1;
68 return it;
69}
70
71template <class T, class C> inline typename LookUpTable<T, C>::pair_type LookUpTable<T, C>::find(const val_type& val) const {
72 vec_type_cit it = lower_bound(m_data.begin(), m_data.end(), val, C());
73 if (it == m_data.end())
74 return std::make_pair(*(it - 2), *(it - 1));
75 else if (it == m_data.begin())
76 return std::make_pair(*it, *(it + 1));
77 else
78 return std::make_pair(*(it - 1), *it);
79}
80
81#endif
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
vec_type_cit lowerBound(const val_type &val)
Definition LookUpTable.h:65
vec_type_cit end() const
Definition LookUpTable.h:41
bool empty() const
Definition LookUpTable.h:33
void setData(const vec_type &data)
Definition LookUpTable.h:60
vec_type_it end()
Definition LookUpTable.h:39
std::pair< val_type, val_type > pair_type
Definition LookUpTable.h:25
vec_type::const_iterator vec_type_cit
Definition LookUpTable.h:28
std::vector< val_type > vec_type
Definition LookUpTable.h:24
vec_type::iterator vec_type_it
Definition LookUpTable.h:27
pair_type find(const val_type &val) const
Definition LookUpTable.h:71
LookUpTable()=default
const vec_type & getData() const
Definition LookUpTable.h:36
~LookUpTable()=default
vec_type_it begin()
Definition LookUpTable.h:38
pair_type find(const val_type &val, const CC &c) const
Definition LookUpTable.h:46
vec_type m_data
Definition LookUpTable.h:57
vec_type_cit begin() const
Definition LookUpTable.h:40
struct color C