Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

indexlist.cpp

Go to the documentation of this file.
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 void IndexList::AddIndex(unsigned index)
00083 {
00084     Item *tmp, *pitem;
00085     tmp = new Item;
00086     tmp->index = index;
00087     if(!first || (first->index > index)) {
00088         tmp->next = first;
00089         first = tmp;
00090     } else {
00091         pitem = first;
00092         while(pitem->next && (pitem->next->index < index)) {
00093             pitem = pitem->next;
00094         }
00095         tmp->next = pitem->next;
00096         pitem->next = tmp;
00097     }
00098     if(index != first_not_listed_index) {
00099         return;
00100     }
00101     while(tmp->next && (tmp->index + 1 == tmp->next->index)) 
00102     {
00103         tmp = tmp->next;
00104     }
00105     first_not_listed_index = tmp->index + 1;
00106 }
00107 */

Generated on Sun May 25 01:58:04 2025 for SmoluchowskiSolver by Doxygen