2012-01-19 13:36:13 +01:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "spdylay_map.h"
|
|
|
|
|
2012-02-21 15:23:47 +01:00
|
|
|
void spdylay_map_init(spdylay_map *map)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
map->root = NULL;
|
2012-01-25 18:28:58 +01:00
|
|
|
map->size = 0;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
static void spdylay_map_entry_free_recur(spdylay_map_entry *entry,
|
|
|
|
int (*func)(spdylay_map_entry *entry,
|
|
|
|
void *ptr),
|
|
|
|
void *ptr)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
if(entry != NULL) {
|
2012-09-11 17:13:02 +02:00
|
|
|
spdylay_map_entry_free_recur(entry->left, func, ptr);
|
|
|
|
spdylay_map_entry_free_recur(entry->right, func, ptr);
|
|
|
|
func(entry, ptr);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
void spdylay_map_free(spdylay_map *map)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-09-11 17:13:02 +02:00
|
|
|
map->root = NULL;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
void spdylay_map_each_free(spdylay_map *map,
|
|
|
|
int (*func)(spdylay_map_entry *entry, void *ptr),
|
|
|
|
void *ptr)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-09-11 17:13:02 +02:00
|
|
|
spdylay_map_entry_free_recur(map->root, func, ptr);
|
2012-01-19 13:36:13 +01:00
|
|
|
map->root = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 32 bit Mix Functions by Thomas Wang
|
|
|
|
*
|
|
|
|
* http://www.concentric.net/~Ttwang/tech/inthash.htm
|
|
|
|
*/
|
|
|
|
static uint32_t hash32shift(uint32_t key)
|
|
|
|
{
|
|
|
|
key = ~key + (key << 15); /* key = (key << 15) - key - 1; */
|
|
|
|
key = key ^ (key >> 12);
|
|
|
|
key = key + (key << 2);
|
|
|
|
key = key ^ (key >> 4);
|
|
|
|
key = key * 2057; /* key = (key + (key << 3)) + (key << 11); */
|
|
|
|
key = key ^ (key >> 16);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
void spdylay_map_entry_init(spdylay_map_entry *entry, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->key = key;
|
|
|
|
entry->left = entry->right = NULL;
|
|
|
|
entry->priority = hash32shift(key);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static spdylay_map_entry* rotate_left(spdylay_map_entry *entry)
|
|
|
|
{
|
|
|
|
spdylay_map_entry *root = entry->right;
|
|
|
|
entry->right = root->left;
|
|
|
|
root->left = entry;
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
static spdylay_map_entry* rotate_right(spdylay_map_entry* entry)
|
|
|
|
{
|
|
|
|
spdylay_map_entry *root = entry->left;
|
|
|
|
entry->left = root->right;
|
|
|
|
root->right = entry;
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
static spdylay_map_entry* insert_recur(spdylay_map_entry *entry,
|
2012-09-11 17:13:02 +02:00
|
|
|
spdylay_map_entry *new_entry,
|
2012-01-19 13:36:13 +01:00
|
|
|
int *error)
|
|
|
|
{
|
|
|
|
if(entry == NULL) {
|
2012-09-11 17:13:02 +02:00
|
|
|
entry = new_entry;
|
|
|
|
} else if(new_entry->key == entry->key) {
|
2012-01-19 13:36:13 +01:00
|
|
|
*error = SPDYLAY_ERR_INVALID_ARGUMENT;
|
2012-09-11 17:13:02 +02:00
|
|
|
} else if(new_entry->key < entry->key) {
|
|
|
|
entry->left = insert_recur(entry->left, new_entry, error);
|
2012-01-19 13:36:13 +01:00
|
|
|
} else {
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->right = insert_recur(entry->right, new_entry, error);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
if(entry->left != NULL && entry->priority > entry->left->priority) {
|
|
|
|
entry = rotate_right(entry);
|
|
|
|
} else if(entry->right != NULL && entry->priority > entry->right->priority) {
|
|
|
|
entry = rotate_left(entry);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
int spdylay_map_insert(spdylay_map *map, spdylay_map_entry *new_entry)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
int error = 0;
|
2012-09-11 17:13:02 +02:00
|
|
|
map->root = insert_recur(map->root, new_entry, &error);
|
2012-01-25 18:28:58 +01:00
|
|
|
if(!error) {
|
|
|
|
++map->size;
|
|
|
|
}
|
2012-01-19 13:36:13 +01:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
spdylay_map_entry* spdylay_map_find(spdylay_map *map, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
spdylay_map_entry *entry = map->root;
|
|
|
|
while(entry != NULL) {
|
|
|
|
if(key < entry->key) {
|
|
|
|
entry = entry->left;
|
|
|
|
} else if(key > entry->key) {
|
|
|
|
entry = entry->right;
|
|
|
|
} else {
|
2012-09-11 17:13:02 +02:00
|
|
|
return entry;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
static spdylay_map_entry* remove_rotate_recur(spdylay_map_entry *entry)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
if(entry->left == NULL) {
|
|
|
|
spdylay_map_entry *right = entry->right;
|
|
|
|
return right;
|
|
|
|
} else if(entry->right == NULL) {
|
|
|
|
spdylay_map_entry *left = entry->left;
|
|
|
|
return left;
|
|
|
|
} else if(entry->left->priority < entry->right->priority) {
|
|
|
|
entry = rotate_right(entry);
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->right = remove_rotate_recur(entry->right);
|
2012-05-18 17:49:15 +02:00
|
|
|
return entry;
|
2012-01-19 13:36:13 +01:00
|
|
|
} else {
|
|
|
|
entry = rotate_left(entry);
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->left = remove_rotate_recur(entry->left);
|
2012-05-18 17:49:15 +02:00
|
|
|
return entry;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
static spdylay_map_entry* remove_recur(spdylay_map_entry *entry, key_type key,
|
2012-01-25 18:28:58 +01:00
|
|
|
int *error)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-01-25 18:28:58 +01:00
|
|
|
if(entry == NULL) {
|
|
|
|
*error = SPDYLAY_ERR_INVALID_ARGUMENT;
|
|
|
|
} else if(key < entry->key) {
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->left = remove_recur(entry->left, key, error);
|
2012-01-25 18:28:58 +01:00
|
|
|
} else if(key > entry->key) {
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->right = remove_recur(entry->right, key, error);
|
2012-01-25 18:28:58 +01:00
|
|
|
} else {
|
2012-09-11 17:13:02 +02:00
|
|
|
entry = remove_rotate_recur(entry);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
int spdylay_map_remove(spdylay_map *map, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
|
|
|
if(map->root != NULL) {
|
2012-01-25 18:28:58 +01:00
|
|
|
int error = 0;
|
2012-09-11 17:13:02 +02:00
|
|
|
map->root = remove_recur(map->root, key, &error);
|
2012-01-25 18:28:58 +01:00
|
|
|
if(!error) {
|
|
|
|
--map->size;
|
|
|
|
}
|
2012-09-11 17:13:02 +02:00
|
|
|
return error;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
2012-09-11 17:13:02 +02:00
|
|
|
return SPDYLAY_ERR_INVALID_ARGUMENT;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 18:28:58 +01:00
|
|
|
size_t spdylay_map_size(spdylay_map *map)
|
|
|
|
{
|
|
|
|
return map->size;
|
|
|
|
}
|
|
|
|
|
2012-05-11 15:58:12 +02:00
|
|
|
static int for_each(spdylay_map_entry *entry,
|
2012-09-11 17:13:02 +02:00
|
|
|
int (*func)(spdylay_map_entry *entry, void *ptr),
|
2012-05-11 15:58:12 +02:00
|
|
|
void *ptr)
|
|
|
|
{
|
|
|
|
if(entry) {
|
|
|
|
int rv;
|
|
|
|
if((rv = for_each(entry->left, func, ptr)) != 0 ||
|
2012-09-11 17:13:02 +02:00
|
|
|
(rv = func(entry, ptr)) != 0 ||
|
2012-05-11 15:58:12 +02:00
|
|
|
(rv = for_each(entry->right, func, ptr)) != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
2012-05-11 15:58:12 +02:00
|
|
|
return 0;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2012-05-11 15:58:12 +02:00
|
|
|
int spdylay_map_each(spdylay_map *map,
|
2012-09-11 17:13:02 +02:00
|
|
|
int (*func)(spdylay_map_entry *entry, void *ptr),
|
2012-05-11 15:58:12 +02:00
|
|
|
void *ptr)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-05-11 15:58:12 +02:00
|
|
|
return for_each(map->root, func, ptr);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|