bintools/rcomp/src/ARRAY.CPP
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <stdio.h>
       
    20 #include <assert.h>
       
    21 #include "ARRAY.H"
       
    22 
       
    23 Array::Array():
       
    24 	iData(NULL),
       
    25 	iItemCount(0),
       
    26 	iItemAllocCount(0)
       
    27 	{}
       
    28 
       
    29 Array::~Array()
       
    30 	{
       
    31 	delete [] iData;
       
    32 	}
       
    33 
       
    34 Array::Array(const Array& aSource)
       
    35 	{
       
    36 	iItemCount=aSource.iItemCount;
       
    37 	iItemAllocCount=aSource.iItemAllocCount;
       
    38 	assert(iItemAllocCount>=iItemCount);
       
    39 	if (iItemAllocCount>0)
       
    40 		{
       
    41 		iData=new ArrayItem* [iItemAllocCount];
       
    42 		assert(iData!=0);
       
    43 		for(int i=0;i<iItemCount;i++)
       
    44 			{
       
    45 			iData[i]=aSource.iData[i];
       
    46 			}
       
    47 		}
       
    48 	}
       
    49 
       
    50 void Array::Empty()
       
    51 	{
       
    52 	delete [] iData;
       
    53 	iData = NULL;
       
    54 	iItemCount = 0;
       
    55 	iItemAllocCount = 0;
       
    56 	}
       
    57 
       
    58 Array& Array::operator= (const Array& aSource)
       
    59 	{
       
    60 	if(&aSource==this)
       
    61 		return *this;
       
    62 	DeleteAll();
       
    63 	iItemCount=aSource.iItemCount;
       
    64 	iItemAllocCount=aSource.iItemAllocCount;
       
    65 	assert(iItemAllocCount>=iItemCount);
       
    66 	if (iItemAllocCount>0)
       
    67 		{
       
    68 		iData=new ArrayItem* [iItemAllocCount];
       
    69 		assert(iData!=0);
       
    70 		for(int i=0;i<iItemCount;i++)
       
    71 			{
       
    72 			iData[i]=aSource.iData[i];
       
    73 			}
       
    74 		}
       
    75 	return *this;
       
    76 	}
       
    77 
       
    78 ArrayItem*& Array::operator[](int aIndex) const
       
    79 	{
       
    80 	assert(aIndex>=0);
       
    81 	assert(aIndex<iItemCount);
       
    82 	return iData[aIndex];
       
    83 	}
       
    84 
       
    85 int Array::Size() const
       
    86 	{
       
    87 	return iItemCount;
       
    88 	}
       
    89 
       
    90 void Array::Add(ArrayItem* aNewItem)
       
    91 	{
       
    92 	Add(iItemCount, aNewItem);
       
    93 	}
       
    94 
       
    95 void Array::Add(int aIndex, ArrayItem* aNewItem)
       
    96 	{
       
    97 	assert(aIndex>=0);
       
    98 	assert(aIndex<=iItemCount);
       
    99 	assert(iItemCount<=iItemAllocCount);
       
   100 	if (iItemCount<iItemAllocCount)
       
   101 		{
       
   102 		++iItemCount;
       
   103 		for (int i=iItemCount-1;i>aIndex;--i)
       
   104 			{
       
   105 			iData[i]=iData[i-1];
       
   106 			}
       
   107 		iData[aIndex]=aNewItem;
       
   108 		}
       
   109 	else
       
   110 		{
       
   111 		iItemAllocCount+=4;
       
   112 		ArrayItem** const data=new ArrayItem* [iItemAllocCount];
       
   113 		assert(data!=0);
       
   114 		++iItemCount;
       
   115 		int i;
       
   116 		for (i=iItemCount-1;i>aIndex;--i)
       
   117 			{
       
   118 			data[i]=iData[i-1];
       
   119 			}
       
   120 		data[aIndex]=aNewItem;
       
   121 		for (i=0;i<aIndex;++i)
       
   122 			{
       
   123 			data[i]=iData[i];
       
   124 			}
       
   125 		delete [] iData;
       
   126 		iData = data;
       
   127 		}
       
   128 	}
       
   129 
       
   130 void Array::Discard(ArrayItem* aItem)
       
   131 	{
       
   132 	// Find index of item to remove.
       
   133 	for(int i=0;i<iItemCount;++i)
       
   134 		{
       
   135 		if(iData[i]==aItem)
       
   136 			{
       
   137 			Discard(i);
       
   138 			break;
       
   139 			}
       
   140 		}
       
   141 	}
       
   142 
       
   143 void Array::Discard(int aIndex)
       
   144 	{
       
   145 	assert(aIndex>=0);
       
   146 	assert(aIndex<iItemCount);
       
   147 	delete iData[aIndex];
       
   148 	--iItemCount;
       
   149 	for (int i=aIndex;i<iItemCount;++i)
       
   150 		{
       
   151 		iData[i]=iData[i+1];
       
   152 		}
       
   153 	}
       
   154 
       
   155 void Array::DeleteAll()
       
   156 	{
       
   157 	for(int i=0;i<iItemCount;i++)
       
   158 		delete iData[i];
       
   159 	delete [] iData;
       
   160 	iData=NULL;
       
   161 	iItemCount = 0;
       
   162 	iItemAllocCount = 0;
       
   163 	}	
       
   164 
       
   165 ArrayIterator::ArrayIterator(const Array& aArray)
       
   166 	{
       
   167 	iArray=&aArray;
       
   168 	iCurrentIndex=0;
       
   169 	}
       
   170 
       
   171 ArrayItem * ArrayIterator::operator()()
       
   172 	{
       
   173 	if(iCurrentIndex>=iArray->Size())
       
   174 		return NULL;
       
   175 	ArrayItem* p=(*iArray)[iCurrentIndex++];
       
   176 	return p;
       
   177 	}
       
   178 
       
   179 void ArrayIterator::Reset()
       
   180 	{
       
   181 	iCurrentIndex=0;
       
   182 	}
       
   183