blob: 136d13d80684fff92b2e9169ed247291c4ffa0f6 [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 {}
Pau Espin Pedrol41c68aa2018-12-03 12:35:21 +010073 ~PointerFIFO();
dburgess82c46ff2011-10-07 02:40:51 +000074
75 unsigned size() const { return mSize; }
kurtis.heimerl5a872472013-05-31 21:47:25 +000076 unsigned totalSize() const { return 0; } // Not used in this version.
dburgess82c46ff2011-10-07 02:40:51 +000077
kurtis.heimerl5a872472013-05-31 21:47:25 +000078 /** Put an item into the FIFO at the back of the queue. */
dburgess82c46ff2011-10-07 02:40:51 +000079 void put(void* val);
kurtis.heimerl5a872472013-05-31 21:47:25 +000080 /** Push an item on the front of the FIFO. */
81 void push_front(void*val); // pat added.
dburgess82c46ff2011-10-07 02:40:51 +000082
83 /**
84 Take an item from the FIFO.
85 Returns NULL for empty list.
86 */
87 void* get();
88
kurtis.heimerl5a872472013-05-31 21:47:25 +000089 /** Peek at front item without removal. */
90 void *front() { return mHead ? mHead->data() : 0; } // pat added
91
dburgess82c46ff2011-10-07 02:40:51 +000092
93 private:
94
95 /** Allocate a new node to extend the FIFO. */
96 ListNode *allocate();
97
98 /** Release a node to the free pool after removal from the FIFO. */
99 void release(ListNode* wNode);
100
101};
102
kurtis.heimerl5a872472013-05-31 21:47:25 +0000103// This is the default type for SingleLinkList Node element;
104// You can derive your class directly from this, but then you must add type casts
105// all over the place.
106class SingleLinkListNode
107{ public:
108 SingleLinkListNode *mNext;
109 SingleLinkListNode *next() {return mNext;}
110 void setNext(SingleLinkListNode *item) {mNext=item;}
111 SingleLinkListNode() : mNext(0) {}
112 virtual unsigned size() { return 0; }
113};
114
115// A single-linked lists of elements with internal pointers.
116// The methods must match those from SingleLinkListNode.
117// This class also assumes the Node has a size() method, and accumulates
118// the total size of elements in the list in totalSize().
119template<class Node=SingleLinkListNode>
120class SingleLinkList
121{
122 Node *mHead, *mTail;
123 unsigned mSize; // Number of elements in list.
124 unsigned mTotalSize; // Total of size() method of elements in list.
125
126 public:
127 SingleLinkList() : mHead(0), mTail(0), mSize(0), mTotalSize(0) {}
128 unsigned size() const { return mSize; }
129 unsigned totalSize() const { return mTotalSize; }
130
131 Node *pop_back() { assert(0); } // Not efficient with this type of list.
132
133 Node *pop_front()
134 {
135 if (!mHead) return NULL;
136 Node *result = mHead;
137 mHead = mHead->next();
138 if (mTail == result) { mTail = NULL; assert(mHead == NULL); }
139 result->setNext(NULL); // be neat
140 mSize--;
141 mTotalSize -= result->size();
142 return result;
143 }
144
145 void push_front(Node *item)
146 {
147 item->setNext(mHead);
148 mHead = item;
149 if (!mTail) { mTail = item; }
150 mSize++;
151 mTotalSize += item->size();
152 }
153
154 void push_back(Node *item)
155 {
156 item->setNext(NULL);
157 if (mTail) { mTail->setNext(item); }
158 mTail = item;
159 if (!mHead) mHead = item;
160 mSize++;
161 mTotalSize += item->size();
162 }
163 Node *front() { return mHead; }
164 Node *back() { return mTail; }
165
166 // Interface to InterthreadQueue so it can used SingleLinkList as the Fifo.
167 void put(void *val) { push_back((Node*)val); }
168 void *get() { return pop_front(); }
169};
170
dburgess82c46ff2011-10-07 02:40:51 +0000171
172
173
174
175#endif
176// vim: ts=4 sw=4