Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
key_mutable_set.h
Go to the documentation of this file.
1 #ifndef _KEY_MUTABLE_SET_H_
2 #define _KEY_MUTABLE_SET_H_
3 #include <set>
4 #include <assert.h>
5 #include <list>
6 template < class T >
8 {
9  mutable T t;
10 public:
11 //flatten restores sortedness--in this case it does nothing
12  void flatten() {}
13  MutableShell( const T &t ) : t( t ) {}
14  T& get() const
15  {
16  return t;
17  }
18  T&operator*() const
19  {
20  return t;
21  }
22  T* operator->() const
23  {
24  return &t;
25  }
26  operator T &() const {
27  return t;
28  }
29  bool operator<( const MutableShell< T > &other ) const
30  {
31  return t < other.t;
32  }
33 };
34 
37 
38 template < class T, class _Compare = std::less< MutableShell< T > > >
39 class KeyMutableSet : public std::multiset< MutableShell< T >, _Compare >
40 {
41  typedef std::multiset< MutableShell< T >, _Compare >SUPER;
42 public:
44  void checkSet()
45  {
46  _Compare comparator;
47  if ( this->begin() != this->end() )
48  for (typename SUPER::iterator newiter = this->begin(), iter = newiter++; newiter != this->end(); iter = newiter++)
49  assert( !comparator( *newiter, *iter ) );
50  }
52 
56  void changeKey( typename SUPER::iterator &iter,
57  const T &newKey,
58  typename SUPER::iterator &templess,
59  typename SUPER::iterator &rettempmore )
60  {
61  MutableShell< T >newKeyShell( newKey );
62  templess = rettempmore = iter;
63  ++rettempmore;
64  typename SUPER::iterator tempmore = rettempmore;
65  if ( tempmore == this->end() )
66  --tempmore;
67  if ( templess != this->begin() )
68  --templess;
69  _Compare comparator;
70 
71  //O(1) amortized time on the insertion - Yippie!
72  bool byebye = false;
73  if ( comparator( newKeyShell, *templess ) ) {
74  rettempmore = templess = this->insert( newKeyShell, templess );
75  byebye = true;
76  } else if ( comparator( *tempmore, newKeyShell ) ) {
77  rettempmore = templess = this->insert( newKeyShell, tempmore );
78  byebye = true;
79  } else {(*iter).get() = newKey; } if (byebye) {
80  this->erase( iter );
81  iter = templess;
82  ++rettempmore;
83  if ( templess != this->begin() )
84  --templess;
85  }
86  }
87  typename SUPER::iterator insert( const T &newKey, typename SUPER::iterator hint )
88  {
89  return this->SUPER::insert( hint, newKey );
90  }
91  typename SUPER::iterator insert( const T &newKey )
92  {
93  return this->SUPER::insert( newKey );
94  }
95  typename SUPER::iterator changeKey( typename SUPER::iterator iter, const T &newKey )
96  {
97  typename SUPER::iterator templess = iter, tempmore = iter;
98  changeKey( iter, newKey, templess, tempmore );
99  return iter;
100  }
101 };
102 
103 template < class T, class _Compare = std::less< T > >
104 class ListMutableSet : public std::list< T >
105 {
106  typedef std::list< T >SUPER;
107 public:
108 //flatten restores sortedness--in this case it does nothing
109  void flatten() {}
111  void checkSet()
112  {
113  _Compare comparator;
114  if ( this->begin() != this->end() )
115  for (typename SUPER::iterator newiter = this->begin(), iter = newiter++; newiter != this->end(); iter = newiter++)
116  assert( !comparator( *newiter, *iter ) );
117  }
119 
122  void changeKey( typename SUPER::iterator &iter,
123  const T &newKey,
124  typename SUPER::iterator &templess,
125  typename SUPER::iterator &rettempmore )
126  {
127  MutableShell< T >newKeyShell( newKey );
128  templess = rettempmore = iter;
129  ++rettempmore;
130  typename SUPER::iterator tempmore = rettempmore;
131  if ( tempmore == this->end() )
132  --tempmore;
133  if ( templess != this->begin() )
134  --templess;
135  _Compare comparator;
136  if ( comparator( newKeyShell, *templess ) || comparator( *tempmore, newKeyShell ) ) {
137  rettempmore = templess = iter = this->insert( newKeyShell, this->erase( iter ) );
138  ++rettempmore;
139  if ( templess != this->begin() )
140  --templess;
141  } else {
142  **iter = newKey;
143  //return iter;
144  }
145  }
146 
147  typename SUPER::iterator changeKey( typename SUPER::iterator iter, const T &newKey )
148  {
149  typename SUPER::iterator templess = iter, tempmore = iter;
150  changeKey( iter, newKey, templess, tempmore );
151  return iter;
152  }
153  typename SUPER::iterator insert( const T &newKey, typename SUPER::iterator hint )
154  {
155  bool gequal = false, lequal = false;
156  _Compare comparator;
157  while (1) {
158  if ( hint != this->end() ) {
159  bool tlequal = !comparator( *hint, newKey );
160  bool tgequal = !comparator( newKey, *hint );
161  if (tlequal) {
162  if ( gequal || tgequal || hint == this->begin() ) {
163  return this->SUPER::insert( hint, newKey );
164  } else {
165  lequal = true;
166  --hint;
167  }
168  }
169  if (tgequal) {
170  gequal = true;
171  ++hint;
172  if (lequal || tlequal)
173  return this->SUPER::insert( hint, newKey );
174  }
175  } else if ( hint == this->begin() ) {
176  this->SUPER::insert( hint, newKey );
177  } else {
178  if (gequal)
179  return this->SUPER::insert( hint, newKey );
180  else
181  --hint;
182  }
183  }
184  }
185  typename SUPER::iterator insert( const T &newKey )
186  {
187  return this->insert( newKey, this->begin() );
188  }
189 };
190 
191 #endif
192