Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
FragTable.h
1 /*
2  * FragTable class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2017, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_FRAGTABLE_H
22 #define ELM_DATA_FRAGTABLE_H
23 
24 #include <elm/assert.h>
25 #include "Vector.h"
26 
27 namespace elm {
28 
29 // FragTable class
30 template <class T, class E = Equiv<T>, class A = DefaultAlloc>
31 class FragTable: public E, public A {
32 public:
33  typedef T t;
35 
36  inline FragTable(int size_pow = 8)
37  : tab(8), size(1 << size_pow), msk(size - 1), shf(size_pow), used(size)
38  { ASSERTP(size_pow > 0, "size must be greater than 0"); }
39  inline FragTable(const FragTable& t)
40  : tab(8), size(t.size), msk(size - 1), shf(t.shf), used(0)
41  { addAll(tab); }
42  inline ~FragTable(void) { clear(); }
43 
44  inline int pageSize() const { return size; }
45  inline int pagePower() const { return shf; }
46  inline bool pageFull() const { return used >= size; }
47 
48  class BaseIter: public PreIter<BaseIter, T> {
49  public:
50  inline BaseIter(): arr(0), i(0), len(0) { }
51  inline BaseIter(const self_t& array, int pos = 0): arr(&array), i(pos), len(array.count()) { }
52  inline void next(void) { ASSERT(i < len); i++; }
53  inline const T& item() const { return arr->get(i); }
54  inline bool ended(void) const { return i >= len; }
55  inline bool equals(const BaseIter& it) const { return arr == it.arr && i == it.i; }
56  protected:
57  friend class FragTable;
58  const self_t *arr;
59  int i, len;
60  };
61 
62  // Collection concept
63  class Iter: public BaseIter, public ConstPreIter<Iter, T> {
64  public:
65  using BaseIter::BaseIter;
66  inline const T& item() const { return BaseIter::arr->get(BaseIter::i); }
67  };
68  inline Iter begin() const { return items(); }
69  inline Iter end() const { return Iter(*this, count()); }
70 
71  inline int count (void) const { return length(); }
72  inline bool contains(const T &v) const
73  { for(int i = 0; i < length(); i++) if(get(i) == v) return true; return false; }
74  template <class C> bool containsAll(const C& c) const
75  { for(typename C::Iter i = c.items(); i; i++) if(!contains(*i)) return false; return false; }
76  inline bool isEmpty() const { return tab.count() == 0; }
77  inline operator bool (void) const { return !isEmpty(); }
78  inline Iter items(void) const { return Iter(*this); }
79  inline Iter operator*(void) const { return items(); }
80  inline operator Iter(void) const { return items(); }
81  inline bool equals(const self_t& t) const
82  { Iter i = begin(), j = t.begin(); for(; i && j; i++, j++) if(*i != *j) return false; return !i && !j; }
83  inline bool operator==(const self_t& t) const { return equals(t); }
84  inline bool operator!=(const self_t& t) const { return !equals(t); }
85 
86  // MutableCollection concept
87  class MutIter: public BaseIter, public MutPreIter<MutIter, T> {
88  public:
89  inline MutIter() { }
90  inline MutIter(self_t& c, int p = 0): BaseIter(c, p) { }
91  inline T& item() { return const_cast<self_t *>(BaseIter::arr)->get(BaseIter::i); }
92  };
93  inline MutIter begin() { return MutIter(*this); }
94  inline MutIter end() { return MutIter(*this, count()); }
95 
96  inline void clear(void)
97  { for(int i = 0; i < tab.count(); i++) delete [] tab[i]; tab.clear(); used = size; }
98  inline void add(const T &value)
99  { if(used >= size) { tab.add(new T[size]); used = 0; }
100  tab[tab.length() - 1][used++] = value; }
101  template <template <class _> class C >
102  void addAll(const C<T> &items)
103  { for(typename C<T>::Iterator i(items); i; i++) add(i); }
104  inline void remove(const T &item)
105  { for(Iter i(*this); i; i++) if(i == item) { remove(i); break; } }
106  template <template <class _> class C >
107  void removeAll(const C<T> &items)
108  { for(typename C<T>::Iterator i(items); i; i++) remove(i); }
109  inline void remove(const Iter &iter) { removeAt(iter.i); }
110  inline self_t& operator+=(const T& v) { add(v); return *this; }
111  inline self_t& operator-=(const T& v) { remove(v); return *this; }
112 
113  // Array concept
114  inline int length(void) const { return ((tab.count() - 1) << shf) + used; }
115  inline const T& get(int index) const
116  { ASSERTP(index >= 0 && index < length(), "index out of bounds"); return tab[index >> shf][index & msk]; }
117  inline int indexOf(const T &value, int start = 0) const
118  { for(Iter i(*this, start); i; i++)
119  if(i == value) return i.i; return -1; }
120  inline int lastIndexOf(const T &value, int start = -1) const
121  { for(int i = (start < 0 ? length() : start) - 1; i >= 0; i--)
122  if(get(i) == value) return i; return -1; }
123  inline const T& operator[](int index) const { return get(index); }
124 
125  // MutableArray concept
126  void shrink(int length)
127  { ASSERTP(length < this->length(), "length too big"); int nl = (length + msk) >> shf;
128  for(int i = nl; i < tab.count(); i++) delete [] tab[i];
129  tab.setLength(nl); used = length & msk; if(!used) used = size; }
130  inline void set(int index, const T &value)
131  { ASSERTP(index >= 0 && index < length(), "index out of bounds"); tab[index >> shf][index & msk] = value; }
132  void set(const Iter &iter, const T &item) { set(iter.i, item); }
133  inline T &get(int index)
134  { ASSERTP(index >= 0 && index < length(), "index out of bounds"); return tab[index >> shf][index & msk]; }
135  inline T &operator[](int index) { return get(index); }
136  void insert(int index, const T &item)
137  { ASSERTP(index >= 0 && index <= length(), "index out of bounds");
138  int len = length(); alloc(1); for(int i = len - 1; i >= index; i--) set(i + 1, get(i)); set(index, item); }
139  inline void insert(const Iter &iter, const T &item)
140  { insert(iter.i, item); }
141  void removeAt(int index) {
142  int len = length(); for(int i = index + 1; i < len; i++) set(i - 1, get(i));
143  used--; if(!used) { delete [] tab[tab.count() - 1]; tab.setLength(tab.count() - 1); used = size; }
144  }
145  inline void removeAt(const Iter &iter) { removeAt(iter.i); }
146 
147  // other methods
148  int alloc(int count)
149  { int res = length(); while(count >= size - used) { count -= size - used; tab.add(new T[size]); used = 0; }
150  used += count; return res; }
151 
152 private:
153  Vector<T *, Equiv<T *>, A> tab;
154  int size, msk, shf, used;
155 };
156 
157 template <class T, class E, class A>
158 inline bool operator<=(const T& v, const FragTable<T, E, A>& t) { return t.contains(v); }
159 
160 } // elm
161 
162 #endif // ELM_DATA_FRAGTABLE_H
elm::FragTable::begin
Iter begin() const
Definition: FragTable.h:68
elm::FragTable::items
Iter items(void) const
Definition: FragTable.h:78
elm::ConstPreIter
Definition: iter.h:83
elm::FragTable::BaseIter::ended
bool ended(void) const
Definition: FragTable.h:54
elm::FragTable::BaseIter::next
void next(void)
Definition: FragTable.h:52
elm::iter
Definition: util_WAHVector.cpp:157
elm::FragTable::operator*
Iter operator*(void) const
Definition: FragTable.h:79
elm::FragTable::length
int length(void) const
Definition: FragTable.h:114
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::FragTable::MutIter::MutIter
MutIter(self_t &c, int p=0)
Definition: FragTable.h:90
elm::operator<=
bool operator<=(const T &v, const FragTable< T, E, A > &t)
Definition: FragTable.h:158
elm::FragTable::set
void set(const Iter &iter, const T &item)
Definition: FragTable.h:132
elm::MutPreIter
Definition: iter.h:90
elm::FragTable::BaseIter
Definition: FragTable.h:48
elm::FragTable::insert
void insert(const Iter &iter, const T &item)
Definition: FragTable.h:139
elm::FragTable::containsAll
bool containsAll(const C &c) const
Definition: FragTable.h:74
elm::FragTable::self_t
FragTable< T, E, A > self_t
Definition: FragTable.h:34
elm::FragTable::operator[]
T & operator[](int index)
Definition: FragTable.h:135
elm::FragTable::alloc
int alloc(int count)
Definition: FragTable.h:148
elm::FragTable::FragTable
FragTable(int size_pow=8)
Definition: FragTable.h:36
elm::FragTable::get
T & get(int index)
Definition: FragTable.h:133
elm::FragTable::operator==
bool operator==(const self_t &t) const
Definition: FragTable.h:83
elm::FragTable::pagePower
int pagePower() const
Definition: FragTable.h:45
elm::FragTable::FragTable
FragTable(const FragTable &t)
Definition: FragTable.h:39
elm::FragTable::operator-=
self_t & operator-=(const T &v)
Definition: FragTable.h:111
elm::FragTable::operator+=
self_t & operator+=(const T &v)
Definition: FragTable.h:110
value
bool
elm::FragTable::end
MutIter end()
Definition: FragTable.h:94
elm::FragTable::removeAll
void removeAll(const C< T > &items)
Definition: FragTable.h:107
elm::FragTable::MutIter::item
T & item()
Definition: FragTable.h:91
elm::FragTable::contains
bool contains(const T &v) const
Definition: FragTable.h:72
elm::FragTable::removeAt
void removeAt(int index)
Definition: FragTable.h:141
elm::FragTable::BaseIter::len
int len
Definition: FragTable.h:59
elm::FragTable::t
T t
Definition: FragTable.h:33
elm::FragTable::isEmpty
bool isEmpty() const
Definition: FragTable.h:76
elm::FragTable::addAll
void addAll(const C< T > &items)
Definition: FragTable.h:102
elm::FragTable::BaseIter::item
const T & item() const
Definition: FragTable.h:53
elm::FragTable::remove
void remove(const T &item)
Definition: FragTable.h:104
elm
Definition: adapter.h:26
elm::FragTable::BaseIter::equals
bool equals(const BaseIter &it) const
Definition: FragTable.h:55
elm::FragTable::indexOf
int indexOf(const T &value, int start=0) const
Definition: FragTable.h:117
elm::FragTable::BaseIter::BaseIter
BaseIter()
Definition: FragTable.h:50
elm::Vector
Definition: Vector.h:34
elm::FragTable::removeAt
void removeAt(const Iter &iter)
Definition: FragTable.h:145
elm::FragTable::equals
bool equals(const self_t &t) const
Definition: FragTable.h:81
elm::FragTable::MutIter::MutIter
MutIter()
Definition: FragTable.h:89
elm::FragTable::set
void set(int index, const T &value)
Definition: FragTable.h:130
array
elm::FragTable::lastIndexOf
int lastIndexOf(const T &value, int start=-1) const
Definition: FragTable.h:120
elm::FragTable::operator[]
const T & operator[](int index) const
Definition: FragTable.h:123
elm::FragTable::~FragTable
~FragTable(void)
Definition: FragTable.h:42
elm::FragTable::BaseIter::i
int i
Definition: FragTable.h:59
elm::FragTable::pageSize
int pageSize() const
Definition: FragTable.h:44
elm::FragTable::Iter::item
const T & item() const
Definition: FragTable.h:66
elm::FragTable::BaseIter::BaseIter
BaseIter(const self_t &array, int pos=0)
Definition: FragTable.h:51
elm::FragTable::shrink
void shrink(int length)
Definition: FragTable.h:126
elm::FragTable::operator!=
bool operator!=(const self_t &t) const
Definition: FragTable.h:84
elm::FragTable::MutIter
Definition: FragTable.h:87
elm::DefaultAllocatorDelegate::alloc
T * alloc() const
Definition: custom.h:33
elm::FragTable::Iter
Definition: FragTable.h:63
elm::FragTable::count
int count(void) const
Definition: FragTable.h:71
elm::FragTable::clear
void clear(void)
Definition: FragTable.h:96
elm::FragTable::insert
void insert(int index, const T &item)
Definition: FragTable.h:136
elm::FragTable::end
Iter end() const
Definition: FragTable.h:69
elm::FragTable::pageFull
bool pageFull() const
Definition: FragTable.h:46
elm::FragTable
Definition: FragTable.h:31
elm::FragTable::remove
void remove(const Iter &iter)
Definition: FragTable.h:109
elm::FragTable::BaseIter::arr
const self_t * arr
Definition: FragTable.h:58
elm::FragTable::begin
MutIter begin()
Definition: FragTable.h:93
elm::PreIter
Definition: iter.h:69
elm::FragTable::add
void add(const T &value)
Definition: FragTable.h:98
elm::FragTable::get
const T & get(int index) const
Definition: FragTable.h:115