All pastes #830648 Raw Edit

Something

public text v1 · immutable
#830648 ·published 2007-12-24 03:41 UTC
rendered paste body
list.c:

#include <list.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ListCreate(List *list)
{
    list->size = 0;
    list->hashTable = malloc(LIST_BLOCK_SIZE * ADDRESS_SIZE);
    list->keysTable = malloc(LIST_BLOCK_SIZE * ADDRESS_SIZE);
    list->openSlots = LIST_BLOCK_SIZE;
}

void ListRemoveIndex(List *list, int index)
{
    free(list->hashTable + index);
    *(list->hashTable + index) = NULL;
    *(list->keysTable + index) = NULL;
    if (index != list->size - 1)
    {
        memmove(list->hashTable + index, list->hashTable + index + 1, list->size - index);
        memmove(list->keysTable + index, list->keysTable + index + 1, list->size - index);
    }
    list->size--;
    list->openSlots++;
}

int ListAppend(List *list, void *p, char *key)
{
    if ((list->openSlots) <= 0)
    {
        list->hashTable = realloc(list->hashTable, (list->size + LIST_BLOCK_SIZE) * ADDRESS_SIZE);
        list->keysTable = realloc(list->keysTable, (list->size + LIST_BLOCK_SIZE) * ADDRESS_SIZE);
        list->openSlots += LIST_BLOCK_SIZE;
    }
    *(list->hashTable + list->size) = p;
    *(list->keysTable + list->size) = key;
    list->size++;
    list->openSlots--;
	
    return list->size - 1;
}

int main(int argc, char **argv)
{
    List l;
    ListCreate(&l);
    
    ListAppend(&l, (void *) "Dakman", NULL);
    ListAppend(&l, (void *) "Penis", NULL);
    printf("%s, %s\n", l.hashTable[1], l.hashTable[0]);
    return 0;
}

list.h:

#ifndef _LIST_H
#define _LIST_H

#ifndef LIST_BLOCK_SIZE
 #define LIST_BLOCK_SIZE 10
#endif
#define ADDRESS_SIZE sizeof(void *)

struct list_t
{
    int size;
    int openSlots;
    
    void **hashTable;
    void **keysTable;
};
typedef struct list_t List;

void ListCreate(List *list); 
void ListRemove(List *list, int index);
int  ListAppend(List *list, void *data, char *key);
int  ListAppendPointer(List *list, void *data, char *key);

#endif