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衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

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

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

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

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

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

ホットトピック











C#とCの歴史と進化はユニークであり、将来の見通しも異なります。 1.Cは、1983年にBjarnestrostrupによって発明され、オブジェクト指向のプログラミングをC言語に導入しました。その進化プロセスには、C 11の自動キーワードとラムダ式の導入など、複数の標準化が含まれます。C20概念とコルーチンの導入、将来のパフォーマンスとシステムレベルのプログラミングに焦点を当てます。 2.C#は2000年にMicrosoftによってリリースされました。CとJavaの利点を組み合わせて、その進化はシンプルさと生産性に焦点を当てています。たとえば、C#2.0はジェネリックを導入し、C#5.0は非同期プログラミングを導入しました。これは、将来の開発者の生産性とクラウドコンピューティングに焦点を当てます。

CとXMLの将来の開発動向は次のとおりです。1)Cは、プログラミングの効率とセキュリティを改善するためのC 20およびC 23の標準を通じて、モジュール、概念、CORoutinesなどの新しい機能を導入します。 2)XMLは、データ交換および構成ファイルの重要なポジションを引き続き占有しますが、JSONとYAMLの課題に直面し、XMLSchema1.1やXpath3.1の改善など、より簡潔で簡単な方向に発展します。

C継続的な使用の理由には、その高性能、幅広いアプリケーション、および進化する特性が含まれます。 1)高効率パフォーマンス:Cは、メモリとハードウェアを直接操作することにより、システムプログラミングと高性能コンピューティングで優れたパフォーマンスを発揮します。 2)広く使用されている:ゲーム開発、組み込みシステムなどの分野での輝き。3)連続進化:1983年のリリース以来、Cは競争力を維持するために新しい機能を追加し続けています。

C#とCおよび開発者の経験の学習曲線には大きな違いがあります。 1)C#の学習曲線は比較的フラットであり、迅速な開発およびエンタープライズレベルのアプリケーションに適しています。 2)Cの学習曲線は急勾配であり、高性能および低レベルの制御シナリオに適しています。

Cは、サードパーティライブラリ(TinyXML、PUGIXML、XERCES-Cなど)を介してXMLと相互作用します。 1)ライブラリを使用してXMLファイルを解析し、それらをC処理可能なデータ構造に変換します。 2)XMLを生成するときは、Cデータ構造をXML形式に変換します。 3)実際のアプリケーションでは、XMLが構成ファイルとデータ交換に使用されることがよくあり、開発効率を向上させます。

最新のCデザインモデルは、C 11以降の新機能を使用して、より柔軟で効率的なソフトウェアを構築するのに役立ちます。 1)ラムダ式とstd :: functionを使用して、オブザーバーパターンを簡素化します。 2)モバイルセマンティクスと完全な転送を通じてパフォーマンスを最適化します。 3)インテリジェントなポインターは、タイプの安全性とリソース管理を保証します。

C学習者と開発者は、Stackoverflow、RedditのR/CPPコミュニティ、CourseraおよびEDXコース、Github、Professional Consulting Services、およびCPPCONのオープンソースプロジェクトからリソースとサポートを得ることができます。 1. StackOverFlowは、技術的な質問への回答を提供します。 2。RedditのR/CPPコミュニティが最新ニュースを共有しています。 3。CourseraとEDXは、正式なCコースを提供します。 4. LLVMなどのGitHubでのオープンソースプロジェクトやスキルの向上。 5。JetBrainやPerforceなどの専門的なコンサルティングサービスは、技術サポートを提供します。 6。CPPCONとその他の会議はキャリアを助けます

Cは、現代のプログラミングにおいて依然として重要な関連性を持っています。 1)高性能および直接的なハードウェア操作機能により、ゲーム開発、組み込みシステム、高性能コンピューティングの分野で最初の選択肢になります。 2)豊富なプログラミングパラダイムとスマートポインターやテンプレートプログラミングなどの最新の機能は、その柔軟性と効率を向上させます。学習曲線は急ですが、その強力な機能により、今日のプログラミングエコシステムでは依然として重要です。
