Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
StaticStack.h
1 /*
2  * StaticStack class
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, 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_STATICSTACK_H_
22 #define ELM_DATA_STATICSTACK_H_
23 
24 #include <elm/array.h>
25 #include <elm/assert.h>
26 #include <elm/PreIterator.h>
27 
28 namespace elm {
29 
30 template <class T, int N>
31 class StaticStack {
32 public:
33  inline StaticStack(void): _t(0) { }
34  inline StaticStack(const StaticStack<T, N>& s): _t(0) { copy(s); }
35  inline void copy(const StaticStack<T, N>& s)
36  { _t = s._t; array::copy(_s, s._s, _t); }
38  { copy(s); return *this; }
39 
40  // stack concept
41  inline bool isEmpty(void) const { return _t == 0; }
42  inline const T& top(void) const { return _s[_t - 1]; }
43  inline const T& pop(void) { ASSERTP(_t != 0, "empty stack"); _t--; return _s[_t]; }
44  inline void push(const T& v) { ASSERTP(_t != N, "stack full"); _s[_t] = v; _t++; }
45  inline void reset(void) { _t = 0; }
46 
47  // Iterator
48  class Iter: public PreIterator<Iter, T> {
49  public:
50  inline Iter(const StaticStack& s, int i = 0): _s(s), _i(i) { }
51  inline bool ended(void) const { return _i >= _s._t; }
52  inline const T& item(void) const { return _s._s[_i]; }
53  inline void next(void) { _i++; }
54  private:
55  const StaticStack& _s;
56  int _i;
57  };
58 
59  // Collection concept
60  inline int count(void) const { return _t; }
61  inline bool contains(const T& v)
62  { for(int i = _t; i >= 0; i--) if(_s[i] == v) return true; return false; }
63  template <class C>
64  bool containsAll(const C& coll)
65  { for(typename C::Iter i(coll); i; i++) if(!contains(*i)) return false; return true; }
66  inline operator bool(void) const { return !isEmpty(); }
67  inline Iter begin(void) const { return Iter(*this); }
68  inline Iter end(void) const { return Iter(*this, _t); }
69  inline Iter items(void) const { return begin(); }
70  inline Iter operator*(void) const { return begin(); }
71  inline bool equals(const StaticStack<T, N>& s) const
72  { if(_t != s._t) return false; for(int i = 0; i < _t; i++) if(_s[i] != s._s[i]) return false; return true; }
73  inline bool contains(const StaticStack<T, N>& s) const
74  { for(int i = 0; i < s._t; i++) if(!contains(s._s[i])) return false; return true; }
75  inline bool operator==(const StaticStack<T, N>& s) const { return equals(s); }
76  inline bool operator!=(const StaticStack<T, N>& s) const { return !equals(s); }
77  inline bool operator<=(const StaticStack<T, N>& s) const { return s.contains(*this); }
78  inline bool operator>=(const StaticStack<T, N>& s) const { return contains(s); }
79  inline bool operator<(const StaticStack<T, N>& s) const { return !equals(s) && s.contains(*this); }
80  inline bool operator>(const StaticStack<T, N>& s) const { return !equals(s) && contains(s); }
81 
82 private:
83  T _s[N];
84  int _t;
85 };
86 
87 } // otawa
88 
89 #endif /* ELM_DATA_STATICSTACK_H_ */
elm::StaticStack::operator>=
bool operator>=(const StaticStack< T, N > &s) const
Definition: StaticStack.h:78
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::StaticStack::Iter::next
void next(void)
Definition: StaticStack.h:53
elm::StaticStack::operator==
bool operator==(const StaticStack< T, N > &s) const
Definition: StaticStack.h:75
elm::StaticStack::copy
void copy(const StaticStack< T, N > &s)
Definition: StaticStack.h:35
elm::StaticStack::Iter
Definition: StaticStack.h:48
elm::StaticStack::isEmpty
bool isEmpty(void) const
Definition: StaticStack.h:41
elm::StaticStack::pop
const T & pop(void)
Definition: StaticStack.h:43
bool
elm::StaticStack::contains
bool contains(const T &v)
Definition: StaticStack.h:61
elm::StaticStack::operator<
bool operator<(const StaticStack< T, N > &s) const
Definition: StaticStack.h:79
elm::StaticStack::Iter::ended
bool ended(void) const
Definition: StaticStack.h:51
elm::StaticStack::Iter::item
const T & item(void) const
Definition: StaticStack.h:52
elm::StaticStack::operator<=
bool operator<=(const StaticStack< T, N > &s) const
Definition: StaticStack.h:77
elm
Definition: adapter.h:26
elm::StaticStack::reset
void reset(void)
Definition: StaticStack.h:45
elm::StaticStack
Definition: StaticStack.h:31
elm::StaticStack::operator>
bool operator>(const StaticStack< T, N > &s) const
Definition: StaticStack.h:80
elm::StaticStack::count
int count(void) const
Definition: StaticStack.h:60
elm::StaticStack::items
Iter items(void) const
Definition: StaticStack.h:69
elm::StaticStack::StaticStack
StaticStack(void)
Definition: StaticStack.h:33
elm::StaticStack::begin
Iter begin(void) const
Definition: StaticStack.h:67
elm::StaticStack::contains
bool contains(const StaticStack< T, N > &s) const
Definition: StaticStack.h:73
elm::StaticStack::operator!=
bool operator!=(const StaticStack< T, N > &s) const
Definition: StaticStack.h:76
elm::StaticStack::operator*
Iter operator*(void) const
Definition: StaticStack.h:70
elm::PreIterator
Definition: iter.h:28
elm::StaticStack::end
Iter end(void) const
Definition: StaticStack.h:68
elm::StaticStack::StaticStack
StaticStack(const StaticStack< T, N > &s)
Definition: StaticStack.h:34
elm::StaticStack::Iter::Iter
Iter(const StaticStack &s, int i=0)
Definition: StaticStack.h:50
elm::StaticStack::containsAll
bool containsAll(const C &coll)
Definition: StaticStack.h:64
elm::StaticStack::operator=
StaticStack< T, N > & operator=(const StaticStack< T, N > &s)
Definition: StaticStack.h:37
elm::StaticStack::push
void push(const T &v)
Definition: StaticStack.h:44
elm::StaticStack::top
const T & top(void) const
Definition: StaticStack.h:42
elm::StaticStack::equals
bool equals(const StaticStack< T, N > &s) const
Definition: StaticStack.h:71