Consolidate datastructure libraries

This commit is contained in:
Kevin Lange 2014-03-24 20:18:40 -07:00
parent 1e91dea4cc
commit 8980f568a3
16 changed files with 45 additions and 191 deletions

View File

@ -12,6 +12,7 @@ CFLAGS = -O2 -std=c99
CFLAGS += -finline-functions -ffreestanding
CFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-parameter
CFLAGS += -Wstrict-prototypes -pedantic -fno-omit-frame-pointer
CFLAGS += -D_KERNEL_
# We have some pieces of assembly sitting around as well...
YASM = yasm

View File

@ -1,7 +1,5 @@
#include <system.h>
#include <logging.h>
#include <list.h>
#include <hashmap.h>
#include "list.h"
#include "hashmap.h"
unsigned int hashmap_string_hash(void * _key) {
unsigned int hash = 0;

View File

@ -2,8 +2,15 @@
*
* General-purpose list implementations.
*/
#include <system.h>
#include <list.h>
#include "list.h"
#ifdef _KERNEL_
# include <system.h>
#else
# include <stddef.h>
# include <stdlib.h>
#endif
void list_destroy(list_t * list) {
/* Free all of the contents of a list */

View File

@ -2,9 +2,15 @@
*
* General-purpose tree implementation
*/
#include <system.h>
#include <list.h>
#include <tree.h>
#include "tree.h"
#ifdef _KERNEL_
# include <system.h>
#else
# include <stddef.h>
# include <stdlib.h>
#endif
tree_t * tree_create(void) {
/* Create a new tree */

View File

@ -14,11 +14,7 @@
#ifdef _KERNEL_
# include <types.h>
#else
# ifdef BOOTLOADER
# include <types.h>
# else
# include <stdint.h>
# endif
# include <stdint.h>
#endif
/*

View File

@ -1,7 +1,15 @@
#ifndef KL_HASHMAP_H
#define KL_HASHMAP_H
#include <list.h>
#include "list.h"
#ifdef _KERNEL_
# include <system.h>
#else
# include <string.h>
# include <stddef.h>
# include <stdlib.h>
#endif
typedef unsigned int (*hashmap_hash_t) (void * key);
typedef int (*hashmap_comp_t) (void * a, void * b);

View File

@ -5,7 +5,12 @@
#ifndef LIST_H
#define LIST_H
#include <types.h>
#ifdef _KERNEL_
# include <types.h>
#else
# include <stdint.h>
# include <stddef.h>
#endif
typedef struct node {
struct node * next;

View File

@ -2,7 +2,6 @@
*/
#ifndef __SYSTEM_H
#define __SYSTEM_H
#define _KERNEL_
#include <types.h>
#include <fs.h>
#include <va_list.h>

View File

@ -5,8 +5,7 @@
#ifndef TREE_H
#define TREE_H
#include <system.h>
#include <list.h>
#include "list.h"
typedef struct tree_node {
void * value;

View File

@ -35,6 +35,7 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lib/list.h"
/* Width of a line to read for a clause. Clauses should not be too terrible long. */

1
userspace/lib/hashmap.c Symbolic link
View File

@ -0,0 +1 @@
../../kernel/ds/hashmap.c

1
userspace/lib/hashmap.h Symbolic link
View File

@ -0,0 +1 @@
../../kernel/include/hashmap.h

View File

@ -1,135 +0,0 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* General-purpose list implementations.
*/
#include <stdlib.h>
#include "list.h"
void list_destroy(list_t * list) {
/* Free all of the contents of a list */
node_t * n = list->head;
while (n) {
free(n->value);
n = n->next;
}
}
void list_free(list_t * list) {
/* Free the actual structure of a list */
node_t * n = list->head;
while (n) {
node_t * s = n->next;
free(n);
n = s;
}
}
void list_append(list_t * list, node_t * node) {
/* Insert a node onto the end of a list */
if (!list->tail) {
list->head = node;
} else {
list->tail->next = node;
node->prev = list->tail;
}
list->tail = node;
list->length++;
}
void list_insert(list_t * list, void * item) {
/* Insert an item into a list */
node_t * node = malloc(sizeof(node_t));
node->value = item;
node->next = NULL;
node->prev = NULL;
list_append(list, node);
}
list_t * list_create() {
/* Create a fresh list */
list_t * out = malloc(sizeof(list_t));
out->head = NULL;
out->tail = NULL;
out->length = 0;
return out;
}
node_t * list_find(list_t * list, void * value) {
foreach(item, list) {
if (item->value == value) {
return item;
}
}
return NULL;
}
void list_remove(list_t * list, size_t index) {
/* remove index from the list */
if (index > list->length) return;
size_t i = 0;
node_t * n = list->head;
while (i < index) {
n = n->next;
i++;
}
list_delete(list, n);
}
void list_delete(list_t * list, node_t * node) {
/* remove node from the list */
if (node == list->head) {
list->head = node->next;
}
if (node == list->tail) {
list->tail = node->prev;
}
if (node->prev) {
node->prev->next = node->next;
}
if (node->next) {
node->next->prev = node->prev;
}
list->length--;
}
node_t * list_pop(list_t * list) {
/* Remove and return the last value in the list
* If you don't need it, you still probably want to free it!
* Try free(list_pop(list)); !
* */
if (!list->tail) return NULL;
node_t * out = list->tail;
list_delete(list, list->tail);
return out;
}
node_t * list_dequeue(list_t * list) {
if (!list->head) return NULL;
node_t * out = list->head;
list_delete(list, list->head);
return out;
}
list_t * list_copy(list_t * original) {
/* Create a new copy of original */
list_t * out = list_create();
node_t * node = original->head;
while (node) {
list_insert(out, node->value);
}
return out;
}
void list_merge(list_t * target, list_t * source) {
/* Destructively merges source into target */
if (target->tail) {
target->tail->next = source->head;
} else {
target->head = source->head;
}
if (source->tail) {
target->tail = source->tail;
}
target->length += source->length;
free(source);
}

1
userspace/lib/list.c Symbolic link
View File

@ -0,0 +1 @@
../../kernel/ds/list.c

View File

@ -1,37 +0,0 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* General-purpose list implementations.
*/
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
typedef struct node {
struct node * next;
struct node * prev;
void * value;
} __attribute__((packed)) node_t;
typedef struct {
node_t * head;
node_t * tail;
size_t length;
} __attribute__((packed)) list_t;
void list_destroy(list_t * list);
void list_free(list_t * list);
void list_append(list_t * list, node_t * item);
void list_insert(list_t * list, void * item);
list_t * list_create();
node_t * list_find(list_t * list, void * value);
void list_remove(list_t * list, size_t index);
void list_delete(list_t * list, node_t * node);
node_t * list_pop(list_t * list);
node_t * list_dequeue(list_t * list);
list_t * list_copy(list_t * original);
void list_merge(list_t * target, list_t * source);
#define foreach(i, list) for (node_t * i = list->head; i != NULL; i = i->next)
#endif

1
userspace/lib/list.h Symbolic link
View File

@ -0,0 +1 @@
../../kernel/include/list.h

1
userspace/lib/tree.c Symbolic link
View File

@ -0,0 +1 @@
../../kernel/ds/tree.c

1
userspace/lib/tree.h Symbolic link
View File

@ -0,0 +1 @@
../../kernel/include/tree.h