Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
ListQueue.h
1 /*
2  * ListQueue class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2015, IRIT UPS.
6  *
7  * ELM 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  * ELM 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 ELM; 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_LISTQUEUE_H_
22 #define ELM_DATA_LISTQUEUE_H_
23 
24 #include <elm/assert.h>
25 #include <elm/data/Manager.h>
26 
27 namespace elm {
28 
29 template <class T, class M = EquivManager<T> >
30 class ListQueue {
31 
32  class Node {
33  public:
34  inline Node(const T& v, Node *n = 0): next(n), val(v) { }
35  Node *next;
36  T val;
37  void *operator new(size_t s, M& m) { return m.alloc.allocate(s); }
38  inline void free(M& m) { this->~Node(); m.alloc.free(this); }
39  private:
40  inline ~Node(void) { }
41  };
42 
43 public:
44  inline ListQueue(void): h(0), t(0), _man(single<M>()) { }
45  inline ~ListQueue(void) { reset(); }
46 
47  inline bool isEmpty(void) const { return !h; }
48  inline const T &head(void) const { ASSERTP(h, "empty queue"); return h->val; }
49  inline T get(void)
50  { ASSERTP(h, "empty queue"); T r = h->val; Node *n = h; h = h->next; if(!h) t = 0; n->free(_man); return r; }
51  inline bool contains(const T& val)
52  { for(Node *n = h; n; n = n->next) if(_man.eq.equals(n->val, val)) return true; return false; }
53 
54  inline void put(const T &item)
55  { Node *n = new(_man) Node(item); (h ? t->next : h) = n; t = n; }
56  inline void reset(void)
57  { for(Node *n = h, *nn; n; n = nn) { nn = n->next; n->free(_man); } }
58 
59  inline operator bool(void) const { return !isEmpty(); }
60  inline ListQueue& operator<<(const T& v) { put(v); return *this; }
61  inline ListQueue& operator>>(T& v) { v = get(); return *this; }
62 
63 private:
64  Node *h, *t;
65  M& _man;
66 };
67 
68 } // elm
69 
70 #endif /* ELM_DATA_LISTQUEUE_H_ */
elm::ListQueue::operator>>
ListQueue & operator>>(T &v)
Definition: ListQueue.h:61
elm::ListQueue::get
T get(void)
Definition: ListQueue.h:49
elm::ListQueue::put
void put(const T &item)
Definition: ListQueue.h:54
bool
elm::ListQueue::head
const T & head(void) const
Definition: ListQueue.h:48
elm::ListQueue::operator<<
ListQueue & operator<<(const T &v)
Definition: ListQueue.h:60
elm::ListQueue::reset
void reset(void)
Definition: ListQueue.h:56
elm
Definition: adapter.h:26
elm::single
T & single(void)
Definition: types.h:35
elm::ListQueue::ListQueue
ListQueue(void)
Definition: ListQueue.h:44
elm::ListQueue::contains
bool contains(const T &val)
Definition: ListQueue.h:51
elm::ListQueue::isEmpty
bool isEmpty(void) const
Definition: ListQueue.h:47
elm::ListQueue::~ListQueue
~ListQueue(void)
Definition: ListQueue.h:45
elm::ListQueue
Definition: ListQueue.h:30