Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

c++ Simple Class Question

Name: Anonymous 2007-06-27 0:40 ID:CjAL5fOI

I have a class called htable, when it reaches a certain size it will rehash itself. Heres the pseudo code:

/* Creates new table, and deletes old one. */
Htable Htable::htable_rehash(){
   make new Htable;
   add old values to new table;
        delete old table;
   return new Htable;
}

to call it I would say hashtable = hashtable .htable_rehash();

My question is this: do i need to free the memory in the old hash table in the class, or will it be deleted when i say table = table.rehash?

Name: Anonymous 2007-06-30 15:15 ID:4ZOB4mgS

IGNORE THE 2 S.

Name: Anonymous 2007-06-30 15:18 ID:gDMKl3mZ

>>39

SHIT CODE YOU FAGGOT

(Map_step(position) % capacity, true) <- WHAT THE FUCK IS THIS YOU IDIOT


int Map::insert(std::string key, std::string value)

{

    // Rehash if we are too full

    if(percent_full() >= REHASH_PERCENT)

        rehash();



    unsigned int position = hash(key)%capacity;



    if(insert_key(key, position))

        return frequencies2[position];



    // There is another key at this position   

    for(unsigned int scanpos=Map_step(position) % capacity; scanpos!=(position); (Map_step(position) % capacity, true))

        if(insert_key(key, position))

            return frequencies2[position];



    return 0;

}



// Returns false if there is another key at that position.

bool Map::insert_key(std::string key, unsigned int position)

{

    if(frequencies[position]==0)

    {

        itemcount++;

        frequencies2[position]=1;

        keys2[position]=key;

        return true;

    }

   

    if(keys2[position]==key)

    {

        frequencies2[position]++;

        return true;

    }

    return false;

}

Name: Anonymous 2007-06-30 18:22 ID:p8vbW2se

(Map_step(position) % capacity, true)
I'm guessing it's magic.
No, probably the guy wrote it as a while, then changed it to a for, and something mixed up.
Anyway that code is shit.

Name: Anonymous 2007-06-30 18:36 ID:hvxu1n6E

ITT, C/C++ and STL amateurs.
Fun fact: most implementations of std::map are actually self-balanching binary search trees, instead of true hash tables.

Name: Anonymous 2007-06-30 19:05 ID:gDMKl3mZ

>>43
I AGREE THE CODE IS SHIT

Name: Anonymous 2007-07-01 7:41 ID:7UC38q+h

>>42 That syntax is valid, what is your problem - never seen it before newbie?

>>43 Yes, in the ops it was a while loop - the purpose of the code was to demonstrate what real c++ could look like, I wasn't concerned about if it actually worked, if it doesn't any changes would be pretty minor.

Overall it's about 20 less lines than the ops, more readable and less error-prone. CODE IS NOT SHIT LOSERS.

If you think it is, rewrite the OPs insert function (in C++) and show me better.

Name: Anonymous 2007-07-01 9:36 ID:VR5aYA7A

>>44
interesting, that would result in a slow down if it got big.

If i remember correctly if you wanted to compile the program in g++ you needed some compiler options:

gcc -W -Wall -ansi -pedantic *.c adt\*.c -o adt

Don't have any idea what they are. They worked on my C program, haven't tested it with my c++ code

>>46
Thanks for your code, but you could have stated it was pseudo =P

I will try to recode it using C++ vectors, but not the namespace std. It's just not my coding style

Name: Anonymous 2011-02-03 6:12

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List