Beispiel #1
0
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;
	}
}
Beispiel #2
0
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
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 (!strcmp(hp->nam, nam))
	    return hp;
    }
    return NULL;
}
Beispiel #6
0
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);
}
Beispiel #7
0
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;
}
Beispiel #8
0
HashNode
gethashnode(HashTable ht, char *nam)
{
    unsigned hashval;
    HashNode hp;

    hashval = ht->hash(nam) % ht->hsize;
    for (hp = ht->nodes[hashval]; hp; hp = hp->next) {
	if (!strcmp(hp->nam, nam)) {
	    if (hp->flags & DISABLED)
		return NULL;
	    else
		return hp;
	}
    }
    return NULL;
}
Beispiel #9
0
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;
}
Beispiel #10
0
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;
	}
}


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";
Beispiel #12
0
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();


    QStringList contigList;
    for(unsigned int i=0;i<count;i++)
    {

        //qDebug()<<"for"<<endl;
        hashTable->locate(FirPrefix[i]);
        //qDebug()<<FirPrefix[i]<<endl;
        if(hashTable->getFreInContig()==1)
            continue;

        QString contig;
        contig+=hashTable->getFirPrefix();
        contig+=hashTable->getSecPrefix();
        while(hashTable->getFreInRead()!=0&&hashTable->getFreInContig()!=1)
        {

            //qDebug()<<"while"<<endl;
            hashTable->setFreInContig();


            contig+=hashTable->getPostfix();


            //qDebug()<<contig<<endl;


            unsigned int next = hashTable->hash(contig.mid(contig.length()-20,10));

            //qDebug()<<next<<endl;
            hashTable->locate(next);

        }
        contigList.append(contig);

    }
    int time = t.elapsed();




    QString outputFileName(fileName);
    outputFileName+=".assembly";
    QFile outputFile(outputFileName);


    if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::information(this,"Error","Can't create the assembly file!");
        return;
    }
    QTextStream out(&outputFile);



    unsigned int totalLength=0;
    unsigned int maximalLength=0;
    unsigned int length[contigList.length()];
    unsigned int medianLength;

    unsigned int N50Length;
    for(unsigned int i=0;i<contigList.length();i++)
    {
        out<<contigList[i]<<endl;
        totalLength+=contigList[i].length();
        length[i]=contigList[i].length();
        if(contigList[i].length()>maximalLength)
            maximalLength=contigList[i].length();

    }
    unsigned int meanLength = totalLength/contigList.length();
    std::sort(length,length+contigList.length());
    for(unsigned int i=0;i<contigList.length();i++)
        //qDebug()<<length[i]<<endl;

    if(contigList.length()%2==0)
        medianLength = (length[contigList.length()/2]+length[contigList.length()/2-1])/2;
    else
        medianLength = length[contigList.length()/2];

    unsigned int sum=0;
    //qDebug()<<length[contigList.length()-1];
    for(unsigned int i=contigList.length()-1;i>=0;i--)
    {
        //qDebug()<<"for";
        sum=length[i]+sum;
        //qDebug()<<i;
        if(sum>=totalLength/2)
        {
            N50Length = length[i];
            break;
        }
    }



    rowCount++;

    table->setRowCount(rowCount);
    table->setItem(table->rowCount()-1,0,new QTableWidgetItem(QString::number(rowCount)));
    table->setItem(table->rowCount()-1,1,new QTableWidgetItem(QString::number(contigList.length())));
    table->setItem(table->rowCount()-1,2,new QTableWidgetItem(QString::number(totalLength)));
    table->setItem(table->rowCount()-1,3,new QTableWidgetItem(QString::number(maximalLength)));
    table->setItem(table->rowCount()-1,4,new QTableWidgetItem(QString::number(N50Length)));
    table->setItem(table->rowCount()-1,5,new QTableWidgetItem(QString::number(meanLength)));
    table->setItem(table->rowCount()-1,6,new QTableWidgetItem(QString::number(medianLength)));
    table->setItem(table->rowCount()-1,7,new QTableWidgetItem(QString::number(time)));



     QString dir("ID ");
     dir+=QString::number(rowCount);
     dir+="  finished assembling  ";
     dir+="<a href = ";
     dir += outputFileName;
     dir += ">";
     dir += outputFileName;
     dir +="</a>";
     QLabel *fileDir = new QLabel(dir);
     connect(fileDir,SIGNAL(linkActivated(QString)),this,SLOT(openCodeFile(QString)));

     mainLayout->addWidget(fileDir);

     QMessageBox::information(this,"Success","Assemble success!");





}