more
This commit is contained in:
parent
6ae7357a33
commit
f8203039ef
|
@ -0,0 +1,10 @@
|
||||||
|
#if !defined(_TD_KEY_H_)
|
||||||
|
#define _TD_KEY_H_
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
|
||||||
|
} key;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _TD_KEY_H_
|
|
@ -0,0 +1,20 @@
|
||||||
|
#if !defined(_TD_LIST_H_)
|
||||||
|
#define _TD_LIST_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum { TD_LIST_ORDERED, TD_LIST_UNORDERED } TLIST_TYPE;
|
||||||
|
|
||||||
|
typedef int32_t (* comparefn(void *key1, void *key2));
|
||||||
|
|
||||||
|
struct _list_type {
|
||||||
|
TLIST_TYPE type;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _list_node {
|
||||||
|
} SListNode;
|
||||||
|
|
||||||
|
typedef struct _list {
|
||||||
|
} SList;
|
||||||
|
|
||||||
|
#endif // _TD_LIST_H_
|
|
@ -0,0 +1,73 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "skiplist.h"
|
||||||
|
|
||||||
|
#define IS_VALID_SKIPLIST_DUPLICATE_KEY_STRATEGY(strategy) \
|
||||||
|
(((strategy) >= SKIPLIST_ALLOW_DUPLICATE_KEY) && ((strategy) <= SKIPLIST_DISCARD_DUPLICATE_KEY))
|
||||||
|
|
||||||
|
SSkipListNode *tdCreateSkiplistNode(int32_t nlevels) {
|
||||||
|
SSkipListNode *pNode = (SSkipListNode *)malloc(sizeof(SSkipListNode));
|
||||||
|
if (pNode == NULL) return NULL;
|
||||||
|
|
||||||
|
pNode->nexts = (struct _skiplist_node **)cmalloc(nlevels, sizeof(struct _skiplist_node *));
|
||||||
|
if (pNode->nexts == NULL) {
|
||||||
|
free(pNode);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pNode->prevs = (struct _skiplist_node **)cmalloc(nlevels, sizeof(struct _skiplist_node *));
|
||||||
|
if (pNode->nexts == NULL) {
|
||||||
|
free(pNode->nexts);
|
||||||
|
free(pNode);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t tdFreeSkiplistNode(SSkipListNode *pNode) {
|
||||||
|
if (pNode == NULL) return 0;
|
||||||
|
// TODO: free key and free value
|
||||||
|
|
||||||
|
// Free the skip list
|
||||||
|
free(pNode->nexts);
|
||||||
|
free(pNode->prevs);
|
||||||
|
free(pNode);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSkipList *tdCreateSkiplist(int16_t nMaxLevels, SKIPLIST_DUPLICATE_KEY_STATEGY strategy) {
|
||||||
|
// Check parameters
|
||||||
|
if (!IS_VALID_SKIPLIST_DUPLICATE_KEY_STRATEGY(strategy)) return NULL;
|
||||||
|
|
||||||
|
SSkipList *pSkipList = (SSkipList *)malloc(sizeof(SSkipList));
|
||||||
|
if (pSkipList == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pSkipList->strategy = strategy;
|
||||||
|
pSkipList->nMaxLevels = nMaxLevels;
|
||||||
|
|
||||||
|
pSkipList->head = tdCreateSkiplistNode(nMaxLevels);
|
||||||
|
if (pSkipList->head == NULL) {
|
||||||
|
free(pSkipList);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pSkipList;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t tdFreeSkipList(SSkipList *pSkipList) {
|
||||||
|
if (pSkipList == NULL) return 0;
|
||||||
|
|
||||||
|
SSkipListNode *pNode = pSkipList->head->nexts[0];
|
||||||
|
while (pNode) {
|
||||||
|
SSkipListNode *pTemp = pNode->nexts[0];
|
||||||
|
tdFreeSkiplistNode(pNode);
|
||||||
|
pNode = pTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(pSkipList);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
#if !defined(_TD_SKIPLIST_H)
|
||||||
|
#define _TD_SKIPLIST_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SKIPLIST_ALLOW_DUPLICATE_KEY,
|
||||||
|
SKIPLIST_REPLACE_DUPLICATE_KEY,
|
||||||
|
SKIPLIST_DISCARD_DUPLICATE_KEY
|
||||||
|
} SKIPLIST_DUPLICATE_KEY_STATEGY;
|
||||||
|
|
||||||
|
typedef struct _skiplist_node {
|
||||||
|
void * key;
|
||||||
|
void * value;
|
||||||
|
struct _skiplist_node **nexts;
|
||||||
|
struct _skiplist_node **prevs;
|
||||||
|
} SSkipListNode;
|
||||||
|
|
||||||
|
// To implement a general skip list
|
||||||
|
typedef struct _skiplist {
|
||||||
|
SKIPLIST_DUPLICATE_KEY_STATEGY strategy;
|
||||||
|
SSkipListNode * head;
|
||||||
|
SSkipListNode * tail;
|
||||||
|
int32_t nMaxLevels;
|
||||||
|
int32_t count;
|
||||||
|
} SSkipList;
|
||||||
|
|
||||||
|
// -- Operations on SSkipListNode
|
||||||
|
SSkipListNode *tdCreateSkiplistNode(int32_t nlevels);
|
||||||
|
int32_t tdFreeSkiplistNode(SSkipListNode *pNode);
|
||||||
|
|
||||||
|
// -- Operations on SSkipList
|
||||||
|
SSkipList *tdCreateSkiplist(int16_t nMaxLevels, SKIPLIST_DUPLICATE_KEY_STATEGY strategy);
|
||||||
|
int32_t tdFreeSkipList(SSkipList *pSkipList);
|
||||||
|
// int32_t tdAddItemToSkiplist(SSkipList *slist, void *key, void *value);
|
||||||
|
// int32_t tdAddNodeToSkiplist(SSkipList *slist, SSkipListNode *node);
|
||||||
|
|
||||||
|
#endif // _TD_SKIPLIST_H
|
Loading…
Reference in New Issue