Commit 45c782e8 authored by Chris Müller's avatar Chris Müller

lib: implement map interface based on red-black trees.

parent 63ac6d1d
...@@ -7,6 +7,7 @@ set(SOURCES ...@@ -7,6 +7,7 @@ set(SOURCES
structures/red_black_tree.c structures/red_black_tree.c
structures/heap.c structures/heap.c
structures/array.c structures/array.c
structures/map.c
structures/structures.c structures/structures.c
) )
......
/*
* Copyright (c) 2012 Christoph Mueller <ruunhb@googlemail.com>
*
* Crystal is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Crystal 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "map.h"
#include "red_black_tree.h"
#include <assert.h>
struct CryMap*
cry_map_new(cry_ordering_funptr compare)
{
return cry_cast(struct CryMap*, cry_rbtree_new(compare));
}
void
cry_map_clear(struct CryMap* map, cry_free_funptr fun_key, cry_free_funptr fun_data)
{
assert(map != 0);
cry_rbtree_clear(cry_cast(struct CryRbTree*, map), fun_key, fun_data);
}
size_t
cry_map_size(struct CryMap* map)
{
assert(map != 0);
return cry_cast(struct CryRbTree*, map)->nodes;
}
pointer
cry_map_lookup(struct CryMap* map, const_pointer key)
{
assert(map != 0);
struct CryRbNode* result = cry_rbtree_lookup(cry_cast(struct CryRbTree*, map), key);
return (result != 0) ? cry_cast(pointer, result->data) : 0;
}
int
cry_map_insert(struct CryMap* map, const_pointer key, const_pointer value)
{
assert(map != 0);
struct CryRbNode* result = cry_rbtree_insert(cry_cast(struct CryRbTree*, map), key, value);
return result != 0;
}
struct CryMapPair
cry_map_remove(struct CryMap* map, const_pointer key)
{
assert(map != 0);
struct CryMapPair pair = { .key = 0, .data = 0 };
struct CryRbNode* result = cry_rbtree_remove(cry_cast(struct CryRbTree*, map), key);
if(result) {
pair.key = result->entry;
pair.data = result->data;
cry_rbnode_free(result, 0, 0);
}
return pair;
}
...@@ -22,20 +22,22 @@ ...@@ -22,20 +22,22 @@
#include "structures.h" #include "structures.h"
struct CryMap; struct CryMapPair {
const_pointer key;
const_pointer data;
};
struct CryMap* cry_map_new(cry_ordering_funptr compare); struct CryMap;
void cry_map_free(struct CryMap* map, cry_free_funptr key_free, cry_free_funptr value_free);
size_t cry_map_size(struct CryMap* map); struct CryMap* cry_map_new(cry_ordering_funptr compare);
pointer cry_map_lookup(struct CryMap* map, const_pointer key); size_t cry_map_size(struct CryMap* map);
return_code cry_map_insert(struct CryMap* map, const_pointer key, pointer value);
pointer cry_map_remove(struct CryMap* map, const_pointer key, cry_free_funptr key_free);
pointer cry_map_lookup(struct CryMap* map, const_pointer key);
int cry_map_insert(struct CryMap* map, const_pointer key, const_pointer value);
struct CryMapPair cry_map_remove(struct CryMap* map, const_pointer key);
void cry_map_clear(struct CryMap* map, cry_free_funptr fun_key, cry_free_funptr fun_data);
void cry_map_dump_debug(struct CryMap* map);
#endif // CRYSTAL_STRUCTURES_MAP_H #endif // CRYSTAL_STRUCTURES_MAP_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment