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