Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
tmVector Class Reference

A simple, clear, self-growing, collection of objects. More...

#include <tmVector.h>

List of all members.

Public Member Functions

 tmVector ()
 Set some sane default values to set up the internal storage.
virtual ~tmVector ()
 Reclaim the memory allocated in the Init() method.
nsresult Init ()
 Allocates the storage back-end.
PRInt32 Append (void *aElement)
void Remove (void *aElement)
 This does not collapse the collection, it leaves holes.
void RemoveAt (PRUint32 aIndex)
 This does not collapse the collection, it leaves holes.
void Clear ()
 Does not call delete on the elements since we have no idea how to reclaim the memory.
voidoperator[] (PRUint32 index)
PRUint32 Count ()
PRUint32 Size ()
 Meant to be used as the conditional in a loop.

Protected Member Functions

nsresult Grow ()
nsresult Shrink ()

Protected Attributes

PRUint32 mNext
PRUint32 mCount
PRUint32 mCapacity
void ** mElements

Detailed Description

A simple, clear, self-growing, collection of objects.

typed independant basically a growing array. Useful in situations where you need an indexed collection but do not know the size in advance and need the ability for increase and decrease in size. Not optimized for anything in particular, or any size in particular.

Is able to guarantee the index of an item will not change due to removals of a lower indexed item. The growing, and shrinking all happens to the end of the collection

Does not backfill, adds to the end. At some point this should be changed to make best use of space.

Definition at line 59 of file tmVector.h.


Constructor & Destructor Documentation

tmVector::tmVector ( ) [inline]

Set some sane default values to set up the internal storage.

Init() must be called after construction of the object to allcate the backing store.

Definition at line 71 of file tmVector.h.

: mNext(0), mCount(0), mCapacity(10), mElements(nsnull) {;}
tmVector::~tmVector ( ) [virtual]

Reclaim the memory allocated in the Init() method.

Definition at line 46 of file tmVector.cpp.

                    {
  if (mElements)
    free((void*)mElements);
}

Member Function Documentation

PRInt32 tmVector::Append ( void aElement)
Returns:
the index of the element added, if successful
-1 if an error occured during allocation of space

Definition at line 67 of file tmVector.cpp.

                              {
  PR_ASSERT(aElement);

  // make sure there is room
  if (mNext == mCapacity)
    if (NS_FAILED(Grow()))
      return -1;

  // put the element in the array
  mElements[mNext] = aElement;
  mCount++;

  // encapsulates the index into a success value
  return mNext++; // post increment.
}

Here is the call graph for this function:

Here is the caller graph for this function:

Does not call delete on the elements since we have no idea how to reclaim the memory.

Sets all array slots to 0.

Definition at line 125 of file tmVector.cpp.

               {
  memset(mElements, 0, mCapacity);
  mCount = 0;
  mNext = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRUint32 tmVector::Count ( ) [inline]
Returns:
the number of elements stored

Definition at line 127 of file tmVector.h.

{ return mCount; }
nsresult tmVector::Grow ( ) [protected]

Definition at line 136 of file tmVector.cpp.

               {

  PRUint32 newcap = mCapacity + GROWTH_INC;
  mElements = (void**) realloc(mElements, (newcap * sizeof(void*)));
  if (mElements) {
    mCapacity = newcap;
    return NS_OK;
  }
  return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Allocates the storage back-end.

Returns:
NS_OK if allocation succeeded
NS_ERROR_OUT_OF_MEMORY if the allocation failed

Definition at line 55 of file tmVector.cpp.

               {

  mElements = (void**) calloc (mCapacity, sizeof(void*));
  if (!mElements)
    return NS_ERROR_OUT_OF_MEMORY;
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* tmVector::operator[] ( PRUint32  index) [inline]
Returns:
the element at the index indicated, including nsnull if the slot is empty.

Definition at line 119 of file tmVector.h.

                                   { 
    PR_ASSERT(index < mNext);
    return mElements[index]; 
  }
void tmVector::Remove ( void aElement)

This does not collapse the collection, it leaves holes.

Note, it also doesn't delete the element, it merely removes it from the collection

Definition at line 84 of file tmVector.cpp.

                               {
  PR_ASSERT(aElement);

  for (PRUint32 index = 0; index < mNext; index++) {
    if (mElements[index] == aElement) {
      mElements[index] = nsnull;
      mCount--;
      if (index == mNext-1) {   // if we removed the last element
        mNext--;
        // don't test for success of the shrink
        Shrink();
      }
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

This does not collapse the collection, it leaves holes.

Note, it also doesn't delete the element, it merely removes it from the collection

Definition at line 101 of file tmVector.cpp.

                                  {
  PR_ASSERT(aIndex < mNext);

  // remove the element if it isn't already nsnull
  if (mElements[aIndex] != nsnull) {
    mElements[aIndex] = nsnull;
    mCount--;
    if (aIndex == mNext-1) {   // if we removed the last element
      mNext--;
      // don't test for success of the shrink
      Shrink();
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult tmVector::Shrink ( ) [protected]

Definition at line 150 of file tmVector.cpp.

                 {

  PRUint32 newcap = mCapacity - GROWTH_INC;
  if (mNext < newcap) {
    mElements = (void**) realloc(mElements, newcap * sizeof(void*));
    if (!mElements)
      return NS_ERROR_OUT_OF_MEMORY;
    mCapacity = newcap;
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRUint32 tmVector::Size ( ) [inline]

Meant to be used as the conditional in a loop.

|index < size| should reach all elements of the collection and not run out of bounds. If slots 0,1,4,5,6 contain elements Size() will return 7, Count() will return 5.

Returns:
the number of slots in the array taken, irrespective of holes in the collection.

Definition at line 138 of file tmVector.h.

{ return mNext; }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 151 of file tmVector.h.

Definition at line 150 of file tmVector.h.

void** tmVector::mElements [protected]

Definition at line 154 of file tmVector.h.

Definition at line 149 of file tmVector.h.


The documentation for this class was generated from the following files: