Tips & Ticks by SAJJAD
Posts tagged Object
C# Sarray (Advaned Generic/Object Array)
Apr 12th
Sarray: Queueable, Stackable, Enumerable, Collectable, Listable, Disposable, Timmeable Array !
Add, Insert, Remove, Enqueue, Dequeue, Pop, Push @ Range
Enjoy, SAJJAD
[System.Diagnostics.DebuggerDisplay("Count = { Count }")]
public class Sarray< T > : ISarray, ISarray< T >
{
protected T[] Items;
public Sarray(bool queueTimered, double timmerTick, int queuesCapacity)
{
this.Clear();
if (queueTimered)
{
this.queueTimer = new Timer(timmerTick);
this.queueTimer.Elapsed += new ElapsedEventHandler(queueTimer_Elapsed);
}
this.queuesCapacity = queuesCapacity;
this.Enqueues();
this.Dequeues();
if (queueTimered)
{
this.queueTimer.Start();
}
}
#region { List }
int IList.Add(object value)
{
return this.InsertCore(this.Items.Length, (T)value);
}
void IList.Clear()
{
this.Clear();
}
bool IList.Contains(object value)
{
return this.Contains((T)value);
}
int IList.IndexOf(object value)
{
return this.IndexOf((T)value);
}
void IList.Insert(int index, object value)
{
this.InsertCore(index, (T)value);
}
bool IList.IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return false; }
}
void IList.Remove(object value)
{
this.DeleteCore((T)value);
}
void IList.RemoveAt(int index)
{
this.DeleteCore(index, index + 1);
}
object IList.this[int index]
{
get
{
return this[index];
}
set
{
this[index] = (T)value;
}
}
void ICollection.CopyTo(Array array, int index)
{
if (this.IsIndex(index, false))
{
Items.CopyTo(array, index);
}
}
int ICollection.Count
{
get { return this.Items.Length; }
}
bool ICollection.IsSynchronized
{
get { return true; }
}
object ICollection.SyncRoot
{
get { return this; }
}
#endregion
#region { IList< T > }
public virtual int IndexOf(T item)
{
for (int i = 0; i < this.Items.Length; i++)
{
if (this.Items[i].Equals(item))
return i;
}
return -1;
}
public virtual void Insert(int index, T item)
{
this.InsertCore(index, item);
}
public virtual void RemoveAt(int index)
{
this.DeleteCore(index, index + 1);
}
public virtual T this[int index]
{
get
{
return this.Seek(index);
}
set
{
T co = this.Seek(index);
if (co != null)
{
co = value;
}
}
}
public virtual void Add(T item)
{
this.InsertCore(this.Items.Length, item);
}
public virtual void Clear()
{
this.Items = new T[] { };
}
public virtual bool Contains(T item)
{
foreach (T co in this.Items)
{
if (co.Equals(item))
return true;
}
return false;
}
public virtual void CopyTo(T[] array, int arrayIndex)
{
if (this.IsIndex(arrayIndex, false))
{
Array.Copy(this.Items, array, arrayIndex);
}
}
public virtual int Count
{
get { return this.Items.Length; }
}
public virtual bool IsReadOnly
{
get { return false; }
}
public virtual bool Remove(T item)
{
return this.RemoveRange(item) > 0;
}
public virtual IEnumerator< T > GetEnumerator()
{
foreach (T co in this.Items)
yield return co;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.Items.GetEnumerator();
}
#endregion
#region { IDisposible }
private bool disposed = false;
public bool Disposed
{
get
{
return this.disposed;
}
}
public virtual void Dispose()
{
if (!this.disposed)
{
this.Items = null;
this.enqueue = null;
this.dequeue = null;
this.queueTimer.Dispose();
this.queueTimer = null;
}
}
#endregion
public virtual bool IsIndex(int index, bool withEnd)
{
if (withEnd)
return index >= 0 && index <= this.Items.Length;
return index >= 0 && index < this.Items.Length;
}
public virtual T Seek(int index)
{
for (int i = 0; i < this.Items.Length; i++)
{
if (i == index)
return this.Items[i];
}
return default(T);
}
public virtual IEnumerable Seek(int from, int to)
{
if (this.IsIndex(from, false) && this.IsIndex(to, true) && from < to && from != to)
{
for (int i = 0; i < this.Items.Length; i++)
{
if (i >= from && i < to)
{
yield return this.Items[i];
}
}
}
}
public virtual int Push(T iteme)
{
return this.InsertCore(this.Items.Length, iteme);
}
public virtual int PushRange(params T[] itemes)
{
return this.InsertCore(this.Items.Length, itemes);
}
public virtual int AddRange(params T[] items)
{
return this.InsertCore(this.Items.Length, items);
}
public virtual int InsertRange(int index, params T[] items)
{
return this.InsertCore(index, items);
}
public virtual int Pop(int count = 1)
{
if (count > 0)
return this.DeleteCore(this.Items.Length - count, this.Items.Length);
return 0;
}
public virtual int RemoveRange(params T[] items)
{
return this.DeleteCore(items);
}
protected virtual int InsertCore(int index, params T[] items)
{
Int32 newRealMembers = 0;
if (items != null && this.IsIndex(index, true))
{
if (items.Length > 0) // At least one new child want added
{
// We thing all new childs are new
T[] newMembers = new T[items.Length];
// Flag for child existance
Boolean exist = false;
// Check existance
foreach (T item in items)
{
if (item != null)
{
exist = false;
foreach (T at in this.Items)
if (at.Equals(item))
exist = true;
if (!exist)
newMembers[newRealMembers++] = item;
}
}
if (newRealMembers > 0)
{
T[] newArray = new T[this.Items.Length + newRealMembers];
for (int i = 0, j = 0; i < newArray.Length; )
{
if (index == i)
{
for (int z = 0; z < newRealMembers; z++)
{
newArray[i++] = newMembers[z];
}
}
else
{
newArray[i++] = this.Items[j++];
}
}
this.Items = newArray;
}
}
}
return newRealMembers;
}
protected virtual int DeleteCore(params T[] items)
{
Int32 newMembers = 0, lastSize = 0;
if (items != null)
{
if (items.Length > 0)
{
T[] newArray = new T[this.Items.Length];
// Is item exist ?
Boolean exist = false;
// Find no selected childs
foreach (T t in this.Items)
{
exist = false;
foreach (T item in items)
{
if (t.Equals(item))
{
exist = true;
break;
}
}
if (!exist)
{
newArray[newMembers++] = t;
}
}
if (newMembers > 0 && newMembers != this.Items.Length)
{
lastSize = this.Items.Length;
Array.Resize(ref newArray, newMembers);
this.Items = newArray;
}
}
}
return lastSize - newMembers;
}
protected virtual int DeleteCore(int from, int to)
{
int applied = 0;
if (this.IsIndex(from, false) && this.IsIndex(to, true) && from < to && from != to)
{
T[] newArray = new T[this.Items.Length - (from - to)];
for (int i = 0, newIndex = 0; i < this.Items.Length; i++)
{
if (i >= from && i < to)
{
newArray[newIndex++] = this.Items[i];
}
}
applied = from - to;
this.Items = newArray;
}
return applied;
}
#region { Timer }
private bool queueTimered = false;
public bool QueueTimered
{
get
{
return this.queueTimered;
}
}
private Timer queueTimer;
private void queueTimer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Enqueues();
this.Dequeues();
}
///
/// Queue Refresh Time (100 to 10000 ms, 1000 is default)
///
public double QueueTick
{
get
{
return this.queueTimer.Interval;
}
set
{
if (value > 100 && value < 10000 && this.queueTimered)
{
this.queueTimer.Interval = value;
}
}
}
#endregion
protected int queuesCapacity = 0;
public int QueuesCapacity
{
get
{
return this.queuesCapacity;
}
set
{
if (value >= 0 && value <= 10000)
{
this.queuesCapacity = value;
}
}
}
#region { Enqueue }
protected T[] enqueue;
protected int enqueueCount = 0;
public int EnqueueCount
{
get
{
return this.enqueueCount;
}
}
public virtual int Enqueue(params T[] items)
{
if (items != null)
{
if (items.Length > 0)
{
if (this.enqueue.Length > enqueueCount + items.Length)
{
int added = 0, nulled = 0;
for (int i = 0; i < this.enqueue.Length; i++)
{
if (this.enqueue[i] == null)
{
foreach (T co in items)
{
if (co == null)
{
nulled++;
}
else
{
this.enqueue[i] = co;
added++;
this.enqueueCount++;
break;
}
}
// All object checked
if (added + nulled == items.Length)
{
break;
}
}
}
return added;
}
else
{
this.Enqueues();
return this.InsertCore(this.Items.Length, items);
}
}
}
return 0;
}
public void Enqueues()
{
if (this.enqueue == null)
{
this.enqueue = new T[this.queuesCapacity];
}
if (this.enqueueCount > 0)
{
this.InsertCore(this.Items.Length, this.enqueue);
this.enqueueCount = 0;
}
}
#endregion
#region { Dequeue }
protected T[] dequeue;
protected int dequeueCount = 0;
public int DequeueCount
{
get
{
return this.dequeueCount;
}
}
public virtual int Dequeue(params T[] items)
{
if (items != null)
{
if (items.Length > 0)
{
if (this.dequeue.Length < dequeueCount + items.Length)
{
int added = 0, nulled = 0;
for (int i = 0; i < this.dequeue.Length; i++)
{
if (this.dequeue[i] == null)
{
foreach (T co in items)
{
if (co == null)
{
nulled++;
}
else
{
this.dequeue[i] = co;
added++;
dequeueCount++;
break;
}
}
// All object checked
if (added + nulled == items.Length)
{
break;
}
}
}
return added;
}
else
{
this.Dequeues();
return this.DeleteCore(items);
}
}
}
return 0;
}
public void Dequeues()
{
if (this.dequeue == null)
{
this.dequeue = new T[this.queuesCapacity];
}
if (this.dequeueCount > 0)
{
this.DeleteCore(this.dequeue);
this.dequeueCount = 0;
}
}
#endregion
}
Recent Comments