Back to index

lightning-sunbird  0.9+nobinonly
tmVector.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Transaction Manager.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   John Gaunt <jgaunt@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdlib.h>
00039 #include "tmVector.h"
00040 
00042 // Constructor(s) & Destructor
00043 
00044 // can not be responsible for reclaiming memory pointed to by the void*s in
00045 //   the collection - how would we reclaim, don't know how they were allocated
00046 tmVector::~tmVector() {
00047   if (mElements)
00048     free((void*)mElements);
00049 }
00050 
00052 // Public Member Functions
00053 
00054 nsresult
00055 tmVector::Init() {
00056 
00057   mElements = (void**) calloc (mCapacity, sizeof(void*));
00058   if (!mElements)
00059     return NS_ERROR_OUT_OF_MEMORY;
00060   return NS_OK;
00061 }
00062 
00064 // mutators
00065 
00066 PRInt32
00067 tmVector::Append(void *aElement){
00068   PR_ASSERT(aElement);
00069 
00070   // make sure there is room
00071   if (mNext == mCapacity)
00072     if (NS_FAILED(Grow()))
00073       return -1;
00074 
00075   // put the element in the array
00076   mElements[mNext] = aElement;
00077   mCount++;
00078 
00079   // encapsulates the index into a success value
00080   return mNext++; // post increment.
00081 }
00082 
00083 void
00084 tmVector::Remove(void *aElement) {
00085   PR_ASSERT(aElement);
00086 
00087   for (PRUint32 index = 0; index < mNext; index++) {
00088     if (mElements[index] == aElement) {
00089       mElements[index] = nsnull;
00090       mCount--;
00091       if (index == mNext-1) {   // if we removed the last element
00092         mNext--;
00093         // don't test for success of the shrink
00094         Shrink();
00095       }
00096     }
00097   }
00098 }
00099 
00100 void
00101 tmVector::RemoveAt(PRUint32 aIndex) {
00102   PR_ASSERT(aIndex < mNext);
00103 
00104   // remove the element if it isn't already nsnull
00105   if (mElements[aIndex] != nsnull) {
00106     mElements[aIndex] = nsnull;
00107     mCount--;
00108     if (aIndex == mNext-1) {   // if we removed the last element
00109       mNext--;
00110       // don't test for success of the shrink
00111       Shrink();
00112     }
00113   }
00114 }
00115 
00116 //void*
00117 //tmVector::operator[](int index) {
00118 //  if (index < mNext && index >= 0)
00119 //    return mElements[index]; 
00120 //  return nsnull;
00121 //}
00122 
00123 // Does not delete any of the data, merely removes references to them
00124 void
00125 tmVector::Clear(){
00126   memset(mElements, 0, mCapacity);
00127   mCount = 0;
00128   mNext = 0;
00129 }
00130 
00132 // Protected Member Functions
00133 
00134 // increases the capacity by the growth increment
00135 nsresult
00136 tmVector::Grow() {
00137 
00138   PRUint32 newcap = mCapacity + GROWTH_INC;
00139   mElements = (void**) realloc(mElements, (newcap * sizeof(void*)));
00140   if (mElements) {
00141     mCapacity = newcap;
00142     return NS_OK;
00143   }
00144   return NS_ERROR_FAILURE;
00145 }
00146 
00147 // reduces the capacity by the growth increment. leaves room
00148 //   for one more add before needing to Grow().
00149 nsresult
00150 tmVector::Shrink() {
00151 
00152   PRUint32 newcap = mCapacity - GROWTH_INC;
00153   if (mNext < newcap) {
00154     mElements = (void**) realloc(mElements, newcap * sizeof(void*));
00155     if (!mElements)
00156       return NS_ERROR_OUT_OF_MEMORY;
00157     mCapacity = newcap;
00158   }
00159   return NS_OK;
00160 }