Skip to content

Commit

Permalink
13/6/2024 - home
Browse files Browse the repository at this point in the history
  • Loading branch information
Yair-vilner committed Jun 13, 2024
1 parent dcbce5b commit 99a1298
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
40 changes: 26 additions & 14 deletions oop1/MyColletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ namespace oop1
public partial class MyColletion<T> : IList<T>, IList, ICollection<T>, ICollection, IEnumerable<T>, IEnumerable
{
public MyColletion() { }
public MyColletion(IEnumerable<T> collection)
{
foreach (var item in collection)
{

}
}

private ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<ContinerOf16Items<T>>>>>>>> internalStorage;
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be 0 or more and less then the the collection length.");
SplitIndexTo8Levels(index, out int level1, out int level2, out int level3, out int level4, out int level5, out int level6, out int level7, out int level8);
return internalStorage[level8][level7][level6][level5][level4][level3][level2][level1];
}
set
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be 0 or more and less then the the collection length.");
SplitIndexTo8Levels(index, out int level1, out int level2, out int level3, out int level4, out int level5, out int level6, out int level7, out int level8);
lock (internalStorage)
{
Expand All @@ -34,7 +41,12 @@ public T this[int index]
object IList.this[int index]
{
get => this[index];
set => this[index] = (T)value;
set
{
if (!(value is T newValue))
throw new ArgumentException($"The value \"{value.GetType().FullName}\" is not of type \"{typeof(T).FullName}\" and cannot be used in this generic collection.", nameof(value));
this[index] = newValue;
}
}

public int Count { get; private set; } = 0;
Expand Down Expand Up @@ -64,7 +76,7 @@ private static void SplitIndexTo8Levels(int index, out int level1, out int level

public void Add(T item)
{
if (Count + 1 == int.MaxValue)
if (Count == int.MaxValue)
throw new InvalidOperationException("Collection length out of bounds");
SplitIndexTo8Levels(Count + 1, out int level1, out int level2, out int level3, out int level4, out int level5, out int level6, out int level7, out int level8);
internalStorage.GetNextLevelContiner(level8).GetNextLevelContiner(level7).GetNextLevelContiner(level6)
Expand All @@ -74,16 +86,12 @@ public void Add(T item)
}
int IList.Add(object value)
{
try
{
Add((T)value);
}
catch (InvalidCastException)
{
if (!(value is T newValue))
return -1;
}
Add(newValue);
return Count - 1;
}
public void AddRange(IEnumerable<T> collection) { throw new NotImplementedException(); } // TODO implement AddRange
public void Clear()
{
internalStorage = new();
Expand All @@ -102,8 +110,8 @@ public void CopyTo(T[] array, int arrayIndex)
}
void ICollection.CopyTo(Array array, int index)
{
if (array.GetType().GetElementType() == typeof(T))
CopyTo((T[])array, index);
if (array is T[] arrayOfT)
CopyTo(arrayOfT, index);
}
public bool Contains(T item)
{
Expand All @@ -128,6 +136,8 @@ int IList.IndexOf(object value) =>
value is T testedValue ? IndexOf(testedValue) : -1;
public void Insert(int index, T item)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be 0 or more and less then the the collection length.");
SplitIndexTo8Levels(index + 1, out int level1, out int level2, out int level3, out int level4, out int level5, out int level6, out int level7, out int level8);
SplitIndexTo8Levels(Count + 1, out int level1Max, out int level2Max, out int level3Max, out int level4Max, out int level5Max, out int level6Max, out int level7Max, out int level8Max);
T lastValue = this[index], newValue;
Expand Down Expand Up @@ -233,6 +243,8 @@ void IList.Remove(object value)
}
public void RemoveAt(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be 0 or more and less then the the collection length.");
SplitIndexTo8Levels(Count - 1, out int level1, out int level2, out int level3, out int level4, out int level5, out int level6, out int level7, out int level8);
SplitIndexTo8Levels(index, out int level1Min, out int level2Min, out int level3Min, out int level4Min, out int level5Min, out int level6Min, out int level7Min, out int level8Min);
T lastValue = this[Count], newValue;
Expand Down Expand Up @@ -317,6 +329,6 @@ public void RemoveAt(int index)
}
}
public override string ToString() =>
string.Format("{0}, count:{1}", base.ToString().Split('`')[0], Count.ToString());
string.Format("{0}, count:{1}", this.GetType().FullName, Count.ToString());
}
}
11 changes: 11 additions & 0 deletions oop1/MyDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace oop1
{
public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, ICollection<KeyValuePair<TKey, TValue>>, ICollection, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull
{

}
}
6 changes: 6 additions & 0 deletions oop1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace oop1
{
Expand All @@ -7,6 +9,10 @@ class Program
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//IList temp = new List<int>();
//temp.Add(1);
//temp[0] = new object();
//var temp2 = new Dictionary<int, object>();
}
}
}

0 comments on commit 99a1298

Please sign in to comment.