1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-18 08:06:04 +02:00

Improve ILIst and add IDictionary

This commit is contained in:
disarray2077 2023-05-03 15:09:20 -03:00
parent d3ca45d80a
commit b6e05186b7
4 changed files with 140 additions and 31 deletions

View file

@ -6,15 +6,31 @@
#define VERSION_DICTIONARY #define VERSION_DICTIONARY
#endif #endif
using System;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Threading;
namespace System.Collections namespace System.Collections
{ {
using System; interface IDictionary
using System.Collections; {
using System.Diagnostics; Variant this[Variant key]
using System.Diagnostics.Contracts; {
using System.Threading; 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<TKey, TValue> : public class Dictionary<TKey, TValue> :
IDictionary,
ICollection<(TKey key, TValue value)>, ICollection<(TKey key, TValue value)>,
IEnumerable<(TKey key, TValue value)>, IEnumerable<(TKey key, TValue value)>,
IRefEnumerable<(TKey key, TValue* valueRef)> where TKey : IHashable 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<TKey>()]);
}
set
{
this[key.Get<TKey>()] = value.Get<TValue>();
}
}
public void Add(TKey key, TValue value) public void Add(TKey key, TValue value)
{ {
Insert(key, value, true); Insert(key, value, true);
} }
void IDictionary.Add(Variant key, Variant value)
{
Add(key.Get<TKey>(), value.Get<TValue>());
}
public void Add(KeyValuePair kvPair) public void Add(KeyValuePair kvPair)
{ {
Insert(kvPair.key, kvPair.value, true); Insert(kvPair.key, kvPair.value, true);
@ -257,6 +291,11 @@ namespace System.Collections
return FindEntry(key) >= 0; return FindEntry(key) >= 0;
} }
bool IDictionary.ContainsKey(Variant key)
{
return ContainsKey(key.Get<TKey>());
}
public bool ContainsKeyAlt<TAltKey>(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey public bool ContainsKeyAlt<TAltKey>(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey
{ {
return FindEntryAlt(key) >= 0; return FindEntryAlt(key) >= 0;
@ -270,6 +309,11 @@ namespace System.Collections
} }
return false; return false;
} }
bool IDictionary.ContainsValue(Variant value)
{
return ContainsValue(value.Get<TValue>());
}
public bool Contains(KeyValuePair kvPair) public bool Contains(KeyValuePair kvPair)
{ {
@ -659,6 +703,11 @@ namespace System.Collections
return false; return false;
} }
void IDictionary.Remove(Variant key)
{
Remove(key.Get<TKey>());
}
public bool RemoveAlt<TAltKey>(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey public bool RemoveAlt<TAltKey>(TAltKey key) where TAltKey : IHashable where bool : operator TKey == TAltKey
{ {
if (mBuckets != null) if (mBuckets != null)

View file

@ -21,6 +21,14 @@ namespace System.Collections
get; get;
set; 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<T> : IEnumerable<T>, IList, ICollection<T> public class List<T> : IEnumerable<T>, IList, ICollection<T>
@ -217,6 +225,19 @@ namespace System.Collections
} }
} }
Variant IList.this[int index]
{
get
{
return [Unbound]Variant.Create(this[index]);
}
set
{
this[index] = value.Get<T>();
}
}
public ref T this[Index index] public ref T this[Index index]
{ {
[Checked] [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) protected virtual T* Alloc(int size)
{ {
return Internal.AllocRawArrayUnmarked<T>(size); return Internal.AllocRawArrayUnmarked<T>(size);
@ -368,6 +376,11 @@ namespace System.Collections
#endif #endif
} }
void IList.Add(Variant item)
{
Add(item.Get<T>());
}
/// Adds an item to the front of the list. /// Adds an item to the front of the list.
public void AddFront(T item) public void AddFront(T item)
{ {
@ -466,6 +479,11 @@ namespace System.Collections
} }
} }
bool IList.Contains(Variant item)
{
return Contains(item.Get<T>());
}
public bool ContainsStrict(T item) public bool ContainsStrict(T item)
{ {
if (item == null) if (item == null)
@ -582,6 +600,11 @@ namespace System.Collections
return -1; return -1;
} }
int IList.IndexOf(Variant item)
{
return IndexOf(item.Get<T>());
}
public int IndexOf(T item, int index) public int IndexOf(T item, int index)
{ {
Debug.Assert((uint)index < (uint)mSize); Debug.Assert((uint)index < (uint)mSize);
@ -681,6 +704,11 @@ namespace System.Collections
#endif #endif
} }
void IList.Insert(int index, Variant item)
{
Insert(index, item.Get<T>());
}
public void Insert(int index, Span<T> items) public void Insert(int index, Span<T> items)
{ {
//TODO: Handle case where Span is a reference to ourselves //TODO: Handle case where Span is a reference to ourselves
@ -840,6 +868,11 @@ namespace System.Collections
return false; return false;
} }
bool IList.Remove(Variant item)
{
return Remove(item.Get<T>());
}
public bool RemoveStrict(T item) public bool RemoveStrict(T item)
{ {
int index = IndexOfStrict(item); int index = IndexOfStrict(item);

View file

@ -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; public int Count => mSize;
[Inline] public Data Data => .(this); [Inline] public Data Data => .(this);
@ -312,6 +325,16 @@ namespace System.Collections
#endif #endif
} }
public void IList.Add(Variant item)
{
Add(item.Get<T>());
}
public void IList.Insert(int index, Variant item)
{
Runtime.NotImplemented();
}
protected override void GCMarkMembers() protected override void GCMarkMembers()
{ {
[Comptime] [Comptime]
@ -358,6 +381,11 @@ namespace System.Collections
return false; return false;
} }
public bool IList.Contains(Variant item)
{
return Contains(item.Get<T>());
}
public void CopyTo(Span<T> span) public void CopyTo(Span<T> span)
{ {
for (int i < span.Length) for (int i < span.Length)
@ -376,6 +404,11 @@ namespace System.Collections
return false; return false;
} }
public bool IList.Remove(Variant item)
{
return Remove(item.Get<T>());
}
public void RemoveAt(int index) public void RemoveAt(int index)
{ {
Debug.Assert((uint)index < (uint)mSize); Debug.Assert((uint)index < (uint)mSize);
@ -417,19 +450,6 @@ namespace System.Collections
#endif #endif
} }
Variant IList.this[int index]
{
get
{
return [Unbound]Variant.Create(this[index]);
}
set
{
ThrowUnimplemented();
}
}
public Enumerator GetEnumerator() public Enumerator GetEnumerator()
{ {
return .(this); return .(this);
@ -451,6 +471,11 @@ namespace System.Collections
return -1; return -1;
} }
public int IList.IndexOf(Variant item)
{
return IndexOf(item.Get<T>());
}
public int IndexOf(T item, int index) public int IndexOf(T item, int index)
{ {
for (int i = index; i < mSize; i++) for (int i = index; i < mSize; i++)

View file

@ -282,6 +282,8 @@ namespace System
} }
} }
public T Get<T>() => Runtime.NotImplemented();
public T Get<T>() where T : class public T Get<T>() where T : class
{ {
Debug.Assert(IsObject); Debug.Assert(IsObject);