How to code and use commonly used algorithms

Started by
65 comments, last by Nice Coder 19 years, 1 month ago
it takes the hsh modulo the tablesize, and then oges through the array to find the full hash. this makes it work for things that don't collide, but with only a key, its a little hard to get it to do more then that.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Advertisement
A memory manager.

Resons for use: It stops system allocations/deallocations.

Psudocode:
Function Acessor(Pointer Memory, Id)Check Records for all memory ID can acessIf id cannot acess Memory, return falurereturn Memoryend functionFunction Allocate(int Howmany, id)Find a spot where there are Howmany items Free.If there are not, then call reallocate until there is.Mark each of those elements as belonging to IDReturn a pointer to the memoryend functionFunction Deallocate(Pointer Memory, int howmany, iD)If memory cannot be acessed using id ID then return falureStart at Memory, and continue until you have reached howmanyMark the memory as freeClear the memoryend functionFunction DeallocateID(id)For each piece of memory ID hasDeallocate itEnd function


From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Stop here, please...
A really fast ABS function, for ints.

For use anywhere.

inline int16ABS(int* x) {return (x & 0x7FFF);}inline int32ABS(int* x) {return (x & 0x7FFFFFFF);}inline int64ABS(int* x) {return (x & 0x7FFFFFFFFFFFFFFF);}inline float24abs(Void* x) {Return (*(int * x) & 0x7FFFFF);}inline float64abs(Void* x) {Return (*(int * x) & 0x7FFFFFFFFFFFFFFFF);}


Those should work well.
Works by resetting the sign bit to 0. Not sure if the pointer conversion in the floats works tho.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
NiceCoder: these do not work well. Why does bitwise-and'ing a pointer to an integer with 0x7(F*) return the abs of the integer being pointed to?

Not to mention, removing the sign bit on negative numbers that are represented as two's complement (which happens on most computers I know of) will not return the opposite of these numbers: for instance, applied to -1 ( which is 0xFFFF, not 0x8001 ), your method would return (0x7FFF) = 32767...

In the special case where negative integers are represented as two's complement, AND right-shifting fills the new bits with '1' if the leftmost bit was '1' prior to the shift, then this works:

abs(i) = i - ((i+i) & (i>>31))
Also concerning your memory manager, I think:
- For deallocation, only a pointer to the memory should be used (so it's not necessary to save it).
- Also, you do not explain how you find a spot where free memory can be taken from, although it's quite important (maybe even the most important thing about this).
How about this,
Why don't you simple negitate the number? (then do the &)? Then you subtract 1?

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
If you negate the number, remove the sign bit, and substract 1, abs(1) = 32766.

Sign storage is not as simple as "the sign bit", as the sign bit is only present to determine whether the number should be read as a negative or as a positive number. Removing the "Chinese version only" label off a DVD won't you listen to the DVD in English, you actually have to both remove the label, AND translate the data.
ok. So how would you turn that negitive number into a positive number?

at least its that easy on floats....

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
To turn a negative number into a positive one:

a = -a;

Also, the method I proposed:

if the sign bit is set (negative number) and right-shifting fills with "1" the bits to the left, then x >> 31 equals 0xFFFFFFFF if x < 0, and 0 otherwise.

Hence (x+x)&(x>>31) equals x+x if x < 0 and 0 otherwise.

So x - ((x+x)&(x>>31)) equals x-(x+x) = -x if x < 0, and x-0 = x otherwise.

Hence abs(x) = (x+x)&(x>>31).

Alternatively, if your processor has a "MAX" operation, abs(x) = max(-x,x).

This topic is closed to new replies.

Advertisement