// Allocate enough entries to store 'maxInstances' items. // This is useful when a Array must be copied into another // Array, to avoid unnecessary realloc() calls. public void Reserve(int maxInstances) { if (_maxInstances >= maxInstances) { return; } if (_instances == null || _instances.Length == 0) { _instances = new T[maxInstances]; //(T *)malloc(maxInstances * sizeof(T)); } else { T[] insts = new T[maxInstances]; for (int xx = 0; xx < _nInstances; ++xx) { insts[xx] = _instances[xx]; } Globals.delete(_instances); _instances = insts; // _instances = (T *)realloc(_instances, maxInstances * sizeof(T)); } if (_instances == null || _instances.Length == 0) { Globals.Panic("out of memory"); } _maxInstances = maxInstances; }
// Add an item to the set public void Add(T item) { if (_nInstances >= _maxInstances) { _maxInstances += Array_INCREMENT; if (_instances == null || _instances.Length == 0) { _instances = new T[_maxInstances]; } else { T[] insts = new T[_maxInstances]; for (int xx = 0; xx < _nInstances; ++xx) { insts[xx] = _instances[xx]; } Globals.delete(_instances); _instances = insts; } if (_instances == null || _instances.Length == 0) { Globals.Panic("out of memory"); } // Erik: Disabled following line // memset(_instances + _maxInstances - Array_INCREMENT, // 0, Array_INCREMENT * sizeof(T)); } _instances[_nInstances++] = item; }
// Return item at position 'i' public T At(int i) { if (i >= _nInstances) { Globals.Panic("out of bounds access"); } return(_instances[i]); }
public T this[int i] { get { if (i >= _nInstances) { Globals.Panic("out of bounds access"); } return(_instances[i]); } }
public T Pop() { if (_nInstances > 0) { --_nInstances; return(_instances[_nInstances]); } Globals.Panic("pop from an empty stack"); return(_instances[0]); }