Table of Contents

Class NumericDictionary<TKey, TValue>

Namespace
AiDotNet.LinearAlgebra
Assembly
AiDotNet.dll

A thread-safe dictionary implementation that uses INumericOperations for key comparison, allowing generic numeric types T to be used as keys without requiring the notnull constraint.

public class NumericDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

Type Parameters

TKey

The numeric type used for keys.

TValue

The type of values stored in the dictionary.

Inheritance
NumericDictionary<TKey, TValue>
Implements
IEnumerable<KeyValuePair<TKey, TValue>>
Inherited Members
Extension Methods

Remarks

This collection solves the problem of using generic numeric types as dictionary keys in a generic context. Standard Dictionary requires TKey to have a notnull constraint, but this library intentionally avoids such constraints to maintain flexibility. NumericDictionary uses INumericOperations.Equals() for key comparison instead of the default equality comparer.

For Beginners: Think of this as a special dictionary designed for numeric keys.

Regular dictionaries in C# have trouble with generic number types because they need special guarantees about how keys are compared. This dictionary uses the same math operations (INumericOperations) that the rest of the library uses, so it works seamlessly with any numeric type (float, double, decimal, etc.).

It's also thread-safe, meaning multiple parts of your code can read from and write to it at the same time without causing errors.

Constructors

NumericDictionary()

Initializes a new instance of the NumericDictionary class.

public NumericDictionary()

NumericDictionary(int)

Initializes a new instance of the NumericDictionary class with specified initial capacity.

public NumericDictionary(int capacity)

Parameters

capacity int

The initial capacity of the dictionary.

Properties

Count

Gets the number of key/value pairs in the dictionary.

public int Count { get; }

Property Value

int

this[TKey]

Gets or sets the value associated with the specified key.

public TValue this[TKey key] { get; set; }

Parameters

key TKey

The key of the value to get or set.

Property Value

TValue

The value associated with the specified key.

Exceptions

KeyNotFoundException

The key was not found in the dictionary.

Keys

Gets a collection containing the keys in the dictionary.

public IReadOnlyList<TKey> Keys { get; }

Property Value

IReadOnlyList<TKey>

Values

Gets a collection containing the values in the dictionary.

public IReadOnlyList<TValue> Values { get; }

Property Value

IReadOnlyList<TValue>

Methods

Add(TKey, TValue)

Adds the specified key and value to the dictionary.

public void Add(TKey key, TValue value)

Parameters

key TKey

The key of the element to add.

value TValue

The value of the element to add.

Exceptions

ArgumentException

An element with the same key already exists.

Clear()

Removes all keys and values from the dictionary.

public void Clear()

ContainsKey(TKey)

Determines whether the dictionary contains the specified key.

public bool ContainsKey(TKey key)

Parameters

key TKey

The key to locate.

Returns

bool

true if the dictionary contains an element with the specified key; otherwise, false.

GetEnumerator()

Returns an enumerator that iterates through the dictionary.

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<TKey, TValue>>

Remove(TKey)

Removes the value with the specified key from the dictionary.

public bool Remove(TKey key)

Parameters

key TKey

The key of the element to remove.

Returns

bool

true if the element was successfully found and removed; otherwise, false.

TryAdd(TKey, TValue)

Attempts to add the specified key and value to the dictionary.

public bool TryAdd(TKey key, TValue value)

Parameters

key TKey

The key of the element to add.

value TValue

The value of the element to add.

Returns

bool

true if the key/value pair was added successfully; false if the key already exists.

TryGetValue(TKey, out TValue)

Attempts to get the value associated with the specified key.

public bool TryGetValue(TKey key, out TValue value)

Parameters

key TKey

The key of the value to get.

value TValue

When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.

Returns

bool

true if the dictionary contains an element with the specified key; otherwise, false.