Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VectorQueue.h
1 /*
2  * $Id$
3  * VectorQueue class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2006-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_GENSTRUCT_VECTORQUEUE_H
23 #define ELM_GENSTRUCT_VECTORQUEUE_H
24 
25 #include <elm/assert.h>
26 #include <elm/util/Equiv.h>
27 
28 namespace elm { namespace genstruct {
29 
30 // VectorQueue class
31 template <class T, class E = Equiv<T> >
32 class VectorQueue {
33  int hd, tl, cap;
34  T *buffer;
35  void enlarge(void);
36 public:
37  inline VectorQueue(int capacity = 4);
38  inline ~VectorQueue(void);
39 
40  inline int capacity(void) const;
41  inline int size(void) const;
42  inline bool isEmpty(void) const;
43 
44  inline bool contains(const T& val) const {
45  for(int i = hd; i != tl; i = (i + 1) & (cap - 1))
46  if(E::equals(buffer[i], val))
47  return true;
48  return false;
49  }
50 
51  inline void put(const T& value);
52  inline const T& get(void);
53  inline T& head(void) const;
54  inline void reset(void);
55 
56  // Operators
57  inline operator bool(void) const;
58  inline T *operator->(void) const;
59  inline T& operator*(void) const;
60 };
61 
62 // VectorQueue implementation
63 
64 template <class T, class E> void VectorQueue<T, E>::enlarge(void) {
65  int new_cap = cap * 2, off = 0;
66  T *new_buffer = new T[new_cap];
67  if( hd > tl) {
68  off = cap - hd;
69  for(int i = 0; i < off; i++)
70  new_buffer[i] = buffer[hd + i];
71  hd = 0;
72  }
73  for(int i = hd; i < tl; i++)
74  new_buffer[off + i - hd] = buffer[i];
75  delete [] buffer;
76  tl = off + tl - hd;
77  cap = new_cap;
78  buffer = new_buffer;
79 }
80 
81 template <class T, class E> inline VectorQueue<T, E>::VectorQueue(int capacity)
82 : hd(0), tl(0), cap(1 << capacity), buffer(new T[cap]) {
83  ASSERTP(cap >= 0, "capacity must be positive");
84 }
85 
86 template <class T, class E> inline VectorQueue<T, E>::~VectorQueue(void) {
87  delete [] buffer;
88 }
89 
90 template <class T, class E> inline int VectorQueue<T, E>::capacity(void) const {
91  return cap;
92 }
93 
94 template <class T, class E> inline int VectorQueue<T, E>::size(void) const {
95  if(hd < tl)
96  return tl - hd;
97  else
98  return (cap - hd) + tl;
99 }
100 
101 template <class T, class E> inline bool VectorQueue<T, E>::isEmpty(void) const {
102  return hd == tl;
103 }
104 
105 template <class T, class E> inline void VectorQueue<T, E>::put(const T& value) {
106  int new_tl = (tl + 1) & (cap - 1);
107  if(new_tl == hd) {
108  enlarge();
109  new_tl = tl + 1;
110  }
111  buffer[tl] = value;
112  tl = new_tl;
113 }
114 
115 template <class T, class E> inline const T& VectorQueue<T, E>::get(void) {
116  ASSERTP(hd != tl, "queue empty");
117  int res = hd;
118  hd = (hd + 1) & (cap - 1);
119  return buffer[res];
120 }
121 
122 template <class T, class E> inline T& VectorQueue<T, E>::head(void) const {
123  ASSERTP(hd != tl, "queue empty");
124  return buffer[hd];
125 }
126 
127 template <class T, class E> inline void VectorQueue<T, E>::reset(void) {
128  hd = 0;
129  tl = 0;
130 }
131 
132 template <class T, class E> inline VectorQueue<T, E>::operator bool(void) const {
133  return !isEmpty();
134 }
135 
136 template <class T, class E> inline T *VectorQueue<T, E>::operator->(void) const {
137  return &head();
138 }
139 
140 template <class T, class E> inline T& VectorQueue<T, E>::operator*(void) const {
141  return head();
142 }
143 
144 } } // elm::genstruct
145 
146 #endif // ELM_GENSTRUCT_VECTORQUEUE_H
147 
T * operator->(void) const
Definition: VectorQueue.h:136
bool contains(const T &val) const
Definition: VectorQueue.h:44
bool isEmpty(void) const
Definition: VectorQueue.h:101
T & head(void) const
Definition: VectorQueue.h:122
int capacity(void) const
Definition: VectorQueue.h:90
VectorQueue(int capacity=4)
Definition: VectorQueue.h:81
~VectorQueue(void)
Definition: VectorQueue.h:86
value_t value(CString name, int value)
Definition: rtti.h:40
void put(const T &value)
Definition: VectorQueue.h:105
void reset(void)
Definition: VectorQueue.h:127
T & operator*(void) const
Definition: VectorQueue.h:140
const T & get(void)
Definition: VectorQueue.h:115
int size(void) const
Definition: VectorQueue.h:94
Definition: VectorQueue.h:32