This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure the behavior of
Map
is deterministic (#259)
- Loading branch information
Showing
9 changed files
with
131 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
|
||
namespace Neo.VM.Collections | ||
{ | ||
internal class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue> | ||
{ | ||
private class TItem | ||
{ | ||
public TKey Key; | ||
public TValue Value; | ||
} | ||
|
||
private class InternalCollection : KeyedCollection<TKey, TItem> | ||
{ | ||
protected override TKey GetKeyForItem(TItem item) | ||
{ | ||
return item.Key; | ||
} | ||
} | ||
|
||
private readonly InternalCollection collection = new InternalCollection(); | ||
|
||
public int Count => collection.Count; | ||
public bool IsReadOnly => false; | ||
public ICollection<TKey> Keys => collection.Select(p => p.Key).ToArray(); | ||
public ICollection<TValue> Values => collection.Select(p => p.Value).ToArray(); | ||
|
||
public TValue this[TKey key] | ||
{ | ||
get | ||
{ | ||
return collection[key].Value; | ||
} | ||
set | ||
{ | ||
if (collection.Contains(key)) | ||
collection[key].Value = value; | ||
else | ||
Add(key, value); | ||
} | ||
} | ||
|
||
public void Add(TKey key, TValue value) | ||
{ | ||
collection.Add(new TItem | ||
{ | ||
Key = key, | ||
Value = value | ||
}); | ||
} | ||
|
||
public bool ContainsKey(TKey key) | ||
{ | ||
return collection.Contains(key); | ||
} | ||
|
||
public bool Remove(TKey key) | ||
{ | ||
return collection.Remove(key); | ||
} | ||
|
||
public bool TryGetValue(TKey key, out TValue value) | ||
{ | ||
if (collection.Contains(key)) | ||
{ | ||
value = collection[key].Value; | ||
return true; | ||
} | ||
value = default; | ||
return false; | ||
} | ||
|
||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) | ||
{ | ||
Add(item.Key, item.Value); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
collection.Clear(); | ||
} | ||
|
||
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return collection.Contains(item.Key); | ||
} | ||
|
||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) | ||
{ | ||
for (int i = 0; i < collection.Count; i++) | ||
array[i + arrayIndex] = new KeyValuePair<TKey, TValue>(collection[i].Key, collection[i].Value); | ||
} | ||
|
||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return collection.Remove(item.Key); | ||
} | ||
|
||
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() | ||
{ | ||
return collection.Select(p => new KeyValuePair<TKey, TValue>(p.Key, p.Value)).GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return collection.Select(p => new KeyValuePair<TKey, TValue>(p.Key, p.Value)).GetEnumerator(); | ||
} | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using Neo.VM.Collections; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections; | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using Neo.VM.Collections; | ||
using Neo.VM.Types; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
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
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
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