moved files from DTLib repo
This commit is contained in:
91
SearchTree/SearchTree.c
Normal file
91
SearchTree/SearchTree.c
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "SearchTree.h"
|
||||
|
||||
STNode* STNode_create(){
|
||||
STNode* node=malloc(sizeof(STNode));
|
||||
node->branches=NULL;
|
||||
node->value.type=Null;
|
||||
node->value.UInt64=0;
|
||||
return node;
|
||||
}
|
||||
|
||||
void STNode_free(STNode* node){
|
||||
if (!node) throw(ERR_NULLPTR);
|
||||
if(node->branches){
|
||||
for(uint8 n32 = 0;n32<8;n32++){
|
||||
STNode*** ptrn32=(STNode***)node->branches[n32];
|
||||
if(ptrn32){
|
||||
for(uint8 n4 = 0;n4<8;n4++){
|
||||
STNode** ptrn4=ptrn32[n4];
|
||||
if (ptrn4){
|
||||
for(uint8 rem=0;rem<4;rem++){
|
||||
STNode* ptrrem=ptrn4[rem];
|
||||
if(ptrrem)
|
||||
STNode_free(ptrrem);
|
||||
}
|
||||
free(ptrn4);
|
||||
}
|
||||
}
|
||||
free(ptrn32);
|
||||
}
|
||||
}
|
||||
free(node->branches);
|
||||
}
|
||||
if(node->value.VoidPtr)
|
||||
Unitype_free(node->value);
|
||||
free(node);
|
||||
}
|
||||
|
||||
typedef struct {uint8 n32, n4, rem;} indexes3;
|
||||
|
||||
indexes3 splitindex(uint8 i){
|
||||
return (indexes3){
|
||||
.n32=i/32,
|
||||
.n4=i%32/4,
|
||||
.rem=i%32%4,
|
||||
};
|
||||
}
|
||||
|
||||
void ST_push(STNode* node_first, const char* key, Unitype value){
|
||||
if (!node_first) throw(ERR_NULLPTR);
|
||||
STNode* node_last=node_first;
|
||||
while(*key){
|
||||
indexes3 i3=splitindex((uint8)*key);
|
||||
if(!node_last->branches){
|
||||
node_last->branches=(STNode****)malloc(8*sizeof(STNode*));
|
||||
for(uint8 i=0;i<8;i++)
|
||||
node_last->branches[i]=(STNode***)NULL;
|
||||
}
|
||||
if(!node_last->branches[i3.n32]){
|
||||
node_last->branches[i3.n32]=(STNode***)malloc(8*sizeof(STNode*));
|
||||
for(uint8 i=0;i<8;i++)
|
||||
node_last->branches[i3.n32][i]=(STNode**)NULL;
|
||||
}
|
||||
if(!node_last->branches[i3.n32][i3.n4]){
|
||||
node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*));
|
||||
for(uint8 i=0;i<4;i++)
|
||||
node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL;
|
||||
}
|
||||
if(!node_last->branches[i3.n32][i3.n4][i3.rem])
|
||||
node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create();
|
||||
node_last=node_last->branches[i3.n32][i3.n4][i3.rem];
|
||||
key++;
|
||||
}
|
||||
node_last->value=value;
|
||||
}
|
||||
|
||||
Unitype ST_pull(STNode* node_first, const char* key){
|
||||
if (!node_first) throw(ERR_NULLPTR);
|
||||
STNode* node_last=node_first;
|
||||
while (*key){
|
||||
indexes3 i3=splitindex((uint8)*key);
|
||||
if(!node_last->branches) return UniNull;
|
||||
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
|
||||
if(!ptrn32) return UniNull;
|
||||
STNode** ptrn4=ptrn32[i3.n4];
|
||||
if(!ptrn4) return UniNull;
|
||||
node_last=ptrn4[i3.rem];
|
||||
if(!node_last) return UniNull;
|
||||
key++;
|
||||
}
|
||||
return node_last->value;
|
||||
}
|
||||
14
SearchTree/SearchTree.h
Normal file
14
SearchTree/SearchTree.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
typedef struct SearchTreeNode{
|
||||
struct SearchTreeNode**** branches; //*STNode[8][8][4]
|
||||
Unitype value;
|
||||
} STNode;
|
||||
|
||||
STNode* STNode_create();
|
||||
void STNode_free(STNode* node);
|
||||
|
||||
void ST_push(STNode* node, const char* key, Unitype value);
|
||||
Unitype ST_pull(STNode* node, const char* key);
|
||||
25
SearchTree/SearchTree.md
Normal file
25
SearchTree/SearchTree.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Search Tree
|
||||
|
||||
byte is dividing into indexes of 3d array of nodes
|
||||
```
|
||||
STNode.branches byte
|
||||
┃
|
||||
┣━[0]━┳━[0]━┳━[0] 0
|
||||
┃ ┃ ┣━...
|
||||
┃ ┃ ┗━[3] 3
|
||||
┃ ┣━...
|
||||
┃ ┃
|
||||
┃ ┗━[7]━┳━[0] 28
|
||||
┃ ┣━...
|
||||
┃ ┗━[3] 31
|
||||
┣━...
|
||||
┃
|
||||
┗━[7]━┳━[0]━┳━[0] 224
|
||||
┃ ┣━...
|
||||
┃ ┗━[3] 227
|
||||
┣━...
|
||||
┃
|
||||
┗━[7]━┳━[0] 252
|
||||
┣━...
|
||||
┗━[3] 255
|
||||
```
|
||||
Reference in New Issue
Block a user