Belle II Software  release-05-02-19
KeyValBox.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 // stl:
13 #include <vector>
14 #include <utility> // for pair
15 #include <algorithm> // for find_if
16 
17 
18 namespace Belle2 {
25  template<class KeyType, class ValueType>
26  class KeyValBox {
27  protected:
28 
30  std::vector<std::pair<KeyType, ValueType> > m_container;
31 
32  public:
33 
35  using BoxEntry = std::pair<KeyType, ValueType>;
36 
37 
39  using Iterator = typename std::vector<BoxEntry>::iterator;
40 
41 
43  using ConstIterator = typename std::vector<BoxEntry>::const_iterator;
44 
45 
47  ValueType* find(const KeyType& aKey)
48  {
49  Iterator foundPos = std::find_if(
50  m_container.begin(),
51  m_container.end(),
52  [&](const BoxEntry & entry) -> bool
53  { return entry.first == aKey; }
54  );
55  return (foundPos == m_container.end() ? nullptr : &foundPos->second);
56  }
57 
58 
60  void push_back(std::pair<KeyType, ValueType>& newPair) { m_container.push_back(newPair); }
61 
62 
64  void push_back(std::pair<KeyType, ValueType> newPair) { m_container.push_back(newPair); }
65 
66 
68  Iterator begin() { return m_container.begin(); }
69 
70 
72  ConstIterator begin() const { return m_container.begin(); }
73 
74 
76  Iterator end() { return m_container.end(); }
77 
78 
80  ConstIterator end() const { return m_container.end(); }
81 
82 
84  unsigned int size() const { return m_container.size(); }
85  };
86 
88 }
Belle2::KeyValBox::end
Iterator end()
returns iterator for container: end
Definition: KeyValBox.h:84
Belle2::KeyValBox::m_container
std::vector< std::pair< KeyType, ValueType > > m_container
the container containing the keys and values
Definition: KeyValBox.h:38
Belle2::KeyValBox::Iterator
typename std::vector< BoxEntry >::iterator Iterator
typedef for more readable iterator-type
Definition: KeyValBox.h:47
Belle2::KeyValBox< TTree * >::ConstIterator
typename std::vector< BoxEntry >::const_iterator ConstIterator
typedef for more readable iterator-type
Definition: KeyValBox.h:51
Belle2::KeyValBox::find
ValueType * find(const KeyType &aKey)
for given key a pointer to the value is returned.
Definition: KeyValBox.h:55
Belle2::KeyValBox::BoxEntry
std::pair< KeyType, ValueType > BoxEntry
typedef for readable entry-type
Definition: KeyValBox.h:43
Belle2::KeyValBox::size
unsigned int size() const
returns number of entries in container:
Definition: KeyValBox.h:92
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::KeyValBox::push_back
void push_back(std::pair< KeyType, ValueType > &newPair)
push_back for new pair given
Definition: KeyValBox.h:68
Belle2::KeyValBox::begin
Iterator begin()
returns iterator for container: begin
Definition: KeyValBox.h:76