Belle II Software  release-08-01-10
KeyValBox.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 // stl:
11 #include <vector>
12 #include <utility> // for pair
13 #include <algorithm> // for find_if
14 
15 
16 namespace Belle2 {
23  template<class KeyType, class ValueType>
24  class KeyValBox {
25  protected:
26 
28  std::vector<std::pair<KeyType, ValueType> > m_container;
29 
30  public:
31 
33  using BoxEntry = std::pair<KeyType, ValueType>;
34 
35 
37  using Iterator = typename std::vector<BoxEntry>::iterator;
38 
39 
41  using ConstIterator = typename std::vector<BoxEntry>::const_iterator;
42 
43 
45  ValueType* find(const KeyType& aKey)
46  {
47  Iterator foundPos = std::find_if(
48  m_container.begin(),
49  m_container.end(),
50  [&](const BoxEntry & entry) -> bool
51  { return entry.first == aKey; }
52  );
53  return (foundPos == m_container.end() ? nullptr : &foundPos->second);
54  }
55 
56 
58  void push_back(std::pair<KeyType, ValueType>& newPair) { m_container.push_back(newPair); }
59 
60 
62  void push_back(std::pair<KeyType, ValueType> newPair) { m_container.push_back(newPair); }
63 
64 
66  Iterator begin() { return m_container.begin(); }
67 
68 
70  ConstIterator begin() const { return m_container.begin(); }
71 
72 
74  Iterator end() { return m_container.end(); }
75 
76 
78  ConstIterator end() const { return m_container.end(); }
79 
80 
82  unsigned int size() const { return m_container.size(); }
83  };
84 
86 }
Minimal container storing a pair of < KeyType, ValueType>
Definition: KeyValBox.h:24
void push_back(std::pair< KeyType, ValueType > newPair)
push_back for new pair given
Definition: KeyValBox.h:62
Iterator begin()
returns iterator for container: begin
Definition: KeyValBox.h:66
std::pair< KeyType, ValueType > BoxEntry
typedef for readable entry-type
Definition: KeyValBox.h:33
ValueType * find(const KeyType &aKey)
for given key a pointer to the value is returned.
Definition: KeyValBox.h:45
unsigned int size() const
returns number of entries in container:
Definition: KeyValBox.h:82
ConstIterator end() const
returns iterator for container: end
Definition: KeyValBox.h:78
typename std::vector< BoxEntry >::iterator Iterator
typedef for more readable iterator-type
Definition: KeyValBox.h:37
Iterator end()
returns iterator for container: end
Definition: KeyValBox.h:74
ConstIterator begin() const
returns iterator for container: begin
Definition: KeyValBox.h:70
void push_back(std::pair< KeyType, ValueType > &newPair)
push_back for new pair given
Definition: KeyValBox.h:58
std::vector< std::pair< KeyType, ValueType > > m_container
the container containing the keys and values
Definition: KeyValBox.h:28
typename std::vector< BoxEntry >::const_iterator ConstIterator
typedef for more readable iterator-type
Definition: KeyValBox.h:41
Abstract base class for different kinds of events.