internal void SetItem(int index, Object item)
{
Object[] aObj = null;
if (item == null)
{
RemoveItem(index);
return;
}
switch (m_cElt)
{
case 0:
// on the first item, we don't create an array, we merely remember it's index and value
// this this the 99% case
m_cElt = 1;
m_maxIndex = (short)index;
m_Obj = item;
break;
case 1:
// we have to decide if a 2nd item has indeed been added and create the array
// if it has
if (index == m_maxIndex)
{
// replacing the one existing item
m_Obj = item;
}
else
{
// adding a second distinct permission
Object objSaved = m_Obj;
int iMax = Math.Max(m_maxIndex, index);
aObj = new Object[iMax+1];
aObj[m_maxIndex] = objSaved;
aObj[index] = item;
m_maxIndex = (short)iMax;
m_cElt = 2;
m_Set = aObj;
m_Obj = null;
}
break;
default:
// this is the general case code for when there is really an array
aObj = m_Set;
// we are now adding an item, check if we need to grow
if (index >= aObj.Length)
{
Object[] newset = new Object[index+1];
System.Array.Copy(aObj, 0, newset, 0, m_maxIndex+1);
m_maxIndex = (short)index;
newset[index] = item;
m_Set = newset;
m_cElt++;
}
else
{
if (aObj[index] == null)
m_cElt++;
aObj[index] = item;
if (index > m_maxIndex)
m_maxIndex = (short)index;
}
break;
}
}