// This function demonstrates the basic methods of our arrays and lists. // To keep it simple it does not check for errors. For real-life code // you have to check the return value of Append() or Insert() before you // access the memory. static void BaseArrayDemo() { BaseArray<VLONG> test; // this could be BlockArray, PointerArray, SortedArray or BaseList VLONG copyMe = 42; VLONG i; test.Append(); // append an element with default value test.Append(copyMe); // append a copy of copyMe test.Insert(1); // insert an element with default value at index 1 test.Insert(2, copyMe); // insert a copy of copyMe at index 2 test.Erase(0); // erase element at index 0 test[2] = 12345; // assign a value to the element at index 2 // iterate over all elements, assign value for (i = 0; i < test.GetCount(); i++) test[i] = i; test.Resize(27); // the array has now 27 elements test.Erase(10, 15); // erase 15 elements from index 10 on test.Append(9876); test.Append(54321); // iterate over all elements, check for some value for (AutoIterator<BaseArray<VLONG> > it(test); it; ++it) { if (*it == 9876) break; // BTW: the index of this element is it - test.Begin(); } }
//----------------------------------------------------------------------------- EStatus BaseArray::Insert (INT iStartIndex, INT iNumToInsert, BOOL bDebug) { // This routine inserts new entries before the iStartIndex. INT iStartIndexActual = iStartIndex; // perform the insertion on the actual sequential data INT iOldLength = iLength; if (iStartIndexActual > iLength) return (EStatus::kFailure); if (SetLength (iOldLength + iNumToInsert) == EStatus::kFailure) {return EStatus::kFailure;}; if (Length () < (iOldLength + iNumToInsert) ) {return EStatus::kFailure;}; if (bDebug) { DBG_INFO ("BA:Ins %d old:%d cur:%d\n", iStartIndexActual, iOldLength, Length ()); }; //printf ("start index %d old length %d\n", iStartIndexActual,iOldLength); if ((iStartIndexActual != iLength) && (iOldLength - iStartIndexActual > 0)) { //DBG_INFO ("Copy values rev %d %d %d\n", iStartIndexActual, iStartIndexActual + iNumToInsert, iOldLength - iStartIndexActual); CopyValuesRev (pArray, iStartIndexActual, iStartIndexActual + iNumToInsert, iOldLength - iStartIndexActual); }; // update siblings BaseArray * pCurr = pNext; while (pCurr != this) { pCurr->Insert (iStartIndex, iNumToInsert); pCurr = pCurr->pNext; }; return (EStatus::kSuccess); };