HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_RingBuffer.C
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_RingBuffer.C ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #include "UT_RingBuffer.h"
12 #include "UT_Assert.h"
13 #include "UT_ArrayHelp.h"
14 #include "UT_Memory.h"
15 #include <utility>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 template <typename T>
22 {
23  clear();
24 }
25 
26 template <typename T>
28  : myData(capacity, capacity)
29 {
30  clear();
31 }
32 
33 template <typename T>
35 {
36 }
37 
38 template <typename T>
39 template <typename S>
40 void
42 {
43  if (!entries())
44  {
45  growIfNeeded(1);
46  myFirst = myLast = 0;
47  myFirstId = myLastId = id;
48  }
49  else
50  {
51  UT_ASSERT(id > myLastId);
52  growIfNeeded(id - myLastId);
53  myLast = idIndex(id);
54  myLastId = id;
55  }
56  myData(myLast) = std::forward<S>(data);
57 }
58 
59 template <typename T>
60 void
62 {
63  int idx;
64 
65  if (id == myFirstId)
66  pop();
67  else
68  {
69  idx = idIndex(id);
70  myData(idx) = 0;
71  }
72 }
73 
74 template <typename T>
75 void
77 {
78  myFirst = 0;
79  myLast = -1;
80  myFirstId = 0;
81  myLastId = -1;
82  myPeak = 0;
83 }
84 
85 template <typename T>
86 void
88 {
89  int i;
90 
91  UT_ASSERT_P(number <= myLastId - myFirstId + 1);
92  for (i = 0; i < number; i++)
93  pop();
94 }
95 
96 template <typename T>
97 T
99 {
100  if (index < myFirstId || index > myLastId)
101  return 0;
102  return myData(idIndex(index));
103 }
104 
105 template <typename T>
106 void
108 {
109  fprintf(stderr, "First = %d\n", myFirst);
110  fprintf(stderr, "Last = %d\n", myLast);
111  fprintf(stderr, "FirstId = %d\n", myFirstId);
112  fprintf(stderr, "LastId = %d\n", myLastId);
113  fprintf(stderr, "Capacity = %d\n", myData.entries());
114  fprintf(stderr, "Peak = %d\n", myPeak);
115 }
116 
117 template <typename T>
118 void
119 UT_RingBuffer<T>::growData(int number)
120 {
121  if (entries() > myPeak)
122  myPeak = entries();
123  if (entries() + number > myData.entries())
124  {
125  int origcapacity;
126  int capacity;
127 
128  origcapacity = myData.entries();
129  capacity = UTbumpAlloc(origcapacity + number);
130 
131  // This is to ensure the array is made exactly the given
132  // capacity
133  myData.setCapacity(capacity);
134  myData.entries(capacity);
135 
136  // If Necessary, we need to move one portion of the data to the end
137  // of the array.
138  if (myLast >= 0 && myLast < myFirst)
139  {
140  myData.move(myFirst,
141  myFirst + capacity - origcapacity,
142  origcapacity - myFirst);
143  myFirst += capacity - origcapacity;
144  }
145  }
146 }
GLboolean * data
Definition: glcorearb.h:131
void display() const
T operator[](int id) const
Definition: UT_RingBuffer.C:98
void pop()
Pop a single element from the start of the buffer.
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
void remove(int id)
Remove item with id.
Definition: UT_RingBuffer.C:61
GLuint id
Definition: glcorearb.h:655
void clear()
Resets the buffer to an empty buffer with a 0 index.
Definition: UT_RingBuffer.C:76
auto fprintf(std::FILE *f, const S &fmt, const T &...args) -> int
Definition: printf.h:602
GLuint index
Definition: glcorearb.h:786
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: format.h:895