1 #ifndef UTIL___STATIC_MAP__HPP
2 #define UTIL___STATIC_MAP__HPP
3 
4 /*  $Id: static_map.hpp 361095 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 
43 BEGIN_NCBI_SCOPE
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> >
103 class CStaticPairArrayMap
104     : public CStaticArraySearchBase<NStaticArray::PKeyValuePair< SStaticPair<KeyType, ValueType> >, KeyCompare>
105 {
106     typedef CStaticArraySearchBase<NStaticArray::PKeyValuePair< SStaticPair<KeyType, ValueType> >, KeyCompare> TBase;
107 public:
108     typedef typename TBase::value_type value_type;
109     typedef typename TBase::const_iterator const_iterator;
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>
CStaticPairArrayMap(const value_type (& arr)[Size],const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)117     CStaticPairArrayMap(const value_type (&arr)[Size],
118                         const char* file, int line,
119                         NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
120         : TBase(arr, file, line, warn)
121     {
122     }
123 
124     /// Constructor to initialize comparator object.
125     template<size_t Size>
CStaticPairArrayMap(const value_type (& arr)[Size],const key_compare & comp,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)126     CStaticPairArrayMap(const value_type (&arr)[Size],
127                         const key_compare& comp,
128                         const char* file, int line,
129                         NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
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>
CStaticPairArrayMap(const Type * array_ptr,size_t array_size,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)138     CStaticPairArrayMap(const Type* array_ptr, size_t array_size,
139                         const char* file, int line,
140                         NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
141         : TBase(array_ptr, array_size, file, line, warn)
142     {
143     }
144 
145     /// Constructor to initialize comparator object.
146     template<class Type>
CStaticPairArrayMap(const Type * array_ptr,size_t array_size,const key_compare & comp,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)147     CStaticPairArrayMap(const Type* array_ptr, size_t array_size,
148                         const key_compare& comp,
149                         const char* file, int line,
150                         NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
151         : TBase(array_ptr, array_size, comp, file, line, warn)
152     {
153     }
154 
155     NCBI_DEPRECATED_CTOR
156     (CStaticPairArrayMap(const_iterator obj,
157                          size_type array_size));
158 
159     NCBI_DEPRECATED_CTOR
160     (CStaticPairArrayMap(const_iterator obj,
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> >
173 class CStaticArrayMap
174     : public CStaticArraySearchBase<NStaticArray::PKeyValuePair< pair<KeyType, ValueType> >, KeyCompare>
175 {
176     typedef CStaticArraySearchBase<NStaticArray::PKeyValuePair< pair<KeyType, ValueType> >, KeyCompare> TBase;
177 public:
178     typedef typename TBase::value_type value_type;
179     typedef typename TBase::const_iterator const_iterator;
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>
CStaticArrayMap(const value_type (& arr)[Size],const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)187     CStaticArrayMap(const value_type (&arr)[Size],
188                     const char* file, int line,
189                     NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
190         : TBase(arr, file, line, warn)
191     {
192     }
193 
194     /// Constructor to initialize comparator object.
195     template<size_t Size>
CStaticArrayMap(const value_type (& arr)[Size],const key_compare & comp,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)196     CStaticArrayMap(const value_type (&arr)[Size],
197                     const key_compare& comp,
198                     const char* file, int line,
199                     NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
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>
CStaticArrayMap(const Type * array_ptr,size_t array_size,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)208     CStaticArrayMap(const Type* array_ptr, size_t array_size,
209                     const char* file, int line,
210                     NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
211         : TBase(array_ptr, array_size, file, line, warn)
212     {
213     }
214 
215     /// Constructor to initialize comparator object.
216     template<class Type>
CStaticArrayMap(const Type * array_ptr,size_t array_size,const key_compare & comp,const char * file,int line,NStaticArray::ECopyWarn warn=NStaticArray::eCopyWarn_default)217     CStaticArrayMap(const Type* array_ptr, size_t array_size,
218                     const key_compare& comp,
219                     const char* file, int line,
220                     NStaticArray::ECopyWarn warn = NStaticArray::eCopyWarn_default)
221         : TBase(array_ptr, array_size, comp, file, line, warn)
222     {
223     }
224 
225     NCBI_DEPRECATED_CTOR
226     (CStaticArrayMap(const_iterator obj,
227                      size_type array_size));
228 
229     NCBI_DEPRECATED_CTOR
230     (CStaticArrayMap(const_iterator obj,
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
CStaticPairArrayMap(const_iterator obj,size_type array_size)241 CStaticPairArrayMap<KeyType, ValueType, KeyCompare>::CStaticPairArrayMap
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
CStaticPairArrayMap(const_iterator obj,size_type array_size,const key_compare & comp)250 CStaticPairArrayMap<KeyType, ValueType, KeyCompare>::CStaticPairArrayMap
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
CStaticArrayMap(const_iterator obj,size_type array_size)260 CStaticArrayMap<KeyType, ValueType, KeyCompare>::CStaticArrayMap
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
CStaticArrayMap(const_iterator obj,size_type array_size,const key_compare & comp)269 CStaticArrayMap<KeyType, ValueType, KeyCompare>::CStaticArrayMap
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 
277 END_NCBI_SCOPE
278 
279 #endif  // UTIL___STATIC_MAP__HPP
280