NCBI C++ ToolKit
static_map.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL___STATIC_MAP__HPP
2 #define UTIL___STATIC_MAP__HPP
3 
4 /* $Id: static_map.hpp 53965 2012-04-30 14:12:22Z vasilche $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio, Eugene Vasilchenko
30  *
31  * File Description:
32  * CStaticArrayMap<> -- template class to provide convenient access to
33  * a statically-defined array, while making sure that
34  * the order of the array meets sort criteria in
35  * debug builds.
36  *
37  */
38 
39 
40 #include <util/static_set.hpp>
41 
42 
44 
45 
46 ///
47 /// class CStaticArrayMap<> is an array adaptor that provides an STLish
48 /// interface to statically-defined arrays, while making efficient use
49 /// of the inherent sort order of such arrays.
50 ///
51 /// This class can be used both to verify sorted order of a static array
52 /// and to access a static array cleanly. The template parameters are
53 /// as follows:
54 ///
55 /// KeyType -- type of key object used for access
56 /// ValueType -- type of object used for access
57 /// KeyCompare -- comparison functor. This must provide an operator().
58 /// This is patterned to accept PCase and PNocase and similar objects.
59 ///
60 /// To use this class, define your static array as follows:
61 ///
62 /// static const char* sc_MyArray[] = {
63 /// "val1",
64 /// "val2",
65 /// "val3"
66 /// };
67 ///
68 /// Then, declare a static variable such as:
69 ///
70 /// typedef StaticArraySet<const char*, PNocase_CStr> TStaticArray;
71 /// static TStaticArray sc_Array(sc_MyArray, sizeof(sc_MyArray));
72 ///
73 /// In debug mode, the constructor will scan the list of items and insure
74 /// that they are in the sort order defined by the comparator used. If the
75 /// sort order is not correct, then the constructor will ASSERT().
76 ///
77 /// This can then be accessed as
78 ///
79 /// if (sc_Array.find(some_value) != sc_Array.end()) {
80 /// ...
81 /// }
82 ///
83 /// or
84 ///
85 /// size_t idx = sc_Array.index_of(some_value);
86 /// if (idx != TStaticArray::eNpos) {
87 /// ...
88 /// }
89 ///
90 ///
91 
92 
93 ///
94 /// class CStaticPairArrayMap<> provides access to a static array of pairs
95 /// in much the same way as CStaticArraySet<>, except that it provides
96 /// binding of a value type to each sorted key, much like an STL map<> would.
97 /// Its first template parameter must satisfy STL pair<> requirements:
98 /// 1. it must define first_type and second_type typedefs,
99 /// 2. it must have two data members: first and second.
100 ///
101 
102 template <class KeyType, class ValueType, class KeyCompare = less<KeyType> >
104  : public CStaticArraySearchBase<NStaticArray::PKeyValuePair< SStaticPair<KeyType, ValueType> >, KeyCompare>
105 {
107 public:
108  typedef typename TBase::value_type value_type;
110  typedef typename TBase::size_type size_type;
111  typedef typename TBase::key_compare key_compare;
112 
113  /// default constructor. This will build a map around a given array; the
114  /// storage of the end pointer is based on the supplied array size. In
115  /// debug mode, this will verify that the array is sorted.
116  template<size_t Size>
118  const char* file, int line,
120  : TBase(arr, file, line, warn)
121  {
122  }
123 
124  /// Constructor to initialize comparator object.
125  template<size_t Size>
127  const key_compare& comp,
128  const char* file, int line,
130  : TBase(arr, comp, file, line, warn)
131  {
132  }
133 
134  /// default constructor. This will build a map around a given array; the
135  /// storage of the end pointer is based on the supplied array size. In
136  /// debug mode, this will verify that the array is sorted.
137  template<class Type>
138  CStaticPairArrayMap(const Type* array_ptr, size_t array_size,
139  const char* file, int line,
141  : TBase(array_ptr, array_size, file, line, warn)
142  {
143  }
144 
145  /// Constructor to initialize comparator object.
146  template<class Type>
147  CStaticPairArrayMap(const Type* array_ptr, size_t array_size,
148  const key_compare& comp,
149  const char* file, int line,
151  : TBase(array_ptr, array_size, comp, file, line, warn)
152  {
153  }
154 
157  size_type array_size));
158 
161  size_type array_size,
162  const key_compare& comp));
163 };
164 
165 
166 ///
167 /// class CStaticArrayMap<> provides access to a static array in much the
168 /// same way as CStaticArraySet<>, except that it provides arbitrary
169 /// binding of a value type to each sorted key, much like an STL map<> would.
170 ///
171 
172 template <class KeyType, class ValueType, class KeyCompare = less<KeyType> >
174  : public CStaticArraySearchBase<NStaticArray::PKeyValuePair< pair<KeyType, ValueType> >, KeyCompare>
175 {
177 public:
178  typedef typename TBase::value_type value_type;
180  typedef typename TBase::size_type size_type;
181  typedef typename TBase::key_compare key_compare;
182 
183  /// default constructor. This will build a map around a given array; the
184  /// storage of the end pointer is based on the supplied array size. In
185  /// debug mode, this will verify that the array is sorted.
186  template<size_t Size>
188  const char* file, int line,
190  : TBase(arr, file, line, warn)
191  {
192  }
193 
194  /// Constructor to initialize comparator object.
195  template<size_t Size>
197  const key_compare& comp,
198  const char* file, int line,
200  : TBase(arr, comp, file, line, warn)
201  {
202  }
203 
204  /// default constructor. This will build a map around a given array; the
205  /// storage of the end pointer is based on the supplied array size. In
206  /// debug mode, this will verify that the array is sorted.
207  template<class Type>
208  CStaticArrayMap(const Type* array_ptr, size_t array_size,
209  const char* file, int line,
211  : TBase(array_ptr, array_size, file, line, warn)
212  {
213  }
214 
215  /// Constructor to initialize comparator object.
216  template<class Type>
217  CStaticArrayMap(const Type* array_ptr, size_t array_size,
218  const key_compare& comp,
219  const char* file, int line,
221  : TBase(array_ptr, array_size, comp, file, line, warn)
222  {
223  }
224 
227  size_type array_size));
228 
231  size_type array_size,
232  const key_compare& comp));
233 };
234 
235 
236 // Deprecated constructors (defined here to avoid GCC 3.3 parse errors)
237 
238 
239 template <class KeyType, class ValueType, class KeyCompare>
240 inline
242 (const_iterator obj,
243  size_type array_size)
244  : TBase(obj, array_size, 0, 0, NStaticArray::eCopyWarn_default)
245 {
246 }
247 
248 template <class KeyType, class ValueType, class KeyCompare>
249 inline
251 (const_iterator obj,
252  size_type array_size,
253  const key_compare& comp)
254  : TBase(obj, array_size, comp, 0, 0, NStaticArray::eCopyWarn_default)
255 {
256 }
257 
258 template <class KeyType, class ValueType, class KeyCompare>
259 inline
261 (const_iterator obj,
262  size_type array_size)
263  : TBase(obj, array_size, 0, 0, NStaticArray::eCopyWarn_default)
264 {
265 }
266 
267 template <class KeyType, class ValueType, class KeyCompare>
268 inline
270 (const_iterator obj,
271  size_type array_size,
272  const key_compare& comp)
273  : TBase(obj, array_size, comp, 0, 0, NStaticArray::eCopyWarn_default)
274 {
275 }
276 
278 
279 #endif // UTIL___STATIC_MAP__HPP
class CStaticArrayMap<> provides access to a static array in much the same way as CStaticArraySet<>,...
Definition: static_map.hpp:175
TBase::const_iterator const_iterator
Definition: static_map.hpp:179
CStaticArrayMap(const value_type(&arr)[Size], const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
default constructor.
Definition: static_map.hpp:187
CStaticArrayMap(const value_type(&arr)[Size], const key_compare &comp, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
Constructor to initialize comparator object.
Definition: static_map.hpp:196
CStaticArrayMap(const Type *array_ptr, size_t array_size, const key_compare &comp, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
Constructor to initialize comparator object.
Definition: static_map.hpp:217
CStaticArrayMap(const Type *array_ptr, size_t array_size, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
default constructor.
Definition: static_map.hpp:208
TBase::key_compare key_compare
Definition: static_map.hpp:181
CStaticArraySearchBase< NStaticArray::PKeyValuePair< pair< KeyType, ValueType > >, KeyCompare > TBase
Definition: static_map.hpp:176
TBase::value_type value_type
Definition: static_map.hpp:178
TBase::size_type size_type
Definition: static_map.hpp:180
class CStaticArraySet<> is an array adaptor that provides an STLish interface to statically-defined a...
Definition: static_set.hpp:561
getter::value_type value_type
Definition: static_set.hpp:568
const value_type * const_iterator
Definition: static_set.hpp:574
class CStaticArrayMap<> is an array adaptor that provides an STLish interface to statically-defined a...
Definition: static_map.hpp:105
CStaticPairArrayMap(const Type *array_ptr, size_t array_size, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
default constructor.
Definition: static_map.hpp:138
TBase::size_type size_type
Definition: static_map.hpp:110
CStaticPairArrayMap(const value_type(&arr)[Size], const key_compare &comp, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
Constructor to initialize comparator object.
Definition: static_map.hpp:126
CStaticPairArrayMap(const Type *array_ptr, size_t array_size, const key_compare &comp, const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
Constructor to initialize comparator object.
Definition: static_map.hpp:147
CStaticPairArrayMap(const value_type(&arr)[Size], const char *file, int line, NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)
default constructor.
Definition: static_map.hpp:117
TBase::value_type value_type
Definition: static_map.hpp:108
TBase::const_iterator const_iterator
Definition: static_map.hpp:109
CStaticArraySearchBase< NStaticArray::PKeyValuePair< SStaticPair< KeyType, ValueType > >, KeyCompare > TBase
Definition: static_map.hpp:106
TBase::key_compare key_compare
Definition: static_map.hpp:111
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1209
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
FILE * file
ECopyWarn
Definition: static_set.hpp:182
@ eCopyWarn_default
Definition: static_set.hpp:183
#define Type
Modified on Mon Apr 22 04:03:53 2024 by modify_doxy.py rev. 669887