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
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/inhstruct/SLList.h>
26 
27 namespace elm {
28 
29 template <class T, class E = Equiv<T> >
30 class ListQueue {
31 
32  typedef struct node_t {
33  inline node_t(const T& v, node_t *n = 0): next(n), val(v) { }
34  node_t *next;
35  T val;
36  } node_t;
37 
38 public:
39  inline ListQueue(void): h(0), t(0) { }
40  inline ~ListQueue(void) { reset(); }
41 
42  inline bool isEmpty(void) const { return !h; }
43  inline const T &head(void) const { ASSERTP(h, "empty queue"); return h->val; }
44  inline T get(void)
45  { ASSERTP(h, "empty queue"); T r = h->val; node_t *n = h; h = h->next; if(!h) t = 0; delete n; return r; }
46 
47  inline void put(const T &item)
48  { node_t *n = new node_t(item); (h ? t->next : h) = n; t = n; }
49  inline void reset(void)
50  { for(node_t *n = h, *nn; n; n = nn) { nn = n->next; delete n; } }
51 
52  inline ListQueue& operator<<(const T& v) { put(v); return *this; }
53  inline ListQueue& operator>>(T& v) { v = get(); return *this; }
54 
55 private:
56  node_t *h, *t;
57 };
58 
59 } // elm
60 
61 #endif /* ELM_DATA_LISTQUEUE_H_ */
const T & head(void) const
Definition: ListQueue.h:43
ListQueue & operator<<(const T &v)
Definition: ListQueue.h:52
bool isEmpty(void) const
Definition: ListQueue.h:42
~ListQueue(void)
Definition: ListQueue.h:40
void reset(void)
Definition: ListQueue.h:49
void put(const T &item)
Definition: ListQueue.h:47
ListQueue(void)
Definition: ListQueue.h:39
Definition: ListQueue.h:30
ListQueue & operator>>(T &v)
Definition: ListQueue.h:53