Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FragTable.h
1 /*
2  * $Id$
3  * FragTable class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2005-07, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_GENSTRUCT_FRAGTABLE_H
23 #define ELM_GENSTRUCT_FRAGTABLE_H
24 
25 #include <elm/assert.h>
26 #include <elm/genstruct/Vector.h>
27 
28 namespace elm { namespace genstruct {
29 
30 // FragTable class
31 template <class T> class FragTable {
33  int size, msk, shf, used;
34 public:
35  inline FragTable(int size_pow = 8);
36  inline FragTable(const FragTable& tab) { addAll(tab); }
37  inline ~FragTable(void);
38 
39  // Collection concept
40  inline int count (void) const { return length(); }
41  inline bool isEmpty(void) const { return tab.count() == 0; }
42  inline operator bool (void) const { return !isEmpty(); }
43  inline bool contains(const T &item) const
44  { for(int i = 0; i < length(); i++)
45  if(get(i) == item) return true; return false; }
46 
47  class Iterator: public PreIterator<Iterator, const T&> {
48  public:
49  inline Iterator(const FragTable<T>& array, int pos = 0);
50  inline Iterator(const Iterator& iter)
51  : arr(iter.arr), i(iter.i), len(iter.len) { }
52  inline void next(void);
53  inline const T& item(void) const;
54  inline bool ended(void) const;
55  protected:
56  friend class FragTable;
57  const FragTable<T> *arr;
58  int i, len;
59  };
60 
61  // MutableCollection concept
62  inline void clear(void);
63  inline void add(const T &item);
64  template <template <class _> class C >
65  void addAll(const C<T> &items)
66  { for(typename C<T>::Iterator i(items); i; i++) add(i); }
67  inline void remove(const T &item)
68  { for(Iterator i(*this); i; i++) if(i == item) { remove(i); break; } }
69  inline void remove(const Iterator &iter) { removeAt(iter.i); }
70  template <template <class _> class C >
71  void removeAll(const C<T> &items)
72  { for(typename C<T>::Iterator i(items); i; i++) remove(i); }
73 
74  // Array concept
75  inline int length(void) const;
76  inline const T& get(int index) const;
77  inline int indexOf(const T &value, int start = 0) const
78  { for(Iterator i(*this, start); i; i++)
79  if(i == value) return i.i; return -1; }
80  inline int lastIndexOf(const T &value, int start = -1) const
81  { for(int i = (start < 0 ? length() : start) - 1; i >= 0; i--)
82  if(get(i) == value) return i; return -1; }
83  inline const T& operator[](int index) const { return get(index); }
84 
85  // MutableArray concept
86  void shrink(int length);
87  inline void set(int index, const T &item);
88  void set(const Iterator &iter, const T &item) { set(iter.i, item); }
89  inline T &get(int index);
90  inline T &operator[](int index) { return get(index); }
91  void insert(int index, const T &item);
92  inline void insert(const Iterator &iter, const T &item)
93  { insert(iter.i, item); }
94  void removeAt(int index);
95  inline void removeAt(const Iterator &iter) { removeAt(iter.i); }
96 
97  // Methods
98  int alloc(int count);
99 
100  // Deprecated
101  inline void clean(void) { clear(); }
102  inline FragTable<T>& operator+=(T value);
103 };
104 
105 
106 // FragTable<T> Inlines
107 template <class T>
108 inline FragTable<T>::FragTable(int size_pow)
109 : size(1 << size_pow), msk(size - 1), shf(size_pow), used(size) {
110  ASSERTP(size_pow > 0, "size must be greater than 0");
111 }
112 
113 template <class T>
115  for(int i = 0; i < tab.count(); i++)
116  delete [] tab[i];
117 }
118 
119 template <class T>
120 inline void FragTable<T>::clear(void) {
121  for(int i = 0; i < tab.count(); i++)
122  delete [] tab[i];
123  tab.setLength(0);
124  used = size;
125 }
126 
127 template <class T>
128 inline int FragTable<T>::length(void) const {
129  return ((tab.count() - 1) << shf) + used;
130 }
131 
132 template <class T>
133 inline const T& FragTable<T>::get(int index) const {
134  ASSERTP(index >= 0 && index < length(), "index out of bounds");
135  return tab[index >> shf][index & msk];
136 }
137 
138 template <class T>
139 inline T& FragTable<T>::get(int index) {
140  ASSERTP(index >= 0 && index < length(), "index out of bounds");
141  return tab[index >> shf][index & msk];
142 }
143 
144 template <class T>
145 inline void FragTable<T>::set(int index, const T& value) {
146  ASSERTP(index >= 0 && index < length(), "index out of bounds");
147  tab[index >> shf][index & msk] = value;
148 }
149 
150 template <class T>
151 inline void FragTable<T>::add(const T& value) {
152  if(used >= size) {
153  tab.add(new T[size]);
154  used = 0;
155  }
156  tab[tab.length() - 1][used++] = value;
157 }
158 
159 template <class T>
161  add(value);
162  return *this;
163 }
164 
165 template <class T>
166 int FragTable<T>::alloc(int count) {
167  int res = length();
168  while(count >= size - used) {
169  count -= size - used;
170  tab.add(new T[size]);
171  used = 0;
172  }
173  used += count;
174  return res;
175 }
176 
177 
178 template <class T>
179 void FragTable<T>::shrink(int length) {
180  ASSERTP(length < this->length(), "length too big");
181  int nl = (length + msk) >> shf;
182  for(int i = nl; i < tab.count(); i++)
183  delete [] tab[i];
184  tab.setLength(nl);
185  used = length & msk;
186  if(!used)
187  used = size;
188 }
189 
190 template <class T>
191 void FragTable<T>::insert(int index, const T &item) {
192  ASSERTP(index >= 0 && index <= length(), "index out of bounds");
193  int len = length();
194  alloc(1);
195  for(int i = len - 1; i >= index; i--)
196  set(i + 1, get(i));
197  set(index, item);
198 }
199 
200 
201 template <class T>
202 void FragTable<T>::removeAt(int index) {
203  int len = length();
204  for(int i = index + 1; i <= len; i++)
205  set(i - 1, get(i));
206  used--;
207  if(!used) {
208  delete [] tab[tab.count() - 1];
209  tab.setLength(tab.count() - 1);
210  used = size;
211  }
212 }
213 
214 
215 // FragTable<T>::Iterator inlines
216 template <class T>
217 inline FragTable<T>::Iterator::Iterator(const FragTable<T>& array, int pos)
218 : arr(&array), i(pos), len(array.count()) {
219 }
220 
221 template <class T>
222 inline void FragTable<T>::Iterator::next(void) {
223  ASSERT(i < len);
224  i++;
225 }
226 
227 template <class T>
228 inline const T& FragTable<T>::Iterator::item(void) const {
229  return arr->get(i);
230 }
231 
232 template <class T>
233 inline bool FragTable<T>::Iterator::ended(void) const {
234  return i >= len;
235 }
236 
237 } } // elm::genstruct
238 
239 #endif // ELM_GENSTRUCT_FRAGTABLE_H
void set(T *target, int size, const T &v)
Definition: array.h:63
int indexOf(const T &value, int start=0) const
Definition: FragTable.h:77
FragTable(const FragTable &tab)
Definition: FragTable.h:36
bool isEmpty(void) const
Definition: FragTable.h:41
Definition: PreIterator.h:29
void removeAt(int index)
Definition: FragTable.h:202
void shrink(int length)
Definition: FragTable.h:179
int length(void) const
Definition: FragTable.h:128
void insert(const Iterator &iter, const T &item)
Definition: FragTable.h:92
const T & get(int index) const
Definition: FragTable.h:133
FragTable< T > & operator+=(T value)
Definition: FragTable.h:160
void insert(int index, const T &item)
Definition: FragTable.h:191
void set(const Iterator &iter, const T &item)
Definition: FragTable.h:88
const T & operator[](int index) const
Definition: FragTable.h:83
uint32 size
Definition: int.h:41
int alloc(int count)
Definition: FragTable.h:166
Iterator(const Iterator &iter)
Definition: FragTable.h:50
void addAll(const C< T > &items)
Definition: FragTable.h:65
void clean(void)
Definition: FragTable.h:101
void removeAt(const Iterator &iter)
Definition: FragTable.h:95
value_t value(CString name, int value)
Definition: rtti.h:40
Iterator(const FragTable< T > &array, int pos=0)
Definition: FragTable.h:217
void set(int index, const T &item)
Definition: FragTable.h:145
int i
Definition: FragTable.h:58
void next(void)
Definition: FragTable.h:222
Definition: FragTable.h:47
void add(const T &item)
Definition: FragTable.h:151
int count(void) const
Definition: FragTable.h:40
FragTable(int size_pow=8)
Definition: FragTable.h:108
T & operator[](int index)
Definition: FragTable.h:90
int len
Definition: FragTable.h:58
~FragTable(void)
Definition: FragTable.h:114
void removeAll(const C< T > &items)
Definition: FragTable.h:71
const T & item(void) const
Definition: FragTable.h:228
const FragTable< T > * arr
Definition: FragTable.h:57
void clear(void)
Definition: FragTable.h:120
int lastIndexOf(const T &value, int start=-1) const
Definition: FragTable.h:80
int count(void) const
Definition: Vector.h:58
bool ended(void) const
Definition: FragTable.h:233
Definition: FragTable.h:31
bool contains(const T &item) const
Definition: FragTable.h:43