Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Bag.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_BAG_H_
22 #define ELM_DATA_BAG_H_
23 
24 #include <elm/data/Array.h>
25 #include <elm/data/Vector.h>
26 
27 namespace elm {
28 
29 template <class T>
30 class Bag {
31 public:
32 
33  static inline void operator delete(void *p)
34  { delete [] reinterpret_cast<char *>(p); }
35 
36  inline static Bag<T> *make(int count, const T *items)
37  { Bag *b = new(count) Bag(count); array::copy(b->base(), items, count); return b; }
38 
39  inline static Bag<T> *make(const Array<T>& a)
40  { return make(a.count(), a.buffer()); }
41 
42  inline static Bag *make(const Vector<T>& v)
43  { return make(v.asArray()); }
44 
45  inline int count() const { return cnt; }
46  inline bool isEmpty() const { return cnt == 0; }
47  inline operator bool() const { return !isEmpty(); }
48  inline const T& get(int i) const { ASSERTP(0 <= i && i < cnt, "bad index"); return *(base() + i); }
49  inline T& get(int i) { ASSERTP(0 <= i && i < cnt, "bad index"); return *(base() + i); }
50 
51  inline const T& operator[](int i) const { return get(i); }
52  inline T& operator[](int i) { return get(i); }
53 
54  class Iter: public PreIterator<Iter, T> {
55  public:
56  inline Iter(const Bag<T> *bag, int idx = 0): b(*bag), i(idx) { }
57  inline bool ended() const { return i >= b->count(); }
58  inline const T& item() const { return b[i]; }
59  inline void next() { i++; }
60  inline bool equals(const Iter& ii) const { return &b == &ii.b && i == ii.i; }
61  private:
62  const Bag<T>& b;
63  int i;
64  };
65 
66  inline Iter begin() const { return Iter(this); }
67  inline Iter end() const { return Iter(this, cnt); }
68 
69  inline bool contains(const T& x) const
70  { for(auto y: *this) if(x == y) return true; return false; }
71  inline bool containsAll(const Bag<T>& c) const
72  { for(auto x: c) if(!contains(x)) return false; return true; }
73 
74  inline bool equals(const Bag<T>& c) const {
75  if(cnt != c.count()) return false;
76  for(int i = 0; i < cnt; i++) if(get(i) != c.get(i)) return false;
77  return true;
78  }
79  inline bool operator==(const Bag<T>& b) const { return equals(b); }
80  inline bool operator!=(const Bag<T>& b) const { return !equals(b); }
81 
82  inline int length() const { return count(); }
83  inline int indexOf(const T& x, int i = -1) const
84  { for(i++; i < cnt; i++) if(get(i) == x) return i; return -1; }
85  inline int lastIndexOf(const T& x, int i = -1) const
86  { if(i < 0) i = cnt; i--; while(i >= 0 && x != get(i)) i--; return i; }
87 
88 private:
89  static inline void *operator new(std::size_t size, int cnt)
90  { return new char[size + sizeof(T) * cnt]; }
91 
92  inline Bag(int count): cnt(count) { }
93  inline const T *base() const { return reinterpret_cast<const T *>(this + 1); }
94  inline T *base() { return reinterpret_cast<T *>(this + 1); }
95  int cnt;
96 };
97 
98 }; // elm
99 
100 #endif /* ELM_DATA_BAG_H_ */
elm::Bag::Iter::item
const T & item() const
Definition: Bag.h:58
elm::Bag::equals
bool equals(const Bag< T > &c) const
Definition: Bag.h:74
elm::Bag::make
static Bag< T > * make(const Array< T > &a)
Definition: Bag.h:39
elm::Array::buffer
const T * buffer(void) const
Definition: Array.h:40
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::Bag::operator[]
const T & operator[](int i) const
Definition: Bag.h:51
elm::Bag::begin
Iter begin() const
Definition: Bag.h:66
elm::Bag::get
const T & get(int i) const
Definition: Bag.h:48
elm::Vector::asArray
Array< const T > asArray(void) const
Definition: Vector.h:53
elm::Bag
Definition: Bag.h:30
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::Bag::count
int count() const
Definition: Bag.h:45
elm::Bag::isEmpty
bool isEmpty() const
Definition: Bag.h:46
elm::Bag::operator==
bool operator==(const Bag< T > &b) const
Definition: Bag.h:79
elm::Bag::containsAll
bool containsAll(const Bag< T > &c) const
Definition: Bag.h:71
bool
elm::Bag::get
T & get(int i)
Definition: Bag.h:49
elm::Bag::contains
bool contains(const T &x) const
Definition: Bag.h:69
elm::Bag::end
Iter end() const
Definition: Bag.h:67
elm::Bag::Iter::equals
bool equals(const Iter &ii) const
Definition: Bag.h:60
elm::Bag::Iter::Iter
Iter(const Bag< T > *bag, int idx=0)
Definition: Bag.h:56
elm
Definition: adapter.h:26
elm::Bag::lastIndexOf
int lastIndexOf(const T &x, int i=-1) const
Definition: Bag.h:85
elm::Vector< T >
elm::t::size
uint64 size
Definition: arch.h:35
elm::Bag::operator!=
bool operator!=(const Bag< T > &b) const
Definition: Bag.h:80
elm::Bag::Iter::next
void next()
Definition: Bag.h:59
elm::Bag::Iter::ended
bool ended() const
Definition: Bag.h:57
elm::Bag::Iter
Definition: Bag.h:54
elm::Bag::operator[]
T & operator[](int i)
Definition: Bag.h:52
elm::Bag::indexOf
int indexOf(const T &x, int i=-1) const
Definition: Bag.h:83
elm::Array::count
int count(void) const
Definition: Array.h:71
elm::Array
Definition: Array.h:32
elm::Bag::make
static Bag< T > * make(int count, const T *items)
Definition: Bag.h:36
elm::Bag::make
static Bag * make(const Vector< T > &v)
Definition: Bag.h:42
elm::Bag::length
int length() const
Definition: Bag.h:82
elm::PreIterator
Definition: iter.h:28