cgma
ArrayBasedContainer Class Reference

#include <ArrayBasedContainer.hpp>

Inheritance diagram for ArrayBasedContainer:
DLList SDLList

List of all members.

Public Member Functions

 ArrayBasedContainer (int size)
 ArrayBasedContainer (const ArrayBasedContainer &copy_from)
virtual ~ArrayBasedContainer ()
int size () const
void clean_out ()
void shrink (int k)
ArrayBasedContaineroperator= (const ArrayBasedContainer &)
ArrayBasedContaineroperator+= (const ArrayBasedContainer &)
ArrayBasedContaineroperator-= (const ArrayBasedContainer &)
virtual void compress ()

Static Public Member Functions

static unsigned int current_allocated_memory ()
static unsigned int maximum_allocated_memory ()

Protected Member Functions

void lengthen_list ()

Protected Attributes

void ** listArray
int itemCount
int listLength
Bit poolAllocated: 1
Bit listIsSorted: 1
Bit listIncrement: 30

Static Private Attributes

static unsigned int currentAllocatedMemory = 0
static unsigned int maxAllocatedMemory = 0

Detailed Description

Definition at line 21 of file ArrayBasedContainer.hpp.


Constructor & Destructor Documentation

Definition at line 24 of file ArrayBasedContainer.cpp.

{
  if ( increment > 0)
    {
      listIncrement = increment;
    }
  else
    {
      listIncrement = INCREMENT;
    }
  itemCount  = 0;
  listLength = 0;
  listArray  = 0;
  poolAllocated = CUBIT_FALSE;
  listIsSorted  = CUBIT_FALSE;
}

Definition at line 43 of file ArrayBasedContainer.cpp.

{
  listLength = from.listLength;
  listIncrement = from.listIncrement;
  poolAllocated = from.poolAllocated;
  listIsSorted  = from.listIsSorted;
  listArray = NULL;

  if (listLength)
    {
      if (listLength == INCREMENT && (int)listIncrement == INCREMENT) {
    listArray = (void **)new ArrayMemory;
    poolAllocated = CUBIT_TRUE;
      }
      else {
    listArray = new void* [listLength];
    poolAllocated = CUBIT_FALSE;
    currentAllocatedMemory += listLength;
    if (currentAllocatedMemory > maxAllocatedMemory)
      maxAllocatedMemory = currentAllocatedMemory;
      }
      assert(listArray != 0);
    }

  itemCount     = from.itemCount;

  if (itemCount)
    memcpy (listArray, from.listArray, itemCount*sizeof(void*));
}

Definition at line 73 of file ArrayBasedContainer.cpp.

{
    if ( listLength )
    { 
      if (poolAllocated) {
    delete ((ArrayMemory*) listArray);
      }
      else {
        delete [] listArray;
    currentAllocatedMemory -= listLength;
      }
      listArray = NULL;
    }
}

Member Function Documentation

void ArrayBasedContainer::clean_out ( ) [inline]

Reimplemented in DLList.

Definition at line 101 of file ArrayBasedContainer.hpp.

{
    itemCount = 0;
}
void ArrayBasedContainer::compress ( ) [virtual]

Reimplemented in DLList.

Definition at line 230 of file ArrayBasedContainer.cpp.

{
  int i, j;
  void *item;
  for ( i = j = 0; i < itemCount; i++ )
    if ((item = listArray[i]) != NULL) // = is ok
      listArray[j++] = item;
}

Definition at line 239 of file ArrayBasedContainer.cpp.

void ArrayBasedContainer::lengthen_list ( ) [protected]

Definition at line 89 of file ArrayBasedContainer.cpp.

{
  void **tempArray = 0;
  if (listLength == 0 && (int)listIncrement == INCREMENT)
  {
    poolAllocated = CUBIT_TRUE;
    listArray = (void **)new ArrayMemory;
    assert(listArray != 0);
    listLength = listIncrement;
    return;
  }
  
  // Normal allocation from here to end.

  tempArray = new void* [listLength + listIncrement];
  currentAllocatedMemory += (listLength + listIncrement);
  if (currentAllocatedMemory > maxAllocatedMemory)
    maxAllocatedMemory = currentAllocatedMemory;
  assert(tempArray != 0);
  
  // copy the old stuff into the new array
  if (listLength)
  {
    memcpy ( tempArray, listArray, listLength*sizeof(void*) );
    // delete the old array
    if (poolAllocated)
    {
      delete ((ArrayMemory*) listArray);
    }
    else {
      delete [] listArray;
      currentAllocatedMemory -= listLength;
    }
  }
  
  // set the new equal to the old
  listArray = tempArray;
  
  // update data
  poolAllocated = CUBIT_FALSE;
  listLength += listIncrement;
  listIncrement *= 2;
  listIncrement = listIncrement > 1000000 ? 1000000 : listIncrement;
}

Definition at line 244 of file ArrayBasedContainer.cpp.

{
  return maxAllocatedMemory;
}
ArrayBasedContainer & ArrayBasedContainer::operator+= ( const ArrayBasedContainer from)

Definition at line 182 of file ArrayBasedContainer.cpp.

{
  int tmp_itemCount = itemCount + from.itemCount;
  if (tmp_itemCount >= listLength)
    {
      if ((listLength + (int)listIncrement) < tmp_itemCount)
    {
      listIncrement = tmp_itemCount - listLength;
    }
      lengthen_list();
    }
  if (from.itemCount == 1)
    {
      listArray[itemCount] = from.listArray[0];
    }
  else
    {
      if (from.itemCount)
    memcpy (&listArray[itemCount], from.listArray,
        from.itemCount*sizeof(void*));
    }
  itemCount += from.itemCount;
  listIsSorted  = listIsSorted && from.listIsSorted;
  return *this;
}
ArrayBasedContainer & ArrayBasedContainer::operator-= ( const ArrayBasedContainer from)

Definition at line 209 of file ArrayBasedContainer.cpp.

{
  if ( !itemCount )
    return *this;
  int i, j;
  void *item;
  for ( i = 0; i < itemCount; i++ )
  {
    item = listArray[i];
    for ( j = from.itemCount; j--; )
      if ( from.listArray[j] == item ) 
      {
        listArray[i] = NULL;
        break;
      }
  }
    // compress, virtual
  compress();
  return *this;
}
ArrayBasedContainer & ArrayBasedContainer::operator= ( const ArrayBasedContainer from)

Definition at line 135 of file ArrayBasedContainer.cpp.

{
  if (this == &from)
    return *this;
  
  // See if the existing listArray length is equal to the from listArray
  // length. If it is, we can reuse it without deleting and newing.
  if (listLength != from.listLength) {
    if (listLength) {
      if (poolAllocated) {
    delete ((ArrayMemory*) listArray);
      } else {
    delete [] listArray;
    currentAllocatedMemory -= listLength;
      }
    }
    
    listLength = from.listLength;
    if (listLength)
      {
    if (listLength == INCREMENT && (int)listIncrement == INCREMENT)
      {
    listArray = (void **)new ArrayMemory;
    poolAllocated = CUBIT_TRUE;
      }
    else
      {
        listArray = new void* [listLength];
        poolAllocated = CUBIT_FALSE;
        currentAllocatedMemory += listLength;
        if (currentAllocatedMemory > maxAllocatedMemory)
          maxAllocatedMemory = currentAllocatedMemory;
      }
    assert(listArray != 0);
      }
  }
  itemCount = from.itemCount;
  listIncrement = from.listIncrement;
  listIsSorted  = from.listIsSorted;
  
  if (itemCount)
    memcpy (listArray, from.listArray, itemCount*sizeof(void*));
  
  return *this;
}
void ArrayBasedContainer::shrink ( int  k) [inline]

Reimplemented in DLList.

Definition at line 88 of file ArrayBasedContainer.hpp.

{
  if (k > 0) 
  {
    if (itemCount >= k)
      itemCount -= k;
    else
      itemCount = 0;
  }
}
int ArrayBasedContainer::size ( ) const [inline]

Definition at line 99 of file ArrayBasedContainer.hpp.

{ return itemCount; }

Member Data Documentation

unsigned int ArrayBasedContainer::currentAllocatedMemory = 0 [static, private]

Definition at line 84 of file ArrayBasedContainer.hpp.

Definition at line 73 of file ArrayBasedContainer.hpp.

void** ArrayBasedContainer::listArray [protected]

Definition at line 72 of file ArrayBasedContainer.hpp.

Definition at line 78 of file ArrayBasedContainer.hpp.

Definition at line 77 of file ArrayBasedContainer.hpp.

Definition at line 74 of file ArrayBasedContainer.hpp.

unsigned int ArrayBasedContainer::maxAllocatedMemory = 0 [static, private]

Definition at line 85 of file ArrayBasedContainer.hpp.

Definition at line 75 of file ArrayBasedContainer.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines