From b6e05186b7c0c1cbb82dfab40bad5b5f95c1c364 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Wed, 3 May 2023 15:09:20 -0300 Subject: [PATCH] Improve ILIst and add IDictionary --- BeefLibs/corlib/src/Collections/Dictionary.bf | 59 +++++++++++++++++-- BeefLibs/corlib/src/Collections/List.bf | 59 +++++++++++++++---- BeefLibs/corlib/src/Collections/SplitList.bf | 51 ++++++++++++---- BeefLibs/corlib/src/Variant.bf | 2 + 4 files changed, 140 insertions(+), 31 deletions(-) diff --git a/BeefLibs/corlib/src/Collections/Dictionary.bf b/BeefLibs/corlib/src/Collections/Dictionary.bf index 89068e69..e9646c99 100644 --- a/BeefLibs/corlib/src/Collections/Dictionary.bf +++ b/BeefLibs/corlib/src/Collections/Dictionary.bf @@ -6,15 +6,31 @@ #define VERSION_DICTIONARY #endif +using System; +using System.Collections; +using System.Diagnostics; +using System.Diagnostics.Contracts; +using System.Threading; + namespace System.Collections { - using System; - using System.Collections; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.Threading; + interface IDictionary + { + Variant this[Variant key] + { + get; + set; + } + + bool ContainsKey(Variant key); + bool ContainsValue(Variant value); + void Add(Variant key, Variant value); + void Clear(); + void Remove(Variant key); + } public class Dictionary : + IDictionary, ICollection<(TKey key, TValue value)>, IEnumerable<(TKey key, TValue value)>, IRefEnumerable<(TKey key, TValue* valueRef)> where TKey : IHashable @@ -130,11 +146,29 @@ namespace System.Collections } } + + Variant IDictionary.this[Variant key] + { + get + { + return [Unbound]Variant.Create(this[key.Get()]); + } + set + { + this[key.Get()] = value.Get(); + } + } + public void Add(TKey key, TValue value) { Insert(key, value, true); } + void IDictionary.Add(Variant key, Variant value) + { + Add(key.Get(), value.Get()); + } + public void Add(KeyValuePair kvPair) { Insert(kvPair.key, kvPair.value, true); @@ -257,6 +291,11 @@ namespace System.Collections return FindEntry(key) >= 0; } + bool IDictionary.ContainsKey(Variant key) + { + return ContainsKey(key.Get()); + } + public bool ContainsKeyAlt(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey { return FindEntryAlt(key) >= 0; @@ -270,6 +309,11 @@ namespace System.Collections } return false; } + + bool IDictionary.ContainsValue(Variant value) + { + return ContainsValue(value.Get()); + } public bool Contains(KeyValuePair kvPair) { @@ -659,6 +703,11 @@ namespace System.Collections return false; } + void IDictionary.Remove(Variant key) + { + Remove(key.Get()); + } + public bool RemoveAlt(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey { if (mBuckets != null) diff --git a/BeefLibs/corlib/src/Collections/List.bf b/BeefLibs/corlib/src/Collections/List.bf index 54882397..4f003795 100644 --- a/BeefLibs/corlib/src/Collections/List.bf +++ b/BeefLibs/corlib/src/Collections/List.bf @@ -21,6 +21,14 @@ namespace System.Collections get; set; } + + void Add(Variant item); + void Clear(); + bool Contains(Variant item); + int IndexOf(Variant item); + void Insert(int index, Variant item); + bool Remove(Variant item); + void RemoveAt(int index); } public class List : IEnumerable, IList, ICollection @@ -217,6 +225,19 @@ namespace System.Collections } } + Variant IList.this[int index] + { + get + { + return [Unbound]Variant.Create(this[index]); + } + + set + { + this[index] = value.Get(); + } + } + public ref T this[Index index] { [Checked] @@ -297,19 +318,6 @@ namespace System.Collections } } - Variant IList.this[int index] - { - get - { - return [Unbound]Variant.Create(this[index]); - } - - set - { - ThrowUnimplemented(); - } - } - protected virtual T* Alloc(int size) { return Internal.AllocRawArrayUnmarked(size); @@ -368,6 +376,11 @@ namespace System.Collections #endif } + void IList.Add(Variant item) + { + Add(item.Get()); + } + /// Adds an item to the front of the list. public void AddFront(T item) { @@ -466,6 +479,11 @@ namespace System.Collections } } + bool IList.Contains(Variant item) + { + return Contains(item.Get()); + } + public bool ContainsStrict(T item) { if (item == null) @@ -582,6 +600,11 @@ namespace System.Collections return -1; } + int IList.IndexOf(Variant item) + { + return IndexOf(item.Get()); + } + public int IndexOf(T item, int index) { Debug.Assert((uint)index < (uint)mSize); @@ -681,6 +704,11 @@ namespace System.Collections #endif } + void IList.Insert(int index, Variant item) + { + Insert(index, item.Get()); + } + public void Insert(int index, Span items) { //TODO: Handle case where Span is a reference to ourselves @@ -840,6 +868,11 @@ namespace System.Collections return false; } + bool IList.Remove(Variant item) + { + return Remove(item.Get()); + } + public bool RemoveStrict(T item) { int index = IndexOfStrict(item); diff --git a/BeefLibs/corlib/src/Collections/SplitList.bf b/BeefLibs/corlib/src/Collections/SplitList.bf index b3fec014..6438fe97 100644 --- a/BeefLibs/corlib/src/Collections/SplitList.bf +++ b/BeefLibs/corlib/src/Collections/SplitList.bf @@ -184,6 +184,19 @@ namespace System.Collections } } + Variant IList.this[int index] + { + get + { + return [Unbound]Variant.Create(this[index]); + } + + set + { + ThrowUnimplemented(); + } + } + public int Count => mSize; [Inline] public Data Data => .(this); @@ -312,6 +325,16 @@ namespace System.Collections #endif } + public void IList.Add(Variant item) + { + Add(item.Get()); + } + + public void IList.Insert(int index, Variant item) + { + Runtime.NotImplemented(); + } + protected override void GCMarkMembers() { [Comptime] @@ -358,6 +381,11 @@ namespace System.Collections return false; } + public bool IList.Contains(Variant item) + { + return Contains(item.Get()); + } + public void CopyTo(Span span) { for (int i < span.Length) @@ -376,6 +404,11 @@ namespace System.Collections return false; } + public bool IList.Remove(Variant item) + { + return Remove(item.Get()); + } + public void RemoveAt(int index) { Debug.Assert((uint)index < (uint)mSize); @@ -417,19 +450,6 @@ namespace System.Collections #endif } - Variant IList.this[int index] - { - get - { - return [Unbound]Variant.Create(this[index]); - } - - set - { - ThrowUnimplemented(); - } - } - public Enumerator GetEnumerator() { return .(this); @@ -451,6 +471,11 @@ namespace System.Collections return -1; } + public int IList.IndexOf(Variant item) + { + return IndexOf(item.Get()); + } + public int IndexOf(T item, int index) { for (int i = index; i < mSize; i++) diff --git a/BeefLibs/corlib/src/Variant.bf b/BeefLibs/corlib/src/Variant.bf index 763c6b16..f130e9ed 100644 --- a/BeefLibs/corlib/src/Variant.bf +++ b/BeefLibs/corlib/src/Variant.bf @@ -282,6 +282,8 @@ namespace System } } + public T Get() => Runtime.NotImplemented(); + public T Get() where T : class { Debug.Assert(IsObject);