1ac3d0c65SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3ac3d0c65SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4ac3d0c65SAndrew Rist * or more contributor license agreements. See the NOTICE file
5ac3d0c65SAndrew Rist * distributed with this work for additional information
6ac3d0c65SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7ac3d0c65SAndrew Rist * to you under the Apache License, Version 2.0 (the
8ac3d0c65SAndrew Rist * "License"); you may not use this file except in compliance
9ac3d0c65SAndrew Rist * with the License. You may obtain a copy of the License at
10ac3d0c65SAndrew Rist *
11ac3d0c65SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12ac3d0c65SAndrew Rist *
13ac3d0c65SAndrew Rist * Unless required by applicable law or agreed to in writing,
14ac3d0c65SAndrew Rist * software distributed under the License is distributed on an
15ac3d0c65SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ac3d0c65SAndrew Rist * KIND, either express or implied. See the License for the
17ac3d0c65SAndrew Rist * specific language governing permissions and limitations
18ac3d0c65SAndrew Rist * under the License.
19ac3d0c65SAndrew Rist *
20ac3d0c65SAndrew Rist *************************************************************/
21ac3d0c65SAndrew Rist
22ac3d0c65SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef CSV_TPLTOOLS_HXX
25cdf0e10cSrcweir #define CSV_TPLTOOLS_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir #include <map>
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir
32cdf0e10cSrcweir
33cdf0e10cSrcweir namespace csv
34cdf0e10cSrcweir {
35cdf0e10cSrcweir
36cdf0e10cSrcweir
37cdf0e10cSrcweir template <class COLLECTION>
38cdf0e10cSrcweir inline void erase_container(
39cdf0e10cSrcweir COLLECTION & o_rCollection );
40cdf0e10cSrcweir
41cdf0e10cSrcweir /// Version for std::map
42cdf0e10cSrcweir template <class COLLECTION>
43cdf0e10cSrcweir void erase_map_of_heap_ptrs(
44cdf0e10cSrcweir COLLECTION & o_rCollection );
45cdf0e10cSrcweir
46cdf0e10cSrcweir /// Version for other containers than std::map, with non-pair value_type.
47cdf0e10cSrcweir template <class COLLECTION>
48cdf0e10cSrcweir void erase_container_of_heap_ptrs(
49cdf0e10cSrcweir COLLECTION & o_rCollection );
50cdf0e10cSrcweir
51cdf0e10cSrcweir template <class VECTOR_ELEM>
52cdf0e10cSrcweir void adjust_vector_size(
53cdf0e10cSrcweir std::vector<VECTOR_ELEM> &
54cdf0e10cSrcweir io_rVector,
55cdf0e10cSrcweir uintt i_nSize,
56cdf0e10cSrcweir const VECTOR_ELEM & i_nFill );
57cdf0e10cSrcweir
58cdf0e10cSrcweir
59cdf0e10cSrcweir template <class KEY, class VAL>
60cdf0e10cSrcweir const VAL * find_in_map( /// Usable for all kinds of values
61cdf0e10cSrcweir const std::map< KEY, VAL > &
62cdf0e10cSrcweir i_rMap,
63cdf0e10cSrcweir const KEY & i_rKey );
64cdf0e10cSrcweir
65cdf0e10cSrcweir
66cdf0e10cSrcweir /** @return the value in the map, if it is in there, else 0.
67cdf0e10cSrcweir @precond VAL has to be convertable to "0".
68cdf0e10cSrcweir */
69cdf0e10cSrcweir template <class KEY, class VAL>
70cdf0e10cSrcweir VAL value_from_map(
71cdf0e10cSrcweir const std::map< KEY, VAL > &
72cdf0e10cSrcweir i_rMap,
73cdf0e10cSrcweir const KEY & i_rKey );
74cdf0e10cSrcweir
75cdf0e10cSrcweir /** @return the value in the map, if it is in there, else i_notFound.
76cdf0e10cSrcweir */
77cdf0e10cSrcweir template <class KEY, class VAL>
78cdf0e10cSrcweir VAL value_from_map(
79cdf0e10cSrcweir const std::map< KEY, VAL > &
80cdf0e10cSrcweir i_rMap,
81cdf0e10cSrcweir const KEY & i_rKey,
82cdf0e10cSrcweir VAL i_notFound );
83cdf0e10cSrcweir
84cdf0e10cSrcweir template <class COLLECTION, class VALUE>
85cdf0e10cSrcweir bool contains(
86cdf0e10cSrcweir const COLLECTION & i_collection,
87cdf0e10cSrcweir const VALUE & i_value );
88cdf0e10cSrcweir
89cdf0e10cSrcweir // Object oriented for_each:
90cdf0e10cSrcweir template <class COLLECTION, class CLASS, class MEMFUNC>
91cdf0e10cSrcweir void call_for_each(
92cdf0e10cSrcweir const COLLECTION & i_rList,
93cdf0e10cSrcweir CLASS * io_pThis,
94cdf0e10cSrcweir MEMFUNC i_fMethod );
95cdf0e10cSrcweir
96cdf0e10cSrcweir
97cdf0e10cSrcweir
98cdf0e10cSrcweir
99cdf0e10cSrcweir // IMPLEMENTATION
100cdf0e10cSrcweir template <class COLLECTION>
101cdf0e10cSrcweir inline void
erase_container(COLLECTION & o_rCollection)102cdf0e10cSrcweir erase_container( COLLECTION & o_rCollection )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir o_rCollection.erase( o_rCollection.begin(),
105cdf0e10cSrcweir o_rCollection.end() );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
108cdf0e10cSrcweir template <class COLLECTION>
109cdf0e10cSrcweir void
erase_map_of_heap_ptrs(COLLECTION & o_rCollection)110cdf0e10cSrcweir erase_map_of_heap_ptrs( COLLECTION & o_rCollection )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir typename COLLECTION::iterator itEnd = o_rCollection.end();
113cdf0e10cSrcweir for ( typename COLLECTION::iterator it = o_rCollection.begin();
114cdf0e10cSrcweir it != itEnd;
115cdf0e10cSrcweir ++it )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir delete (*it).second;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir o_rCollection.erase( o_rCollection.begin(),
121cdf0e10cSrcweir o_rCollection.end() );
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir template <class COLLECTION>
125cdf0e10cSrcweir void
erase_container_of_heap_ptrs(COLLECTION & o_rCollection)126cdf0e10cSrcweir erase_container_of_heap_ptrs( COLLECTION & o_rCollection )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir typename COLLECTION::iterator itEnd = o_rCollection.end();
129cdf0e10cSrcweir for ( typename COLLECTION::iterator it = o_rCollection.begin();
130cdf0e10cSrcweir it != itEnd;
131cdf0e10cSrcweir ++it )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir delete *it;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
136cdf0e10cSrcweir o_rCollection.erase( o_rCollection.begin(),
137cdf0e10cSrcweir o_rCollection.end() );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir template <class VECTOR_ELEM>
141cdf0e10cSrcweir void
adjust_vector_size(std::vector<VECTOR_ELEM> & io_rVector,uintt i_nSize,const VECTOR_ELEM & i_nFill)142cdf0e10cSrcweir adjust_vector_size( std::vector<VECTOR_ELEM> & io_rVector,
143cdf0e10cSrcweir uintt i_nSize,
144cdf0e10cSrcweir const VECTOR_ELEM & i_nFill )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir if ( io_rVector.size() > i_nSize )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir io_rVector.erase( io_rVector.begin() + i_nSize, io_rVector.end() );
149cdf0e10cSrcweir }
150cdf0e10cSrcweir else
151cdf0e10cSrcweir {
152cdf0e10cSrcweir io_rVector.reserve(i_nSize);
153cdf0e10cSrcweir while ( io_rVector.size() < i_nSize )
154cdf0e10cSrcweir io_rVector.push_back(i_nFill);
155cdf0e10cSrcweir }
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir
159cdf0e10cSrcweir template <class KEY, class VAL>
160cdf0e10cSrcweir const VAL *
find_in_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey)161cdf0e10cSrcweir find_in_map( const std::map< KEY, VAL > & i_rMap,
162cdf0e10cSrcweir const KEY & i_rKey )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir typename std::map< KEY, VAL >::const_iterator
165cdf0e10cSrcweir ret = i_rMap.find(i_rKey);
166cdf0e10cSrcweir return ret != i_rMap.end()
167cdf0e10cSrcweir ? & (*ret).second
168cdf0e10cSrcweir : (const VAL*)0;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir template <class KEY, class VAL>
172cdf0e10cSrcweir VAL
value_from_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey)173cdf0e10cSrcweir value_from_map( const std::map< KEY, VAL > & i_rMap,
174cdf0e10cSrcweir const KEY & i_rKey )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir typename std::map< KEY, VAL >::const_iterator
177cdf0e10cSrcweir ret = i_rMap.find(i_rKey);
178cdf0e10cSrcweir return ret != i_rMap.end()
179cdf0e10cSrcweir ? (*ret).second
180cdf0e10cSrcweir : VAL(0);
181cdf0e10cSrcweir }
182cdf0e10cSrcweir
183cdf0e10cSrcweir template <class KEY, class VAL>
184cdf0e10cSrcweir VAL
value_from_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey,VAL i_notFound)185cdf0e10cSrcweir value_from_map( const std::map< KEY, VAL > & i_rMap,
186cdf0e10cSrcweir const KEY & i_rKey,
187cdf0e10cSrcweir VAL i_notFound )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir typename std::map< KEY, VAL >::const_iterator
190cdf0e10cSrcweir ret = i_rMap.find(i_rKey);
191cdf0e10cSrcweir return ret != i_rMap.end()
192cdf0e10cSrcweir ? (*ret).second
193cdf0e10cSrcweir : i_notFound;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
196cdf0e10cSrcweir template <class COLLECTION, class VALUE>
197cdf0e10cSrcweir bool
contains(const COLLECTION & i_collection,const VALUE & i_value)198cdf0e10cSrcweir contains( const COLLECTION & i_collection,
199cdf0e10cSrcweir const VALUE & i_value )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir return std::find(i_collection.begin(), i_collection.end(), i_value)
202cdf0e10cSrcweir !=
203cdf0e10cSrcweir i_collection.end();
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir template <class COLLECTION, class CLASS, class MEMFUNC>
207cdf0e10cSrcweir void
call_for_each(const COLLECTION & i_rList,CLASS * io_pThis,MEMFUNC i_fMethod)208cdf0e10cSrcweir call_for_each( const COLLECTION & i_rList,
209cdf0e10cSrcweir CLASS * io_pThis,
210cdf0e10cSrcweir MEMFUNC i_fMethod )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir typename COLLECTION::const_iterator it = i_rList.begin();
213cdf0e10cSrcweir typename COLLECTION::const_iterator itEnd = i_rList.end();
214cdf0e10cSrcweir for ( ; it != itEnd; ++it )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir (io_pThis->*i_fMethod)(*it);
217cdf0e10cSrcweir }
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir
221cdf0e10cSrcweir
222cdf0e10cSrcweir
223cdf0e10cSrcweir } // namespace csv
224cdf0e10cSrcweir #endif
225