HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 template <typename T>
21 {
22  clear();
23 }
24 
25 template <typename T>
27  : myData(capacity, capacity)
28 {
29  clear();
30 }
31 
32 template <typename T>
34 {
35 }
36 
37 template <typename T>
38 void
39 UT_RingBuffer<T>::insert(int id, const T &data)
40 {
41  if (!entries())
42  {
43  growIfNeeded(1);
44  myFirst = myLast = 0;
45  myFirstId = myLastId = id;
46  }
47  else
48  {
49  UT_ASSERT(id > myLastId);
50  growIfNeeded(id - myLastId);
51  myLast = idIndex(id);
52  myLastId = id;
53  }
54  myData(myLast) = data;
55 }
56 
57 template <typename T>
58 void
60 {
61  int idx;
62 
63  if (id == myFirstId)
64  pop();
65  else
66  {
67  idx = idIndex(id);
68  myData(idx) = 0;
69  }
70 }
71 
72 template <typename T>
73 void
75 {
76  myFirst = 0;
77  myLast = -1;
78  myFirstId = 0;
79  myLastId = -1;
80  myPeak = 0;
81 }
82 
83 template <typename T>
84 void
86 {
87  int i;
88 
89  UT_ASSERT_P(number <= myLastId - myFirstId + 1);
90  for (i = 0; i < number; i++)
91  pop();
92 }
93 
94 template <typename T>
95 T
97 {
98  if (index < myFirstId || index > myLastId)
99  return 0;
100  return myData(idIndex(index));
101 }
102 
103 template <typename T>
104 void
106 {
107  fprintf(stderr, "First = %d\n", myFirst);
108  fprintf(stderr, "Last = %d\n", myLast);
109  fprintf(stderr, "FirstId = %d\n", myFirstId);
110  fprintf(stderr, "LastId = %d\n", myLastId);
111  fprintf(stderr, "Capacity = %d\n", myData.entries());
112  fprintf(stderr, "Peak = %d\n", myPeak);
113 }
114 
115 template <typename T>
116 void
117 UT_RingBuffer<T>::growData(int number)
118 {
119  if (entries() > myPeak)
120  myPeak = entries();
121  if (entries() + number > myData.entries())
122  {
123  int origcapacity;
124  int capacity;
125 
126  origcapacity = myData.entries();
127  capacity = UTbumpAlloc(origcapacity + number);
128 
129  // This is to ensure the array is made exactly the given
130  // capacity
131  myData.setCapacity(capacity);
132  myData.entries(capacity);
133 
134  // If Necessary, we need to move one portion of the data to the end
135  // of the array.
136  if (myLast >= 0 && myLast < myFirst)
137  {
138  myData.move(myFirst,
139  myFirst + capacity - origcapacity,
140  origcapacity - myFirst);
141  myFirst += capacity - origcapacity;
142  }
143  }
144 }
void display() const
T operator[](int id) const
Definition: UT_RingBuffer.C:96
png_uint_32 i
Definition: png.h:2877
void pop()
Pop a single element from the start of the buffer.
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
GLuint id
Definition: glcorearb.h:654
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
void remove(int id)
Definition: UT_RingBuffer.C:59
void insert(int id, const T &data)
Definition: UT_RingBuffer.C:39
GLboolean * data
Definition: glcorearb.h:130
void clear()
Resets the buffer to an empty buffer with a 0 index.
Definition: UT_RingBuffer.C:74
png_infop png_sPLT_tpp entries
Definition: png.h:2481
GLuint index
Definition: glcorearb.h:785