public
anonymous / FrontLoader
Created

FrontLoader; immutable random access builder

  • Download Gist
FrontLoader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace Living.In.Some.Namespace
{
public interface IReadOnlyWindow<T>
: IEnumerable<T>
{
int Count { get; }
T this[int index] { get; }
IFrontLoader<T> Mitosis();
}
 
public interface IFrontLoader<T>
{
IReadOnlyWindow<T> PushFront(T item);
IReadOnlyWindow<T> Current { get; }
T PeekFront { get; }
T PeekBack { get; }
}
 
public enum FrontLoaderTypes
{
Grow,
Circular,
}
 
public class FrontLoader<T>
: IFrontLoader<T>
{
private const int MinArraySize = 4;
 
private T[] _data;
private int _offset;
 
private readonly int _initial;
private readonly FrontLoaderTypes _type;
 
public FrontLoader(FrontLoaderTypes type, int initial)
{
_type = type;
_initial = initial;
 
var arraySize = Math.Max(MinArraySize, initial * 2);
_data = new T[arraySize];
_offset = arraySize;
}
 
// Used for Mitosis
private FrontLoader(T[] roData, int roOffset, int roCount, int initial)
{
_data = new T[roData.Length];
_offset = _data.Length - roCount;
_initial = initial;
 
Array.Copy(roData, roOffset, _data, _offset, roCount);
}
 
class ReadOnlyWindow
: IReadOnlyWindow<T>
, IList<T>
{
private readonly FrontLoader<T> _parent;
private readonly T[] _data;
private readonly int _offset;
private readonly int _count;
 
public ReadOnlyWindow(FrontLoader<T> parent, T[] data, int offset, int count)
{
_parent = parent;
_data = data;
_offset = offset;
_count = count;
}
 
private static Exception NotSupported() { return new NotSupportedException("Collection is read-only."); }
 
void IList<T>.Insert(int index, T item) { throw NotSupported(); }
void IList<T>.RemoveAt(int index) { throw NotSupported(); }
void ICollection<T>.Add(T item) { throw NotSupported(); }
void ICollection<T>.Clear() { throw NotSupported(); }
bool ICollection<T>.Remove(T item) { throw NotSupported(); }
 
public T this[int index]
{
get
{
if (index < 0 || index >= _count)
throw new IndexOutOfRangeException();
 
return _data[_offset + index];
}
set { throw NotSupported(); }
}
 
int IList<T>.IndexOf(T item)
{
for (int i = _offset; i < _offset + _count; ++i)
{
if (_data[i].Equals(item))
return i - _offset;
}
return -1;
}
 
bool ICollection<T>.Contains(T item)
{
var index = ((IList<T>)this).IndexOf(item);
return index >= 0;
}
 
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
Array.Copy(_data, _offset, array, 0, _count);
}
 
public int Count { get { return _count; } }
bool ICollection<T>.IsReadOnly { get { return true; } }
 
public IEnumerator<T> GetEnumerator()
{
for (int i = _offset; i < _offset + _count; ++i)
yield return _data[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
 
public IFrontLoader<T> Mitosis()
{
return new FrontLoader<T>(_data, _offset, _count, _parent._initial);
}
}
 
public IReadOnlyWindow<T> PushFront(T item)
{
if (--_offset < 0)
{
switch (_type)
{
case FrontLoaderTypes.Circular:
var circular = new T[_data.Length];
Array.Copy(_data, 0, circular, _data.Length - _initial, _initial);
_data = circular;
_offset = _data.Length - _initial - 1;
break;
 
case FrontLoaderTypes.Grow:
var grow = new T[_data.Length * 2];
Array.Copy(_data, 0, grow, _data.Length, _data.Length);
_offset = _data.Length - 1;
_data = grow;
break;
 
default:
throw new ApplicationException(string.Format("Unknown type {0}", _type));
}
}
_data[_offset] = item;
 
return Current;
}
 
public IReadOnlyWindow<T> Current
{
get
{
if (_type == FrontLoaderTypes.Circular)
return new ReadOnlyWindow(this, _data, _offset, Math.Min(_initial, _data.Length - _offset));
 
return new ReadOnlyWindow(this, _data, _offset, _data.Length - _offset);
}
}
 
public T PeekFront
{
get { return _data[_offset]; }
}
 
public T PeekBack
{
get { return _data[_data.Length - 1]; }
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.