Loading...
Searching...
No Matches
htable.cc
Go to the documentation of this file.
/****************************************
* Computer Algebra System SINGULAR *
****************************************/
#include "kernel/mod2.h"
#include "resources/hash_me.h"
#include "reporter/reporter.h"
#include "Singular/htable.h"
stablerec* t_createTable(int s)
{
stablerec* t=(stablerec*)omAlloc(sizeof(stablerec));
t->max=s;
t->t=(telem*)omAlloc0(s*sizeof(telem));
t->ref=1;
return t;
}
void t_destroyTable(stablerec* t)
{
t->ref--;
if (t->ref<=0)
{
for(int i=t->max-1;i>=0;i--)
{
telem p=t->t[i];
while(p!=NULL)
{
omFree(p->key);
p->val.CleanUp();
telem pp=p;
p=p->next;
omFreeSize(pp,sizeof(stelem));
}
}
omFreeSize(t->t,t->max*sizeof(telem));
omFreeSize(t,sizeof(stablerec));
}
}
stablerec* copyTable(stablerec* t)
{
t->ref++;
return t;
}
char* stringTable(stablerec* t)
{
StringSetS("table:\n");
char *s;
for(int i=t->max-1;i>=0;i--)
{
telem p=t->t[i];
while(p!=NULL)
{
StringAppendS(p->key);
StringAppendS(" -> ");
s=p->val.String();
StringAppendS(s);
omFree(s);
StringAppendS("\n");
telem pp=p;
p=p->next;
}
}
return StringEndS();
}
/// find the entry to key s
telem t_findTable(stablerec* t,const char *s)
{
uint32_t h=hashlittle(s,strlen(s));
h=h%t->max;
telem p=t->t[h];
while(p!=NULL)
{
if (strcmp(s,p->key)==0) return p;
p=p->next;
}
return NULL;
}
/// find the data to key s
leftv t_findTabelVal(stablerec* t,const char *s)
{
telem p=t_findTable(t,s);
if (p==NULL) return NULL;
return &(p->val);
}
/// add a new entry (key s, data v) to table t, eats s, copies v
void t_addTable(stablerec* t,char *s, leftv v)
{
uint32_t h=hashlittle(s,strlen(s));
telem p=(telem)omAlloc(sizeof(*p));
p->next=NULL;
p->key=s;
p->val.Init();
p->val.rtyp=v->Typ();
p->val.data=v->CopyD();
p->hash=h;
h=h%t->max;
p->next=t->t[h];
t->t[h]=p;
}
//-------------------------------------------------------
void htable_Print(stablerec *d)
{
stablerec* lt=(stablerec*)d;
char* s=stringTable(lt);
PrintS(s);
omFree(s);
int cnt=0;int cnt2=0;
for(int i=0;i<lt->max;i++)
{
if (lt->t[i]!=NULL)
{
cnt++;
telem p=lt->t[i];
while(p!=NULL) { cnt2++;p=p->next;}
}
}
Print("%d columns, %d entries, size:%d",cnt,cnt2,lt->max);
}
pp
CanonicalForm FACTORY_PUBLIC pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f ).
Definition cf_gcd.cc:676
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
Print
#define Print
Definition emacs.cc:80
s
const CanonicalForm int s
Definition facAbsFact.cc:51
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
hashlittle
uint32_t hashlittle(const void *key, size_t length)
Definition hash_me.c:68
hash_me.h
t_destroyTable
void t_destroyTable(stablerec *t)
Definition htable.cc:20
stringTable
char * stringTable(stablerec *t)
Definition htable.cc:48
t_createTable
stablerec * t_createTable(int s)
Definition htable.cc:11
htable_Print
void htable_Print(stablerec *d)
Definition htable.cc:108
t_findTabelVal
leftv t_findTabelVal(stablerec *t, const char *s)
find the data to key s
Definition htable.cc:85
t_addTable
void t_addTable(stablerec *t, char *s, leftv v)
add a new entry (key s, data v) to table t, eats s, copies v
Definition htable.cc:93
t_findTable
telem t_findTable(stablerec *t, const char *s)
find the entry to key s
Definition htable.cc:71
copyTable
stablerec * copyTable(stablerec *t)
Definition htable.cc:42
htable.h
stablerec::t
telem * t
Definition htable.h:24
stablerec::max
int max
Definition htable.h:25
stablerec::ref
int ref
Definition htable.h:26
stablerec
Definition htable.h:23
stelem
Definition htable.h:15
h
STATIC_VAR Poly * h
Definition janet.cc:971
mod2.h
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omFree
#define omFree(addr)
Definition omAllocDecl.h:261
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
StringSetS
void StringSetS(const char *st)
Definition reporter.cc:128
StringAppendS
void StringAppendS(const char *st)
Definition reporter.cc:107
PrintS
void PrintS(const char *s)
Definition reporter.cc:288
StringEndS
char * StringEndS()
Definition reporter.cc:151
reporter.h
leftv
sleftv * leftv
Definition structs.h:53