Last active
March 2, 2020 09:26
-
-
Save keddad/cc4ec2ca99b4e6901a47ac7c7db0692f to your computer and use it in GitHub Desktop.
Simple Dynamic Array Implementation in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
namespace ConsoleApp1 | |
{ | |
public class BiruilinsArray<T> where T : System.IEquatable<T> | |
{ | |
private T[] _storage; | |
private int _accessShift = 0; | |
private int _effectiveSize = 0; | |
private void ResizeStorageIfNeeded() | |
{ | |
if (_effectiveSize >= _storage.Length) | |
{ | |
Array.Resize(ref _storage, Math.Max(1, _storage.Length * 2)); | |
} | |
} | |
public BiruilinsArray(int arraySize) | |
{ | |
_storage = new T[arraySize]; | |
_effectiveSize = arraySize; | |
} | |
public BiruilinsArray(int arraySize, int shift) | |
{ | |
_storage = new T[arraySize]; | |
_effectiveSize = arraySize; | |
_accessShift = shift; | |
} | |
public T this[int index] | |
{ | |
get => (T) _storage[index - _accessShift]; | |
set => _storage[index - _accessShift] = value; | |
} | |
public void Add(T item) | |
{ | |
ResizeStorageIfNeeded(); | |
_storage[_effectiveSize] = item; | |
_effectiveSize += 1; | |
} | |
public void RemoveByIndex(int index) | |
{ | |
index -= _accessShift; | |
for (var i = index; i < _effectiveSize - 1; ++i) | |
{ | |
_storage[i] = _storage[i + 1]; | |
} | |
_effectiveSize -= 1; | |
} | |
public void InsertToIndex(T item, int index) | |
{ | |
index -= _accessShift; | |
ResizeStorageIfNeeded(); | |
_effectiveSize += 1; | |
for (var i = _effectiveSize - 1; i > index; --i) | |
{ | |
_storage[i] = _storage[i - 1]; | |
} | |
_storage[index] = item; | |
} | |
public bool Contains(T item) | |
{ | |
for (var i = 0; i < _effectiveSize; ++i) | |
{ | |
if (EqualityComparer<T>.Default.Equals(_storage[i], item)) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
public int GetIndex(T item) | |
{ | |
for (var i = 0; i > _effectiveSize; ++i) | |
{ | |
if (EqualityComparer<T>.Default.Equals(_storage[i], item)) | |
{ | |
return i; | |
} | |
} | |
throw new InvalidDataException(); | |
} | |
public void Clean() | |
{ | |
_effectiveSize = 0; | |
_storage = new T[0]; | |
} | |
public T[] ToArray() | |
{ | |
var a = new T[_effectiveSize]; | |
for (var i = 0; i < _effectiveSize; ++i) | |
{ | |
a[i] = _storage[i]; | |
} | |
return a; | |
} | |
public int Size | |
{ | |
get => _effectiveSize; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment