| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/lib/psclass.h $ |
| 21 | * $Revision: 8 $ |
| 22 | * $Date: 4/16/99 4:26a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * New Class Library |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/psclass.h $ |
| 28 | * |
| 29 | * 8 4/16/99 4:26a Jeff |
| 30 | * removed ifdef of linux since new compiler compiles it |
| 31 | * |
| 32 | * 7 4/15/99 1:44a Jeff |
| 33 | * changes for linux compile |
| 34 | * |
| 35 | * 6 12/16/98 1:57p Samir |
| 36 | * Replaced CleanupString2 with CleanupStr |
| 37 | * |
| 38 | * 5 10/02/98 11:16a Jeff |
| 39 | * removed 'struct' from the template, generates an error in VC6 that |
| 40 | * keeps it from compiling |
| 41 | * |
| 42 | * 4 9/17/98 7:05p Samir |
| 43 | * added string token class. |
| 44 | * |
| 45 | * 3 7/28/98 5:42p Samir |
| 46 | * added queue class. |
| 47 | * |
| 48 | * 2 7/23/98 11:36a Samir |
| 49 | * Added class library header. |
| 50 | * |
| 51 | * $NoKeywords: $ |
| 52 | */ |
| 53 | |
| 54 | #ifndef PSCLASS_H |
| 55 | #define PSCLASS_H |
| 56 | |
| 57 | // a universal list node to use with the list type |
| 58 | template <class T> struct tListNode { |
| 59 | T t; |
| 60 | tListNode<T> *next; |
| 61 | }; |
| 62 | |
| 63 | // LIST CLASSES |
| 64 | // |
| 65 | |
| 66 | // tList |
| 67 | // this is a VERY BASIC list class that encapsulates a lot |
| 68 | // of list operations into one class. this class handles list NODES ONLY. |
| 69 | // a more advanced form is to come, but this is for those hardcode programmers. |
| 70 | |
| 71 | // to declare say, a list that will contain integers, do this: |
| 72 | // tList<int> list; |
| 73 | // tListNode<int> *node; |
| 74 | // node = new tListNode<int>; |
| 75 | // node->data = 1; |
| 76 | // list.Link(node); |
| 77 | // it is the programmers responsibility to create the nodes DYNAMICALLY |
| 78 | |
| 79 | // universal single-linked-list type |
| 80 | template <class T> class tList { |
| 81 | tListNode<T> *m_link, *m_mark; |
| 82 | int m_length; |
| 83 | |
| 84 | public: |
| 85 | tList() { |
| 86 | m_link = m_mark = NULL; |
| 87 | m_length = 0; |
| 88 | }; |
| 89 | ~tList() { tList::free(); }; |
| 90 | |
| 91 | // moves to start of list. used for iteration |
| 92 | tListNode<T> *start() { |
| 93 | m_mark = m_link; |
| 94 | return m_mark; |
| 95 | } |
| 96 | |
| 97 | // gets next list item in iteration. if at end of list, last item will be returned. |
| 98 | tListNode<T> *next() { |
| 99 | m_mark = (m_mark->next) ? m_mark->next : m_mark; |
| 100 | return get(); |
| 101 | }; |
| 102 | |
| 103 | // returns the node at the current link location in iteration. |
| 104 | tListNode<T> *get() const { return m_mark ? m_mark : NULL; }; |
| 105 | |
| 106 | // length |
| 107 | int length() const { return m_length; }; |
| 108 | |
| 109 | // frees list |
| 110 | void free() { |
| 111 | m_link = m_mark = NULL; |
| 112 | m_length = 0; |
| 113 | }; |
| 114 | |
| 115 | // make a link at the current mark location. |
| 116 | void link(tListNode<T> *node) { |
| 117 | if (m_link) { |
| 118 | node->next = m_mark->next; |
| 119 | m_mark->next = node; |
| 120 | } else { |
| 121 | m_link = node; |
| 122 | node->next = NULL; |
| 123 | } |
| 124 | m_mark = node; |
| 125 | m_length++; |
| 126 | }; |
| 127 | |
| 128 | // unlink a node on the list at the current mark, returning the unlinked node. |
| 129 | tListNode<T> *unlink() { |
| 130 | tListNode<T> *freenode, *node; |
| 131 | if (!m_link) |
| 132 | return NULL; |
| 133 | if (m_link == m_mark) { |
| 134 | freenode = m_mark; |
| 135 | m_mark = m_link = m_link->next; |
| 136 | } else { |
| 137 | node = m_link; |
| 138 | while (node->next != m_mark) |
| 139 | node = node->next; |
| 140 | freenode = m_mark; |
| 141 | node->next = m_mark->next; |
| 142 | } |
| 143 | freenode->next = NULL; |
| 144 | return freenode; |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | // tQueue |
| 149 | // a circular queue implementation |
| 150 | |
| 151 | template <class T, int t_LEN> class tQueue { |
| 152 | T m_items[t_LEN]; |
| 153 | int16_t m_head, m_tail; |
| 154 | |
| 155 | public: |
| 156 | tQueue() { m_head = m_tail = 0; }; |
| 157 | ~tQueue(){}; |
| 158 | |
| 159 | void send(T &item) { // sends an item onto the queue |
| 160 | int16_t temp = m_tail + 1; |
| 161 | if (temp == t_LEN) |
| 162 | temp = 0; |
| 163 | if (temp != m_head) { |
| 164 | m_items[m_tail] = item; |
| 165 | m_tail = temp; |
| 166 | } |
| 167 | }; |
| 168 | bool recv(T *item) { // returns an item from the queue, false if no item. |
| 169 | if (m_head == m_tail) |
| 170 | return false; |
| 171 | *item = m_items[m_head++]; |
| 172 | if (m_head == t_LEN) |
| 173 | m_head = 0; |
| 174 | return true; |
| 175 | }; |
| 176 | |
| 177 | void flush() { // flush queue entries. |
| 178 | m_head = m_tail = 0; |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | #endif |