Home / .NET / Associative Arrays

Associative Arrays

Associative Array Overview

An associative array, also called a dictionary, is an abstract group kind that connects One key with a value. Each key in the set is exceptional. Difference between a hash table and an associative array. How are defined by an associative array a group should act, like connecting a key with a value. You can execute an associative array in many manners. One potential way is by using a hash table.

Uses of Associative Arrays

Associative arrays are useful when you’ve a value you should look up with a key that is unique. If you never have to use the key you should use other group types. Say You’ve a Person class which has staff information that you simply must accessibility and keep If the set includes hundreds of Examples of the Person class, you’d need to continuously traverse the group to find
the particular record. This would be a performance hit even or if you’d to hunt thousands Would have the ability to get the Individual example rapidly through her or his phone number.

Advantages and Disadvantages of Associative Arrays

The pros and cons of an associative array are normally determined by the implementation you use for the key/value organization. Some enactments, including association lists, are very simple to execute but quite inefficient for big lists. Others, such as red-black trees and hash tables, are more challenging to implement but a lot more efficient for bigger Lists, and not efficient for smaller lists. In case you are willing to do the research and invest the Time that is coding, associative arrays can be a very efficient tool.

  • Getting Started
  • Creating Constructors
  • Associate Values with Keys
  • Remove Keys
  • Adding Helper Methods and Properties
  • Creating the Constructor
  • Associate Values with Keys

 

C#

[DebuggerDisplay("Count={Count}")]
[DebuggerTypeProxy(typeof(AssociativeArrayDebugView))]
public class AssociativeArrayAL<TKey, TValue>
{
 // Fields
 private IEqualityComparer<TKey> m_comparer;
 private DoubleLinkedList<KVPair<TKey, TValue>> m_list;
 private int m_updateCode;
 // Constructor
 public AssociativeArrayAL();
 public AssociativeArrayAL(IEqualityComparer<TKey> comparer);
 // Methods
 public void Add(TKey key, TValue value);
 private void Add(TKey key, TValue value, bool overwrite);
 public void Clear();
 public bool ContainsKey(TKey key);
 public bool ContainsValue(TValue value);
 private DoubleLinkedListNode<KVPair<TKey, TValue>> FindKey(TKey key);
 private DoubleLinkedListNode<KVPair<TKey, TValue>> FindValue(TValue value);
 public bool Remove(TKey key);
 private bool Remove(DoubleLinkedListNode<KVPair<TKey, TValue>> node);
 public bool RemoveValue(TValue value);
 public bool RemoveValue(TValue value, bool allOccurrences);
 public bool TryGetValue(TKey key, out TValue value);
 // Properties
 public int Count { get; }
 public bool IsEmpty { get; }
 public TValue this[TKey key] { get; set; }
 public TKey[] Keys { get; }
 public TValue[] Values { get; }
 // Nested Types
 private struct KVPair
 {
 private TKey m_key;
 private TValue m_value;
 public KVPair(TKey key, TValue value);
 public TKey Key { get; }
 public TValue Value { get; set; }
 }
}

 

Visual Basic

<DefaultMember("Item")> _
<DebuggerDisplay("Count={Count}")> _
<DebuggerTypeProxy(GetType(AssociativeArrayDebugView))> _
Public Class AssociativeArrayAL(Of TKey, TValue)
 ' Fields
 Private m_comparer As IEqualityComparer(Of TKey)
 Private m_list As DoubleLinkedList(Of KVPair(Of TKey, TValue))
 Private m_updateCode As Integer
 ' Constructors
 Public Sub New()
 Public Sub New(ByVal comparer As IEqualityComparer(Of TKey))
 ' Methods
 Public Sub Add(ByVal key As TKey, ByVal value As TValue)
 Private Sub Add(ByVal key As TKey, ByVal value As TValue, ByVal overwrite As Boolean)
 Public Sub Clear()
 Public Function ContainsKey(ByVal key As TKey) As Boolean
 Public Function ContainsValue(ByVal value As TValue) As Boolean
 Private Function FindKey(ByVal key As TKey) _
 As DoubleLinkedListNode(Of KVPair(Of TKey, TValue))
 Private Function FindValue(ByVal value As TValue) _
 As DoubleLinkedListNode(Of KVPair(Of TKey, TValue))
 Public Function Remove(ByVal key As TKey) As Boolean
 Private Function Remove(ByVal node As DoubleLinkedListNode(Of KVPair(Of TKey, TValue))) _
 As Boolean
 Public Function RemoveValue(ByVal value As TValue) As Boolean
 Public Function RemoveValue(ByVal value As TValue, ByVal allOccurrences As Boolean) _
 As Boolean
 Public Function TryGetValue(ByVal key As TKey, ByRef value As TValue) As Boolean
 ' Properties
 Public ReadOnly Property Count As Integer
 Public ReadOnly Property IsEmpty As Boolean
 Public Default Property Item(ByVal key As TKey) As TValue
 Public ReadOnly Property Keys As TKey()
 Public ReadOnly Property Values As TValue()
 ' Nested Types
 <StructLayout(LayoutKind.Sequential)> _
 Private Structure KVPair
 Public m_key As TKey
 Public m_value As TValue
 Public Sub New(ByVal key As TKey, ByVal value As TValue)
 Public ReadOnly Property Key As TKey
 Public Property Value As TValue
 End Structure
End Class

 

 

C#

C#
/// <summary>
/// Initializes a new instance of the AssociativeArrayAL(TKey,TValue) class
/// that is empty.
/// </summary>
public AssociativeArrayAL()
{
 m_comparer = EqualityComparer<TKey>.Default;
 m_list = new DoubleLinkedList<KVPair>();
}

Visual Basic

''' <summary>
''' Initializes a new instance of the AssociativeArrayAL(TKey,TValue) class
''' that is empty.
''' </summary>
Public Sub New()
 m_comparer = EqualityComparer(Of TKey).Default
 m_lis

 

C#

C#
/// <summary>
/// Adds the key value pair to the AssociativeArrayAL(TKey,TValue).
/// </summary>
/// <param name="key">The key to associate with the value.</param>
/// <param name="value">The value to add.</param>
public void Add(TKey key, TValue value)
{
 Add(key, value, false);
}
/// <summary>
/// Gets or sets the value at the specified key.
/// </summary>
/// <param name="key">The key to use for finding the value.</param>
/// <returns>The value associated with the specified key.</returns>
public TValue this[TKey key]
{
 set
 {
 Add(key, value, true);

 Visual Basic

''' <summary>
''' Adds the key value pair to the AssociativeArrayAL(TKey,TValue).
''' </summary>
''' <param name="key">The key to associate with the value.</param>
''' <param name="value">The value to add.</param>
Public Sub Add(ByVal key As TKey, ByVal value As TValue)
 Add(key, value, False)
End Sub
''' <summary>
''' Gets or sets the value at the specified key.
''' </summary>
''' <param name="key">The key to use for finding the value.</param>
''' <returns>The value associated with the specified key.</returns>
Default Public Property Item(ByVal key As TKey) As TValue
 set
 Add(key, Value, True)
 End Set
End Property

C#

/// <summary>
/// Removes the first occurrence of the specified value.
/// </summary>
/// <param name="value">The value to remove.</param>
/// <returns>
/// True if the value was removed,
/// false if it wasn't present in the AssociativeArrayAL(TKey,TValue).
/// </returns>
public bool RemoveValue(TValue value)
{
 return RemoveValue(value, false);
}
/// <summary>
/// Removes the specified value.
/// </summary>
/// <param name="value">The value to remove.</param>
/// <param name="allOccurrences">
/// True if all occurrences of the value should be removed, false if not.
/// </param>
/// <returns>
/// True if the value was removed,
/// false if it wasn't present in the AssociativeArrayAL(TKey,TValue).
/// </returns>
public bool RemoveValue(TValue value, bool allOccurrences)
{
 bool removed = false;
 DoubleLinkedListNode<KVPair> node = FindValue(value);
 while (node != null)

 {
 removed = Remove(node) || removed;
 if (!allOccurrences)
 {
 return removed;
 }
 node = FindValue(value);
 }
 return removed;
}

 Visual Basic

''' <summary>
''' Removes the first occurrence of the specified value.
''' </summary>
''' <param name="value">The value to remove.</param>
''' <returns>
''' True if the value was removed,
''' false if it wasn't present in the AssociativeArrayAL(TKey,TValue).
''' </returns>
Public Function RemoveValue(ByVal value As TValue) As Boolean
 Return RemoveValue(value, False)
End Function
''' <summary>
''' Removes the specified value.
''' </summary>
''' <param name="value">The value to remove.</param>
''' <param name="allOccurrences">
''' True if all occurrences of the value should be removed, false if not.
''' </param>
''' <returns>
''' True if the value was removed,
''' false if it wasn't present in the AssociativeArrayAL(TKey,TValue).
''' </returns>
Public Function RemoveValue(ByVal value As TValue, ByVal allOccurrences As Boolean) _
 As Boolean
 Dim removed As Boolean = False
 Dim node As DoubleLinkedListNode(Of KVPair) = FindValue(value)
 While (node IsNot Nothing)
 removed = Remove(node) Or removed
 If (Not allOccurrences) Then
 Return removed
 End If
 node = FindValue(value)
 End While
 Return removed
End Function

By using the Count property users will get the amount of objects within the series.

C#

/// <summary>
/// Gets the number of items in the AssociativeArrayAL(TKey,TValue).
/// </summary>
public int Count
{
 get { return m_list.Count; }
}

 Visual Basic

''' <summary>
''' Gets the number of items in the AssociativeArrayAL(TKey,TValue).
''' </summary>
Public ReadOnly Property Count() As Integer
 Get
 Return m_list.Count
 End Get
End Property

 

Using the IsEmpty property, people can verify if the variety is not full before doing some operations.

C#

/// <summary>
/// States if the AssociativeArrayAL(TKey,TValue) is empty.
/// </summary>
public bool IsEmpty
{
 get { return Count <= 0; }
}

 Visual Basic

''' <summary>
''' States if the AssociativeArrayAL(TKey,TValue) is empty.
''' </summary>
Public ReadOnly Property IsEmpty() As Boolean
 Get
 Return Count <= 0
 End Get
End Property

Often customers might want to learn if a particular key or benefit occurs in the selection. The ContainsValue and ContainsKey approaches may be used to do this.

C#

/// <summary>
/// Checks to see if the AssociativeArrayAL(TKey,TValue) contains the specified value.
/// </summary>
/// <param name="value">The value to look for.</param>
/// <returns>True if the value was found, false otherwise.</returns>
public bool ContainsValue(TValue value)
{
 return FindValue(value) != null;
}
/// <summary>
/// Checks to see if the specified key is present in the AssociativeArrayAL(TKey,TValue).
/// </summary>
/// <param name="key">The key to look for.</param>
/// <returns>True if the key was found, false otherwise.</returns>
public bool ContainsKey(TKey key)
{
 return FindKey(key) != null;
}

 Visual Basic

''' <summary>
''' Checks to see if the specified key is present in the AssociativeArrayAL(TKey,TValue).
''' </summary>
''' <param name="key">The key to look for.</param>
''' <returns>True if the key was found, false otherwise.</returns>
Public Function ContainsKey(ByVal key As TKey) As Boolean
 Return FindKey(key) IsNot Nothing
End Function
''' <summary>
''' Checks to see if the AssociativeArrayAL(TKey,TValue) contains the specified value.
''' </summary>
''' <param name="value">The value to look for.</param>
''' <returns>True if the value was found, false otherwise.</returns>
Public Function ContainsValue(ByVal value As TValue) As Boolean
 Return FindValue(value) IsNot Nothing
End Function

The Item property uses the FindKey method to locate the node that contains the specified key. If the node doesn’t exist, the Item property throws an exception. This may not always be what the user wants. The TryGetValue method allows a user to try and retrieve a value without throwing an exception if the key doesn’t exist in the collection.

C#

/// <summary>
/// Tries to get the value at the specified key without throwing an exception.
/// </summary>
/// <param name="key">The key to get the value for.</param>
/// <param name="value">
/// The value that is associated with the specified key or the default value for the type.
/// </param>
/// <returns>True if the key was found, false otherwise.</returns>
public bool TryGetValue(TKey key, out TValue value)
{
 DoubleLinkedListNode<KVPair> node = FindKey(key);
 if (node != null)
 {
 value = node.Data.Value;
 return true;
 }
 value = default(TValue);
 return false;
}

 Visual Basic

''' <summary>
''' Tries to get the value at the specified key without throwing an exception.
''' </summary>
''' <param name="key">The key to get the value for.</param>
''' <param name="value">
''' The value that is associated with the specified key or the default value for the type.
''' </param>
''' <returns>True if the key was found, false otherwise.</returns>
Public Function TryGetValue(ByVal key As TKey, ByRef value As TValue) As Boolean
 Dim node As DoubleLinkedListNode(Of KVPair) = FindKey(key)
 If (node IsNot Nothing) Then
 value = node.Data.Value
 Return True
 End If
 value = CType(Nothing, TValue)
 Return False
End Function

The AssociativeArrayHT(T) course may include two constructors. For creating one constructor is the other, and also a clear school is for indicating a comparer to become employed for the key
Reviews.

The default constructor for the AssociativeArrayHT(T) class allows users to create an empty associative array.

C#

/// <summary>
/// Initializes a new instance of the AssociativeArrayHT(TKey,TValue) class that is empty.
/// </summary>
public AssociativeArrayHT()
{
 m_comparer = EqualityComparer<TKey>.Default;
}

 Visual Basic

''' <summary>
''' Initializes a new instance of the AssociativeArrayHT(TKey,TValue) class that is empty.
''' </summary>
Public Sub New()
 m_comparer = EqualityComparer(Of TKey).Default
End Sub

 C#

/// <summary>
/// Initializes a new instance of the AssociativeArrayHT(TKey,TValue) class
/// that is empty and uses the specified comparer.
/// </summary>
/// <param name="comparer">The comparer to use for the keys.</param>
public AssociativeArrayHT(IEqualityComparer<TKey> comparer)
{
 if (comparer == null)
 {
 throw new ArgumentNullException("comparer");
 }

 m_comparer = comparer;
}

 Visual Basic

''' <summary>
''' Initializes a new instance of the AssociativeArrayHT(TKey,TValue) class
''' that is empty and uses the specified comparer.
''' </summary>
''' <param name="comparer">The comparer to use for the keys.</param>
Public Sub New(ByVal comparer As IEqualityComparer(Of TKey))
 If (comparer Is Nothing) Then
 Throw New ArgumentNullException("comparer")
 End If
 m_comparer = comparer
End Sub

Customers require the capability to reassign keys, and also to associate beliefs with secrets aswell. They’ll be Ready to do this utilizing Product house and the Include technique. The approach and Product Home are defined as follows.

 

Users need the ability to associate values with keys, and to reassign keys as well. They will be able to accomplish this using the Add method and Item property. The Add method and Item property are defined as follows.

 

Leave a Reply

Your email address will not be published. Required fields are marked *