Skip to content

MapPolicy

Description

Probing / rehash policy currently installed on m. The returned MapPolicy value is borrowed from inside the map – read-only.

Parameters

Name Direction Description
m in Map.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    void validate_map_policy(const MapPolicy *policy) {
        static const struct {
            u64 length;
    
        if (!policy) {
            LOG_FATAL("Expected a valid MapPolicy pointer");
        }
    
        if (!policy->name || !policy->name[0]) {
            LOG_FATAL("MapPolicy must have a non-empty name");
        }
    
        if (!policy->should_rehash || !policy->next_capacity || !policy->first_index || !policy->next_index) {
            LOG_FATAL("MapPolicy '{}' must provide all required callbacks", policy->name);
        }
    
        if (policy->max_probe_count == 0) {
            LOG_FATAL("MapPolicy '{}' must provide a non-zero max_probe_count", policy->name);
        }
    
            if ((next0 == 0) && ((length != 0) || (capacity != 0) || (tombstones != 0))) {
                LOG_FATAL("MapPolicy '{}' returned zero capacity for a non-empty snapshot", policy->name);
            }
    
            if ((next_same != 0) && (next_same < length)) {
                LOG_FATAL("MapPolicy '{}' returned capacity smaller than current length", policy->name);
            }
    
            if (next_more < ((size)length + 1)) {
                LOG_FATAL("MapPolicy '{}' returned capacity smaller than requested minimum entries", policy->name);
            }
        }
    
                if (next == first) {
                    LOG_FATAL("MapPolicy '{}' produced a stuck probe sequence for capacity {}", policy->name, capacity);
                }
            }
    }
    
    MapPolicy validate_map_policy_copy(MapPolicy policy) {
        validate_map_policy(&policy);
        return policy;
    }
    
    const MapPolicy MapPolicyLinear = {
        .name            = "linear",
        .should_rehash   = default_should_rehash,
    };
    
    const MapPolicy MapPolicyQuadratic = {
        .name            = "quadratic",
        .should_rehash   = default_should_rehash,
        size        hash_offset,
        size        n,
        MapPolicy   policy
    ) {
        u8 *old_entries;
        WriteFmt("Testing ValidateMapPolicy without name\n");
    
        MapPolicy policy = {
            .name            = "",
            .should_rehash   = always_rehash,
        WriteFmt("Testing ValidateMapPolicy without probe limit\n");
    
        MapPolicy policy = {
            .name            = "invalid",
            .should_rehash   = always_rehash,
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
    
        for (int i = 0; i < 24; i++) {
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc         = DefaultAllocatorInit();
        MapPolicy        custom_policy = {
                   .name            = "five-step",
                   .should_rehash   = custom_should_rehash,
    
        result = result && (MapCapacity(&map) == 10);
        result = result && (MapPolicy(&map).next_capacity == custom_next_capacity);
    
        for (int i = 0; i < 6; i++) {
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
        result = result && (MapValueCountForKey(&map, "red") == 2);
                      (MapPolicy(&map).next_index == MapPolicyQuadratic.next_index) &&
                      (MapPolicy(&map).next_capacity == MapPolicyQuadratic.next_capacity) &&
                      (MapPolicy(&map).should_rehash == MapPolicyQuadratic.should_rehash);
        result = result && (MapValueCountForKey(&map, "red") == 2);
        result = result && MapGetFirstPtr(&map, "red") && (ZstrCompare(*MapGetFirstPtr(&map, "red"), "apple") == 0);
                      MapEntries(&map) == NULL && MapStates(&map) == NULL && MapKeyCompare(&map) == i32_compare &&
                      MapValueCompare(&map) == NULL && MapKeyHash(&map) == i32_hash &&
                      MapPolicy(&map).should_rehash == MapPolicyLinear.should_rehash &&
                      MapPolicy(&map).next_capacity == MapPolicyLinear.next_capacity &&
                      MapPolicy(&map).first_index == MapPolicyLinear.first_index &&
                      MapValueCompare(&map) == NULL && MapKeyHash(&map) == i32_hash &&
                      MapPolicy(&map).should_rehash == MapPolicyLinear.should_rehash &&
                      MapPolicy(&map).next_capacity == MapPolicyLinear.next_capacity &&
                      MapPolicy(&map).first_index == MapPolicyLinear.first_index &&
                      MapPolicy(&map).next_index == MapPolicyLinear.next_index &&
                      MapPolicy(&map).should_rehash == MapPolicyLinear.should_rehash &&
                      MapPolicy(&map).next_capacity == MapPolicyLinear.next_capacity &&
                      MapPolicy(&map).first_index == MapPolicyLinear.first_index &&
                      MapPolicy(&map).next_index == MapPolicyLinear.next_index &&
                      MapPolicy(&map).max_probe_count == MapPolicyLinear.max_probe_count;
                      MapPolicy(&map).next_capacity == MapPolicyLinear.next_capacity &&
                      MapPolicy(&map).first_index == MapPolicyLinear.first_index &&
                      MapPolicy(&map).next_index == MapPolicyLinear.next_index &&
                      MapPolicy(&map).max_probe_count == MapPolicyLinear.max_probe_count;
                      MapPolicy(&map).first_index == MapPolicyLinear.first_index &&
                      MapPolicy(&map).next_index == MapPolicyLinear.next_index &&
                      MapPolicy(&map).max_probe_count == MapPolicyLinear.max_probe_count;
    
        MapDeinit(&map);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc         = DefaultAllocatorInit();
        MapPolicy        custom_policy = {
                   .name            = "custom-linear",
                   .should_rehash   = custom_should_rehash_snapshot,
        custom_policy.max_probe_count = 0;
    
        bool result = ZstrCompare(MapPolicy(&map).name, "custom-linear") == 0 &&
                      MapPolicy(&map).should_rehash == custom_should_rehash_snapshot &&
                      MapPolicy(&map).next_capacity == custom_next_capacity &&
    
        bool result = ZstrCompare(MapPolicy(&map).name, "custom-linear") == 0 &&
                      MapPolicy(&map).should_rehash == custom_should_rehash_snapshot &&
                      MapPolicy(&map).next_capacity == custom_next_capacity &&
                      MapPolicy(&map).first_index == custom_first_index &&
        bool result = ZstrCompare(MapPolicy(&map).name, "custom-linear") == 0 &&
                      MapPolicy(&map).should_rehash == custom_should_rehash_snapshot &&
                      MapPolicy(&map).next_capacity == custom_next_capacity &&
                      MapPolicy(&map).first_index == custom_first_index &&
                      MapPolicy(&map).next_index == custom_next_index && MapPolicy(&map).max_probe_count == 11;
                      MapPolicy(&map).should_rehash == custom_should_rehash_snapshot &&
                      MapPolicy(&map).next_capacity == custom_next_capacity &&
                      MapPolicy(&map).first_index == custom_first_index &&
                      MapPolicy(&map).next_index == custom_next_index && MapPolicy(&map).max_probe_count == 11;
                      MapPolicy(&map).next_capacity == custom_next_capacity &&
                      MapPolicy(&map).first_index == custom_first_index &&
                      MapPolicy(&map).next_index == custom_next_index && MapPolicy(&map).max_probe_count == 11;
    
        MapDeinit(&map);
    
    static bool test_validate_map_policy(void) {
        MapPolicy custom_policy = {
            .name            = "custom-linear",
            .should_rehash   = custom_should_rehash_snapshot,
        MapPolicyNextIndexFn    next_index;
        size                    max_probe_count;
    } MapPolicy;
    
    ///
    /// TAGS: Map, Constant, Policy
    ///
    extern const MapPolicy MapPolicyLinear;
    
    ///
    /// TAGS: Map, Constant, Policy
    ///
    extern const MapPolicy MapPolicyQuadratic;
    
    typedef struct {
        u8               *entries;
        u8               *states;
        MapPolicy         policy;
        Allocator        *allocator;
        u64               __magic;
            MapEntry(K, V) * entries;                                                                                      \
            u8        *states;                                                                                             \
            MapPolicy  policy;                                                                                             \
            Allocator *allocator;                                                                                          \
            u64        __magic;                                                                                            \
Last updated on