Otawa  0.10
Bag.h
Go to the documentation of this file.
1 /*
2  * otawa::Bag class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2014, 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 OTAWA_UTIL_BAG_H
22 #define OTAWA_UTIL_BAG_H
23 
24 #include <elm/assert.h>
25 #include <elm/util/array.h>
26 #include <elm/genstruct/Vector.h>
27 #include <elm/util/Pair.h>
28 
29 namespace otawa {
30 
31 using namespace elm;
32 
33 template <class T>
34 class Give {
35 public:
36  inline Give(int c, T *a): cnt(c), arr(a) { }
37  inline Give(genstruct::Vector<T>& v): cnt(v.length()), arr(v.detach()) { }
38  inline int count(void) const { return cnt; }
39  inline T *array(void) const { return arr; }
40 private:
41  int cnt;
42  T *arr;
43 };
44 
45 template <class T>
46 class Bag {
47 public:
48 
49  // constructors
50  inline Bag(void): cnt(0), arr(0) { }
51  inline Bag(const Bag& bag) { copy(bag.cnt, bag.arr); }
52  inline Bag(int c, const T *a) { copy(c, a); }
53  inline Bag(int c, T *a) { copy(c, a); }
54  inline Bag(const genstruct::Vector<T>& v) { copy(v); }
55  inline Bag(Pair<int, T *> p) { copy(p.fst, p.snd); }
56  inline Bag(const Give<T>& g): cnt(g.count()), arr(g.array()) { }
57  inline ~Bag(void) { clear(); }
58 
59  // accessors
60  inline bool isEmpty(void) const { return cnt == 0; }
61  inline operator bool(void) const { return !isEmpty(); }
62  inline int count(void) const { return cnt; }
63  inline int size(void) const { return count(); }
64  inline const T& get(int i) const { ASSERT(i >= 0 && i < cnt); return arr[i]; }
65  inline T& get(int i) { ASSERT(i >= 0 && i < cnt); return arr[i]; }
66  inline const T& operator[](int i) const { return get(i); }
67  inline T& operator[](int i) { return get(i); }
68 
69  // mutators
70  inline Bag& operator=(const Bag& bag) { clear(); copy(bag.cnt, bag.arr); return *this; }
71  inline Bag& operator=(const genstruct::Vector<T>& v) { clear(); copy(v); return *this; }
72  inline Bag& operator=(Pair<int, T *> p) { clear(); copy(p.fst, p.snd); return *this; }
73  inline Bag& operator=(const Give<T>& g) { clear(); cnt = g.count(); arr = g.array(); return *this; }
74  inline void clear(void) { if(arr) delete [] arr; }
75 
76 private:
77  inline void copy(int c, const T *a)
78  { cnt = c; arr = new T[c]; elm::array::copy(arr, a, c); }
79  inline void copy(const genstruct::Vector<T>& v)
80  { cnt = v.length(); arr = new T[cnt]; for(int i = 0; i < cnt; i++) arr[i] = v[i]; }
81  int cnt;
82  T *arr;
83 };
84 
85 }; // otawa
86 
87 #endif // OTAWA_UTIL_BAG_H
void copy(T *target, const T *source, int size)
Definition: Bag.h:34
void clear(void)
Clear the content of the bag.
Definition: Bag.h:74
T & operator[](int i)
Definition: Bag.h:67
Bag(const Bag &bag)
Clone constructor.
Definition: Bag.h:51
Bag(const Give< T > &g)
Construction from a given array.
Definition: Bag.h:56
Bag & operator=(Pair< int, T * > p)
Definition: Bag.h:72
void copy(int c, const T *a)
Definition: Bag.h:77
Bag & operator=(const genstruct::Vector< T > &v)
Definition: Bag.h:71
Bag(Pair< int, T * > p)
Construction from a pair (array count, array base).
Definition: Bag.h:55
Bag & operator=(const Give< T > &g)
Definition: Bag.h:73
Give(genstruct::Vector< T > &v)
Definition: Bag.h:37
Give(int c, T *a)
Definition: Bag.h:36
int length(void) const
Bag(int c, T *a)
Constructor using a raw array.
Definition: Bag.h:53
int cnt
Definition: Bag.h:81
int size(void) const
Same as count().
Definition: Bag.h:63
T * arr
Definition: Bag.h:82
const T & operator[](int i) const
Definition: Bag.h:66
Bag & operator=(const Bag &bag)
Definition: Bag.h:70
bool isEmpty(void) const
Test if the bag is empty.
Definition: Bag.h:60
int cnt
Definition: Bag.h:41
T * arr
Definition: Bag.h:42
void clear(T *target, int size)
Bag(int c, const T *a)
Constructor using a raw array.
Definition: Bag.h:52
void copy(const genstruct::Vector< T > &v)
Definition: Bag.h:79
T * array(void) const
Definition: Bag.h:39
int count(void) const
Definition: Bag.h:38
~Bag(void)
Definition: Bag.h:57
Bag(void)
Empty array constructor.
Definition: Bag.h:50
int count(void) const
Get the count of items in the bag.
Definition: Bag.h:62
Bag(const genstruct::Vector< T > &v)
Constructor from a vector.
Definition: Bag.h:54
Definition: Bag.h:46