gatelib  2.1
g_cont_gmap_common.h
1 #pragma once
2 
3 #include "g_cont_common.h"
4 #include "g_cont_lst.h"
5 
6 namespace g
7 {
8 namespace cont
9 {
10 
11 G_EXC_DEFINE(KeyNotFoundException,ContException);
12 
13 template <class T> struct comparer
14 {
15  typedef bool (*Operator_t) ( const T& a1 , const T& a2 );
16 
17  static bool less ( const T& a1 , const T& a2 ) { return (a1 < a2); }
18  static bool more ( const T& a1 , const T& a2 ) { return (a1 > a2); }
19 };
20 
21 template < class KEY , class T > class gpair
22 {
23 public:
24  typedef KEY Key_t;
25  typedef T Value_t;
26 
27  gpair ( const Key_t& aKey , const Value_t& aValue ) : mKey ( aKey ) , mValue ( aValue ) {}
28 
29  gpair& operator = ( const gpair& aPair ) { mKey = aPair.mKey ; mValue = aPair.mValue; return *this; }
30 
31  const Key_t& key ( ) const { return mKey; };
32  const Value_t& value ( ) const { return mValue; }
33 
34  Key_t& key ( ) { return mKey; };
35  Value_t& value ( ) { return mValue; }
36 
37 private:
38  Key_t mKey;
39  Value_t mValue;
40 };
41 
42 template < class KEY , class T , template<class,class,class>class SRC , class REF_C = const T& > class gmap_common : protected lst < gpair <KEY,T> >
43 {
44 public:
45  typedef gpair <KEY,T> Pair_t;
47  typedef typename PairLst_t::It_t It_t;
48  typedef typename PairLst_t::ItConst_t ItConst_t;
49  typedef KEY Key_t;
50  typedef T Value_t;
51  typedef REF_C ConstRef_t;
52  typedef typename comparer<KEY>::Operator_t KeyComparer_t;
53 
54 protected:
55  gmap_common ( AllocationPolicyAbstract* ap , int lst_page_mask_bits , int al_delta_mask_bits ):PairLst_t(ap,lst_page_mask_bits,al_delta_mask_bits){}
56 
57 public:
58  Value_t& operator [] ( const Key_t& aKey );
59  ConstRef_t operator [] ( const Key_t& aKey ) const;
60 
61  bool isValueContained ( const Value_t& ) const;
62  bool isKeyContained ( const Key_t& ) const;
63  bool tryToGet ( const Key_t& , Value_t& );
64 
65  virtual void setEmpty ( ) { PairLst_t::setEmpty ( ); }
66 
67  bool tryRemove ( const Key_t& , Value_t& );
68  bool tryRemove ( const Key_t& );
69  Value_t doRemove ( const Key_t& );
70 
71  //returns true if the value is added, otherwise the value is updated
72  virtual bool doAdd ( const Key_t& , const Value_t& ) = 0;
73 
74  size_t getSize ( ) const { return PairLst_t::getLen ( ); }
75 
76  typename PairLst_t::ItConst_t getIterator ( ) const { return PairLst_t::getIterator ( ); }
77 
78  const PairLst_t& getPairList ( ) const { return (const PairLst_t&)(*this); }
79 
80  //not used by not-sorted map
81  virtual KeyComparer_t getComparer ( ) const { return 0; }
82 };
83 
84 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
85  typename gmap_common<KEY,T,SRC,REF_C>::Value_t& gmap_common<KEY,T,SRC,REF_C>::operator [] ( const Key_t& aKey )
86 {
87  G_EXC_SET_CONTEXT ( "gmap_common<KEY,T,SRC,REF_C>::Value_t& gmap_common<KEY,T,SRC,REF_C>::operator [] ( const Key_t& aKey )" );
88 
89  It_t it = PairLst_t::getIterator ( );
90 
91  if ( SRC<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer() ) )
92  {
93  return it->value();
94  }
95 
96  G_EXC_RAISE_MSG ( KeyNotFoundException , "Key not found!" );
97  return it->value();//fake avoid warning
98 }
99 
100 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
101  typename gmap_common<KEY,T,SRC,REF_C>::ConstRef_t gmap_common<KEY,T,SRC,REF_C>::operator [] ( const Key_t& aKey ) const
102 {
103  G_EXC_SET_CONTEXT ( "gmap_common<KEY,T,SRC,REF_C>::ConstRef_t gmap_common<KEY,T,SRC,REF_C>::operator [] ( const Key_t& aKey ) const " );
104 
105  ItConst_t it = PairLst_t::getIterator ( );
106 
107  if ( SRC<Key_t,Value_t,ItConst_t>::search_Key ( aKey , it , this->getComparer() ) )
108  {
109  return it->value();
110  }
111 
112  G_EXC_RAISE_MSG ( KeyNotFoundException , "Key not found!" );
113 }
114 
115 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
116  bool gmap_common<KEY,T,SRC,REF_C>::tryToGet ( const Key_t& aKey , Value_t& aValue )
117 {
118  It_t it = PairLst_t::getIterator ( );
119 
120  if ( SRC<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer() ) )
121  {
122  aValue = it->value();
123  return true;
124  }
125  else
126  {
127  return false;
128  }
129 }
130 
131 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
132  bool gmap_common<KEY,T,SRC,REF_C>::isValueContained ( const Value_t& aValue ) const
133 {
134  ItConst_t it = PairLst_t::getIterator ( );
135 
136  return SRC<Key_t,Value_t,ItConst_t>::search_Value ( aValue , it );
137 }
138 
139 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
140  bool gmap_common<KEY,T,SRC,REF_C>::isKeyContained ( const Key_t& aKey ) const
141 {
142  ItConst_t it = PairLst_t::getIterator ( );
143 
144  return SRC<Key_t,Value_t,ItConst_t>::search_Key ( aKey , it , this->getComparer() );
145 }
146 
147 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
148  typename gmap_common<KEY,T,SRC,REF_C>::Value_t gmap_common<KEY,T,SRC,REF_C>::doRemove ( const Key_t& aKey )
149 {
150  G_EXC_SET_CONTEXT ( "gmap_common<KEY,T,SRC,REF_C>::Value_t gmap_common<KEY,T,SRC,REF_C>::doRemove ( const Key_t& aKey )" );
151 
152  It_t it = PairLst_t::getIterator ( );
153 
154  if ( SRC<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer() ) )
155  {
156  return this->remove ( it ).value();
157  }
158  else
159  {
160  G_EXC_RAISE_MSG ( KeyNotFoundException , "Key not found!" );
161  return it->value();//fake avoid
162  }
163 }
164 
165 
166 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
167  bool gmap_common<KEY,T,SRC,REF_C>::tryRemove ( const Key_t& aKey , Value_t& aValue )
168 {
169  It_t it = PairLst_t::getIterator ( );
170 
171  if ( SRC<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer() ) )
172  {
173  aValue = it->value ( );
174  this->remove ( it );
175  return true;
176  }
177  else
178  {
179  return false;
180  }
181 }
182 
183 template < class KEY , class T , template<class,class,class>class SRC , class REF_C >
184  bool gmap_common<KEY,T,SRC,REF_C>::tryRemove ( const Key_t& aKey )
185 {
186  It_t it = PairLst_t::getIterator ( );
187 
188  if ( SRC<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer() ) )
189  {
190  this->remove ( it );
191  return true;
192  }
193  else
194  {
195  return false;
196  }
197 }
198 
199 #undef G_LOCAL_CLASS
200 
201 }//namespace cont
202 }//namespace g
203 
Definition: g_cont_gmap_common.h:13
Definition: g_cont_AllocationPolicyAbstract.h:16
Definition: g_cont_lst.h:10
#define G_EXC_DEFINE(aexctypename, abaseexctypename)
Definition: g_exception_macros.h:39
Definition: g.mthread.ThreadSimpleEvent.h:5
#define G_EXC_RAISE_MSG(aexctype, amessage)
Raises an exception specifying the message.
Definition: g_exception_macros.h:58
Definition: g_cont_it.h:39
Definition: g_cont_gmap_common.h:42
Definition: g_cont_it.h:10
Definition: g_cont_gmap_common.h:21
#define G_EXC_SET_CONTEXT(acontextstr)
Sets the method context.
Definition: g_exception_macros.h:52