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