blob: 31fb9c56fe4ec83f684966f953e45a16a9884c88 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
kurtis.heimerl5a872472013-05-31 21:47:25 +00004* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion.
5*
dburgess82c46ff2011-10-07 02:40:51 +00006* This software is distributed under the terms of the GNU Affero Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Affero General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Affero General Public License for more details.
21
22 You should have received a copy of the GNU Affero General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
27
28
29#ifndef LINKEDLISTS_H
30#define LINKEDLISTS_H
31
32#include <stdlib.h>
kurtis.heimerl5a872472013-05-31 21:47:25 +000033#include <assert.h>
dburgess82c46ff2011-10-07 02:40:51 +000034
35
36
37/** This node class is used to build singly-linked lists. */
38class ListNode {
39
40 private:
41
42 ListNode* mNext;
43 void* mData;
44
45 public:
46
47 ListNode* next() { return mNext; }
48 void next(ListNode* wNext) { mNext=wNext; }
49
50 void* data() { return mData; }
51 void data(void* wData) { mData=wData; }
52};
53
54
55
56
57/** A fast FIFO for pointer-based storage. */
58class PointerFIFO {
59
kurtis.heimerl5a872472013-05-31 21:47:25 +000060 protected:
dburgess82c46ff2011-10-07 02:40:51 +000061
62 ListNode* mHead; ///< points to next item out
63 ListNode* mTail; ///< points to last item in
64 ListNode* mFreeList; ///< pool of previously-allocated nodes
65 unsigned mSize; ///< number of items in the FIFO
66
67 public:
68
69 PointerFIFO()
70 :mHead(NULL),mTail(NULL),mFreeList(NULL),
71 mSize(0)
72 {}
73
74 unsigned size() const { return mSize; }
kurtis.heimerl5a872472013-05-31 21:47:25 +000075 unsigned totalSize() const { return 0; } // Not used in this version.
dburgess82c46ff2011-10-07 02:40:51 +000076
kurtis.heimerl5a872472013-05-31 21:47:25 +000077 /** Put an item into the FIFO at the back of the queue. */
dburgess82c46ff2011-10-07 02:40:51 +000078 void put(void* val);
kurtis.heimerl5a872472013-05-31 21:47:25 +000079 /** Push an item on the front of the FIFO. */
80 void push_front(void*val); // pat added.
dburgess82c46ff2011-10-07 02:40:51 +000081
82 /**
83 Take an item from the FIFO.
84 Returns NULL for empty list.
85 */
86 void* get();
87
kurtis.heimerl5a872472013-05-31 21:47:25 +000088 /** Peek at front item without removal. */
89 void *front() { return mHead ? mHead->data() : 0; } // pat added
90
dburgess82c46ff2011-10-07 02:40:51 +000091
92 private:
93
94 /** Allocate a new node to extend the FIFO. */
95 ListNode *allocate();
96
97 /** Release a node to the free pool after removal from the FIFO. */
98 void release(ListNode* wNode);
99
100};
101
kurtis.heimerl5a872472013-05-31 21:47:25 +0000102// This is the default type for SingleLinkList Node element;
103// You can derive your class directly from this, but then you must add type casts
104// all over the place.
105class SingleLinkListNode
106{ public:
107 SingleLinkListNode *mNext;
108 SingleLinkListNode *next() {return mNext;}
109 void setNext(SingleLinkListNode *item) {mNext=item;}
110 SingleLinkListNode() : mNext(0) {}
111 virtual unsigned size() { return 0; }
112};
113
114// A single-linked lists of elements with internal pointers.
115// The methods must match those from SingleLinkListNode.
116// This class also assumes the Node has a size() method, and accumulates
117// the total size of elements in the list in totalSize().
118template<class Node=SingleLinkListNode>
119class SingleLinkList
120{
121 Node *mHead, *mTail;
122 unsigned mSize; // Number of elements in list.
123 unsigned mTotalSize; // Total of size() method of elements in list.
124
125 public:
126 SingleLinkList() : mHead(0), mTail(0), mSize(0), mTotalSize(0) {}
127 unsigned size() const { return mSize; }
128 unsigned totalSize() const { return mTotalSize; }
129
130 Node *pop_back() { assert(0); } // Not efficient with this type of list.
131
132 Node *pop_front()
133 {
134 if (!mHead) return NULL;
135 Node *result = mHead;
136 mHead = mHead->next();
137 if (mTail == result) { mTail = NULL; assert(mHead == NULL); }
138 result->setNext(NULL); // be neat
139 mSize--;
140 mTotalSize -= result->size();
141 return result;
142 }
143
144 void push_front(Node *item)
145 {
146 item->setNext(mHead);
147 mHead = item;
148 if (!mTail) { mTail = item; }
149 mSize++;
150 mTotalSize += item->size();
151 }
152
153 void push_back(Node *item)
154 {
155 item->setNext(NULL);
156 if (mTail) { mTail->setNext(item); }
157 mTail = item;
158 if (!mHead) mHead = item;
159 mSize++;
160 mTotalSize += item->size();
161 }
162 Node *front() { return mHead; }
163 Node *back() { return mTail; }
164
165 // Interface to InterthreadQueue so it can used SingleLinkList as the Fifo.
166 void put(void *val) { push_back((Node*)val); }
167 void *get() { return pop_front(); }
168};
169
dburgess82c46ff2011-10-07 02:40:51 +0000170
171
172
173
174#endif
175// vim: ts=4 sw=4