Skip to content

Instantly share code, notes, and snippets.

@DK013
Forked from efranford/ObjectDictionary.cs
Created January 24, 2023 07:35
Show Gist options
  • Select an option

  • Save DK013/cfefbadfeff0406a7e59d3f258c8ce82 to your computer and use it in GitHub Desktop.

Select an option

Save DK013/cfefbadfeff0406a7e59d3f258c8ce82 to your computer and use it in GitHub Desktop.

Revisions

  1. @darktable darktable revised this gist Mar 19, 2012. No changes.
  2. @darktable darktable revised this gist Mar 19, 2012. No changes.
  3. @darktable darktable revised this gist Mar 11, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion ObjectDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@
    using System.Collections.Generic;

    [System.Serializable]
    public class ObjectKvp : UnityNameValuePair<Object> {
    public sealed class ObjectKvp : UnityNameValuePair<Object> {
    public Object value = null;

    override public Object Value {
    2 changes: 1 addition & 1 deletion UnityDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,7 @@ virtual public V this[K key] {
    return default(V);
    }

    return (V) result.Value;
    return result.Value;
    }
    set {
    if (key == null) {
  4. @darktable darktable revised this gist Mar 11, 2012. 2 changed files with 42 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions ObjectDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,24 @@
    // Copyright (c) 2012 Calvin Rien
    // http://the.darktable.com
    //
    // This software is provided 'as-is', without any express or implied warranty. In
    // no event will the authors be held liable for any damages arising from the use
    // of this software.
    //
    // Permission is granted to anyone to use this software for any purpose,
    // including commercial applications, and to alter it and redistribute it freely,
    // subject to the following restrictions:
    //
    // 1. The origin of this software must not be misrepresented; you must not claim
    // that you wrote the original software. If you use this software in a product,
    // an acknowledgment in the product documentation would be appreciated but is not
    // required.
    //
    // 2. Altered source versions must be plainly marked as such, and must not be
    // misrepresented as being the original software.
    //
    // 3. This notice may not be removed or altered from any source distribution.

    using UnityEngine;
    using System.Collections.Generic;

    21 changes: 21 additions & 0 deletions UnityDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,24 @@
    // Copyright (c) 2012 Calvin Rien
    // http://the.darktable.com
    //
    // This software is provided 'as-is', without any express or implied warranty. In
    // no event will the authors be held liable for any damages arising from the use
    // of this software.
    //
    // Permission is granted to anyone to use this software for any purpose,
    // including commercial applications, and to alter it and redistribute it freely,
    // subject to the following restrictions:
    //
    // 1. The origin of this software must not be misrepresented; you must not claim
    // that you wrote the original software. If you use this software in a product,
    // an acknowledgment in the product documentation would be appreciated but is not
    // required.
    //
    // 2. Altered source versions must be plainly marked as such, and must not be
    // misrepresented as being the original software.
    //
    // 3. This notice may not be removed or altered from any source distribution.

    using System.Collections;
    using System.Collections.Generic;

  5. @darktable darktable revised this gist Mar 11, 2012. 2 changed files with 152 additions and 138 deletions.
    64 changes: 32 additions & 32 deletions ObjectDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -1,57 +1,57 @@
    using UnityEngine;
    using System.Collections.Generic;

    [System.Serializable]
    public class ObjectKvp : UnityNameValuePair<Object> {
    public Object value = null;

    override public Object Value {
    get { return this.value; }
    set { this.value = value; }
    }

    public ObjectKvp(string key, Object value) : base(key, value) {
    }
    }

    [System.Serializable]
    public class ObjectDictionary : UnityDictionary<Object> {
    public List<UnityKvp> values;
    public List<ObjectKvp> values;

    override protected List<UnityKeyValuePair<Object>> Collection {
    override protected List<UnityKeyValuePair<string, Object>> KeyValuePairs {
    get {
    return values.ConvertAll<UnityKeyValuePair<Object>>(
    new System.Converter<UnityKvp, UnityKeyValuePair<Object>>(x=>{
    return x as UnityKeyValuePair<Object>;
    })
    );
    return values.ConvertAll<UnityKeyValuePair<string,Object>>(new System.Converter<ObjectKvp, UnityKeyValuePair<string,Object>>(
    x => {
    return x as UnityKeyValuePair<string,Object>;
    }));
    }
    set {
    values = value.ConvertAll<UnityKvp>(
    new System.Converter<UnityKeyValuePair<Object>, UnityKvp>(x=>{
    return new UnityKvp(x.Key, x.Value as Object);
    })
    );
    if (value == null) {
    values = new List<ObjectKvp>();
    return;
    }

    values = value.ConvertAll<ObjectKvp>(new System.Converter<UnityKeyValuePair<string,Object>, ObjectKvp>(
    x => {
    return new ObjectKvp(x.Key, x.Value as Object);
    }));
    }
    }

    override protected void SetKeyValuePair(string k, Object v) {
    var index = values.FindIndex(x => {return x.Key == k;});
    var index = values.FindIndex(x => {
    return x.Key == k;});

    if (index != -1) {
    if (v == null) {
    values.RemoveAt(index);
    return;
    }

    values[index] = new UnityKvp(k, v);
    values[index] = new ObjectKvp(k, v);
    return;
    }

    values.Add(new UnityKvp(k, v));
    }

    [System.Serializable]
    public class UnityKvp : UnityKeyValuePair<Object> {
    public Object value = null;

    override public Object Value {
    get {
    return this.value;
    }
    set {
    this.value = value;
    }
    }

    public UnityKvp(string key, Object value) : base(key, value) {
    }
    values.Add(new ObjectKvp(k, v));
    }
    }
    226 changes: 120 additions & 106 deletions UnityDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -2,45 +2,50 @@
    using System.Collections.Generic;

    namespace UnityEngine {
    public abstract class UnityDictionary<T> : IDictionary<string, T> {
    public class UnityKeyValuePair<V> {
    public string name = null;
    public class UnityNameValuePair<V> : UnityKeyValuePair<string, V> {
    public string name = null;

    public string Key {
    get {
    return name;
    }
    set {
    name = value;
    }
    }
    override public string Key {
    get { return name; }
    set { name = value; }
    }

    public virtual V Value {
    get;
    set;
    }
    public UnityNameValuePair() : base() {
    }

    public UnityKeyValuePair() {
    Key = null;
    Value = default(V);
    }
    public UnityNameValuePair(string key, V value) : base(key, value) {
    }
    }

    public UnityKeyValuePair(string key, V value) {
    Key = key;
    Value = value;
    }
    public class UnityKeyValuePair<K, V> {
    virtual public K Key {
    get;
    set;
    }

    abstract protected List<UnityKeyValuePair<T>> Collection {
    virtual public V Value {
    get;
    set;
    }

    public UnityDictionary() {
    public UnityKeyValuePair() {
    Key = default(K);
    Value = default(V);
    }

    protected abstract void SetKeyValuePair(string k, T v); /*
    {
    public UnityKeyValuePair(K key, V value) {
    Key = key;
    Value = value;
    }
    }

    public abstract class UnityDictionary<K,V> : IDictionary<K,V> {
    abstract protected List<UnityKeyValuePair<K,V>> KeyValuePairs {
    get;
    set;
    }

    protected abstract void SetKeyValuePair(K k, V v); /* {
    var index = Collection.FindIndex(x => {return x.Key == k;});
    if (index != -1) {
    @@ -56,179 +61,174 @@ public UnityDictionary() {
    values.Add(new UnityKeyValuePair(key, value));
    } */

    public T this[string key] {
    virtual public V this[K key] {
    get {
    var result = Collection.Find(x => {return x.Key == key;});
    var result = KeyValuePairs.Find(x => {
    return x.Key.Equals(key);});

    if (result == null) {
    return default(T);
    return default(V);
    }

    return (T) result.Value;
    return (V) result.Value;
    }
    set {
    if (string.IsNullOrEmpty(key))
    if (key == null) {
    return;
    }

    SetKeyValuePair(key, value);
    }
    }

    #region IDictionary interface

    public void Add(string key, T value) {
    this[key as string] = (T) value;
    public void Add(K key, V value) {
    this[key] = value;
    }

    public void Add(KeyValuePair<string, T> kvp) {
    public void Add(KeyValuePair<K, V> kvp) {
    this[kvp.Key] = kvp.Value;
    }

    public bool TryGetValue(string key, out T value) {
    public bool TryGetValue(K key, out V value) {
    if (!this.ContainsKey(key)) {
    value = default(T);
    value = default(V);
    return false;
    }

    value = this[key];
    return true;
    }

    public bool Remove(KeyValuePair<string, T> item) {
    public bool Remove(KeyValuePair<K, V> item) {
    return Remove(item.Key);
    }

    public bool Remove(string key) {
    var list = Collection;
    public bool Remove(K key) {
    var list = KeyValuePairs;

    var index = list.FindIndex(x=>{return x.Key == key;});
    var index = list.FindIndex(x => {
    return x.Key.Equals(key);});

    if (index == -1) return false;
    if (index == -1) {
    return false;
    }

    list.RemoveAt(index);

    Collection = list;
    KeyValuePairs = list;

    return true;
    }

    public void Clear() {
    var list = Collection;
    var list = KeyValuePairs;

    list.Clear();

    Collection = list;
    KeyValuePairs = list;
    }

    public bool ContainsKey(string key) {
    return Collection.FindIndex(x=>{return x.Key == (key as string);}) != -1;
    public bool ContainsKey(K key) {
    return KeyValuePairs.FindIndex(x => {
    return x.Key.Equals(key);}) != -1;
    }

    public bool Contains(KeyValuePair<string, T> kvp) {
    public bool Contains(KeyValuePair<K, V> kvp) {
    return this[kvp.Key].Equals(kvp.Value);
    }

    public int Count {
    get {
    return Collection.Count;
    return KeyValuePairs.Count;
    }
    }

    public void CopyTo(KeyValuePair<string, T>[] array, int index) {
    var copy = Collection.ConvertAll<KeyValuePair<string, T>>(
    new System.Converter<UnityKeyValuePair<T>, KeyValuePair<string, T>>(
    x=>{
    return new KeyValuePair<string, T>(x.Key, (T) x.Value);
    }));
    public void CopyTo(KeyValuePair<K, V>[] array, int index) {
    var copy = KeyValuePairs.ConvertAll<KeyValuePair<K, V>>(
    new System.Converter<UnityKeyValuePair<K, V>, KeyValuePair<K, V>>(
    x => {
    return new KeyValuePair<K, V>(x.Key, (V) x.Value);
    }));

    copy.CopyTo(array, index);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
    return GetEnumerator() as IEnumerator;
    }
    IEnumerator IEnumerable.GetEnumerator() {
    return GetEnumerator() as IEnumerator;
    }

    public IEnumerator<KeyValuePair<string, T>> GetEnumerator() {
    public IEnumerator<KeyValuePair<K, V>> GetEnumerator() {
    return new UnityDictionaryEnumerator(this);
    }

    public ICollection<string> Keys {
    public ICollection<K> Keys {
    get {
    return Collection.ConvertAll(new System.Converter<UnityKeyValuePair<T>, string>(x=>{return x.Key;}));
    return KeyValuePairs.ConvertAll(new System.Converter<UnityKeyValuePair<K,V>, K>(x => {
    return x.Key;}));
    }
    }

    public ICollection<T> Values {
    public ICollection<V> Values {
    get {
    return Collection.ConvertAll(new System.Converter<UnityKeyValuePair<T>, T>(x=>{return (T) x.Value;}));
    return KeyValuePairs.ConvertAll(new System.Converter<UnityKeyValuePair<K,V>, V>(x => {
    return x.Value;}));
    }
    }

    public KeyValuePair<string, T>[] Items {
    public ICollection<KeyValuePair<K, V>> Items {
    get {
    return Collection.ConvertAll<KeyValuePair<string, T>>(new System.Converter<UnityKeyValuePair<T>, KeyValuePair<string, T>>(x=>{return new KeyValuePair<string, T>(x.Key, x.Value);})).ToArray();
    return KeyValuePairs.ConvertAll<KeyValuePair<K, V>>(new System.Converter<UnityKeyValuePair<K,V>, KeyValuePair<K, V>>(x => {
    return new KeyValuePair<K, V>(x.Key, x.Value);}));
    }
    }

    public T SyncRoot {
    get {
    return default(T);
    }
    public V SyncRoot {
    get { return default(V); }
    }

    public bool IsFixedSize {
    get {
    return false;
    }
    get { return false; }
    }

    public bool IsReadOnly {
    get {
    return false;
    }
    get { return false; }
    }

    public bool IsSynchronized {
    get {
    return false;
    }
    get { return false; }
    }

    class UnityDictionaryEnumerator : IEnumerator<KeyValuePair<string, T>> {
    internal sealed class UnityDictionaryEnumerator : IEnumerator<KeyValuePair<K, V>> {
    // A copy of the SimpleDictionary T's key/value pairs.
    KeyValuePair<string, T>[] items;
    KeyValuePair<K, V>[] items;
    int index = -1;

    public UnityDictionaryEnumerator() {

    internal UnityDictionaryEnumerator() {
    }

    public UnityDictionaryEnumerator(UnityDictionary<T> ud)
    {
    internal UnityDictionaryEnumerator(UnityDictionary<K,V> ud) {
    // Make a copy of the dictionary entries currently in the SimpleDictionary T.
    items = new KeyValuePair<string, T>[ud.Count];
    items = new KeyValuePair<K, V>[ud.Count];

    ud.CopyTo(items, 0);
    }

    object IEnumerator.Current {
    get {
    return Current;
    }
    get { return Current; }
    }

    public KeyValuePair<string, T> Current {
    public KeyValuePair<K, V> Current {
    get {
    ValidateIndex();
    return items[index];
    }
    }

    // Return the current dictionary entry.
    public KeyValuePair<string, T> Entry
    {
    get { return (KeyValuePair<string, T>) Current; }
    public KeyValuePair<K, V> Entry {
    get { return (KeyValuePair<K, V>) Current; }
    }

    public void Dispose() {
    @@ -237,32 +237,46 @@ public void Dispose() {
    }

    // Return the key of the current item.
    public string Key { get { ValidateIndex(); return items[index].Key; } }
    public K Key {
    get {
    ValidateIndex();
    return items[index].Key;
    }
    }

    // Return the value of the current item.
    public T Value { get { ValidateIndex(); return items[index].Value; } }
    public V Value {
    get {
    ValidateIndex();
    return items[index].Value;
    }
    }

    // Advance to the next item.
    public bool MoveNext()
    {
    if (index < items.Length - 1) { index++; return true; }
    public bool MoveNext() {
    if (index < items.Length - 1) {
    index++;
    return true;
    }
    return false;
    }

    // Validate the enumeration index and throw an exception if the index is out of range.
    private void ValidateIndex()
    {
    if (index < 0 || index >= items.Length)
    throw new System.InvalidOperationException("Enumerator is before or after the collection.");
    private void ValidateIndex() {
    if (index < 0 || index >= items.Length) {
    throw new System.InvalidOperationException("Enumerator is before or after the collection.");
    }
    }

    // Reset the index to restart the enumeration.
    public void Reset()
    {
    public void Reset() {
    index = -1;
    }
    #endregion
    }
    }

    public abstract class UnityDictionary<V> : UnityDictionary<string, V> {

    #endregion
    }
    }
    }
  6. @darktable darktable created this gist Mar 10, 2012.
    57 changes: 57 additions & 0 deletions ObjectDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    using UnityEngine;
    using System.Collections.Generic;

    [System.Serializable]
    public class ObjectDictionary : UnityDictionary<Object> {
    public List<UnityKvp> values;

    override protected List<UnityKeyValuePair<Object>> Collection {
    get {
    return values.ConvertAll<UnityKeyValuePair<Object>>(
    new System.Converter<UnityKvp, UnityKeyValuePair<Object>>(x=>{
    return x as UnityKeyValuePair<Object>;
    })
    );
    }
    set {
    values = value.ConvertAll<UnityKvp>(
    new System.Converter<UnityKeyValuePair<Object>, UnityKvp>(x=>{
    return new UnityKvp(x.Key, x.Value as Object);
    })
    );
    }
    }

    override protected void SetKeyValuePair(string k, Object v) {
    var index = values.FindIndex(x => {return x.Key == k;});

    if (index != -1) {
    if (v == null) {
    values.RemoveAt(index);
    return;
    }

    values[index] = new UnityKvp(k, v);
    return;
    }

    values.Add(new UnityKvp(k, v));
    }

    [System.Serializable]
    public class UnityKvp : UnityKeyValuePair<Object> {
    public Object value = null;

    override public Object Value {
    get {
    return this.value;
    }
    set {
    this.value = value;
    }
    }

    public UnityKvp(string key, Object value) : base(key, value) {
    }
    }
    }
    268 changes: 268 additions & 0 deletions UnityDictionary.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,268 @@
    using System.Collections;
    using System.Collections.Generic;

    namespace UnityEngine {
    public abstract class UnityDictionary<T> : IDictionary<string, T> {
    public class UnityKeyValuePair<V> {
    public string name = null;

    public string Key {
    get {
    return name;
    }
    set {
    name = value;
    }
    }

    public virtual V Value {
    get;
    set;
    }

    public UnityKeyValuePair() {
    Key = null;
    Value = default(V);
    }

    public UnityKeyValuePair(string key, V value) {
    Key = key;
    Value = value;
    }
    }

    abstract protected List<UnityKeyValuePair<T>> Collection {
    get;
    set;
    }

    public UnityDictionary() {
    }

    protected abstract void SetKeyValuePair(string k, T v); /*
    {
    var index = Collection.FindIndex(x => {return x.Key == k;});
    if (index != -1) {
    if (v == null) {
    Collection.RemoveAt(index);
    return;
    }
    values[index] = new UnityKeyValuePair(key, value);
    return;
    }
    values.Add(new UnityKeyValuePair(key, value));
    } */

    public T this[string key] {
    get {
    var result = Collection.Find(x => {return x.Key == key;});

    if (result == null) {
    return default(T);
    }

    return (T) result.Value;
    }
    set {
    if (string.IsNullOrEmpty(key))
    return;

    SetKeyValuePair(key, value);
    }
    }

    #region IDictionary interface

    public void Add(string key, T value) {
    this[key as string] = (T) value;
    }

    public void Add(KeyValuePair<string, T> kvp) {
    this[kvp.Key] = kvp.Value;
    }

    public bool TryGetValue(string key, out T value) {
    if (!this.ContainsKey(key)) {
    value = default(T);
    return false;
    }

    value = this[key];
    return true;
    }

    public bool Remove(KeyValuePair<string, T> item) {
    return Remove(item.Key);
    }

    public bool Remove(string key) {
    var list = Collection;

    var index = list.FindIndex(x=>{return x.Key == key;});

    if (index == -1) return false;

    list.RemoveAt(index);

    Collection = list;

    return true;
    }

    public void Clear() {
    var list = Collection;

    list.Clear();

    Collection = list;
    }

    public bool ContainsKey(string key) {
    return Collection.FindIndex(x=>{return x.Key == (key as string);}) != -1;
    }

    public bool Contains(KeyValuePair<string, T> kvp) {
    return this[kvp.Key].Equals(kvp.Value);
    }

    public int Count {
    get {
    return Collection.Count;
    }
    }

    public void CopyTo(KeyValuePair<string, T>[] array, int index) {
    var copy = Collection.ConvertAll<KeyValuePair<string, T>>(
    new System.Converter<UnityKeyValuePair<T>, KeyValuePair<string, T>>(
    x=>{
    return new KeyValuePair<string, T>(x.Key, (T) x.Value);
    }));

    copy.CopyTo(array, index);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
    return GetEnumerator() as IEnumerator;
    }

    public IEnumerator<KeyValuePair<string, T>> GetEnumerator() {
    return new UnityDictionaryEnumerator(this);
    }

    public ICollection<string> Keys {
    get {
    return Collection.ConvertAll(new System.Converter<UnityKeyValuePair<T>, string>(x=>{return x.Key;}));
    }
    }

    public ICollection<T> Values {
    get {
    return Collection.ConvertAll(new System.Converter<UnityKeyValuePair<T>, T>(x=>{return (T) x.Value;}));
    }
    }

    public KeyValuePair<string, T>[] Items {
    get {
    return Collection.ConvertAll<KeyValuePair<string, T>>(new System.Converter<UnityKeyValuePair<T>, KeyValuePair<string, T>>(x=>{return new KeyValuePair<string, T>(x.Key, x.Value);})).ToArray();
    }
    }

    public T SyncRoot {
    get {
    return default(T);
    }
    }

    public bool IsFixedSize {
    get {
    return false;
    }
    }

    public bool IsReadOnly {
    get {
    return false;
    }
    }

    public bool IsSynchronized {
    get {
    return false;
    }
    }

    class UnityDictionaryEnumerator : IEnumerator<KeyValuePair<string, T>> {
    // A copy of the SimpleDictionary T's key/value pairs.
    KeyValuePair<string, T>[] items;
    int index = -1;

    public UnityDictionaryEnumerator() {

    }

    public UnityDictionaryEnumerator(UnityDictionary<T> ud)
    {
    // Make a copy of the dictionary entries currently in the SimpleDictionary T.
    items = new KeyValuePair<string, T>[ud.Count];

    ud.CopyTo(items, 0);
    }

    object IEnumerator.Current {
    get {
    return Current;
    }
    }

    public KeyValuePair<string, T> Current {
    get {
    ValidateIndex();
    return items[index];
    }
    }

    // Return the current dictionary entry.
    public KeyValuePair<string, T> Entry
    {
    get { return (KeyValuePair<string, T>) Current; }
    }

    public void Dispose() {
    index = -1;
    items = null;
    }

    // Return the key of the current item.
    public string Key { get { ValidateIndex(); return items[index].Key; } }

    // Return the value of the current item.
    public T Value { get { ValidateIndex(); return items[index].Value; } }

    // Advance to the next item.
    public bool MoveNext()
    {
    if (index < items.Length - 1) { index++; return true; }
    return false;
    }

    // Validate the enumeration index and throw an exception if the index is out of range.
    private void ValidateIndex()
    {
    if (index < 0 || index >= items.Length)
    throw new System.InvalidOperationException("Enumerator is before or after the collection.");
    }

    // Reset the index to restart the enumeration.
    public void Reset()
    {
    index = -1;
    }
    }

    #endregion
    }
    }