Dictionaries

Dictionary Syntax and Semantics

A dictionary is a mapping from a key type to a value type.

dictionary是从键类型到值类型的映射

For example:

1
2
3
4
5
6
7
8
9
10
11
module M
{
struct Employee
{
long number;
string firstName;
string lastName;
}

dictionary<long, Employee> EmployeeMap;
}

This definition creates a dictionary named EmployeeMap that maps from an employee number to a structure containing the details for an employee. Whether or not the key type (the employee number, of type long in this example) is also part of the value type (the Employee structure in this example) is up to you — as far as Slice is concerned, there is no need to include the key as part of the value.

此定义创建一个名为 EmployeeMap 的dictionary,该dictionary从员工编号映射到包含员工详细信息的结构。 键类型(员工编号,本例中为 long 类型)是否也是值类型(本例中为 Employee 结构)的一部分取决于您 — 就 Slice 而言,不需要 包含键作为值的一部分。

Dictionaries can be used to implement sparse arrays, or any lookup data structure with non-integral key type. Even though a sequence of structures containing key-value pairs could be used to model the same thing, a dictionary is more appropriate:

dictionary可用于实现稀疏数组或任何具有非整数键类型的查找数据结构。 尽管包含键值对的结构序列可用于对同一事物进行建模,但dictionary更合适:

A dictionary clearly signals the intent of the designer, namely, to provide a mapping from a domain of values to a range of values. (A sequence of structures of key-value pairs does not signal that same intent as clearly.)

dictionary清楚地表明了设计者的意图,即提供从值域到值范围的映射。 (键值对结构的sequence并不能清楚地表明相同的意图。)

At the programming language level, sequences are implemented as vectors (or possibly lists), that is, they are not well suited to model sparsely populated domains and require a linear search to locate an element with a particular value. On the other hand, dictionaries are implemented as a data structure (typically a hash table or red-black tree) that supports efficient searching in O(log n) average time or better.

在编程语言级别,sequences被实现为数组(或可能是list),也就是说,它们不太适合对稀疏填充的域进行建模,并且需要线性搜索来定位具有特定值的元素。 另一方面,dictionary被实现为另一种数据结构(通常是哈希表或红黑树),支持平均时间为 O(log n) 或更好的高效搜索。

Allowable Types for Dictionary Keys and Values

The key type of a dictionary need not be an integral type. For example, we could use the following definition to translate the names of the days of the week:

dictionary的键类型不必是整型。 例如,我们可以使用以下定义来翻译一周中各天的名称:

1
dictionary<string, string> WeekdaysEnglishToGerman;

The server implementation would take care of initializing this map with the key-value pairs Monday-Montag, Tuesday-Dienstag, and so on.

服务器实现将负责使用键值对 Monday-Montag、Tuesday-Dienstag 等初始化该映射。

The value type of a dictionary can be any Slice type. However, the key type of a dictionary is limited to one of the following types:

dictionary的值类型可以是任何 Slice 类型。 但是,字典的键类型仅限于以下类型之一:

Integral types (short, int, long)
bool
byte
string
enum
Structures containing only data members of legal key types

Other complex types, such as dictionaries, and floating-point types (float and double) cannot be used as the key type. Complex types are disallowed because they complicate the language mappings for dictionaries, and floating-point types are disallowed because representational changes of values as they cross machine boundaries can lead to ill-defined semantics for equality.

其他复杂类型,例如dictionary和浮点类型(float 和 double)不能用作键类型。 不允许使用复杂类型,因为它们使dictionary的语言映射变得复杂;并且不允许使用浮点类型,因为跨机器边界时值的表示变化可能会导致平等语义定义不明确。