mirror of https://github.com/bol-van/zapret/
12 changed files with 1294 additions and 108 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,83 +0,0 @@ |
|||
#include "chartree.h" |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
|
|||
static cptr *CharTreeInit(char c) |
|||
{ |
|||
cptr *p; |
|||
p=(cptr *)calloc(1,sizeof(cptr)); |
|||
if (p) p->chr = c; |
|||
return p; |
|||
} |
|||
void CharTreeDestroy(cptr *p) |
|||
{ |
|||
cptr *p2; |
|||
while (p) |
|||
{ |
|||
CharTreeDestroy(p->leaf); |
|||
p2 = p; |
|||
p = p->next; |
|||
free(p2); |
|||
} |
|||
} |
|||
static cptr *CharTreeFindChar(cptr *p,char c) |
|||
{ |
|||
while (p) |
|||
{ |
|||
if (p->chr==c) return p; |
|||
p = p->next; |
|||
} |
|||
return NULL; |
|||
} |
|||
bool CharTreeAddStr(cptr **pp,const char *s) |
|||
{ |
|||
cptr *p; |
|||
if (*pp) |
|||
{ |
|||
if (!(p=CharTreeFindChar(*pp,*s))) |
|||
{ |
|||
// already present. append to list head
|
|||
if (!(p = CharTreeInit(*s))) |
|||
return false; |
|||
p->next = *pp; |
|||
*pp = p; |
|||
} |
|||
} |
|||
else |
|||
if (!(p = *pp = CharTreeInit(*s))) return false; |
|||
if (!*s) return true; |
|||
return CharTreeAddStr(&p->leaf,s+1); |
|||
} |
|||
bool CharTreeCheckStr(cptr *p,const char *s) |
|||
{ |
|||
p = CharTreeFindChar(p,*s); |
|||
if (!p) return false; |
|||
if (!*s) return true; |
|||
return CharTreeCheckStr(p->leaf,s+1); |
|||
} |
|||
|
|||
static char *DupLower(const char *s) |
|||
{ |
|||
char *sp,*sl = strdup(s); |
|||
if (!sl) return false; |
|||
for(sp=sl;*sp;sp++) *sp=tolower(*sp); |
|||
return sl; |
|||
} |
|||
bool CharTreeAddStrLower(cptr **pp,const char *s) |
|||
{ |
|||
bool b; |
|||
char *sl = DupLower(s); |
|||
if (!sl) return false; |
|||
b=CharTreeAddStr(pp,sl); |
|||
free(sl); |
|||
return b; |
|||
} |
|||
bool CharTreeCheckStrLower(cptr *pp,const char *s) |
|||
{ |
|||
bool b; |
|||
char *sl = DupLower(s); |
|||
if (!sl) return false; |
|||
b=CharTreeCheckStr(pp,sl); |
|||
free(sl); |
|||
return b; |
|||
} |
@ -1,16 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include <stdbool.h> |
|||
#include <ctype.h> |
|||
|
|||
typedef struct cptr |
|||
{ |
|||
char chr; |
|||
struct cptr *leaf,*next; |
|||
} cptr; |
|||
|
|||
void CharTreeDestroy(cptr *p); |
|||
bool CharTreeAddStr(cptr **pp,const char *s); |
|||
bool CharTreeAddStrLower(cptr **pp,const char *s); |
|||
bool CharTreeCheckStr(cptr *p,const char *s); |
|||
bool CharTreeCheckStrLower(cptr *pp,const char *s); |
@ -0,0 +1,50 @@ |
|||
#include "strpool.h" |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
|
|||
#undef uthash_nonfatal_oom |
|||
#define uthash_nonfatal_oom(elt) ut_oom_recover(elt) |
|||
|
|||
static bool oom=false; |
|||
static void ut_oom_recover(strpool *elem) |
|||
{ |
|||
oom=true; |
|||
} |
|||
|
|||
bool StrPoolAddStr(strpool **pp,const char *s) |
|||
{ |
|||
strpool *elem; |
|||
if (!(elem = (strpool*)malloc(sizeof(strpool)))) |
|||
return false; |
|||
if (!(elem->str = strdup(s))) |
|||
{ |
|||
free(elem); |
|||
return false; |
|||
} |
|||
oom = false; |
|||
HASH_ADD_KEYPTR( hh, *pp, elem->str, strlen(elem->str), elem ); |
|||
if (oom) |
|||
{ |
|||
free(elem->str); |
|||
free(elem); |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
bool StrPoolCheckStr(strpool *p,const char *s) |
|||
{ |
|||
strpool *elem; |
|||
HASH_FIND_STR( p, s, elem); |
|||
return elem!=NULL; |
|||
} |
|||
|
|||
void StrPoolDestroy(strpool **p) |
|||
{ |
|||
strpool *elem,*tmp; |
|||
HASH_ITER(hh, *p, elem, tmp) { |
|||
free(elem->str); |
|||
HASH_DEL(*p, elem); |
|||
free(elem); |
|||
} |
|||
*p = NULL; |
|||
} |
@ -0,0 +1,18 @@ |
|||
#pragma once |
|||
|
|||
#include <stdbool.h> |
|||
#include <ctype.h> |
|||
|
|||
//#define HASH_BLOOM 20
|
|||
#define HASH_NONFATAL_OOM 1 |
|||
#define HASH_FUNCTION HASH_BER |
|||
#include "uthash.h" |
|||
|
|||
typedef struct strpool { |
|||
char *str; /* key */ |
|||
UT_hash_handle hh; /* makes this structure hashable */ |
|||
} strpool; |
|||
|
|||
void StrPoolDestroy(strpool **p); |
|||
bool StrPoolAddStr(strpool **pp,const char *s); |
|||
bool StrPoolCheckStr(strpool *p,const char *s); |
File diff suppressed because it is too large
Loading…
Reference in new issue