HoverRace  2.0
FastFifo.h
Go to the documentation of this file.
1 
2 // FastFifo.h
3 //
4 // Copyright (c) 1995-1998 - Richard Langlois and Grokksoft Inc.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 //
22 
23 #pragma once
24 
25 #include "MR_Types.h"
26 
27 template <class pType>
29 {
30 protected:
31  int mHead;
32  int mNbItem;
34  pType *mArray;
35 
36 public:
37  MR_FastFifoBase(int pSize, pType *pData) :
38  mHead(0), mNbItem(0), mArraySize(pSize), mArray(pData) { }
39 
40  void Add(pType pData)
41  {
42  mArray[(mHead + mNbItem) % mArraySize] = pData;
43  if(mNbItem == mArraySize) {
44  mHead = (mHead + 1) % mArraySize;
45  }
46  else {
47  mNbItem++;
48  }
49  };
50 
51  void Remove(int pCount = 1)
52  {
53  if(pCount > mNbItem) {
54  pCount = mNbItem;
55  }
56  mNbItem -= pCount;
57  mHead = (mHead + pCount) % mArraySize;
58  };
59 
60  pType GetHead()
61  {
62  return mArray[mHead];
63  };
64 
65  void Clean()
66  {
67  mNbItem = 0;
68  }
69 
70  int Used() const
71  {
72  return mNbItem;
73  }
74 
75  int TotalSize() const
76  {
77  return mArraySize;
78  }
79 
80  int Full() const
81  {
82  return mNbItem == mArraySize;
83  }
84 
85  BOOL CanAdd(int pCount = 1) const
86  {
87  return mNbItem + pCount <= mArraySize;
88  }
89 
90  BOOL IsEmpty() const
91  {
92  return (mNbItem == 0);
93  }
94 
95  const pType operator[](int pIndex) const
96  {
97  return mArray[(mHead + pIndex) % mArraySize];
98  };
99 
100  // Special functions to add at the head and remove at the tail
101  void AddHead(pType pData)
102  {
103  mHead = (mHead + mArraySize - 1) % mArraySize;
104  mArray[mHead] = pData;
105  if(mNbItem != mArraySize) {
106  mNbItem++;
107  }
108  };
109 
110  void RemoveTail(int pCount = 1)
111  {
112  if(pCount > mNbItem) {
113  pCount = mNbItem;
114  }
115  mNbItem -= pCount;
116  };
117 };
118 
119 template <class pType>
120 class MR_FastFifo : public MR_FastFifoBase<pType>
121 {
123 
124 public:
125  MR_FastFifo(int pSize) : SUPER(pSize, new pType[pSize]) { };
126 
128  {
130  };
131 };
132 
133 template <class pType, int pSize>
134 class MR_FixedFastFifo : public MR_FastFifoBase<pType>
135 {
137 
138 protected:
139  pType mData[pSize];
140 
141 public:
142  MR_FixedFastFifo() : SUPER(pSize, mData) { }
143 };
144 
145 #undef MR_DllDeclare
MR_FastFifo(int pSize)
Definition: FastFifo.h:125
int mNbItem
Definition: FastFifo.h:32
int Used() const
Definition: FastFifo.h:70
void RemoveTail(int pCount=1)
Definition: FastFifo.h:110
MR_FastFifoBase(int pSize, pType *pData)
Definition: FastFifo.h:37
void AddHead(pType pData)
Definition: FastFifo.h:101
MR_FixedFastFifo()
Definition: FastFifo.h:142
BOOL IsEmpty() const
Definition: FastFifo.h:90
void Clean()
Definition: FastFifo.h:65
void Add(pType pData)
Definition: FastFifo.h:40
~MR_FastFifo()
Definition: FastFifo.h:127
void Remove(int pCount=1)
Definition: FastFifo.h:51
int mArraySize
Definition: FastFifo.h:33
const pType operator[](int pIndex) const
Definition: FastFifo.h:95
pType * mArray
Definition: FastFifo.h:34
pType GetHead()
Definition: FastFifo.h:60
int Full() const
Definition: FastFifo.h:80
Definition: FastFifo.h:28
Definition: FastFifo.h:120
Definition: FastFifo.h:134
BOOL CanAdd(int pCount=1) const
Definition: FastFifo.h:85
int TotalSize() const
Definition: FastFifo.h:75
int mHead
Definition: FastFifo.h:31