📜 ⬆️ ⬇️

[Translation] Associative arrays in the D language

This is a translation of the article “Associative arrays,” published on January 1, 2016. To my taste, the article is somewhat unnecessarily superficial and does not contain much detail, but it may be useful to those who are familiar with associative arrays in other programming languages .

D has built-in support for associative arrays, also known as hash tables.
They are similar to Map in Java or std::unordered_map in C ++.

Associative array declaration


')
To declare an associative array, use the following syntax:

 // . .: value —  , key —   value[key] myAssociativeArray; 



Insert elements into an associative array


To insert elements into an associative array, use the operator [] .
Below is an example of creating an associative array of squares of integers from 0 to 10 and displaying them on the screen.

 import std.stdio; void main() { int[int] squares; //  for (int i = 0; i <= 10; ++i) squares[i] = i * i; //     writeln(squares); } 


Running the example, we get the following output:
[0:0, 6:36, 7:49, 2:4, 3:9, 10:100, 1:1, 8:64, 5:25, 4:16, 9:81]
Note that the numbers are not sorted - this is expected: associative arrays are not internally sorted.
Notes :


Removing elements from an associative array


To remove elements from an associative array, use the remove() function.

 aa.remove("hello"); 


Check for the existence of a key


To check the key for existence, use the in operator, which returns a pointer to the value. If the key does not exist, the pointer will be null .

 int[int] squares; // ... int* p = 10 in squares; // . .:    null  ,  is if (p !is null) writeln(*p); else writeln(" ."); 


Cleaning the associative array


There are two ways to clear an associative array:
  1. Go through the keys and delete them
  2. Discard the old array and create a new one.


Method 1: delete the keys


 foreach (key; aa.keys) aa.remove(key); 


Method 2: create a new array


To discard an existing array, assign it a null value.

 aa = null; //     aa[1] = 1; //       


Properties


We are already familiar with some properties of associative arrays, for example, remove() or keys . Below are the rest:
PropertyDescription
.sizeofReturns the size of the reference to the associative array. In 32-bit builds, this is 4, and in 64-bit builds 8.
.lengthReturns the number of values ​​in an array. Unlike dynamic arrays, this property is read only.
.dupCreates an associative array of the same size and copies the contents of the first array into it.
.keysReturns a dynamic array whose elements are the keys of the source associative array.
.valuesReturns a dynamic array whose elements are the values ​​of the source associative array.
.rehashOn-site reorganizes the array, optimizing the search for it. rehash is useful, for example, when a symbol table is loaded into a program, and then it is necessary to search by it. Returns a reference to the reorganized array.
.byKey()Returns the range suitable for iteration using the foreach keys of an associative array.
.byValue()Returns a range suitable for iteration using the foreach of an associative array.
.byKeyValue()Returns the range suitable for iteration using the foreach key-value pairs of an associative array. The returned pairs are represented as an opaque type with the properties .key and .value , which allow accessing the key and the value of the pair, respectively.
.get(Key key, lazy Value defVal)Looking for a key . If it exists, returns the corresponding value; if it does not exist, executes and returns the default defVal .


What else to read


For more information (for example, to learn about working with classes and structures in associative arrays), refer to the official dlang.org documentation .

Source: https://habr.com/ru/post/274723/


All Articles