Skip to content

Commit

Permalink
Merge branch 'rename'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 28, 2010
2 parents 5733a1d + cef669d commit f0812aa
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 235 deletions.
54 changes: 27 additions & 27 deletions benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ static void
bm_push() {
start();
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_push(list, ListNode_new("foo"));
list_push(list, list_node_new("foo"));
}
stop();
}
Expand All @@ -39,97 +39,97 @@ static void
bm_unshift() {
start();
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
list_unshift(list, list_node_new("foo"));
}
stop();
}

static void
bm_find() {
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
list_unshift(list, list_node_new("foo"));
}
List_push(list, ListNode_new("bar"));
list_push(list, list_node_new("bar"));
start();
List_find(list, "bar");
list_find(list, "bar");
stop();
}

static void
bm_iterate() {
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
list_unshift(list, list_node_new("foo"));
}
ListIterator *it = ListIterator_new(list, LIST_HEAD);
ListNode *node;
list_iterator_t *it = list_iterator_new(list, LIST_HEAD);
list_node_t *node;
start();
while ((node = ListIterator_next(it)))
while ((node = list_iterator_next(it)))
;
stop();
}

static void
bm_pop() {
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
list_unshift(list, list_node_new("foo"));
}
ListNode *node;
list_node_t *node;
start();
while ((node = List_pop(list)))
while ((node = list_pop(list)))
;
stop();
}

static void
bm_shift() {
int n = nnodes;
List *list = List_new();
list_t *list = list_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
list_unshift(list, list_node_new("foo"));
}
ListNode *node;
list_node_t *node;
start();
while ((node = List_shift(list)))
while ((node = list_shift(list)))
;
stop();
}

static List *list;
static list_t *list;

static void
bm_at() {
start();
List_at(list, 100000);
list_at(list, 100000);
stop();
}

static void
bm_at2() {
start();
List_at(list, 1000000);
list_at(list, 1000000);
stop();
}

static void
bm_at3() {
start();
List_at(list, -100000);
list_at(list, -100000);
stop();
}

int
main(int argc, const char **argv){
int n = nnodes;
list = List_new();
while (n--) List_unshift(list, ListNode_new("foo"));
list = list_new();
while (n--) list_unshift(list, list_node_new("foo"));
puts("\n 10,000,000 nodes\n");
bm("pushed", bm_push);
bm("unshifted", bm_unshift);
Expand Down
32 changes: 16 additions & 16 deletions src/iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@
#include "list.h"

/*
* Allocate a new ListIterator. NULL on failure.
* Allocate a new list_iterator_t. NULL on failure.
* Accepts a direction, which may be LIST_HEAD or LIST_TAIL.
*/

ListIterator *
ListIterator_new(List *list, ListDirection direction) {
ListNode *node = direction == LIST_HEAD
list_iterator_t *
list_iterator_new(list_t *list, list_direction_t direction) {
list_node_t *node = direction == LIST_HEAD
? list->head
: list->tail;
return ListIterator_newFromNode(node, direction);
return list_iterator_new_from_node(node, direction);
}

/*
* Allocate a new ListIterator with the given start
* Allocate a new list_iterator_t with the given start
* node. NULL on failure.
*/

ListIterator *
ListIterator_newFromNode(ListNode *node, ListDirection direction) {
ListIterator *self;
if (!(self = LIST_MALLOC(sizeof(ListIterator))))
list_iterator_t *
list_iterator_new_from_node(list_node_t *node, list_direction_t direction) {
list_iterator_t *self;
if (!(self = LIST_MALLOC(sizeof(list_iterator_t))))
return NULL;
self->next = node;
self->direction = direction;
return self;
}

/*
* Return the next ListNode or NULL when no more
* Return the next list_node_t or NULL when no more
* nodes remain in the list.
*/

ListNode *
ListIterator_next(ListIterator *self) {
ListNode *curr = self->next;
list_node_t *
list_iterator_next(list_iterator_t *self) {
list_node_t *curr = self->next;
if (curr) {
self->next = self->direction == LIST_HEAD
? curr->next
Expand All @@ -56,6 +56,6 @@ ListIterator_next(ListIterator *self) {
*/

void
ListIterator_destroy(ListIterator *self) {
list_iterator_destroy(list_iterator_t *self) {
LIST_FREE(self);
}
}
70 changes: 35 additions & 35 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "list.h"

/*
* Allocate a new List. NULL on failure.
* Allocate a new list_t. NULL on failure.
*/

List *
List_new() {
List *self;
if (!(self = LIST_MALLOC(sizeof(List))))
list_t *
list_new() {
list_t *self;
if (!(self = LIST_MALLOC(sizeof(list_t))))
return NULL;
self->head = NULL;
self->tail = NULL;
Expand All @@ -29,10 +29,10 @@ List_new() {
*/

void
List_destroy(List *self) {
list_destroy(list_t *self) {
unsigned int len = self->len;
ListNode *next;
ListNode *curr = self->head;
list_node_t *next;
list_node_t *curr = self->head;
while (len--) {
next = curr->next;
if (self->free) self->free(curr->val);
Expand All @@ -47,8 +47,8 @@ List_destroy(List *self) {
* and return the node, NULL on failure.
*/

ListNode *
List_push(List *self, ListNode *node) {
list_node_t *
list_push(list_t *self, list_node_t *node) {
if (!node) return NULL;
if (self->len) {
node->prev = self->tail;
Expand All @@ -67,10 +67,10 @@ List_push(List *self, ListNode *node) {
* Return / detach the last node in the list, or NULL.
*/

ListNode *
List_pop(List *self) {
list_node_t *
list_pop(list_t *self) {
if (!self->len) return NULL;
ListNode *node = self->tail;
list_node_t *node = self->tail;
if (--self->len) {
(self->tail = node->prev)->next = NULL;
} else {
Expand All @@ -84,10 +84,10 @@ List_pop(List *self) {
* Return / detach the first node in the list, or NULL.
*/

ListNode *
List_shift(List *self) {
list_node_t *
list_shift(list_t *self) {
if (!self->len) return NULL;
ListNode *node = self->head;
list_node_t *node = self->head;
if (--self->len) {
(self->head = node->next)->prev = NULL;
} else {
Expand All @@ -102,8 +102,8 @@ List_shift(List *self) {
* and return the node, NULL on failure.
*/

ListNode *
List_unshift(List *self, ListNode *node) {
list_node_t *
list_unshift(list_t *self, list_node_t *node) {
if (!node) return NULL;
if (self->len) {
node->next = self->head;
Expand All @@ -122,47 +122,47 @@ List_unshift(List *self, ListNode *node) {
* Return the node associated to val or NULL.
*/

ListNode *
List_find(List *self, void *val) {
ListIterator *it = ListIterator_new(self, LIST_HEAD);
ListNode *node;
while ((node = ListIterator_next(it))) {
list_node_t *
list_find(list_t *self, void *val) {
list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
list_node_t *node;
while ((node = list_iterator_next(it))) {
if (self->match) {
if (self->match(val, node->val)) {
ListIterator_destroy(it);
list_iterator_destroy(it);
return node;
}
} else {
if (val == node->val) {
ListIterator_destroy(it);
list_iterator_destroy(it);
return node;
}
}
}
ListIterator_destroy(it);
list_iterator_destroy(it);
return NULL;
}

/*
* Return the node at the given index or NULL.
*/

ListNode *
List_at(List *self, int index) {
ListDirection direction = LIST_HEAD;
list_node_t *
list_at(list_t *self, int index) {
list_direction_t direction = LIST_HEAD;

if (index < 0) {
direction = LIST_TAIL;
index = ~index;
}

if (index < self->len) {
ListIterator *it = ListIterator_new(self, direction);
ListNode *node = ListIterator_next(it);
list_iterator_t *it = list_iterator_new(self, direction);
list_node_t *node = list_iterator_next(it);
while (index--) {
node = ListIterator_next(it);
node = list_iterator_next(it);
};
ListIterator_destroy(it);
list_iterator_destroy(it);
return node;
}

Expand All @@ -174,7 +174,7 @@ List_at(List *self, int index) {
*/

void
List_remove(List *self, ListNode *node) {
list_remove(list_t *self, list_node_t *node) {
node->prev
? (node->prev->next = node->next)
: (self->head = node->next);
Expand All @@ -184,4 +184,4 @@ List_remove(List *self, ListNode *node) {
if (self->free) self->free(node);
LIST_FREE(node);
--self->len;
}
}
Loading

0 comments on commit f0812aa

Please sign in to comment.