Search Members Help

» Welcome Guest
[ Log In :: Register ]

Page 1 of 212>>

[ Track This Topic :: Email This Topic :: Print this topic ]

reply to topic new topic new poll
Topic: A Topic< Next Oldest | Next Newest >
 Post Number: 1
StreetRaver Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 93
Joined: Aug. 2001
PostIcon Posted on: Oct. 12 2001,15:13  Skip to the next post in this topic. Ignore posts   QUOTE

So, What does everyone want to talk about?

------------------
Take XTC, Then you wake up in the morning wondering "Wheres My Underwear?"

This message has been edited by StreetRaver on October 13, 2001 at 03:19 PM

Offline
Top of Page Profile Contact Info 
 Post Number: 2
StreetRaver Search for posts by this member.
FNG
Avatar



Group: Members
Posts: 93
Joined: Aug. 2001
PostIcon Posted on: Oct. 12 2001,15:14 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

To*, Damn I can't spell

------------------
Take XTC, Then you wake up in the morning wondering "Wheres My Underwear?"

Offline
Top of Page Profile Contact Info 
 Post Number: 3
DeadAnztac Search for posts by this member.
I am almost one of Us.
Avatar



Group: Members
Posts: 0
Joined: Feb. 2001
PostIcon Posted on: Oct. 12 2001,15:28 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

Templated Linked Lists. I'm having trouble making an efficent array system here. My goal was to make a define on use, dynamic sized array class here. In other words on the outside it has all the ease of use of an array (like the [ ] operator) but it really has a dynamic inside. See my plan was to make the index a linked list with pointers to dynamic typed data. Another thing this could help is that the index would be independent of the numbers, so, like a[5] could be the first index, a[1] could be the second. I was also going to add in string indexing as well, like a["foo"], but I want to get this other stuff down first. It's rather complicated, does anyone have a tutorial on templated classes that might help?

Edit: In other words PHP like arrays.

/Thus endeth Anztac's idea for a discussion./
------------------
~Anztac [ I'm just this guy, you know? ]

This message has been edited by DeadAnztac on October 13, 2001 at 10:30 AM

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 4
Beldurin Search for posts by this member.
Mayor of Detnet
Avatar



Group: Members
Posts: 1242
Joined: Aug. 2001
PostIcon Posted on: Oct. 12 2001,15:58 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

Disclaimer: it's been a couple of years since I've had a real programming class.

But if you want to use the [] operator, you would either have to decide on some kind of regular expression to use as your index value( i.e. a[#0] or some other kind of reserved character so that you could do a string or integer search within), or else go w/out a true index.

Also, if you made it string-indexable such as a["foo"], your search times (for large, arrays) are going to approach the mythic O unless you force the arrays to be ordered, arent't you?

------------------

quote:
Originally posted by Dark-Angel99:
How come {name removed} doesn't like you? I find you really funny :D


Never argue with an idiot...he may be doing the same thing

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 5
veilside Search for posts by this member.
can i borrow a feeling?
Avatar



Group: Members
Posts: 209
Joined: Jul. 2001
PostIcon Posted on: Oct. 12 2001,16:36 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

quote:
Originally posted by StreetRaver:
To*, Damn I can't spell


that's what the edit button's for.
Offline
Top of Page Profile Contact Info 
 Post Number: 6
damien_s_lucifer Search for posts by this member.
Emperor of Detnet
Avatar



Group: Members
Posts: 33
Joined: Jan. 1970
PostIcon Posted on: Oct. 12 2001,19:29 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

quote:
Originally posted by Beldurin:
Also, if you made it string-indexable such as a["foo"], your search times (for large, arrays) are going to approach the mythic O unless you force the arrays to be ordered, arent't you?

Not if you use a hash, which is probably what you want in the first place. They use more memory than arrays but they're a *lot* faster - with a decent implementation, retrieval from an arbitrarily large hash happens in constant time, i.e. retrieving a value from a 1 bazillion element hash happens just as quickly as retrieving a value from a 10 element hash. w00t!

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 7
miNus Search for posts by this member.
PH34R M3
Avatar



Group: Members
Posts: 859
Joined: Mar. 2001
PostIcon Posted on: Oct. 12 2001,19:50 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

quote:
Originally posted by damien_s_lucifer:
hash
*snip*
hash
*snip*
hash
*snip
hash. w00t!

Dear God, is that ALL you think about!!??!?

...

I took a C++ class in high school, just to get started off with it, and we got just as far as arrays. I never really learned about pointers. Half the class could barely open up Microsoft Visual C++, so it was a VERY slowly progressing class.

Get your mind off of the drugs damien!

------------------
"Take a cable and simulate a sex act on each end. Press some combonation of play and record buttons." - askheaves

Offline
Top of Page Profile Contact Info 
 Post Number: 8
Beldurin Search for posts by this member.
Mayor of Detnet
Avatar



Group: Members
Posts: 1242
Joined: Aug. 2001
PostIcon Posted on: Oct. 13 2001,02:26 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

Stupid me forgot about hashes. Had a 200-level college data structures class that we dealt briefly with them, but since the prof couldn't teach worth a fuck, noone really got them. All I remember is that we had to put the American Heritage Dictionary into a hash and then spell check the first two books of the Bible with it.

And get this...there were errors...sheeh...you'd think God could spell.

P.S. It's a joke...don't flame me for it

------------------

quote:
Originally posted by Dark-Angel99:
How come {name removed} doesn't like you? I find you really funny :D


Never argue with an idiot...he may be doing the same thing

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 9
damien_s_lucifer Search for posts by this member.
Emperor of Detnet
Avatar



Group: Members
Posts: 33
Joined: Jan. 1970
PostIcon Posted on: Oct. 13 2001,04:59 Skip to the previous post in this topic. Skip to the next post in this topic. Ignore posts   QUOTE

quote:
Originally posted by miNus:
Dear God, is that ALL you think about!!??!?

Yes. And thanks for calling me God

DeadAnztac, implementing a hash is fairly straightforward. Essentially, you create an array of known size whose elements are a simple struct (forgive my C syntax, I'm pretty rusty) :

struct hashrec
{
char key[32];
void* data;
}

Then you define a hashing function :

unsigned int hash_func(char* key)
{
int keylen,genlen,count,return_value;
return_value = 0;
keylen = strlen(key);

// sum up all the characters in our key
for (count=0; count<keylen; count++)
{
return_value += key[count];
}
// return the modulus of our sum and our array size. For efficient hashing,
// HASH_ARRAY_SIZE should be a prime number
return_value = return_value \% HASH_ARRAY_SIZE;
return return_value;
}

Your array should be as large as the *maximum* value your hash function will ever return. For the function above, it will be HASH_ARRAY_SIZE - 1 :

struct hashrec hash_array[HASH_ARRAY_SIZE -1];

I'm just going to pseudo-code retrieval :

int hash_retrieve(char *key, char *data)
{
// figure out what our hash value is
int hash_array_offset = hash_func(key);

// Is our hashed struct null?
if (hash_array[hash_array_offset].key == NULL)
{
// yes, return failure
return 0;
}

// no. see if keys match
if (strcmp(hash_array[hash_array_offset].key,key) == 0)
{
// we found it. Copy out our data and return success.
copy_data(hash_array[hash_array_offset],data); // you'll have to define this
return 1;
}

// No match. Fall back on one of many collision resolution schemes.
}

Insertion is almost the same as retrieval. I'll leave it to you to figure out how to do it.

The only tricky part is collision resolution. With the hash function listed above, the keys 'fuck you' and 'you fuck' will both produce the same hash value, so if you try and insert those two keys into the hash you'll end up with a collision.

There are a lot of ways to resolve this - my favorite is to expand your hash_struct so it can become a linked list. After hashing, you search thru the list until you find a match or run off the end of the list.

Obviously there's more to hash implementation than just the above, but that should give you an idea of how they work at a fundamental level. Have fun!

This message has been edited by damien_s_lucifer on October 14, 2001 at 12:01 AM

Offline
Top of Page Profile Contact Info WEB 
 Post Number: 10
damien_s_lucifer Search for posts by this member.
Emperor of Detnet
Avatar



Group: Members
Posts: 33
Joined: Jan. 1970
PostIcon Posted on: Oct. 13 2001,07:55 Skip to the previous post in this topic.  Ignore posts   QUOTE

I'm not sure what you mean by just hashing the indeces... do you mean storing an index into an array of pointers? If so, you're adding an unnecessary layer of indirection, because you'd have to :

- perform the hash lookup and get the index
- use the index to retrieve a pointer from your array
- then dereference the pointer to get the data

If you're storing the data itself in the array, that's a little different... but then you have the problem of having a fixed limit on how much data you can store per key.

Either way, though, inserts are going to slow down drastically as things are added and removed from the array, since you'll pretty much have to start at the beginning of the array and hunt until you find an empty slot, thereby negating a lot of the benefit you'd get from a hash.

Better to just alloc the memory you need during an insert, and store the data pointer in the hash itself. Free the memory on a delete.

As far as autogenerating enumerated data types, you'll have to use strings for that. The compiler's going to balk any time it sees something like

my_hash[foo] = "bar";

if you haven't declared foo somewhere else *and* made your class use the enum. But it won't care about

my_hash["foo"] = "bar";

as long as you've overloaded "foo" properly, i.e. as taking a char*.

A really good hash class should be able to handle

my_hash[char*] = char*
my_hash[int] = char*
my_hash[float] = char*

So that you can use arbitrary keys to store arbitrary data (you can store just about anything in a char* if you're careful )

<advocacy>
All of this crap is why I went to Perl in the first place. Weakly typed data + native lists, hashes, and regular expressions make it a programmer's dream.
</advocacy>

Offline
Top of Page Profile Contact Info WEB 
15 replies since Oct. 12 2001,15:13 < Next Oldest | Next Newest >

[ Track This Topic :: Email This Topic :: Print this topic ]


Page 1 of 212>>
reply to topic new topic new poll

» Quick Reply A Topic
iB Code Buttons
You are posting as:

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code