Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Network/uip/fifo.h@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
Update to latest Smoothie.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael J. Spencer |
2:1df0b61d3b5a | 1 | /************************************************************************* |
Michael J. Spencer |
2:1df0b61d3b5a | 2 | * |
Michael J. Spencer |
2:1df0b61d3b5a | 3 | * $Author: Jim Morris $ |
Michael J. Spencer |
2:1df0b61d3b5a | 4 | * $Date: 1999/02/05 21:05:00 $ |
Michael J. Spencer |
2:1df0b61d3b5a | 5 | * |
Michael J. Spencer |
2:1df0b61d3b5a | 6 | * this code is Licensed LGPL |
Michael J. Spencer |
2:1df0b61d3b5a | 7 | * |
Michael J. Spencer |
2:1df0b61d3b5a | 8 | *************************************************************************/ |
Michael J. Spencer |
2:1df0b61d3b5a | 9 | #ifndef _FIFO_H_ |
Michael J. Spencer |
2:1df0b61d3b5a | 10 | #define _FIFO_H_ |
Michael J. Spencer |
2:1df0b61d3b5a | 11 | |
Michael J. Spencer |
2:1df0b61d3b5a | 12 | #include <stdlib.h> |
Michael J. Spencer |
2:1df0b61d3b5a | 13 | |
Michael J. Spencer |
2:1df0b61d3b5a | 14 | // Doubly Linked list class |
Michael J. Spencer |
2:1df0b61d3b5a | 15 | |
Michael J. Spencer |
2:1df0b61d3b5a | 16 | template<class T> class LList; |
Michael J. Spencer |
2:1df0b61d3b5a | 17 | |
Michael J. Spencer |
2:1df0b61d3b5a | 18 | template<class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 19 | class Tlink |
Michael J. Spencer |
2:1df0b61d3b5a | 20 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 21 | public: |
Michael J. Spencer |
2:1df0b61d3b5a | 22 | Tlink<T> *pnext; |
Michael J. Spencer |
2:1df0b61d3b5a | 23 | Tlink<T> *pprev; |
Michael J. Spencer |
2:1df0b61d3b5a | 24 | |
Michael J. Spencer |
2:1df0b61d3b5a | 25 | public: |
Michael J. Spencer |
2:1df0b61d3b5a | 26 | Tlink() |
Michael J. Spencer |
2:1df0b61d3b5a | 27 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 28 | pnext = pprev = 0; |
Michael J. Spencer |
2:1df0b61d3b5a | 29 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 30 | Tlink(Tlink *p, Tlink *n) |
Michael J. Spencer |
2:1df0b61d3b5a | 31 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 32 | pprev = p; |
Michael J. Spencer |
2:1df0b61d3b5a | 33 | pnext = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 34 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 35 | Tlink(const T &d) : data(d) |
Michael J. Spencer |
2:1df0b61d3b5a | 36 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 37 | pnext = pprev = 0; |
Michael J. Spencer |
2:1df0b61d3b5a | 38 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 39 | T data; |
Michael J. Spencer |
2:1df0b61d3b5a | 40 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 41 | |
Michael J. Spencer |
2:1df0b61d3b5a | 42 | template<class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 43 | class list_base |
Michael J. Spencer |
2:1df0b61d3b5a | 44 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 45 | private: |
Michael J. Spencer |
2:1df0b61d3b5a | 46 | Tlink<T> *head; |
Michael J. Spencer |
2:1df0b61d3b5a | 47 | Tlink<T> *tail; |
Michael J. Spencer |
2:1df0b61d3b5a | 48 | int cnt; |
Michael J. Spencer |
2:1df0b61d3b5a | 49 | |
Michael J. Spencer |
2:1df0b61d3b5a | 50 | protected: |
Michael J. Spencer |
2:1df0b61d3b5a | 51 | list_base() |
Michael J. Spencer |
2:1df0b61d3b5a | 52 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 53 | head = tail = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 54 | cnt = 0; |
Michael J. Spencer |
2:1df0b61d3b5a | 55 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 56 | |
Michael J. Spencer |
2:1df0b61d3b5a | 57 | list_base(Tlink<T> *n) // link into head of list |
Michael J. Spencer |
2:1df0b61d3b5a | 58 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 59 | cnt = 1; |
Michael J. Spencer |
2:1df0b61d3b5a | 60 | n->pnext = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 61 | n->pprev = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 62 | head = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 63 | tail = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 64 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 65 | |
Michael J. Spencer |
2:1df0b61d3b5a | 66 | Tlink<T> *gethead(void) const |
Michael J. Spencer |
2:1df0b61d3b5a | 67 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 68 | return head; |
Michael J. Spencer |
2:1df0b61d3b5a | 69 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 70 | Tlink<T> *gettail(void) const |
Michael J. Spencer |
2:1df0b61d3b5a | 71 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 72 | return tail; |
Michael J. Spencer |
2:1df0b61d3b5a | 73 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 74 | Tlink<T> *getnext(Tlink<T> *n) const |
Michael J. Spencer |
2:1df0b61d3b5a | 75 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 76 | return n->pnext; |
Michael J. Spencer |
2:1df0b61d3b5a | 77 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 78 | Tlink<T> *getprev(Tlink<T> *n) const |
Michael J. Spencer |
2:1df0b61d3b5a | 79 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 80 | return n->pprev; |
Michael J. Spencer |
2:1df0b61d3b5a | 81 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 82 | |
Michael J. Spencer |
2:1df0b61d3b5a | 83 | void addtohead(Tlink<T> *n) // add at head of list |
Michael J. Spencer |
2:1df0b61d3b5a | 84 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 85 | n->pnext = head; |
Michael J. Spencer |
2:1df0b61d3b5a | 86 | n->pprev = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 87 | if (head) head->pprev = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 88 | head = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 89 | if (tail == NULL) // first one |
Michael J. Spencer |
2:1df0b61d3b5a | 90 | tail = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 91 | cnt++; |
Michael J. Spencer |
2:1df0b61d3b5a | 92 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 93 | |
Michael J. Spencer |
2:1df0b61d3b5a | 94 | void addtohead(int c, Tlink<T> *a, Tlink<T> *b) // add list at head of list |
Michael J. Spencer |
2:1df0b61d3b5a | 95 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 96 | b->pnext = head; |
Michael J. Spencer |
2:1df0b61d3b5a | 97 | a->pprev = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 98 | if (head) head->pprev = b; |
Michael J. Spencer |
2:1df0b61d3b5a | 99 | head = a; |
Michael J. Spencer |
2:1df0b61d3b5a | 100 | if (tail == NULL) // first one |
Michael J. Spencer |
2:1df0b61d3b5a | 101 | tail = b; |
Michael J. Spencer |
2:1df0b61d3b5a | 102 | cnt += c; |
Michael J. Spencer |
2:1df0b61d3b5a | 103 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 104 | |
Michael J. Spencer |
2:1df0b61d3b5a | 105 | void addtotail(Tlink<T> *n) // add to tail of list |
Michael J. Spencer |
2:1df0b61d3b5a | 106 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 107 | n->pnext = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 108 | n->pprev = tail; |
Michael J. Spencer |
2:1df0b61d3b5a | 109 | if (tail) tail->pnext = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 110 | tail = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 111 | if (head == NULL) // first one |
Michael J. Spencer |
2:1df0b61d3b5a | 112 | head = n; |
Michael J. Spencer |
2:1df0b61d3b5a | 113 | cnt++; |
Michael J. Spencer |
2:1df0b61d3b5a | 114 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 115 | |
Michael J. Spencer |
2:1df0b61d3b5a | 116 | void remove(Tlink<T> *n) // remove it by relinking |
Michael J. Spencer |
2:1df0b61d3b5a | 117 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 118 | cnt--; |
Michael J. Spencer |
2:1df0b61d3b5a | 119 | if (n->pprev) n->pprev->pnext = n->pnext; |
Michael J. Spencer |
2:1df0b61d3b5a | 120 | else head = n->pnext; // it must be the head |
Michael J. Spencer |
2:1df0b61d3b5a | 121 | if (n->pnext) n->pnext->pprev = n->pprev; |
Michael J. Spencer |
2:1df0b61d3b5a | 122 | else tail = n->pprev; |
Michael J. Spencer |
2:1df0b61d3b5a | 123 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 124 | |
Michael J. Spencer |
2:1df0b61d3b5a | 125 | void reset() |
Michael J. Spencer |
2:1df0b61d3b5a | 126 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 127 | head = tail = NULL; |
Michael J. Spencer |
2:1df0b61d3b5a | 128 | cnt = 0; |
Michael J. Spencer |
2:1df0b61d3b5a | 129 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 130 | int count() const |
Michael J. Spencer |
2:1df0b61d3b5a | 131 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 132 | return cnt; |
Michael J. Spencer |
2:1df0b61d3b5a | 133 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 134 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 135 | |
Michael J. Spencer |
2:1df0b61d3b5a | 136 | // fifo |
Michael J. Spencer |
2:1df0b61d3b5a | 137 | template<class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 138 | class Fifo : private list_base<T> |
Michael J. Spencer |
2:1df0b61d3b5a | 139 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 140 | public: |
Michael J. Spencer |
2:1df0b61d3b5a | 141 | Fifo(){} |
Michael J. Spencer |
2:1df0b61d3b5a | 142 | |
Michael J. Spencer |
2:1df0b61d3b5a | 143 | void push(const T &a); |
Michael J. Spencer |
2:1df0b61d3b5a | 144 | T pop(); |
Michael J. Spencer |
2:1df0b61d3b5a | 145 | T peek(); |
Michael J. Spencer |
2:1df0b61d3b5a | 146 | int size() const; |
Michael J. Spencer |
2:1df0b61d3b5a | 147 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 148 | |
Michael J. Spencer |
2:1df0b61d3b5a | 149 | template <class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 150 | int Fifo<T>::size() const |
Michael J. Spencer |
2:1df0b61d3b5a | 151 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 152 | return list_base<T>::count(); |
Michael J. Spencer |
2:1df0b61d3b5a | 153 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 154 | |
Michael J. Spencer |
2:1df0b61d3b5a | 155 | // add to end of list (FIFO) |
Michael J. Spencer |
2:1df0b61d3b5a | 156 | template <class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 157 | void Fifo<T>::push(const T &a) |
Michael J. Spencer |
2:1df0b61d3b5a | 158 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 159 | list_base<T>::addtotail(new Tlink<T>(a)); |
Michael J. Spencer |
2:1df0b61d3b5a | 160 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 161 | |
Michael J. Spencer |
2:1df0b61d3b5a | 162 | // return the first item in the list |
Michael J. Spencer |
2:1df0b61d3b5a | 163 | template <class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 164 | T Fifo<T>::peek() |
Michael J. Spencer |
2:1df0b61d3b5a | 165 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 166 | Tlink<T> *p = list_base<T>::gethead(); |
Michael J. Spencer |
2:1df0b61d3b5a | 167 | return p->data; |
Michael J. Spencer |
2:1df0b61d3b5a | 168 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 169 | |
Michael J. Spencer |
2:1df0b61d3b5a | 170 | // pop the first item off the fifo |
Michael J. Spencer |
2:1df0b61d3b5a | 171 | template <class T> |
Michael J. Spencer |
2:1df0b61d3b5a | 172 | T Fifo<T>::pop() |
Michael J. Spencer |
2:1df0b61d3b5a | 173 | { |
Michael J. Spencer |
2:1df0b61d3b5a | 174 | Tlink<T> *p = list_base<T>::gethead(); |
Michael J. Spencer |
2:1df0b61d3b5a | 175 | T data = p->data; |
Michael J. Spencer |
2:1df0b61d3b5a | 176 | list_base<T>::remove(p); |
Michael J. Spencer |
2:1df0b61d3b5a | 177 | delete p; |
Michael J. Spencer |
2:1df0b61d3b5a | 178 | return data; |
Michael J. Spencer |
2:1df0b61d3b5a | 179 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 180 | #endif |