ATLAS Offline Software
Loading...
Searching...
No Matches
ArrayStore.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TRTCONDITIONSDATA_ARRAYSTORE_H
6#define TRTCONDITIONSDATA_ARRAYSTORE_H
7/********************************************************************
8
9NAME:
10PACKAGE:
11
12AUTHORS: Jorgen Beck Hansen
13CREATED: 1/8/05
14
15PURPOSE: Access and manipulate an indexed storage
16
17
18********************************************************************/
19
20// INCLUDE HEADER FILES:
21
22#include <vector>
23#include <map>
24#include <string>
25#include <iostream>
26
31template <typename IdType = int, typename StorageType = std::vector<float> >
33public:
34
35 typedef std::less<IdType> lessp;
36 typedef std::map<IdType,int,lessp> BufferMap;
37 typedef typename BufferMap::const_iterator map_citr;
38 typedef typename BufferMap::iterator map_itr;
39
42 ArrayStore(const std::string& tag);
44 virtual ~ArrayStore();
45
47 const std::string& tag() const;
51 const StorageType& operator[](const IdType& ident) const;
53 bool sharedID(const IdType& ident);
55 bool existID(const IdType& ident);
57 void dbg() const;
60 map_citr cend() const;
61
63 StorageType& operator[](const IdType& ident);
65 void share(const IdType& ident, const IdType& referenceIdent);
67 void push_back(const IdType& ident, const StorageType& value);
69 void removeID(const IdType& ident);
71 void clear();
73 void cleanUp();
77
78private:
79
80std::string m_tag;
82std::vector<StorageType> m_buf;
83
84};
85
86// inline methods
87template <typename IdType, typename StorageType>
89
90template <typename IdType, typename StorageType>
92
93template <typename IdType, typename StorageType>
95
96template <typename IdType, typename StorageType>
97inline void ArrayStore<IdType, StorageType>::clear() {m_bufmap.clear(); m_buf.clear(); }
98
99template <typename IdType, typename StorageType>
100inline void ArrayStore<IdType, StorageType>::push_back(const IdType& ident, const StorageType& value){
101 m_buf.push_back(value);
102 m_bufmap[ident]= m_buf.size()-1;
103}
104
105template <typename IdType, typename StorageType>
107
108template <typename IdType, typename StorageType>
109inline void ArrayStore<IdType, StorageType>::removeID(const IdType& ident) { m_bufmap.erase(ident); }
110
111template <typename IdType, typename StorageType>
112inline bool ArrayStore<IdType, StorageType>::existID(const IdType& ident) { return m_bufmap.find(ident)!=m_bufmap.end(); }
113
114template <typename IdType, typename StorageType>
115inline bool ArrayStore<IdType, StorageType>::sharedID(const IdType& ident) { return existID(ident)? m_bufmap.find(ident)->second <0 : false; }
116
117template <typename IdType, typename StorageType>
118inline void ArrayStore<IdType, StorageType>::dbg() const {std::cout << "dbg:" << m_bufmap.size() << " " << m_buf.size() << std::endl;}
119
120template <typename IdType, typename StorageType>
121inline const StorageType& ArrayStore<IdType, StorageType>::operator[](const IdType& ident) const {return m_buf[std::abs(m_bufmap.find(ident)->second)]; }
122
123template <typename IdType, typename StorageType>
124inline StorageType& ArrayStore<IdType, StorageType>::operator[](const IdType& ident) {return m_buf[std::abs(m_bufmap[ident])]; }
125
126template <typename IdType, typename StorageType>
127inline void ArrayStore<IdType, StorageType>::share(const IdType& ident, const IdType& referenceIdent) {
128 if (existID(referenceIdent)){
129 m_bufmap[ident] = -std::abs(m_bufmap[referenceIdent]);
130 m_bufmap[referenceIdent] = -std::abs(m_bufmap[referenceIdent]);
131 }
132}
133
134template <typename IdType, typename StorageType>
135inline const std::string& ArrayStore<IdType, StorageType>::tag() const {return m_tag;}
136
137template <typename IdType, typename StorageType>
139
140template <typename IdType, typename StorageType>
142
143template <typename IdType, typename StorageType>
145
146template <typename IdType, typename StorageType>
148
149#ifndef TRTCONDITIONSDATA_ARRAYSTORE_ICC
150#define TRTCONDITIONSDATA_ARRAYSTORE_ICC
151
152template <typename IdType, typename StorageType>
154 std::multimap<int,IdType> indexmap;
155
156 map_citr itr=m_bufmap.begin();
157 for (;itr!=m_bufmap.end();++itr)
158 indexmap.insert(std::pair<int,IdType>(std::abs(itr->second),itr->first));
159 std::vector<StorageType> tmp(m_buf);
160 dbg();
161 this->clear();
162 int i(-tmp.size());
163 for (;i<(int)tmp.size();i++){
164 bool first(true);
165 IdType shared_id;
166 typename std::multimap<int,IdType>::iterator pos;
167 for ( pos=indexmap.lower_bound(i); pos!=indexmap.upper_bound(i); ++pos){
168 if (first){
169 first=false;
170 shared_id=pos->second;
171 this->push_back(shared_id,tmp[std::abs(i)]);
172 } else
173 this->share(pos->second,shared_id);
174 }
175 }
176}
177#endif
178
179#endif
void cleanUp()
remove data with no index pointing to it
Definition ArrayStore.h:153
BufferMap m_bufmap
The std::map.
Definition ArrayStore.h:81
void dbg() const
print method
Definition ArrayStore.h:118
map_citr cbegin() const
begin/end const iterators
Definition ArrayStore.h:144
const StorageType & operator[](const IdType &ident) const
Overload [] const access.
Definition ArrayStore.h:121
bool sharedID(const IdType &ident)
returns flag for index sharing data
Definition ArrayStore.h:115
std::less< IdType > lessp
Definition ArrayStore.h:35
void push_back(const IdType &ident, const StorageType &value)
makes new index
Definition ArrayStore.h:100
std::string m_tag
The tag std::string.
Definition ArrayStore.h:80
void clear()
remove all indices
Definition ArrayStore.h:97
map_itr end()
Definition ArrayStore.h:141
std::map< IdType, int, lessp > BufferMap
Definition ArrayStore.h:36
void share(const IdType &ident, const IdType &referenceIdent)
makes index ident share data with referenceIdent
Definition ArrayStore.h:127
std::vector< StorageType > m_buf
The object stored in each second element.
Definition ArrayStore.h:82
void removeID(const IdType &ident)
remove index
Definition ArrayStore.h:109
ArrayStore()
constructors, optionally with a tag
Definition ArrayStore.h:88
virtual ~ArrayStore()
destructor
Definition ArrayStore.h:94
bool existID(const IdType &ident)
returns flag for index existing in the std::map
Definition ArrayStore.h:112
BufferMap::iterator map_itr
Definition ArrayStore.h:38
BufferMap::const_iterator map_citr
Definition ArrayStore.h:37
int numberOfIDs()
Size accessor.
Definition ArrayStore.h:106
map_itr begin()
begin/end iterators
Definition ArrayStore.h:138
map_citr cend() const
Definition ArrayStore.h:147