hash.c 3.79 KB
/**
 * \file
 * dyntype/hash.c: very simple string indexed hash mainly to hold json objects
 * Copyright (C) 2011  Georg Hopp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <string.h>
#include <stdlib.h>
#include <json/json.h>

#include "token/cclass.h"
#include "token/dyntype.h"
#include "token/dyntype/hash.h"


#define HASH_ENTRY_CHUNK_SIZE 128


static void _updateHashSize(DYNTYPE_HASH this);

INIT_CLASS(DYNTYPE_HASH);

__construct(DYNTYPE_HASH)
{
    this->size = 0;
    this->used = 0;
    _updateHashSize(this);
}

#undef __construct

__jsonConst(DYNTYPE_HASH)
{
    __construct(this, NULL);

    if (json_type_object != json_object_get_type(json)) {
        return;
    }

    json_object_object_foreach(json, key, value) {
        dyntype_hash_set(this, key, newFromJson(DYNTYPE, value));
    }
}

__clear(DYNTYPE_HASH) {}

__destruct(DYNTYPE_HASH)
{
    size_t index;

    for (index = 0; index < this->used; index ++) {
        free(this->keys[index]);
        delete(this->values[index]);
    }

    free(this->keys);
    free(this->values);

    this->size = this->used = 0;
    _updateHashSize(this);
}

__toJson(DYNTYPE_HASH)
{
    size_t index;
    *json = json_object_new_object();

    for (index = 0; index < this->used; index ++) {
        struct json_object * values;

        toJson(this->values[index], &values);

        json_object_object_add(*json, this->keys[index], values);
    }
}

static
void
_updateHashSize(DYNTYPE_HASH this)
{
    if (this->used == this->size) {
        this->size += HASH_ENTRY_CHUNK_SIZE;

        this->keys = realloc(this->keys, sizeof(char*) * this->size);
        memset(this->keys + (this->used * sizeof(char*)),
                0,
                HASH_ENTRY_CHUNK_SIZE * sizeof(char*));

        this->values = realloc(
                this->values,
                sizeof(DYNTYPE) * this->size);
        memset(this->values + (this->used * sizeof(DYNTYPE)),
                0,
                HASH_ENTRY_CHUNK_SIZE * sizeof(DYNTYPE));
    }
}

static
size_t
_getHashIdx(DYNTYPE_HASH this, const char * key)
{
    size_t index;

    for (index = 0;
            index < this->used && strcmp(this->keys[index], key);
            index++);

    return index;
}

void
dyntype_hash_set(DYNTYPE_HASH this, const char * key, DYNTYPE value)
{
    size_t index = _getHashIdx(this, key);

    this->keys[index] = calloc(strlen(key) + 1, sizeof(char));
    memcpy(this->keys[index], key, strlen(key));

    this->values[index] = value;

    if (index == this->used) {
        this->used += 1;
    }

    _updateHashSize(this);
}

DYNTYPE
dyntype_hash_get(DYNTYPE_HASH this, const char * key)
{
    size_t index = _getHashIdx(this, key);

    if (index == this->used) {
        return NULL;
    }

    return this->values[index];
}

void
dyntype_hash_del(DYNTYPE_HASH this, const char * key)
{
    size_t index = _getHashIdx(this, key);

    if (index == this->used) {
        return;
    }

    free(this->keys[index]);

    memmove(this->keys + index + 1,
            this->keys + index,
            (this->size - index) * sizeof(char*));

    memmove(this->values + index + 1,
            this->values + index,
            (this->size - index) * sizeof(DYNTYPE));

    this->used -= 1;
}

// vim: set et ts=4 sw=4: