dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
| 5 | * |
| 6 | * This software is distributed under multiple licenses; see the COPYING file in |
| 7 | * the main directory for licensing information for this specific distribuion. |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 8 | * |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 9 | * 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.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 36 | #include <assert.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | |
| 40 | /** This node class is used to build singly-linked lists. */ |
| 41 | class 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. */ |
| 61 | class PointerFIFO { |
| 62 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 63 | protected: |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 64 | |
| 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 Pedrol | 41c68aa | 2018-12-03 12:35:21 +0100 | [diff] [blame] | 76 | ~PointerFIFO(); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 77 | |
| 78 | unsigned size() const { return mSize; } |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 79 | unsigned totalSize() const { return 0; } // Not used in this version. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 80 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 81 | /** Put an item into the FIFO at the back of the queue. */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 82 | void put(void* val); |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 83 | /** Push an item on the front of the FIFO. */ |
| 84 | void push_front(void*val); // pat added. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | Take an item from the FIFO. |
| 88 | Returns NULL for empty list. |
| 89 | */ |
| 90 | void* get(); |
| 91 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 92 | /** Peek at front item without removal. */ |
| 93 | void *front() { return mHead ? mHead->data() : 0; } // pat added |
| 94 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 95 | |
| 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.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 106 | // 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. |
| 109 | class 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(). |
| 122 | template<class Node=SingleLinkListNode> |
| 123 | class 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 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | #endif |
| 179 | // vim: ts=4 sw=4 |