feisty meow concerns codebase  2.140
doubly_linked_list.h
Go to the documentation of this file.
1 #ifndef DOUBLY_LINKED_LIST_CLASS
2 #define DOUBLY_LINKED_LIST_CLASS
3 
4 /*
5 * Name : doubly_linked_list
6 * Author : Chris Koeritz
7 **
8 * Copyright (c) 1998-$now By Author. This program is free software; you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either version 2 of
11 * the License or (at your option) any later version. This is online at:
12 * http://www.fsf.org/copyleft/gpl.html
13 * Please send any updates to: fred@gruntose.com
14 */
15 
16 namespace nodes {
17 
18 class node; // forward.
19 
21 
33 //hmmm: is this class really for real? it's doing all sorts of stuff with nodes, rather than with the list object itself.
34 // consider dropping the current implementation and providing more standard list operations, on an object that is actually using itself in its definition, rather than a different class (node).
35 // this would be easy to do if we just break down and define the dl list as a node itself. then we have something to work with.
36 
37 //current iterator implementation here is bunk.
38 
39 
41 {
42 public:
45 
48 
49  int elements() const;
51 
53  bool empty() const;
55 
58 
62  class iterator {
63  public:
64  iterator(const doubly_linked_list *mgr, node *start) : _cursor(start), _manager(mgr) {}
66 
69  void operator ++();
70  void operator --();
71  void operator ++(int) { ++(*this); }
72  void operator --(int) { --(*this); }
73 
74 
75  void next() { (*this)++; }
76  void previous() { (*this)--; }
77 
78  bool operator ==(const iterator &to_compare) const;
80 
81  bool is_head() const;
83 
92  bool is_tail() const;
94 
95  void jump_head();
96  void jump_tail();
97 
98  const node *observe();
107  node *access();
108 
109  private:
110  node *_cursor;
111  friend class doubly_linked_list;
112  const doubly_linked_list *_manager;
113  };
114 
115  iterator head() const { return iterator(this, _head); }
117  iterator tail() const { return iterator(this, _tail); }
119 
120  int index(const iterator &it) const { return items_from_head(it); }
122 
124  bool set_index(iterator &to_set, int new_index);
126 
127  // storage and retrieval actions.
128  // Note: all of these methods shift the iterator onto the next valid node
129  // if it is positioned at the beginning or end of the list.
130 
131  void append(iterator &where, node *new_node);
133 
137  void insert(iterator &where, node *new_node);
139 
144  node *remove(iterator &where);
146 
150  void zap(iterator &where);
152 
155  void zap_all();
157 
159  // the following positioning functions could fail if the request is out of
160  // bounds. for example, forward cannot go beyond the end of the list. in
161  // such cases, false is returned and the list pointer is not moved.
162 
163  bool forward(iterator &where, int count);
165 
168  bool backward(iterator &where, int count);
170 
171  int items_from_head(const iterator &where) const;
173 
174  int items_from_tail(const iterator &where) const;
176 
178 private:
179  friend class iterator;
180  node *_head;
181  node *_tail;
182 
183  bool skip_or_ignore(iterator &where, int count);
185 
186  // not permitted.
188  doubly_linked_list &operator =(const doubly_linked_list &);
189 };
190 
191 } // namespace.
192 
193 #endif
194 
iterators allow the list to be traversed.
void jump_head()
set the iterator to the head.
void jump_tail()
set the iterator to the tail.
iterator(const doubly_linked_list *mgr, node *start)
opens up an iterator on a list.
bool is_tail() const
returns true if the cursor is at the tail of the list.
bool is_head() const
returns true if the cursor is at the head of the list.
node * access()
obtain access to the current element.
const node * observe()
peek at the current element.
bool operator==(const iterator &to_compare) const
returns true if the two iterators are at the same position.
void operator--()
go to previous item.
Implements a guarded, doubly linked list structure.
bool backward(iterator &where, int count)
moves the list pointer "count" items towards the head.
iterator tail() const
returns an iterator located at the tail of the list.
bool forward(iterator &where, int count)
moves the list pointer "count" items towards the tail.
int elements() const
returns the number of items currently in the list.
int index(const iterator &it) const
returns the zero-based index of the cursor's position from the head.
bool empty() const
returns true if the list is empty.
~doubly_linked_list()
invalidates all contents of the list and destroys all child nodes.
int items_from_head(const iterator &where) const
Returns the number of elements between the current item and the head.
doubly_linked_list()
constructs a blank list.
iterator head() const
returns an iterator located at the head of the list.
node * remove(iterator &where)
extracts the current item from "where" and repairs the hole.
void append(iterator &where, node *new_node)
adds a "new_node" into the list immediately after "where".
void zap(iterator &where)
wipes out the item at "where", including its contents.
int items_from_tail(const iterator &where) const
Returns the number of elements between the current item and the tail.
void zap_all()
destroys all the list elements and makes the list empty.
void insert(iterator &where, node *new_node)
places a "new_node" immediately before the current node in the list.
bool set_index(iterator &to_set, int new_index)
positions the iterator "to_set" at the index specified.
An object representing the interstitial cell in most linked data structures.
Definition: node.h:41