gatelib  2.1
g_cont_gmap_sorted.h
1 #pragma once
2 
3 #include "g_cont_gmap_common.h"
4 #include "g_cont_ref.h"
5 
6 namespace g
7 {
8 namespace cont
9 {
10 
11 #define G_LOCAL_CLASS "g::cont::gmap_sorted<KEY_T>::"
12 
13 template < class KEY , class T , class IT > struct map_sorted_searcher
14 {
15  typedef T Value_t;
16  typedef KEY Key_t;
17  typedef typename comparer<KEY>::Operator_t KeyComparer_t;
18 
19  static bool search_Value ( const Value_t& aValue , IT& aIterator )
20  {
21  for ( ; aIterator.isIn ( ) ; aIterator++ )
22  {
23  if ( aIterator->value ( ) == aValue )
24  {
25  return true;
26  }
27  }
28 
29  return false;
30  }
31 
32  // Returns true if the search succeeds 'aIterator' is positioned on the iten corresponding to aKey
33  // or to the last item where aKey < content(aIterator)
34  // i.e map={ {1,"one"} , {3,"three"} , {4,"four"} } search_Key ( 2 , it ) will return false and it point to {1,"one"}
35  static bool search_Key ( const Key_t& aKey , IT& aIterator , KeyComparer_t aLessComparer )
36  {
37  G_VERBOSE_MSG_L1("gmap_sorted:search_Key");
38 
39  //uses the ordering
40  for ( ; aIterator.isIn ( ) ; aIterator++ )
41  {
42  if ( (*aLessComparer)( aIterator->key ( ) , aKey ) )
43  {
44  G_VERBOSE_MSG_L1( "aIterator->key ( ) = " << aIterator->key ( ) << " is 'less' than " << aKey << endl );
45 
46  continue;
47  }
48  else
49  {
50  G_VERBOSE_MSG_L1( "aIterator->key ( ) = " << aIterator->key ( ) << " is not 'less' than " << aKey << endl );
51 
52  return ( aIterator->key ( ) == aKey );
53  }
54  }
55 
56  return false;
57  }
58 };
59 
60 template < class KEY , class T , class REF_C = const T&> class gmap_sorted : public gmap_common <KEY,T,map_sorted_searcher,REF_C>
61 {
62 public:
63  typedef gpair <KEY,T> Pair_t;
64  typedef lst <Pair_t> PairLst_t;
65  typedef typename PairLst_t::It_t It_t;
66  typedef typename PairLst_t::ItConst_t ItConst_t;
67  typedef KEY Key_t;
68  typedef T Value_t;
69  typedef REF_C ConstRef_t;
71  typedef typename comparer<KEY>::Operator_t KeyComparer_t;
72 
73  gmap_sorted (
74  KeyComparer_t aKeyLessComparer = comparer<KEY>::less ,
75  AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy() ,
76  int aListPageRightBits = G_LST_PAGE_MASK_BITS ,
77  int aVectorAllocDeltaRightBits = G_VECT_AL_DELTA_MASK_BITS ) :
78  Base_t(aAllocPolicyP,aListPageRightBits,aVectorAllocDeltaRightBits),
79  mKeyLessComparer(aKeyLessComparer) {}
80 
81  //returns true if the value is added, otherwise the value is updated
82  virtual bool doAdd ( const Key_t& , const Value_t& );
83 
84  virtual KeyComparer_t getComparer ( ) const { return mKeyLessComparer; }
85 
86 private:
87  KeyComparer_t mKeyLessComparer;
88 };
89 
90 template < class KEY , class T , class REF=ref<T> , class REF_C = ref_const<T> > class ref_gmap_sorted : public gmap_sorted <KEY,REF,REF_C>
91 {
92 public:
93  typedef gpair <KEY,REF> Pair_t;
94  typedef lst <Pair_t> PairLst_t;
95  typedef typename PairLst_t::It_t It_t;
96  typedef typename PairLst_t::ItConst_t ItConst_t;
97  typedef KEY Key_t;
98  typedef T Value_t;
99  typedef REF Ref_t;
100  typedef REF_C ConstRef_t;
102  typedef typename comparer<KEY>::Operator_t KeyComparer_t;
103 
104  ref_gmap_sorted (
105  KeyComparer_t aKeyLessComparer = comparer<KEY>::less ,
106  AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy() ,
107  int aListPageRightBits = G_LST_PAGE_MASK_BITS ,
108  int aVectorAllocDeltaRightBits = G_VECT_AL_DELTA_MASK_BITS ) :
109  Base_t(aKeyLessComparer,aAllocPolicyP,aListPageRightBits,aVectorAllocDeltaRightBits){}
110 };
111 
112 template < class KEY , class T , class REF_C> bool gmap_sorted <KEY,T,REF_C>::doAdd(const Key_t &aKey, const Value_t &aValue)
113 {
114  It_t it = PairLst_t::getIterator ( );
115 
116  if ( map_sorted_searcher<Key_t,Value_t,It_t>::search_Key ( aKey , it , this->getComparer ()) )
117  {
118  G_VERBOSE_MSG_L1 ("Key: " << aKey << " supreseeding value " << it->value ( ) );
119  it->value ( ) = aValue;
120  return false;
121  }
122  else
123  {
124  if ( it.isIn ( ) )
125  {
126  G_VERBOSE_MSG_L1 ("Key: " << aKey << " before of " << it->key ( ) );
127 
128  this->pushBefore ( Pair_t(aKey,aValue) , it );
129  }
130  else
131  {
132  G_VERBOSE_MSG_L1 ("Key: " << aKey << " at end!" );
133 
134  this->pushTail( Pair_t(aKey,aValue) );
135  }
136  return true;
137  }
138 }
139 
140 #undef G_LOCAL_CLASS
141 
142 }//namespace cont
143 }//namespace g
144 
Definition: g_cont_gmap_common.h:13
Definition: g_cont_AllocationPolicyAbstract.h:16
Definition: g_cont_lst.h:10
Definition: g_cont_gmap_sorted.h:90
Definition: g.mthread.ThreadSimpleEvent.h:5
Definition: g_cont_gmap_sorted.h:60
Definition: g_cont_gmap_common.h:42
Definition: g_cont_it.h:10
Definition: g_cont_gmap_common.h:21
Definition: g_cont_gmap_sorted.h:13