diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
commit | f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch) | |
tree | 13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/old/ringlist.h | |
parent | 74d4c8cd27334fc7204d5a8773deb3d424565778 (diff) | |
download | libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2 libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip |
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a
unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/old/ringlist.h')
-rw-r--r-- | src/old/ringlist.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/old/ringlist.h b/src/old/ringlist.h new file mode 100644 index 0000000..bc069f3 --- /dev/null +++ b/src/old/ringlist.h | |||
@@ -0,0 +1,112 @@ | |||
1 | #ifndef RINGLIST_H | ||
2 | #define RINGLIST_H | ||
3 | |||
4 | #include "list.h" | ||
5 | |||
6 | /** | ||
7 | * A RingList or Ring Buffer implementation. This is a list that never grows in | ||
8 | * size once it is created, but instead once it is full new items added to the | ||
9 | * RingList replace the oldest items and the zero-index is virtually shifted. | ||
10 | * Since no data is actually moved when zero-index moves, this is very | ||
11 | * efficient. | ||
12 | * <br> | ||
13 | * The items removed are not actually deleted by the RingList, so instead they | ||
14 | * are first moved into a temporary "Push Buffer" that can be accessed so that | ||
15 | * elements pushed off the edge of the RingList can be accessed for cleanup. | ||
16 | *@author Mike Buland | ||
17 | */ | ||
18 | class RingList : public List | ||
19 | { | ||
20 | public: | ||
21 | /** | ||
22 | * Construct a RingList with a fixed initial size. This size never changes | ||
23 | * unless setSize is called later during normal operation. | ||
24 | *@param nInitSize The number of elements to allocate. | ||
25 | */ | ||
26 | RingList( int nInitSize ); | ||
27 | |||
28 | /** | ||
29 | * Clean up the data structures, but not the contained elements. | ||
30 | */ | ||
31 | virtual ~RingList(); | ||
32 | |||
33 | /** | ||
34 | * Get an element at the specified index. | ||
35 | *@param nIndex The index of the element to retreive. | ||
36 | *@returns A pointer to the requested element, or NULL if the element is | ||
37 | * not found or not initialized yet. | ||
38 | */ | ||
39 | void *getAt( int nIndex ); | ||
40 | |||
41 | /** | ||
42 | * Append an element to the end of the list, overwriting the begining if | ||
43 | * necesarry. | ||
44 | *@param pData The pointer to append to the RingList. | ||
45 | */ | ||
46 | void append( void *pData ); | ||
47 | |||
48 | /** | ||
49 | * Insert an element before another in the RingList, pushing all after it | ||
50 | * down the list. | ||
51 | *@param pData The data to insert. | ||
52 | *@param nPos The position that new the element should occupy in the list. | ||
53 | */ | ||
54 | void insertBefore( void *pData, int nPos = 0 ); | ||
55 | |||
56 | /** | ||
57 | * Get the size of the array. | ||
58 | */ | ||
59 | int getSize(); | ||
60 | |||
61 | /** | ||
62 | * Is the RingList empty? | ||
63 | *@returns True if it is empty, false otherwise. | ||
64 | */ | ||
65 | bool isEmpty(); | ||
66 | |||
67 | /** | ||
68 | * Delete an element in the list, moving all later elements down one index. | ||
69 | *@param nIndex The index of the element to delete. | ||
70 | */ | ||
71 | void deleteAt( int nIndex ); | ||
72 | |||
73 | /** | ||
74 | * Remove all elements from the RingList. | ||
75 | */ | ||
76 | void empty(); | ||
77 | |||
78 | /** | ||
79 | * Set a new size for the RingList. Be careful with this one, if shrinking | ||
80 | * this may quietly create a memory leak. | ||
81 | *@param nNewSize The new size to set the RingList to. | ||
82 | *@todo Either fix this memory leak somehow or remove this function. | ||
83 | */ | ||
84 | void setSize( int nNewSize ); | ||
85 | |||
86 | /** | ||
87 | * Set a specific element to a new value. | ||
88 | *@param nIndex The zero-based index to change the value of. | ||
89 | *@param pData The data to put at the location specified by nIndex. | ||
90 | */ | ||
91 | void setAt( int nIndex, void *pData ); | ||
92 | |||
93 | /** | ||
94 | * Retrieve the contents of the push buffer. This is the data that is | ||
95 | * pushed off the end of the array if you append data and the list is full. | ||
96 | * This should be checked after every append operation to be sure there | ||
97 | * isn't anything that needs deleting. | ||
98 | *@returns The last value pushed off the RingList, or NULL if nothing was | ||
99 | * pushed off. | ||
100 | */ | ||
101 | void *getPushBuf(); | ||
102 | |||
103 | private: | ||
104 | int nFirstIndex; /**< The index to be translated as zero. */ | ||
105 | int nRealLength; /**< The Amount of storage space available. */ | ||
106 | int nDataLength; /**< The number of elements filled in. */ | ||
107 | void **apData; /**< The actual data storage. */ | ||
108 | void *pPushBuf; /**< The push buffer. */ | ||
109 | |||
110 | }; | ||
111 | |||
112 | #endif | ||