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
DLList.h
1 /*
2  * $Id$
3  * Copyright (c) 2004, Alfheim Corporation.
4  *
5  * dllist.h -- double link list classes interface.
6  */
7 #ifndef ELM_INHSTRUCT_DLLIST_H
8 #define ELM_INHSTRUCT_DLLIST_H
9 
10 #include <elm/assert.h>
11 
12 namespace elm { namespace inhstruct {
13 
14 // DLList class
15 class DLList;
16 class DLNode {
17  friend class DLList;
18  DLNode *nxt, *prv;
19 public:
20  inline DLNode *next(void) const;
21  inline DLNode *previous(void) const;
22  inline bool atBegin(void) const;
23  inline bool atEnd(void) const;
24 
25  inline void replace(DLNode *node);
26  inline void insertAfter(DLNode *node);
27  inline void insertBefore(DLNode *node);
28  inline void remove(void);
29  inline void removeNext(void);
30  inline void removePrevious(void);
31 };
32 
33 
34 // DLList class
35 class DLList {
36  DLNode hd, tl;
37 public:
38  inline DLList(void);
39  inline DLList(DLList& list);
40 
41  inline DLNode *first(void) const;
42  inline DLNode *last(void) const;
43  inline bool isEmpty(void) const;
44  inline int count(void) const;
45 
46  inline void addFirst(DLNode *node);
47  inline void addLast(DLNode *node);
48  inline void removeFirst(void);
49  inline void removeLast(void);
50 };
51 
52 
53 // DLNode methods
54 DLNode *DLNode::next(void) const {
55  return nxt;
56 }
57 DLNode *DLNode::previous(void) const {
58  return prv;
59 }
60 void DLNode::replace(DLNode *node) {
61  ASSERTP(node, "null node for replacement");
62  nxt->prv = node;
63  node->nxt = nxt;
64  prv->nxt = node;
65  node->prv = prv;
66 }
68  ASSERTP(node, "null node to insert");
69  nxt->prv = node;
70  node->nxt = nxt;
71  nxt = node;
72  node->prv = this;
73 }
75  ASSERTP(node, "null node to insert");
76  prv->nxt = node;
77  node->prv = prv;
78  prv = node;
79  node->nxt = this;
80 }
81 void DLNode::remove(void) {
82  prv->nxt = nxt;
83  nxt->prv = prv;
84 }
85 void DLNode::removeNext(void) {
86  ASSERTP(!nxt->atEnd(), "no next node");
87  nxt->remove();
88 }
90  ASSERTP(!prv->atBegin(), "no previous node");
91  prv->remove();
92 }
93 bool DLNode::atBegin(void) const {
94  return prv == 0;
95 }
96 bool DLNode::atEnd(void) const {
97  return nxt == 0;
98 }
99 
100 
101 // DLList methods
103  hd.nxt = &tl;
104  hd.prv = 0;
105  tl.prv = &hd;
106  tl.nxt = 0;
107 }
108 
109 inline DLList::DLList(DLList& list) {
110  hd.prv = 0;
111  tl.nxt = 0;
112  if(list.isEmpty()) {
113  hd.nxt = &tl;
114  tl.prv = &hd;
115  }
116  else {
117  hd.nxt = list.hd.nxt;
118  list.hd.nxt = 0;
119  hd.nxt->prv = &hd;
120  tl.prv = list.tl.prv;
121  list.tl.prv = 0;
122  tl.prv->nxt = &tl;
123 
124  }
125 }
126 
127 DLNode *DLList::first(void) const {
128  return hd.nxt;
129 }
130 DLNode *DLList::last(void) const {
131  return tl.prv;
132 }
133 bool DLList::isEmpty(void) const {
134  return hd.nxt == &tl;
135 }
136 int DLList::count(void) const {
137  int cnt = 0;
138  for(DLNode *cur = hd.nxt; cur != &tl; cur =cur->nxt)
139  cnt++;
140  return cnt;
141 }
143  ASSERTP(node, "null node added");
144  hd.insertAfter(node);
145 }
146 void DLList::addLast(DLNode *node) {
147  ASSERTP(node, "null node added");
148  tl.insertBefore(node);
149 }
151  ASSERTP(!isEmpty(), "list empty");
152  hd.removeNext();
153 }
154 void DLList::removeLast(void) {
155  ASSERTP(!isEmpty(), "list empty");
156  tl.removePrevious();
157 }
158 
159 
160 } } // elm::inhstruct
161 
162 #endif // ELM_INHSTRUCT_DLLIST_H
bool isEmpty(void) const
Definition: DLList.h:133
DLNode * last(void) const
Definition: DLList.h:130
DLNode * previous(void) const
Definition: DLList.h:57
int count(void) const
Definition: DLList.h:136
Definition: DLList.h:16
void removePrevious(void)
Definition: DLList.h:89
void replace(DLNode *node)
Definition: DLList.h:60
void removeFirst(void)
Definition: DLList.h:150
bool atEnd(void) const
Definition: DLList.h:96
void addFirst(DLNode *node)
Definition: DLList.h:142
DLList(void)
Definition: DLList.h:102
void insertAfter(DLNode *node)
Definition: DLList.h:67
DLNode * next(void) const
Definition: DLList.h:54
void remove(void)
Definition: DLList.h:81
DLNode * first(void) const
Definition: DLList.h:127
Definition: DLList.h:35
void addLast(DLNode *node)
Definition: DLList.h:146
bool atBegin(void) const
Definition: DLList.h:93
void insertBefore(DLNode *node)
Definition: DLList.h:74
void removeLast(void)
Definition: DLList.h:154
void removeNext(void)
Definition: DLList.h:85