本文整理汇总了C++中HashTable::hash方法的典型用法代码示例。如果您正苦于以下问题:C++ HashTable::hash方法的具体用法?C++ HashTable::hash怎么用?C++ HashTable::hash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable::hash方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: put
void put(HashTable** htPtrPtr,void* value){
HashTable* htPtr = *htPtrPtr;
HashTable ht = *htPtr;
int key = ht.hash(value, ht.size)%ht.size;
if(key<0)
key+=ht.size;
void* val = ht.table[key].value;
if(get(&ht,value)!=NULL){
return;
}
if(((double)(ht.elem+1)/ht.size)>ht.alpha){
*htPtrPtr = rehash(htPtr);
htPtr = *htPtrPtr;
ht = *htPtr;
}
htPtr->elem = htPtr->elem + 1;
if(val==NULL){
ht.table[key].value = value;
ht.table[key].next = NULL;
ht.table[key].key = key;
}
else{
Entry* ent = &(ht.table[key]);
while(ent->next!=NULL){
ent = ent->next;
}
ent->next = (Entry*)calloc(1,sizeof(Entry));
(ent->next)->value = value;
(ent->next)->key = key;
}
}
示例2:
HashNode
removehashnode(HashTable ht, char *nam)
{
unsigned hashval;
HashNode hp, hq;
hashval = ht->hash(nam) % ht->hsize;
hp = ht->nodes[hashval];
/* if no nodes at this hash value, return NULL */
if (!hp)
return NULL;
/* else check if the key in the first one matches */
if (!strcmp(hp->nam, nam)) {
ht->nodes[hashval] = hp->next;
ht->ct--;
return hp;
}
/* else run through the list and check the rest of the keys */
hq = hp;
hp = hp->next;
for (; hp; hq = hp, hp = hp->next) {
if (!strcmp(hp->nam, nam)) {
hq->next = hp->next;
ht->ct--;
return hp;
}
}
/* else it is not in the list, so return NULL */
return NULL;
}
示例3: expandhashtable
HashNode
addhashnode2(HashTable ht, char *nam, void *nodeptr)
{
unsigned hashval;
HashNode hn, hp, hq;
hn = (HashNode) nodeptr;
hn->nam = nam;
hashval = ht->hash(hn->nam) % ht->hsize;
hp = ht->nodes[hashval];
/* check if this is the first node for this hash value */
if (!hp) {
hn->next = NULL;
ht->nodes[hashval] = hn;
if (++ht->ct >= ht->hsize * 2 && !ht->scan)
expandhashtable(ht);
return NULL;
}
/* else check if the first node contains the same key */
if (ht->cmpnodes(hp->nam, hn->nam) == 0) {
ht->nodes[hashval] = hn;
replacing:
hn->next = hp->next;
if(ht->scan) {
if(ht->scan->sorted) {
HashNode *tab = ht->scan->u.s.tab;
int i;
for(i = ht->scan->u.s.ct; i--; )
if(tab[i] == hp)
tab[i] = hn;
} else if(ht->scan->u.u == hp)
ht->scan->u.u = hn;
}
return hp;
}
/* else run through the list and check all the keys */
hq = hp;
hp = hp->next;
for (; hp; hq = hp, hp = hp->next) {
if (ht->cmpnodes(hp->nam, hn->nam) == 0) {
hq->next = hn;
goto replacing;
}
}
/* else just add it at the front of the list */
hn->next = ht->nodes[hashval];
ht->nodes[hashval] = hn;
if (++ht->ct >= ht->hsize * 2 && !ht->scan)
expandhashtable(ht);
return NULL;
}
示例4:
mod_export HashNode
gethashnode2(HashTable ht, char *nam)
{
unsigned hashval;
HashNode hp;
hashval = ht->hash(nam) % ht->hsize;
for (hp = ht->nodes[hashval]; hp; hp = hp->next) {
if (ht->cmpnodes(hp->nam, nam) == 0)
return hp;
}
return NULL;
}
示例5: expandhashtable
void
addhashnode(HashTable ht, char *nam, void *nodeptr)
{
unsigned hashval;
HashNode hn, hp, hq;
hn = (HashNode) nodeptr;
hn->nam = nam;
hashval = ht->hash(hn->nam) % ht->hsize;
hp = ht->nodes[hashval];
/* check if this is the first node for this hash value */
if (!hp) {
hn->next = NULL;
ht->nodes[hashval] = hn;
if (++ht->ct == ht->hsize * 2)
expandhashtable(ht);
return;
}
/* else check if the first node contains the same key */
if (!strcmp(hp->nam, hn->nam)) {
hn->next = hp->next;
ht->nodes[hashval] = hn;
ht->freenode(hp);
return;
}
/* else run through the list and check all the keys */
hq = hp;
hp = hp->next;
for (; hp; hq = hp, hp = hp->next) {
if (!strcmp(hp->nam, hn->nam)) {
hn->next = hp->next;
hq->next = hn;
ht->freenode(hp);
return;
}
}
/* else just add it at the front of the list */
hn->next = ht->nodes[hashval];
ht->nodes[hashval] = hn;
if (++ht->ct == ht->hsize * 2)
expandhashtable(ht);
}
示例6:
mod_export HashNode
gethashnode(HashTable ht, const char *nam)
{
unsigned hashval;
HashNode hp;
hashval = ht->hash(nam) % ht->hsize;
for (hp = ht->nodes[hashval]; hp; hp = hp->next) {
if (ht->cmpnodes(hp->nam, nam) == 0) {
if (hp->flags & DISABLED)
return NULL;
else
return hp;
}
}
return NULL;
}
示例7: if
mod_export HashNode
removehashnode(HashTable ht, char *nam)
{
unsigned hashval;
HashNode hp, hq;
hashval = ht->hash(nam) % ht->hsize;
hp = ht->nodes[hashval];
/* if no nodes at this hash value, return NULL */
if (!hp)
return NULL;
/* else check if the key in the first one matches */
if (ht->cmpnodes(hp->nam, nam) == 0) {
ht->nodes[hashval] = hp->next;
gotit:
ht->ct--;
if(ht->scan) {
if(ht->scan->sorted) {
HashNode *tab = ht->scan->u.s.tab;
int i;
for(i = ht->scan->u.s.ct; i--; )
if(tab[i] == hp)
tab[i] = NULL;
} else if(ht->scan->u.u == hp)
ht->scan->u.u = hp->next;
}
return hp;
}
/* else run through the list and check the rest of the keys */
hq = hp;
hp = hp->next;
for (; hp; hq = hp, hp = hp->next) {
if (ht->cmpnodes(hp->nam, nam) == 0) {
hq->next = hp->next;
goto gotit;
}
}
/* else it is not in the list, so return NULL */
return NULL;
}
示例8: get
void* get(HashTable* htPtr,void* value){
HashTable ht = *htPtr;
if(value==NULL)
return NULL;
int key = ht.hash(value, ht.size)%ht.size;
void* val = ht.table[key].value;
if(val==NULL){
return NULL;
}
else{
Entry* ent = &(ht.table[key]);
while(ent!=NULL){
if(ht.compare(ent->value,value)){
return ent->value;
}
ent = ent->next;
}
return NULL;
}
}
示例9: Item
TEST_CASE ("hash function and hash table functionality") {
Item items[15] = {Item("apple"), Item("banana"), Item("grape"), Item("lemon"),
Item("melon"), Item("orange"), Item("strawberry"),
Item("pear"), Item("blueberry"), Item("grapefruit"),
Item("apple"), Item("banana"), Item("grape"), Item("lemon"),
Item("melon")};
HashTable ht;
CHECK(ht.size() == 0);
CHECK(ht.printDetail() ==
"Items in hash table: 0\n[0]\t\n[1]\t\n[2]\t\n[3]\t\n[4]\t\n");
CHECK(ht.hash(items[0]) == 0);
CHECK(ht.hash(items[4]) == 4);
CHECK(ht.hash(items[8]) == 2);
CHECK(ht.hash(items[12]) == 2);
CHECK(ht.hash(items[14]) == 4);
for (Item i : items) {
ht.insert(i);
}
CHECK(ht.size() == 10);
stringstream output;
output << "Items in hash table: 10\n" << "[0]\t apple (2) \n";
output << "[1]\t orange (1) grapefruit (1) \n" << "[2]\t grape (2) blueberry (1) \n";
output << "[3]\t\n";
示例10: code
void MainWindow::code()
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(this,"Error","Can't open the file!");
return;
}
QTextStream in(&file);
QStringList text;
int i=0;
while(!in.atEnd())
{
if(i==1)
text.append(in.readLine());
else
in.readLine();
i++;
if(i==4)
i=0;
}
//qDebug()<<text;
file.close();
HashTable *hashTable = new HashTable;
unsigned int FirPrefix[1048576];
unsigned int count=0;
for(i=0;i<text.length();i++)
{
for(int j=0;j<65;j++)
{
hashTable->addTable(text[i].mid(j,24));
}
}
/*for(i=0;i<1048576;i++)
{
hashTable->locate(i);
if(hashTable->getFreInRead()!=0)
{
FirPrefix[count]=i;
count++;
}
}
unsigned int countFreInRead[9];
for(i=0;i<9;i++)
countFreInRead[i]=0;
for(unsigned int i=0;i<count;i++)
{
hashTable->locate(FirPrefix[i]);
unsigned short freInRead=hashTable->getFreInRead();
//qDebug()<<freInRead<<endl;
for(int j=10;j<=90;j+=10)
{
if(freInRead<=j&&(freInRead>j-10))
{
countFreInRead[j/10-1]++;
//qDebug()<<"countFreInRead["<<j/10-1<<"]="<<countFreInRead[j/10-1];
}
}
}
for(i=0;i<9;i++)
{
qDebug()<<i*10<<"~"<<(i+1)*10<<" "<<countFreInRead[i]<<endl;
} //研究发现,fre 值在 1~30 之间分布较集中*/
/*qDebug()<<"--------------------------"<<endl;
hashTable->locate(955561);
hashTable->getFirPrefix();*/
for(i=0;i<1048576;i++)
{
hashTable->locate(i);
if(hashTable->getFreInRead()<=30&&hashTable->getFreInRead()>20)
{
FirPrefix[count]=i;
//qDebug()<<FirPrefix[count]<<endl;
count++;
}
}
qDebug()<<"--------------------------------";
QTime t;
t.start();
//.........这里部分代码省略.........