C# で汎用 OrderedDictionary を実装する理由
汎用 OrderedDictionary の実装は特に難しいことではありませんが、不必要に時間がかかり、率直に言ってこのクラスは Microsoft 側の大きな見落としです。これを実装するには複数の方法がありますが、私は内部ストレージに KeyedCollection を使用することを選択しました。また、List
インターフェイスは次のとおりです。これには、Microsoft によって提供されたこのインターフェイスの非汎用バージョンである System.Collections.Specialized.IOrderedDictionary が含まれていることに注意してください。
// https://choosealicense.com/licenses/unlicense
// または https://choosealicense.com/licenses/mit/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace mattmc3.Common.Collections.Generic {
public interface IOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IOrderedDictionary { new TValue this[int index] { get; set; } new TValue this[TKey key] { get; set; } new int Count { get; } new ICollection<TKey> Keys { get; } new ICollection<TValue> Values { get; } new void Add(TKey key, TValue value); new void Clear(); void Insert(int index, TKey key, TValue value); int IndexOf(TKey key); bool ContainsValue(TValue value); bool ContainsValue(TValue value, IEqualityComparer<TValue> comparer); new bool ContainsKey(TKey key); new IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(); new bool Remove(TKey key); new void RemoveAt(int index); new bool TryGetValue(TKey key, out TValue value); TValue GetValue(TKey key); void SetValue(TKey key, TValue value); KeyValuePair<TKey, TValue> GetItem(int index); void SetItem(int index, TValue value); }
}
ヘルパーを含む実装は次のとおりですクラス:
// http://unlicense.org
System を使用;
System.Collections.ObjectModel を使用;
System.Diagnostics を使用;
System.Collections を使用;
System.Collections.Specialized を使用;
System.Collections.Generic を使用;
を使用System.Linq;
名前空間 mattmc3.Common.Collections.Generic {
/// <summary> /// A dictionary object that allows rapid hash lookups using keys, but also /// maintains the key insertion order so that values can be retrieved by /// key index. /// </summary> public class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue> { #region Fields/Properties private KeyedCollection2<TKey, KeyValuePair<TKey, TValue>> _keyedCollection; /// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">The key associated with the value to get or set.</param> public TValue this[TKey key] { get { return GetValue(key); } set { SetValue(key, value); } } /// <summary> /// Gets or sets the value at the specified index. /// </summary> /// <param name="index">The index of the value to get or set.</param> public TValue this[int index] { get { return GetItem(index).Value; } set { SetItem(index, value); } } public int Count { get { return _keyedCollection.Count; } } public ICollection<TKey> Keys { get { return _keyedCollection.Select(x => x.Key).ToList(); } } public ICollection<TValue> Values { get { return _keyedCollection.Select(x => x.Value).ToList(); } } public IEqualityComparer<TKey> Comparer { get; private set; } #endregion #region Constructors public OrderedDictionary() { Initialize(); } public OrderedDictionary(IEqualityComparer<TKey> comparer) { Initialize(comparer); } public OrderedDictionary(IOrderedDictionary<TKey, TValue> dictionary) { Initialize(); foreach (KeyValuePair<TKey, TValue> pair in dictionary) { _keyedCollection.Add(pair); } } public OrderedDictionary(IOrderedDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) { Initialize(comparer); foreach (KeyValuePair<TKey, TValue> pair in dictionary) { _keyedCollection.Add(pair); } } #endregion #region Methods private void Initialize(IEqualityComparer<TKey> comparer = null) { this.Comparer = comparer; if (comparer != null) { _keyedCollection = new KeyedCollection2<TKey, KeyValuePair<TKey, TValue>>(x => x.Key, comparer); } else { _keyedCollection = new KeyedCollection2<TKey, KeyValuePair<TKey, TValue>>(x => x.Key); } } public void Add(TKey key, TValue value) { _keyedCollection.Add(new KeyValuePair<TKey, TValue>(key, value)); } public void Clear() { _keyedCollection.Clear(); } public void Insert(int index, TKey key, TValue value) { _keyedCollection.Insert(index, new KeyValuePair<TKey, TValue>(key, value)); } public int IndexOf(TKey key) { if (_keyedCollection.Contains(key)) { return _keyedCollection.IndexOf(_keyedCollection[key]); } else { return -1; } } public bool ContainsValue(TValue value) { return this.Values.Contains(value); } public bool ContainsValue(TValue value, IEqualityComparer<TValue> comparer) { return this.Values.Contains(value, comparer); } public bool ContainsKey(TKey key) { return _keyedCollection.Contains(key); } public KeyValuePair<TKey, TValue> GetItem(int index) { if (index < 0 || index >= _keyedCollection.Count) { throw new ArgumentException(String.Format("The index was outside the bounds of the dictionary: {0}", index)); } return _keyedCollection[index]; } /// <summary> /// Sets the value at the index specified. /// </summary> /// <param name="index">The index of the value desired</param> /// <param name="value">The value to set</param> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown when the index specified does not refer to a KeyValuePair in this object /// </exception> public void SetItem(int index, TValue value) { if (index < 0 || index >= _keyedCollection.Count) { throw new ArgumentException("The index is outside the bounds of the dictionary: {0}".FormatWith(index)); } var kvp = new KeyValuePair<TKey, TValue>(_keyedCollection[index].Key, value); _keyedCollection[index] = kvp; } public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return _keyedCollection.GetEnumerator(); } public bool Remove(TKey key) { return _keyedCollection.Remove(key); } public void RemoveAt(int index
以上がC# で汎用 OrderedDictionary を実装する理由の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









C言語データ構造:ツリーとグラフのデータ表現は、ノードからなる階層データ構造です。各ノードには、データ要素と子ノードへのポインターが含まれています。バイナリツリーは特別なタイプの木です。各ノードには、最大2つの子ノードがあります。データは、structreenode {intdata; structreenode*left; structreenode*右;}を表します。操作は、ツリートラバーサルツリー(前向き、順序、および後期)を作成します。検索ツリー挿入ノード削除ノードグラフは、要素が頂点であるデータ構造のコレクションであり、近隣を表す右または未照明のデータを持つエッジを介して接続できます。

ファイルの操作の問題に関する真実:ファイルの開きが失敗しました:不十分な権限、間違ったパス、およびファイルが占有されます。データの書き込みが失敗しました:バッファーがいっぱいで、ファイルは書き込みできず、ディスクスペースが不十分です。その他のFAQ:遅いファイルトラバーサル、誤ったテキストファイルエンコード、およびバイナリファイルの読み取りエラー。

記事では、移動セマンティクス、完璧な転送、リソース管理のためのcでのr値参照の効果的な使用について説明し、ベストプラクティスとパフォーマンスの改善を強調しています。(159文字)

C 20の範囲は、表現力、複合性、効率を伴うデータ操作を強化します。複雑な変換を簡素化し、既存のコードベースに統合して、パフォーマンスと保守性を向上させます。

C35の計算は、本質的に組み合わせ数学であり、5つの要素のうち3つから選択された組み合わせの数を表します。計算式はC53 = 5です! /(3! * 2!)。これは、ループで直接計算して効率を向上させ、オーバーフローを避けることができます。さらに、組み合わせの性質を理解し、効率的な計算方法をマスターすることは、確率統計、暗号化、アルゴリズム設計などの分野で多くの問題を解決するために重要です。

この記事では、不必要なコピーを回避することにより、パフォーマンスを向上させるために、CのMove Semanticsを使用することについて説明します。 STD :: MOVEを使用して、移動コンストラクターと割り当てオペレーターの実装をカバーし、効果的なAPPLの重要なシナリオと落とし穴を識別します

この記事では、Cでの動的発送、そのパフォーマンスコスト、および最適化戦略について説明します。動的ディスパッチがパフォーマンスに影響を与え、静的ディスパッチと比較するシナリオを強調し、パフォーマンスとパフォーマンスのトレードオフを強調します

C言語関数は、コードモジュール化とプログラム構築の基礎です。それらは、宣言(関数ヘッダー)と定義(関数体)で構成されています。 C言語は値を使用してパラメーターをデフォルトで渡しますが、外部変数はアドレスパスを使用して変更することもできます。関数は返品値を持つか、または持たない場合があり、返品値のタイプは宣言と一致する必要があります。機能の命名は、ラクダを使用するか、命名法を強調して、明確で理解しやすい必要があります。単一の責任の原則に従い、機能をシンプルに保ち、メンテナビリティと読みやすさを向上させます。
