libdht

A simple helper library for distributed hash tables.
git clone git://r-36.net/libdht
Log | Files | Refs | README | LICENSE

dhtlist.c (2731B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #define _GNU_SOURCE
      7 #include <unistd.h>
      8 #include <stdlib.h>
      9 #include <stdio.h>
     10 #include <string.h>
     11 #include <sys/types.h>
     12 #include <regex.h>
     13 
     14 #include "ind.h"
     15 #include "dht.h"
     16 
     17 dhtlistelem_t *
     18 dhtlistelem_set(dhtlistelem_t *elem, dhtnode_t *node)
     19 {
     20 	elem->node = node;
     21 
     22 	return elem;
     23 }
     24 
     25 dhtlistelem_t *
     26 dhtlistelem_new(dhtnode_t *node)
     27 {
     28 	return dhtlistelem_set(mallocz(sizeof(dhtlistelem_t), 2), node);
     29 }
     30 
     31 void
     32 dhtlistelem_free(dhtlistelem_t *elem)
     33 {
     34 	free(elem);
     35 }
     36 
     37 dhtlist_t *
     38 dhtlist_new(void)
     39 {
     40 	return (dhtlist_t *)mallocz(sizeof(dhtlist_t), 2);
     41 }
     42 
     43 void
     44 dhtlist_free(dhtlist_t *node)
     45 {
     46 	dhtlistelem_t *elem;
     47 
     48 	if (node->first != NULL) {
     49 		for (elem = node->first;;) {
     50 			if (elem->next != NULL) {
     51 				elem = elem->next;
     52 				free(elem->prev);
     53 			} else {
     54 				free(elem);
     55 				break;
     56 			}
     57 		}
     58 	}
     59 	free(node);
     60 }
     61 
     62 dhtlistelem_t *
     63 dhtlist_addelem(dhtlist_t *dhtlist, dhtlistelem_t *elem)
     64 {
     65 	if (dhtlist->first == NULL)
     66 		dhtlist->first = elem;
     67 	if (dhtlist->last == NULL)
     68 		dhtlist->last = elem;
     69 	else {
     70 		dhtlist->last->next = elem;
     71 		elem->prev = dhtlist->last;
     72 		dhtlist->last = elem;
     73 	}
     74 	dhtlist->len++;
     75 
     76 	return elem;
     77 }
     78 
     79 dhtlistelem_t *
     80 dhtlist_add(dhtlist_t *dhtlist, dhtnode_t *node)
     81 {
     82 	return dhtlist_addelem(dhtlist, dhtlistelem_new(node));
     83 }
     84 
     85 dhtlistelem_t *
     86 dhtlist_push(dhtlist_t *dhtlist, dhtnode_t *node)
     87 {
     88 	dhtlistelem_t *elem;
     89 
     90 	elem = dhtlistelem_new(node);
     91 
     92 	if (dhtlist->first == NULL)
     93 		dhtlist->first = elem;
     94 	else {
     95 		dhtlist->first->prev = elem;
     96 		elem->next = dhtlist->first;
     97 		dhtlist->first = elem;
     98 	}
     99 	if (dhtlist->last == NULL)
    100 		dhtlist->last = elem;
    101 	dhtlist->len++;
    102 
    103 	return elem;
    104 }
    105 
    106 void
    107 dhtlist_delelemlinks(dhtlist_t *dhtlist, dhtlistelem_t *elem)
    108 {
    109 	dhtlistelem_t *prev, *next;
    110 
    111 	prev = elem->prev;
    112 	next = elem->next;
    113 
    114 	if (prev != NULL)
    115 		prev->next = next;
    116 	if (next != NULL)
    117 		next->prev = prev;
    118 	if (dhtlist->first == elem)
    119 		dhtlist->first = next;
    120 	if (dhtlist->last == elem)
    121 		dhtlist->last = prev;
    122 	dhtlist->len--;
    123 }
    124 
    125 dhtlistelem_t *
    126 dhtlist_insert(dhtlist_t *dhtlist, dhtlistelem_t *elem, int idx)
    127 {
    128 	int i;
    129 	dhtlistelem_t *next;
    130 
    131 	i = 0;
    132 
    133 	if (idx > dhtlist->len-1)
    134 		return NULL;
    135 	if (idx == dhtlist->len-1)
    136 		return dhtlist_addelem(dhtlist, elem);
    137 
    138 	forodhtlist(dhtlist, next)
    139 		if (i == idx)
    140 			break;
    141 
    142 	if (next->prev != NULL)
    143 		next->prev->next = elem;
    144 	elem->prev = next->prev;
    145 	next->prev = elem;
    146 	elem->next = next;
    147 	dhtlist->len++;
    148 
    149 	return elem;
    150 }
    151 
    152 dhtlistelem_t *
    153 dhtlist_move(dhtlist_t *dhtlist, dhtlistelem_t *elem, int idx)
    154 {
    155 	dhtlist_delelemlinks(dhtlist, elem);
    156 	return dhtlist_insert(dhtlist, elem, idx);
    157 }
    158 
    159 void
    160 dhtlist_print(dhtlist_t *dhtlist)
    161 {
    162 	fordhtlist(dhtlist, elem)
    163 		printf("%p\n", (void *)elem);
    164 	fflush(stdout);
    165 }
    166