944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2990
  • C++ RSS
Mar 28th, 2006
0

C++hash table lookin' 4 help~~

Expand Post »
hey, guys & experts
i'm havin' a big pro now~~i can't past the test 3 now...
i know i may hav a bug but seems too hard for me to find it out
can anyone help me???seg fault... i hav asked many ppl but....plzzzz


====the file====

dict.cc
#include "dict.h"
#include <iostream>
#include <cmath>
using namespace std;

dict_t::dict_t() {
n=0;
m = 5;
table = new record_t[m];
for(int i=0;i<m;i++)
{table[i].key="";
table[i].value=0;
}

}

int dict_t::hash(const string &key) const {
float e=0;
long long code=0;
int j;
int k;
j=key.length();

while(j>0){
code=code+int(key[j-1])* pow(128,e);
j--;
e++;
}
k=code%m;
if(table[k].key==""||table[k].key=="DEL")
return k;

else
for(int i=1;i+k<m;i++){
if(table[k+i].key==""||table[k+i].key=="DEL")
return k+i;}
int t;
for(t=0;t<k;t++)
if(table[t].key==""||table[t].key=="DEL")
return t;
return -1;}


int dict_t::get(const string &key) const {
// do the search
for(int i=0;i<m;i++)
{
if(table[i].key==key)
return table[i].value;}
return -1;
}

void dict_t::set(const string &key, int value) {
int i;
i=dict_t::hash(key);

table[i].key=key;
table[i].value=value;
n++;
double a=n/m;
if(a<=0.3&&m>=10)rehash(m/2);
if(a>=0.8&&m>=5)rehash(m*2);

}

void dict_t::remove(const string &key) {
for(int i=0;i<m;i++)
{
if(table[i].key==key)//search first
{table[i].key="DEL";
table[i].value=0;
n--;
double a=n/m;
if(a<=0.3&&m>=10)rehash(m/2);
if(a>=0.8&&m>=5)rehash(m*2);
return;}
}}

void dict_t::rehash(int new_size) {

p=table;

table=new record_t[new_size];
for(int s=0;s<new_size;s++)
{table[s].key="";
table[s].value=0;
}
int original_size=m;
m=new_size;

for(int i=0;i<original_size;i++)
{
if(p[i].key!=""&&p[i].key!="DEL"){
int g;
g=dict_t::hash(p[i].key);
table[g].key=p[i].key;
table[g].value=p[i].value;
}
}
p=NULL;
delete p;

}
head==>
#ifndef __DICT_H // to prevent multiple definition
#define __DICT_H

#include <string>
using std::string; // import the symbol 'string' from 'std::string'

// a dictionary from string to positive integer
class dict_t {
public:
dict_t();

// return the hash table size
unsigned int slots() const { return m; }

// return the number of elements stored
unsigned int size() const { return n; }

// return a non-negative integer hash value of a key
int hash(const string &key) const;

// return the value associated with the key
// return -1 if the key has no associated value
int get(const string &key) const;

// set a new value for the key
// overwrite the old value, if any
void set(const string &key, int value);

// remove the key and its associated value
// do nothing if the key does not exist
void remove(const string &key);

private:
// resize the hash table to a new size
void rehash(int new_size);

int m; // number of slots
int n; // number of elements stored

// a table of m records, n of which storing real data
struct record_t {
string key;
int value;
record_t() : key(""), value(0) {} // default values
} *table,*p;

// extra variables here, if any
};

#endif
test 3(>0<!!)
#include "dict.h"
#include <iostream>
#include <cstdlib>
#include <map>
using namespace std;

// Generating collission
int main(int argc, char *argv[]) {
dict_t d;

// Generate a pair of collission v1 and v2
typedef map<int, string> seen_t;
seen_t seen;
srand(time(0)); // seed prng by current time
string v1, v2;
while (true) {
string s = "";
for (int i = 0; i < 6; ++i) {
s += 'A' + rand() % 26; // random alphabet
}
int h = d.hash(s);

if (seen.find(h) != seen.end()) { // found
v1 = seen.find(h)->second;
v2 = s;
break;
}
seen[h] = s;

d.set(s, 10 + seen.size());
assert(d.size() == seen.size());
assert(d.slots() < 10 || d.slots() < d.size() * 4);
}

d.set(v1, 8);

d.set(v2, 9);
d.remove(v1);
assert(d.get(v1) == -1);

assert(d.get(v2) == 9);
assert(d.size() == seen.size());

for (seen_t::const_iterator it = seen.begin(); it != seen.end(); ++it) {
if (it->second != v2) d.remove(it->second);
}
assert(d.get(v1) == -1);
assert(d.get(v2) == 9);
assert(d.size() == 1);
assert(d.slots() < 10 || d.slots() < d.size() * 4);

cout << argv[0] << ": Test case passed." << endl;
}
i'm using Dev-C++ latest version
really appreciate it if you can help me~~
thx ^_^
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tiffani is offline Offline
4 posts
since Mar 2006
Mar 28th, 2006
0

Re: C++hash table lookin' 4 help~~

First, format your code so that it's readable. Then post the code using code tags so that the formatting isn't lost. Finally, give way more information than "it doesn't work" and "seg fault". Despite how it may seem, we're not here to be your testers and debuggers. Please consider that many of us are professionals that are busy with our own problems, and you need to help us to help you.

That said, a segmentation fault is caused by accessing memory outside of your address space. With a hash table, and it looks like you aren't using separate chaining, that means you've got an invalid index somewhere.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 28th, 2006
0

Re: C++hash table lookin' 4 help~~

sorry....but still thx all the same
it's done
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tiffani is offline Offline
4 posts
since Mar 2006
Mar 28th, 2006
0

Re: C++hash table lookin' 4 help~~

>it's done
And what was the problem?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to compile and run this C source!!!!
Next Thread in C++ Forum Timeline: calling >> of one struct from >> in another struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC