SUMMARY: MODULE | CLASS | TYPE | PROC | VAR | CONST | DETAIL: TYPE | PROC | VAR | CONST

# ADT:Dictionary:AddressKey:IntValue

This module is a reimplementation of the Python dictionary code in Oberon-2. It is based on version 2.65 of the python file ``src/Objects/dictobject.c`'. It maps objects on other objects. Some bit twiddling is required, partially due to the algorithm and partially due to performance, which may impede portability of the code.

Differences between ``dictobject.c`' and this implementation:

- NIL can be used as a value. This implies that the field ``Entry.value`' cannot be used to determine if the entry is in use or not.

- Any inefficiencies in this implementation are my fault, and do not originate in the Python implementation.

### Import List

        ADT:Storable
        IO
        Object
        Object
        RT0

Class List
Dictionary |

Class Summary: Dictionary [Detail]
      +---RT0.Object
           |
           +---Object.Object
                |
                +---ADT:Storable.Object
                     |
                     +--ADT:Dictionary:AddressKey:IntValue.Dictionary
Constructor Summary
Init(Dictionary) Initializes dict to an empty dictionary.
New(): Dictionary Creates a new, empty dictionary.
Method Summary
Clear() Removes all items from the dictionary dict.
Copy(): Dictionary Creates a copy of the dictionary dict.
Delete(Key) Removes the item with the key key from the dictionary dict.
Destroy()
Get(Key): Value Retrieves the value associated with key in the dictionary dict.
HasKey(Key): BOOLEAN Tests if an item in dict exists with the key key.
Keys(): ObjectArrayPtr Returns the list of keys of the dictionary dict.
Load(Reader) Loads data of dict from r.
Lookup(Key, VAR Value): BOOLEAN If key exists in the dictionary dict, then the procedure assigns the value of key to value and returns TRUE; otherwise, it returns FALSE and value is undefined.
Set(Key, Value) Sets the value for key key in the dictionary dict to value.
Size(): LONGINT Returns the number of items in the dictionary.
Store(Writer) Stores data of dict to w.
Inherited Methods
From RT0.Object: Finalize From Object.Object: Equals, HashCode, ToString From ADT:Storable.Object: Load, Store

Type Summary
[Entry] = RECORD ... END
[Hash] = LONGINT
[Index] = LONGINT
Key = Object
[Table] = POINTER TO ARRAY OF Entry
Value = LONGINT

Class Detail: Dictionary

Constructor Detail

### Init

    PROCEDURE Init(dict: Dictionary)

> Initializes dict to an empty dictionary.

### New

    PROCEDURE New(): Dictionary

> Creates a new, empty dictionary.

Method Detail

### Clear

    PROCEDURE (dict: Dictionary) Clear()

> Removes all items from the dictionary dict.

### Copy

    PROCEDURE (dict: Dictionary) Copy(): Dictionary

> Creates a copy of the dictionary dict.

### Delete

    PROCEDURE (dict: Dictionary) Delete(key: Key)

> Removes the item with the key key from the dictionary dict.

> Pre-condition: n item with the key key exists in the dictionary dict. This implies that key is not NIL.

### Destroy

    PROCEDURE (dict: Dictionary) Destroy()

### Get

    PROCEDURE (dict: Dictionary) Get(key: Key): Value

> Retrieves the value associated with key in the dictionary dict.

> Pre-condition: An item with the key key exists in the dictionary dict. This implies that key is not NIL.

### HasKey

    PROCEDURE (dict: Dictionary) HasKey(key: Key): BOOLEAN

> Tests if an item in dict exists with the key key.

> Pre-condition: key is not NIL.

### Keys

    PROCEDURE (dict: Dictionary) Keys(): ObjectArrayPtr

> Returns the list of keys of the dictionary dict. The keys are listed in no particular order.

### Load

    PROCEDURE (dict: Dictionary) Load(r: Reader)
      RAISES Error;

> Loads data of dict from r. Nested record pointers are loaded by calling the type-bound procecdure Reader.ReadObject. This procedure must be symmetric to Object.Store, or data internalization will break, causing undefined object state or program termination.

> Note: When internalizing a file with alien objects, it is possible that the type-bound procedure Object.Load is invoked more than once for a single object. Except for the results of the last call, all duplicates are discarded. Because of this, all changes by this procedure to any program state that is not part of the object dict are deprecated.

> Pre-condition: This procedure is either activated by a super call, or from the procedure Reader.ReadObject.

> [Description inherited from Load]

> Redefines: Load

### Lookup

    PROCEDURE (dict: Dictionary) Lookup(key: Key;
                     VAR value: Value): BOOLEAN

> If key exists in the dictionary dict, then the procedure assigns the value of key to value and returns TRUE; otherwise, it returns FALSE and value is undefined.

> Pre-condition: key is not NIL.

### Set

    PROCEDURE (dict: Dictionary) Set(key: Key;
                  value: Value)

> Sets the value for key key in the dictionary dict to value. If an item with this key already exists, its value is replaced. Otherwise, a new item is created with the given (key, value) pair.

> Pre-condition: key is not NIL.

### Size

    PROCEDURE (dict: Dictionary) Size(): LONGINT

> Returns the number of items in the dictionary.

### Store

    PROCEDURE (dict: Dictionary) Store(w: Writer)
      RAISES Error;

> Stores data of dict to w. Nested record pointers are stored by calling the type-bound procedure Writer.WriteObject. The procedure is not allowed to make any changes to the global state of the program, except for calling the ``Write`' methods of the writer w. Any redefinition of this procedure must include a super call, preferably as the first statement of the procedure body.

> Pre-condition: This procedure is either activated by a super call, or from the procedure Writer.WriteObject.

> [Description inherited from Store]

> Redefines: Store

Type Detail

### Entry

    TYPE [Entry] = RECORD
                 END

### Hash

    TYPE [Hash] = LONGINT

### Index

    TYPE [Index] = LONGINT

### Key

    TYPE Key = Object

### Table

    TYPE [Table] = POINTER TO ARRAY OF Entry

### Value

    TYPE Value = LONGINT