ui_access

source of std::map

1// The hash map.2//3// Map<K, V> takes any key type satisfying std::hash::Key: every primitive4// type, String, every enum and struct whose parts are keys, and the types5// declaring it. It keeps its entries densely6// in insertion order in a Vector, so iterating them is a walk over one7// array, and indexes them with an open-addressing table of 64-bit slots8// probed linearly: a slot holds 32 bits of the key's hash above the9// entry's position plus one, 0 marking an empty slot, so a probe compares10// hashes before it touches an entry. The table is kept at most half full11// and removal shifts the following slots back, so no tombstones12// accumulate. Removing an entry moves the last one into its place, which13// changes the iteration order. A map with String keys also takes `&[u8]`14// keys, so looking text up needs no String. Maps of disposable values are15// disposable themselves (section 5.2).1617import std::option;18import std::vector;19import std::string;20import std::hash;21import std::dispose;2223// One key and its value, as a map's entries() gives them.24exp type Entry<K, V> : Disposable if (V: Disposable) = struct {25    exp key: K,26    exp value: V,27};2829exp fn dispose<K, V: Disposable>(self entry: &var Entry<K, V>) {30    entry.value.dispose();31}3233exp type Map<K: Key, V> : Disposable if (V: Disposable) = struct {34    entries: Vector<Entry<K, V>>,35    table: Table,36};3738// An empty map, allocating nothing.39exp fn Map::empty() -> Map<K, V> {40    return Map { .entries = Vector<Entry<K, V>>::empty(), .table = Table::empty() };41}4243exp fn dispose<K: Key, V: Disposable>(self map: &var Map<K, V>) {44    map.entries.dispose();45    map.table = Table::empty();46}4748// The number of entries.49exp fn length<K: Key, V>(self map: &Map<K, V>) -> u64 {50    return map.entries.length();51}5253// The entries in insertion order, as long as none is removed.54exp fn entries<K: Key, V>(self map: &Map<K, V>) -> &[Entry<K, V>] {55    return &map.entries.view();56}5758// Gives the key the value and returns the value it replaces, if any.59exp fn insert<K: Key, V>(self map: &var Map<K, V>, key: K, value: V) -> Option<V> {60    const hash = short_hash(key.hash());61    if (map.position_of(&key, hash) is ::Some |found|) {62        return ::Some(map.replace_at(found, move value));63    }64    map.add(hash, Entry { .key = move key, .value = move value });65    return ::None;66}6768// The key's value in place; a fault when the map holds no such key.69exp fn get<K: Key, V>(self map: &Map<K, V>, key: &K) -> &V {70    if (map.try_get(&key) is ::Some |&value|) {71        return &value;72    }73    panic "the map holds no such key";74}7576// The key's value in place for writing; a fault when the map holds no77// such key.78exp fn get_var<K: Key, V>(self map: &var Map<K, V>, key: &K) -> &var V {79    if (map.try_get_var(&key) is ::Some |&var value|) {80        return &var value;81    }82    panic "the map holds no such key";83}8485// The key's value in place, or `None`.86exp fn try_get<K: Key, V>(self map: &Map<K, V>, key: &K) -> Option<&V> {87    return map.value_at(map.position_of(&key, short_hash(key.hash())));88}8990// The key's value in place for writing, or `None`.91exp fn try_get_var<K: Key, V>(self map: &var Map<K, V>, key: &K) -> Option<&var V> {92    return map.value_var_at(map.position_of(&key, short_hash(key.hash())));93}9495exp fn contains<K: Key, V>(self map: &Map<K, V>, key: &K) -> bool {96    return map.position_of(&key, short_hash(key.hash())) is ::Some;97}9899// Removes the key and returns its value, or `None` when absent.100exp fn remove<K: Key, V>(self map: &var Map<K, V>, key: &K) -> Option<V> {101    const hash = short_hash(key.hash());102    return map.remove_at(map.position_of(&key, hash), hash);103}104105// Removes every entry, keeping the allocations.106exp fn clear<K: Key, V>(self map: &var Map<K, V>) {107    map.entries.clear();108    map.table.clear();109}110111/* --- Text keys --- */112113// Gives the text the value and returns the value it replaces, if any; a114// new key is copied into a String.115exp fn insert<V>(self map: &var Map<String, V>, key: &[u8], value: V) -> Option<V> {116    const hash = short_hash(hash_bytes(&key));117    if (map.text_position(&key, hash) is ::Some |found|) {118        return ::Some(map.replace_at(found, move value));119    }120    map.add(hash, Entry { .key = String::from(&key), .value = move value });121    return ::None;122}123124exp fn get<V>(self map: &Map<String, V>, key: &[u8]) -> &V {125    if (map.try_get(&key) is ::Some |&value|) {126        return &value;127    }128    panic "the map holds no such key";129}130131exp fn get_var<V>(self map: &var Map<String, V>, key: &[u8]) -> &var V {132    if (map.try_get_var(&key) is ::Some |&var value|) {133        return &var value;134    }135    panic "the map holds no such key";136}137138exp fn try_get<V>(self map: &Map<String, V>, key: &[u8]) -> Option<&V> {139    return map.value_at(map.text_position(&key, short_hash(hash_bytes(&key))));140}141142exp fn try_get_var<V>(self map: &var Map<String, V>, key: &[u8]) -> Option<&var V> {143    return map.value_var_at(map.text_position(&key, short_hash(hash_bytes(&key))));144}145146exp fn contains<V>(self map: &Map<String, V>, key: &[u8]) -> bool {147    return map.text_position(&key, short_hash(hash_bytes(&key))) is ::Some;148}149150exp fn remove<V>(self map: &var Map<String, V>, key: &[u8]) -> Option<V> {151    const hash = short_hash(hash_bytes(&key));152    return map.remove_at(map.text_position(&key, hash), hash);153}154155/* --- Positions --- */156157// The entry's position, if the map holds the key.158fn position_of<K: Key, V>(self map: &Map<K, V>, key: &K, hash: u32) -> Option<u64> {159    if (map.table.slots is ::Some |&slots|) {160        const entries = &map.entries.view();161        var index = (hash to u64) & map.table.mask;162        while (slots[index] != EMPTY) {163            const slot = slots[index];164            if (slot_hash(slot) == hash && entries[slot_position(slot)].key.same(&key)) {165                return ::Some(slot_position(slot));166            }167            index = (index + 1) & map.table.mask;168        }169    }170    return ::None;171}172173fn text_position<V>(self map: &Map<String, V>, key: &[u8], hash: u32) -> Option<u64> {174    if (map.table.slots is ::Some |&slots|) {175        const entries = &map.entries.view();176        var index = (hash to u64) & map.table.mask;177        while (slots[index] != EMPTY) {178            const slot = slots[index];179            if (slot_hash(slot) == hash && entries[slot_position(slot)].key.view() == key) {180                return ::Some(slot_position(slot));181            }182            index = (index + 1) & map.table.mask;183        }184    }185    return ::None;186}187188fn add<K: Key, V>(self map: &var Map<K, V>, hash: u32, entry: Entry<K, V>) {189    map.table.add(hash, map.entries.length());190    map.entries.push(move entry);191}192193fn value_at<K: Key, V>(self map: &Map<K, V>, position: Option<u64>) -> Option<&V> {194    if (position is ::Some |at|) {195        return ::Some(&map.entries.get(at).value);196    }197    return ::None;198}199200fn value_var_at<K: Key, V>(self map: &var Map<K, V>, position: Option<u64>) -> Option<&var V> {201    if (position is ::Some |at|) {202        return ::Some(&var map.entries.get_var(at).value);203    }204    return ::None;205}206207fn replace_at<K: Key, V>(self map: &var Map<K, V>, position: u64, value: V) -> V {208    const entry = &var map.entries.get_var(position);209    var old = move entry.value;210    entry.value = move value;211    return move old;212}213214// Removes the entry at the position, the last one taking its place.215fn remove_at<K: Key, V>(self map: &var Map<K, V>, position: Option<u64>, hash: u32) -> Option<V> {216    const at = if (position is ::Some |p|) p else {217        return ::None;218    };219    const last = map.entries.length() - 1;220    map.table.release(hash, at);221    if (at != last) {222        const moved = short_hash(map.entries.get(last).key.hash());223        map.table.reposition(moved, last, at);224    }225    var entry = map.entries.swap_remove(at);226    return ::Some(move entry.value);227}228229/* --- The table --- */230231// The slots, a power of two of them, `None` until the first insert.232type Table = struct {233    slots: Option<*var [u64]>,234    mask: u64,235    used: u64,236};237238const EMPTY: u64 = 0;239240fn Table::empty() -> Table {241    return Table { .slots = ::None, .mask = 0, .used = 0 };242}243244fn slot_of(hash: u32, position: u64) -> u64 => ((hash to u64) << 32) | (position + 1);245246fn slot_hash(slot: u64) -> u32 => (slot >> 32) to u32;247248fn slot_position(slot: u64) -> u64 => (slot & 0xFFFFFFFF) - 1;249250// Records the entry at the position under the hash, growing first when251// the table would be more than half full.252fn add(self table: &var Table, hash: u32, position: u64) {253    if ((table.used + 1) * 2 > table.mask + 1) {254        table.grow();255    }256    if (table.slots is ::Some |&var slots|) {257        var index = (hash to u64) & table.mask;258        while (slots[index] != EMPTY) {259            index = (index + 1) & table.mask;260        }261        slots[index] = slot_of(hash, position);262    }263    table.used += 1;264}265266fn grow(self table: &var Table) {267    var capacity: u64 = 16;268    if (table.slots is ::Some |&old|) {269        capacity = old.length() * 2;270    }271    var slots: *var [u64] = new [EMPTY : capacity];272    const mask = capacity - 1;273    if (table.slots is ::Some |&old|) {274        for (old) |slot| {275            if (slot != EMPTY) {276                var index = (slot_hash(slot) to u64) & mask;277                while (slots[index] != EMPTY) {278                    index = (index + 1) & mask;279                }280                slots[index] = slot;281            }282        }283    }284    table.slots = ::Some(move slots);285    table.mask = mask;286}287288// Empties the slot of the entry under the hash, shifting back the slots289// after it that probed past it.290fn release(self table: &var Table, hash: u32, position: u64) {291    var hole = table.find(hash, position);292    if (table.slots is ::Some |&var slots|) {293        const mask = table.mask;294        var index = hole;295        while (true) {296            index = (index + 1) & mask;297            const slot = slots[index];298            if (slot == EMPTY) {299                break;300            }301            const home = (slot_hash(slot) to u64) & mask;302            // the slot may move into the hole when its home is not after it303            if (((index + mask + 1 - home) & mask) >= ((index + mask + 1 - hole) & mask)) {304                slots[hole] = slot;305                hole = index;306            }307        }308        slots[hole] = EMPTY;309        table.used -= 1;310    }311}312313// Points the slot of the entry under the hash at its new position.314fn reposition(self table: &var Table, hash: u32, from: u64, to_position: u64) {315    const index = table.find(hash, from);316    if (table.slots is ::Some |&var slots|) {317        slots[index] = slot_of(hash, to_position);318    }319}320321// The index of the slot of the entry under the hash, which must be there.322fn find(self table: &Table, hash: u32, position: u64) -> u64 {323    var index = (hash to u64) & table.mask;324    if (table.slots is ::Some |&slots|) {325        while (slots[index] != slot_of(hash, position)) {326            index = (index + 1) & table.mask;327        }328    }329    return index;330}331332fn clear(self table: &var Table) {333    if (table.slots is ::Some |&var slots|) {334        for (slots) |&var slot| {335            slot = EMPTY;336        }337    }338    table.used = 0;339}340341// The hash's low half, which the slots keep.342fn short_hash(hash: u64) -> u32 => (hash & 0xFFFFFFFF) to u32;