using System; using System.Collections.Generic; using System.Text; namespace Collections { /// /// 辞書。 /// /// 鍵の型 /// 値の型 public interface IDictionary : IEnumerable> { /// /// 新しい要素の挿入。 /// /// 新しい要素の鍵 /// 新しい要素の値 void Insert(TKey key, TValue val); /// /// 要素の削除。 /// /// 削除したい要素 void Erase(TKey key); /// /// 要素を含むかどうか。 /// /// 検索したい要素 /// 見つかった場合 true bool Contains(TKey key); /// /// [] を使って値を取り出す。 /// /// 鍵 /// TValue this[TKey key] { set; get; } /// /// 鍵一覧取得 /// IEnumerable Keys { get; } /// /// 値一覧取得 /// IEnumerable Values { get; } } /// /// 辞書のエントリー。 /// /// 鍵の型 /// 値の型 internal class Entry { internal TKey key; internal TValue val; internal Entry(TKey key) : this(key, default(TValue)) { } internal Entry(TKey key, TValue val) { this.key = key; this.val = val; } #region object メンバ public override int GetHashCode() { return this.key.GetHashCode(); } public override bool Equals(object obj) { Entry ent = obj as Entry; if (ent == null) return false; return this.key.Equals(ent.key); } #endregion } /// /// 辞書のエントリー。 /// (SortedArray, Tree 用) /// 鍵が IComparable を実装している必要あり。 /// /// 鍵の型 /// 値の型 internal class ComparableEntry : Entry, IComparable> where TKey : IComparable { internal ComparableEntry(TKey key) : base(key) { } internal ComparableEntry(TKey key, TValue val) : base(key, val) { } #region IComparable> メンバ public int CompareTo(ComparableEntry other) { return this.key.CompareTo(other.key); } #endregion } public class HashDictionary : IDictionary { #region フィールド HashTable> table; #endregion #region 初期化 public HashDictionary() : this(256) { } public HashDictionary(int capacity) { this.table = new HashTable>(capacity); } #endregion #region IDictionary メンバ public void Insert(TKey key, TValue val) { this.table.Insert(new Entry(key, val)); } public void Erase(TKey key) { this.table.Erase(new Entry(key)); } public bool Contains(TKey key) { return this.table.Contains(new Entry(key)); } public TValue this[TKey key] { get { Entry entry = this.table.Find(new Entry(key)); if (entry == null) return default(TValue); return entry.val; } set { Entry entry = this.table.Find(new Entry(key)); if (entry == null) this.Insert(key, value); else entry.val = value; } } public IEnumerable Keys { get { foreach (Entry ent in this.table) { yield return ent.key; } } } public IEnumerable Values { get { foreach (Entry ent in this.table) { yield return ent.val; } } } #endregion #region IEnumerable メンバ public IEnumerator> GetEnumerator() { foreach (Entry ent in this.table) { yield return new KeyValuePair(ent.key, ent.val); } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } public class SortedDictionary : IDictionary where TKey : IComparable { #region フィールド SortedArray> table; #endregion #region 初期化 public SortedDictionary() : this(256) { } public SortedDictionary(int capacity) { this.table = new SortedArray>(capacity); } #endregion #region IDictionary メンバ public void Insert(TKey key, TValue val) { this.table.Insert(new ComparableEntry(key, val)); } public void Erase(TKey key) { this.table.Erase(new ComparableEntry(key)); } public bool Contains(TKey key) { return this.table.Contains(new ComparableEntry(key)); } public TValue this[TKey key] { get { ComparableEntry entry = this.table.Find(new ComparableEntry(key)); if (entry == null) return default(TValue); return entry.val; } set { ComparableEntry entry = this.table.Find(new ComparableEntry(key)); if (entry == null) this.Insert(key, value); else entry.val = value; } } public IEnumerable Keys { get { foreach (Entry ent in this.table) { yield return ent.key; } } } public IEnumerable Values { get { foreach (Entry ent in this.table) { yield return ent.val; } } } #endregion #region IEnumerable メンバ public IEnumerator> GetEnumerator() { foreach (Entry ent in this.table) { yield return new KeyValuePair(ent.key, ent.val); } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } public class TreeDictionary : IDictionary where TKey : IComparable { #region フィールド BinaryTree> table; #endregion #region 初期化 public TreeDictionary() { this.table = new BinaryTree>(); } #endregion #region IDictionary メンバ public void Insert(TKey key, TValue val) { this.table.Insert(new ComparableEntry(key, val)); } public void Erase(TKey key) { this.table.Erase(new ComparableEntry(key)); } public bool Contains(TKey key) { return this.table.Contains(new ComparableEntry(key)); } public TValue this[TKey key] { get { BinaryTree>.Node node = this.table.Find(new ComparableEntry(key)); if (node == null) return default(TValue); return node.Value.val; } set { BinaryTree>.Node node = this.table.Find(new ComparableEntry(key)); if (node == null) this.Insert(key, value); else node.Value.val = value; } } public IEnumerable Keys { get { foreach (Entry ent in this.table) { yield return ent.key; } } } public IEnumerable Values { get { foreach (Entry ent in this.table) { yield return ent.val; } } } #endregion #region IEnumerable メンバ public IEnumerator> GetEnumerator() { foreach (Entry ent in this.table) { yield return new KeyValuePair(ent.key, ent.val); } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } }