Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
ArrayList.h
1 /*
2  * ArrayList class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, 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_ARRAYLIST_H_
22 #define ELM_DATA_ARRAYLIST_H_
23 
24 #include <elm/assert.h>
25 #include <elm/PreIterator.h>
26 #include <elm/equiv.h>
27 
28 namespace elm {
29 
30 template <class T, class E = Equiv<T> >
31 class ArrayList {
32  friend class iter;
33 
34  typedef struct chunk_t {
35  struct chunk_t *next;
36  T items[];
37  } chunk_t;
38 
39 public:
40 
41  inline ArrayList(int n = 6): _csize(1 << n), _size(0), _avail(0), _fst(0), _lst(0) { }
42  inline ~ArrayList(void) { clear(); }
43 
44  class Iter: public PreIterator<Iter, const T&> {
45  friend class ArrayList;
46  public:
47  inline Iter(const ArrayList<T>& array): a(array), i(0), c(_fst) { }
48  inline Iter(const ArrayList<T>& array, int start): a(array)
49  { ASSERT(start <= a._size); c = a.chunk(start); i = start; }
50  inline Iter(const ArrayList<T>& array, bool end)
51  : a(array), i(end ? a._size : 0), c(end ? a._lst : a._fst) { }
52  inline bool ended(void) const { return i < a._size; }
53  inline const T& item(void) const { return c->items[a.offset(i)]; }
54  inline void next(void) { i++; if(!offset(i)) c = c->next; }
55  inline int index(void) const { return i; }
56  private:
57  const ArrayList& a;
58  int i;
59  chunk_t *c;
60  };
61 
62  // Collection concept
63  inline int count(void) const { return _size; }
64  bool contains(const T &item)
65  { for(Iter i(*this); i; i++) if(E::equals(item, *i)) return true; return false; }
66  template<template< class _ > class C>
67  inline bool containsAll(const C<T> &coll)
68  { for(typename C<T>::iter i(coll); i; i++) if(!contains(*i)) return false; return true; }
69  inline bool isEmpty(void) const { return !_fst; }
70  inline operator bool(void) const { return !isEmpty(); }
71 
72  // MutableCollection concept
73  void clear(void)
74  { for(chunk_t *c = _fst, *n = 0; c != nullptr; c = n) { n = c->next; delete n; }
75  _size = _avail = 0; _fst = _lst = 0; }
76  inline void add(const T& v)
77  { if(_size == _avail) extend(); _lst->items[offset(_size)] = v; _size++; }
78  template <template <class _> class C>
79  void addAll (const C<T> &items) { for(typename C<T>::iter i; i;i++) add(*i); }
80  void remove(const T &item)
81  { for(Iter i(*this); i; i++) if(E::equals(*i, item)) { remove(i); break; }; }
82  template <template <class _> class C>
83  void removeAll (const C<T> &items) { for(typename C<T>::iter i; i;i++) remove(*i); }
84  void remove(Iter i)
85  { /*mutable_iter mi(*this, i); for(i++; i; i++, mi++) *mi = *i; _size--;
86  if(!offset(_size)) { mi.c->next = 0; _lst = mi.c; delete i.c; }*/ }
87 
88  // Array concept
89  inline int length(void) const { return _size; }
90  inline const T &get(int index) const
91  { ASSERT(index < _size); return chunk(index)->items[offset(index)]; }
92  int indexOf(const T &value, int start = 0) const
93  { for(Iter i(*this, start); i; i++) if(E::equals(*i, value)) return i.index(); return -1; }
94  int lastIndexOf(const T &value, int start = -1) const
95  { int r = -1; for(Iter i(*this, start); i && i.index() <= start; i++) if(E::equals(*i, value)) r = i.index(); return r; }
96  inline const T &operator[](int index) const { return get(index); }
97 
98  // MutableArray concept
99  inline void shrink (int length)
100  { _lst = chunk(length); for(chunk_t *c = _lst, *n; c; c = n) { n = c->next; delete c; } _size = length; }
101  inline void set(int index, const T &item) { chunk(index)->items[offset(index)] = item; }
102  inline void set(const Iter &it, const T &item) { mutable_iter(*this, it); *it = item; }
103  inline T &get(int index) { return chunk(index)->items[offset(index)]; }
104  inline T &operator[](int index) { return get(index); }
105  inline void insert(int index, const T &item) { insert(iter(*this, index)); }
106  void insert(const Iter &it, T item)
107  { /*mutable_iter mi(*this, it); while(mi) { T x = *mi; *mi = item; item = x; }
108  _size++; if(!offset(_size)) { extend(); _lst->items[0] = item; } else *mi = item;*/ }
109  inline void removeAt(int index) { removeAt(iter(*this, index)); }
110  inline void removeAt(const Iter &it) { remove(it); }
111 
112  // List concept
113  inline const T &first(void) const { ASSERT(_fst); return _fst->items[0]; }
114  inline const T &last(void) const { ASSERT(_lst); return _lst->items[offset(_size) - 1]; }
115  inline Iter find(const T &item) { return find(iter(*this)); }
116  inline Iter find(const T &item, Iter start)const
117  { while(start && !E::equals(item, *start)) start++; return start; }
118 
119  // MutableList concept
120  inline void addFirst(const T &item) { insert(0, item); }
121  void addLast(const T &item) { if(!offset(_lst)) extend(); _lst->items[offset(_size)] = item; _size++; }
122  inline void removeFirst(void) { removeAt(0); }
123  inline void removeLast(void) { removeAt(_size - 1); }
124  inline void addAfter(const Iter &pos, const T &item) { insert(pos.index() + 1, item); }
125  inline void addBefore (const Iter &pos, const T &item) { insert(pos.index() - 1, item); }
126 
127 private:
128 
129  void extend(void)
130  { chunk_t *c = new chunk_t; c->next = 0; if(_lst) _lst->next = c; else _fst = c; _lst = c; _avail += _csize; }
131  inline int offset(int n) const { return n & (_csize - 1); }
132  inline chunk_t *chunk(int n) { chunk_t *c = _fst; for(; n >= _csize; n -= _csize) c = c->next(); return c; }
133 
134  int _csize, _size, _avail;
135  chunk_t *_fst, *_lst;
136 };
137 
138 } // elm
139 
140 #endif /* ELM_DATA_ARRAYLIST_H_ */
elm::ArrayList::Iter::Iter
Iter(const ArrayList< T > &array, bool end)
Definition: ArrayList.h:50
elm::iter
Definition: util_WAHVector.cpp:157
elm::ArrayList::removeAt
void removeAt(const Iter &it)
Definition: ArrayList.h:110
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::ArrayList::removeAll
void removeAll(const C< T > &items)
Definition: ArrayList.h:83
elm::ArrayList::addAll
void addAll(const C< T > &items)
Definition: ArrayList.h:79
elm::ArrayList::insert
void insert(const Iter &it, T item)
Definition: ArrayList.h:106
elm::ArrayList::contains
bool contains(const T &item)
Definition: ArrayList.h:64
elm::ArrayList::addAfter
void addAfter(const Iter &pos, const T &item)
Definition: ArrayList.h:124
elm::equals
bool equals(const C1 &c1, const C2 &c2)
Definition: util.h:107
elm::ArrayList::length
int length(void) const
Definition: ArrayList.h:89
elm::ArrayList::removeLast
void removeLast(void)
Definition: ArrayList.h:123
elm::ArrayList::Iter::Iter
Iter(const ArrayList< T > &array, int start)
Definition: ArrayList.h:48
elm::ArrayList::clear
void clear(void)
Definition: ArrayList.h:73
elm::ArrayList
Definition: ArrayList.h:31
elm::ArrayList::get
T & get(int index)
Definition: ArrayList.h:103
value
elm::ArrayList::set
void set(int index, const T &item)
Definition: ArrayList.h:101
bool
elm::ArrayList::operator[]
const T & operator[](int index) const
Definition: ArrayList.h:96
elm::ArrayList::remove
void remove(Iter i)
Definition: ArrayList.h:84
elm::ArrayList::indexOf
int indexOf(const T &value, int start=0) const
Definition: ArrayList.h:92
elm::ArrayList::Iter::Iter
Iter(const ArrayList< T > &array)
Definition: ArrayList.h:47
elm::ArrayList::Iter::ended
bool ended(void) const
Definition: ArrayList.h:52
elm::ArrayList::get
const T & get(int index) const
Definition: ArrayList.h:90
elm::ArrayList::containsAll
bool containsAll(const C< T > &coll)
Definition: ArrayList.h:67
elm::ArrayList::Iter::item
const T & item(void) const
Definition: ArrayList.h:53
elm::ArrayList::addBefore
void addBefore(const Iter &pos, const T &item)
Definition: ArrayList.h:125
elm::ArrayList::Iter
Definition: ArrayList.h:44
elm::ArrayList::count
int count(void) const
Definition: ArrayList.h:63
elm::ArrayList::Iter::next
void next(void)
Definition: ArrayList.h:54
elm::ArrayList::remove
void remove(const T &item)
Definition: ArrayList.h:80
elm::ArrayList::ArrayList
ArrayList(int n=6)
Definition: ArrayList.h:41
elm
Definition: adapter.h:26
elm::ArrayList::first
const T & first(void) const
Definition: ArrayList.h:113
mutable_iter
array
elm::ArrayList::find
Iter find(const T &item)
Definition: ArrayList.h:115
elm::ArrayList::shrink
void shrink(int length)
Definition: ArrayList.h:99
elm::ArrayList::~ArrayList
~ArrayList(void)
Definition: ArrayList.h:42
elm::ArrayList::insert
void insert(int index, const T &item)
Definition: ArrayList.h:105
elm::ArrayList::operator[]
T & operator[](int index)
Definition: ArrayList.h:104
elm::ArrayList::addLast
void addLast(const T &item)
Definition: ArrayList.h:121
elm::ArrayList::addFirst
void addFirst(const T &item)
Definition: ArrayList.h:120
elm::ArrayList::isEmpty
bool isEmpty(void) const
Definition: ArrayList.h:69
elm::ArrayList::find
Iter find(const T &item, Iter start) const
Definition: ArrayList.h:116
elm::ArrayList::Iter::index
int index(void) const
Definition: ArrayList.h:55
elm::ArrayList::iter
friend class iter
Definition: ArrayList.h:32
elm::ArrayList::last
const T & last(void) const
Definition: ArrayList.h:114
elm::ArrayList::add
void add(const T &v)
Definition: ArrayList.h:76
elm::ArrayList::removeAt
void removeAt(int index)
Definition: ArrayList.h:109
elm::ArrayList::set
void set(const Iter &it, const T &item)
Definition: ArrayList.h:102
elm::ArrayList::removeFirst
void removeFirst(void)
Definition: ArrayList.h:122
elm::PreIterator
Definition: iter.h:28
elm::t::offset
uint64 offset
Definition: arch.h:36
elm::ArrayList::lastIndexOf
int lastIndexOf(const T &value, int start=-1) const
Definition: ArrayList.h:94