00001 #include "indexlist.hpp"
00002
00003 IndexList::IndexList(unsigned allocsize)
00004 : alloc_size(0), current_size(0), arr(0),
00005 first_not_listed_index(0)
00006 {
00007 Resize(allocsize);
00008 }
00009
00010 IndexList::~IndexList()
00011 {
00012 if(arr) {
00013 delete [] arr;
00014 }
00015 }
00016
00017 unsigned IndexList::GetFirstNotListed() const
00018 {
00019 return first_not_listed_index;
00020 }
00021
00022 unsigned IndexList::GetCurrentSize() const
00023 {
00024 return current_size;
00025 }
00026
00027 unsigned IndexList::operator[](unsigned i) const
00028 {
00029 #if HANDLEXCEPTION
00030 if(i >= current_size) {
00031 throw "assume i < current_size";
00032 }
00033 #endif
00034 return arr[i];
00035 }
00036
00037 void IndexList::Resize(unsigned need_size)
00038 {
00039 unsigned *tmp;
00040 unsigned i;
00041 if(need_size > alloc_size) {
00042 alloc_size = need_size;
00043 tmp = new unsigned[alloc_size];
00044 for(i = 0; i < current_size; ++i) {
00045 tmp[i] = arr[i];
00046 }
00047 delete [] arr;
00048 arr = tmp;
00049 }
00050 }
00051
00052 void IndexList::AddIndex(unsigned index)
00053 {
00054 unsigned i, j;
00055 i = first_not_listed_index;
00056 for(; i < current_size; ++i) {
00057 if(arr[i] > index) {
00058 break;
00059 }
00060 }
00061 if(current_size == alloc_size) {
00062 Resize(alloc_size + indexlist_add_allocsize);
00063 }
00064 for(j = current_size; j > i; --j) {
00065 arr[j] = arr[j - 1];
00066 }
00067 arr[i] = index;
00068 current_size++;
00069 if(index != first_not_listed_index) {
00070 return;
00071 }
00072 for(j = i; j + 1 < current_size; ++j) {
00073 if((arr[j] + 1) != arr[j + 1]) {
00074 break;
00075 }
00076 }
00077 first_not_listed_index = arr[j] + 1;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107