hash.c 3.9 KB
/**
 * 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);

static
void
__construct(DYNTYPE_HASH _this, va_list * params)
{
    _this->size = 0;
    _this->used = 0;
    _updateHashSize(_this);
}

static
void
__jsonConst(DYNTYPE_HASH _this, struct json_object * json)
{
    __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));
    }
}

static
void
__destruct(DYNTYPE_HASH _this)
{
    size_t index;

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

    free(_this->keys);
    free(_this->values);
}

static
struct json_object *
__toJson(DYNTYPE_HASH _this)
{
    size_t index;
    struct json_object * json = json_object_new_object();

    for (index = 0; index < _this->used; index ++) {
        json_object_object_add(
                json,
                _this->keys[index],
                toJson(_this->values[index]));
    }

    return json;
}

INIT_CCLASS(DYNTYPE_HASH, __jsonConst, __toJson);

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];
}

DYNTYPE dyntype_hash_del(DYNTYPE_HASH _this, const char * key)
{
    DYNTYPE found = NULL;
    size_t index = _getHashIdx(_this, key);

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

    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: