Panda3D
linkedListNode.I
1 // Filename: linkedListNode.I
2 // Created by: drose (16Mar06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: LinkedListNode::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE LinkedListNode::
22 LinkedListNode() {
23 #ifndef NDEBUG
24  _next = NULL;
25  _prev = NULL;
26 #endif
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: LinkedListNode::Constructor 2
31 // Access: Protected
32 // Description: This constructor should be invoked for any
33 // LinkedListNodes that will be used to serve as the
34 // root of a list. It sets up the pointers as an empty
35 // list.
36 ////////////////////////////////////////////////////////////////////
37 INLINE LinkedListNode::
38 LinkedListNode(bool) {
39  _next = this;
40  _prev = this;
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: LinkedListNode::Destructor
45 // Access: Protected
46 // Description:
47 ////////////////////////////////////////////////////////////////////
48 INLINE LinkedListNode::
49 ~LinkedListNode() {
50  nassertv((_next == NULL && _prev == NULL) || (_next == this && _prev == this));
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: LinkedListNode::is_on_list
55 // Access: Protected
56 // Description: Returns true if the node is member of any list, false
57 // if it has been removed or never added. The head of a
58 // list generally appears to to always be a member of
59 // itself.
60 ////////////////////////////////////////////////////////////////////
61 INLINE bool LinkedListNode::
62 is_on_list() const {
63  return (_next != NULL);
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: LinkedListNode::remove_from_list
68 // Access: Protected
69 // Description: Removes a LinkedListNode record from the
70 // doubly-linked list.
71 ////////////////////////////////////////////////////////////////////
72 INLINE void LinkedListNode::
73 remove_from_list() {
74  nassertv(_prev != NULL && _next != NULL);
75  nassertv(_prev->_next == this && _next->_prev == this);
76  _prev->_next = _next;
77  _next->_prev = _prev;
78 #ifndef NDEBUG
79  _next = NULL;
80  _prev = NULL;
81 #endif
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function: LinkedListNode::insert_before
86 // Access: Protected
87 // Description: Adds a LinkedListNode record before the indicated
88 // node in the doubly-linked list.
89 ////////////////////////////////////////////////////////////////////
90 INLINE void LinkedListNode::
91 insert_before(LinkedListNode *node) {
92  nassertv(node->_prev != NULL && node->_prev->_next == node && node->_next->_prev == node);
93  nassertv(_prev == (LinkedListNode *)NULL &&
94  _next == (LinkedListNode *)NULL);
95  _prev = node->_prev;
96  _next = node;
97  _prev->_next = this;
98  node->_prev = this;
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: LinkedListNode::insert_after
103 // Access: Protected
104 // Description: Adds a LinkedListNode record after the indicated
105 // node in the doubly-linked list.
106 ////////////////////////////////////////////////////////////////////
107 INLINE void LinkedListNode::
108 insert_after(LinkedListNode *node) {
109  nassertv(node->_prev != NULL && node->_prev->_next == node && node->_next->_prev == node);
110  nassertv(_prev == (LinkedListNode *)NULL &&
111  _next == (LinkedListNode *)NULL);
112  _next = node->_next;
113  _prev = node;
114  _next->_prev = this;
115  node->_next = this;
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: LinkedListNode::take_list_from
120 // Access: Protected
121 // Description: Given that this LinkedListNode represents the root of
122 // a list, and the other pointer represents the root of
123 // a different list, move all of the nodes (except the
124 // root itself) from other_root onto this list.
125 ////////////////////////////////////////////////////////////////////
126 INLINE void LinkedListNode::
127 take_list_from(LinkedListNode *other_root) {
128  other_root->_next->_prev = _prev;
129  _prev->_next = other_root->_next;
130  other_root->_prev->_next = this;
131  _prev = other_root->_prev;
132 
133  other_root->_next = other_root;
134  other_root->_prev = other_root;
135 }
This just stores the pointers to implement a doubly-linked list of some kind of object.