using System; using System.Collections; namespace MagicArray { /// /// ACHTUNG: /// NACH COPYTO ARBEITET FASTCONTAINS NICHT DA HASHTABLE LEER! /// public class FastArrayList : ArrayList { // für viele Elemente optimieren Hashtable _innerHash = new Hashtable(20000); /* * Wird von Add Range gebraucht, scheint allerdings keine Seiteneffekte zu haben * public override void CopyTo(int index, Array array, int arrayIndex, int count) { throw new NotImplementedException("CopyTo steht in FastArrayList nicht zur Verfügung"); } public override void CopyTo(Array array) { throw new NotImplementedException("CopyTo steht in FastArrayList nicht zur Verfügung"); } public override void CopyTo(Array array, int arrayIndex) { throw new NotImplementedException("CopyTo steht in FastArrayList nicht zur Verfügung"); } */ private void addToHash(object obj) { if (_innerHash.ContainsKey(obj)) { // erhöhe Zähler für dieses Object _innerHash[obj] = (int)_innerHash[obj] + 1; } else { _innerHash[obj] = 1; } } private void removeFromHash(object obj) { if (_innerHash.Contains(obj)) { int c = (int)_innerHash[obj]; if (c > 1) { _innerHash[obj] = c - 1; } else { _innerHash.Remove(obj); } } } public FastArrayList() : base() { } public FastArrayList(ICollection c) : base(c) { foreach (object obj in c) addToHash(obj); } public override int Add(object o) { addToHash(o); return base.Add(o); } public override void AddRange(ICollection c) { base.AddRange(c); foreach (object o in c) addToHash(o); } public override void Clear() { base.Clear(); _innerHash.Clear(); } public override object Clone() { FastArrayList o = (FastArrayList) base.Clone(); o._innerHash = (Hashtable) _innerHash.Clone(); return o; } public bool FastContains(object o) { return _innerHash.ContainsKey(o); } public override bool Contains(object o) { return base.Contains(o); } public override void Insert(int index, object value) { if(index < base.Count && index >= 0 && base[index]!=null) { removeFromHash(base[index]); } base.Insert(index, value); addToHash(value); } public override void InsertRange(int index, ICollection c) { for(int i = index; i < Math.Min(this.Count, index + c.Count); i++) { removeFromHash(base[i]); } base.InsertRange(index, c); foreach (object o in c) addToHash(o); } public override void Remove(object o) { base.Remove(o); removeFromHash(o); } public override void RemoveAt(int index) { removeFromHash(base[index]); base.RemoveAt(index); } public override void RemoveRange(int index, int count) { for(int i = index; i < count; i++) { removeFromHash(base[i]); } base.RemoveRange (index, count); } public override void SetRange(int index, ICollection c) { int j = 0; int i = 0; IEnumerator ienum = c.GetEnumerator(); while(ienum.MoveNext()) { if(i >= index) { this.Insert(j, ienum.Current); j++; } i++; } } public override object this[int index] { get { return base[index]; } set { removeFromHash(base[index]); base[index] = value; addToHash(value); } } } }