Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
DLList.h
1 /*
2  * inhstruct::BiDiList class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2004-16, 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_INHSTRUCT_DLLIST_H
22 #define ELM_INHSTRUCT_DLLIST_H
23 
24 #include <elm/assert.h>
25 
26 namespace elm { namespace inhstruct {
27 
28 // DLList class
29 class DLList;
30 class DLNode {
31  friend class DLList;
32  DLNode *nxt, *prv;
33 public:
34  inline DLNode *next(void) const { return nxt; }
35  inline DLNode *previous(void) const { return prv; }
36  inline bool atBegin(void) const { return prv == 0; }
37  inline bool atEnd(void) const { return nxt == 0; }
38 
39  inline void replace(DLNode *node) {
40  ASSERTP(node, "null node for replacement");
41  nxt->prv = node; node->nxt = nxt;
42  prv->nxt = node; node->prv = prv;
43  }
44 
45  inline void insertAfter(DLNode *node) {
46  ASSERTP(node, "null node to insert");
47  nxt->prv = node; node->nxt = nxt;
48  nxt = node; node->prv = this;
49  }
50 
51  inline void insertBefore(DLNode *node) {
52  ASSERTP(node, "null node to insert");
53  prv->nxt = node; node->prv = prv;
54  prv = node; node->nxt = this;
55  }
56 
57  inline void remove(void)
58  { prv->nxt = nxt; nxt->prv = prv; }
59  inline void removeNext(void)
60  { ASSERTP(!nxt->atEnd(), "no next node"); nxt->remove(); }
61  inline void removePrevious(void)
62  { ASSERTP(!prv->atBegin(), "no previous node"); prv->remove(); }
63 };
64 
65 
66 // DLList class
67 class DLList {
68  mutable DLNode hd, tl;
69 public:
70  inline DLList(void) {
71  hd.nxt = &tl; hd.prv = 0;
72  tl.prv = &hd; tl.nxt = 0;
73  }
74 
75  inline DLList(DLList& list) {
76  hd.prv = 0;
77  tl.nxt = 0;
78  if(list.isEmpty()) {
79  hd.nxt = &tl;
80  tl.prv = &hd;
81  }
82  else {
83  hd.nxt = list.hd.nxt;
84  list.hd.nxt = 0;
85  hd.nxt->prv = &hd;
86  tl.prv = list.tl.prv;
87  list.tl.prv = 0;
88  tl.prv->nxt = &tl;
89 
90  }
91  }
92 
93  inline DLNode *first(void) const { return hd.nxt; }
94  inline DLNode *last(void) const { return tl.prv; }
95  inline bool isEmpty(void) const { return hd.nxt == &tl; }
96  inline DLNode *head(void) const { return &hd; }
97  inline DLNode *tail(void) const { return &tl; }
98 
99  inline int count(void) const {
100  int cnt = 0;
101  for(DLNode *cur = hd.nxt; cur != &tl; cur =cur->nxt)
102  cnt++;
103  return cnt;
104  }
105 
106  inline void addFirst(DLNode *node)
107  { ASSERTP(node, "null node added"); hd.insertAfter(node); }
108  inline void addLast(DLNode *node)
109  { ASSERTP(node, "null node added"); tl.insertBefore(node); }
110  inline void removeFirst(void)
111  { ASSERTP(!isEmpty(), "list empty"); hd.removeNext(); }
112  inline void removeLast(void)
113  { ASSERTP(!isEmpty(), "list empty"); tl.removePrevious(); }
114 };
115 
116 
117 } } // elm::inhstruct
118 
119 #endif // ELM_INHSTRUCT_DLLIST_H
elm::inhstruct::DLList::first
DLNode * first(void) const
Definition: DLList.h:93
elm::inhstruct::DLNode::next
DLNode * next(void) const
Definition: DLList.h:34
elm::inhstruct::DLNode::atEnd
bool atEnd(void) const
Definition: DLList.h:37
elm::inhstruct::DLList::head
DLNode * head(void) const
Definition: DLList.h:96
elm::inhstruct::DLNode::removeNext
void removeNext(void)
Definition: DLList.h:59
elm::inhstruct::DLList::addFirst
void addFirst(DLNode *node)
Definition: DLList.h:106
DLList
elm::inhstruct::DLList::count
int count(void) const
Definition: DLList.h:99
elm::inhstruct::DLNode::previous
DLNode * previous(void) const
Definition: DLList.h:35
elm::inhstruct::DLList::tail
DLNode * tail(void) const
Definition: DLList.h:97
elm::inhstruct::DLList::removeFirst
void removeFirst(void)
Definition: DLList.h:110
elm::inhstruct::DLNode::atBegin
bool atBegin(void) const
Definition: DLList.h:36
elm::inhstruct::DLList
Definition: DLList.h:67
elm::inhstruct::DLNode::insertAfter
void insertAfter(DLNode *node)
Definition: DLList.h:45
elm::inhstruct::DLNode::replace
void replace(DLNode *node)
Definition: DLList.h:39
elm::inhstruct::DLList::DLList
DLList(DLList &list)
Definition: DLList.h:75
elm::io::list
ListPrinter< T > list(const T &l, cstring s="", typename ListPrinter< T >::fun_t f=ListPrinter< T >::asis)
Definition: Output.h:321
elm
Definition: adapter.h:26
elm::inhstruct::DLNode::remove
void remove(void)
Definition: DLList.h:57
elm::inhstruct::DLList::addLast
void addLast(DLNode *node)
Definition: DLList.h:108
elm::inhstruct::DLList::DLList
DLList(void)
Definition: DLList.h:70
elm::inhstruct::DLList::isEmpty
bool isEmpty(void) const
Definition: DLList.h:95
elm::inhstruct::DLList::last
DLNode * last(void) const
Definition: DLList.h:94
elm::inhstruct::DLNode::insertBefore
void insertBefore(DLNode *node)
Definition: DLList.h:51
elm::inhstruct::DLList::removeLast
void removeLast(void)
Definition: DLList.h:112
elm::inhstruct::DLNode::removePrevious
void removePrevious(void)
Definition: DLList.h:61
elm::inhstruct::DLNode
Definition: DLList.h:30