Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.
I'm guessing this was generated by dreamweaver, or I hope it was, at least. If someone wrote this, they should be shot, regardless of if it works.

code:
<select name="TshirtSize"<option>-Size-
<option>SS<option>S<option>M<option>L<option>XL<option>XXL
</select>
It's not so bad, in itself, but I just inherited a form with about 30 of these for various options, and debating if it really annoys me enough to try to fix them all.

Adbot
ADBOT LOVES YOU

casey
Dec 12, 2002

noonches posted:

If someone wrote this, they should be shot, regardless of if it works.

he was just optimizing it for download over 2400bps modems, half the HTML == twice as fast.

HIERARCHY OF WEEDZ
Aug 1, 2005

An enumeration I found, the values of which are used in a database table.

code:

    public enum FooType 
    {
        Foo = 0,
        Bar = 1,
        Baz = 3,
        Bip = 5,
        Bob = 10,
        Jam = 20
    }

They tried to come up with numbers that they could add together for a combination of options. :haw:


:suicide:

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

shopvac4christ posted:

An enumeration I found, the values of which are used in a database table.

code:

    public enum FooType 
    {
        Foo = 0,
        Bar = 1,
        Baz = 3,
        Bip = 5,
        Bob = 10,
        Jam = 20
    }

They tried to come up with numbers that they could add together for a combination of options. :haw:


:suicide:

Everything is a Foo!

Randomosity
Sep 21, 2003
My stalker WAS watching me...

shopvac4christ posted:

An enumeration I found, the values of which are used in a database table.

code:

    public enum FooType 
    {
        Foo = 0,
        Bar = 1,
        Baz = 3,
        Bip = 5,
        Bob = 10,
        Jam = 20
    }

They tried to come up with numbers that they could add together for a combination of options. :haw:


:suicide:

Everyone remember this when they hear CS students complaining about CPU logic and close-to-the-metal programming classes! :haw:

atomic johnson
Dec 7, 2000

peeping-tom techie with x-ray eyes

shopvac4christ posted:


They tried to come up with numbers that they could add together for a combination of options. :haw:

Christ... everybody knows the way to do this is to make them all prime numbers so that you can just factor the resulting number to get the options that were selected.

chocojosh
Jun 9, 2007

D00D.
Wouldn't the correct solution be to use a bitmask? Define each enum value to 2^i (i = 0,1,2,...,n) and then just check if the relevant is set?

Zombywuf
Mar 29, 2008

atomic johnson posted:

Christ... everybody knows the way to do this is to make them all prime numbers so that you can just factor the resulting number to get the options that were selected.

chocojosh posted:

Wouldn't the correct solution be to use a bitmask? Define each enum value to 2^i (i = 0,1,2,...,n) and then just check if the relevant is set?

Gah, you're both wrong. Obviously the answer is a bloom filter. It's more extensible that way.

nebby
Dec 21, 2000
resident mog
Personally I would probably fork off an ec2 instance for each choice and pass the choice in as a boot parameter so it can be save to disk. You could then query each of the machines in parallel via a rest interface to quickly determine the set of choices.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

chocojosh posted:

Wouldn't the correct solution be to use a bitmask? Define each enum value to 2^i (i = 0,1,2,...,n) and then just check if the relevant is set?

No, because then you need a whole bit for each individual choice. Once you get past 32 choices it takes more than a word of memory, so it's very space-inefficient. With primes, you can fit 4294967296 different options in a word (well, not that many because not every number is prime, but it's still a hell of a lot more than 32).

TSDK
Nov 24, 2003

I got a wooden uploading this one

JoeNotCharles posted:

No, because then you need a whole bit for each individual choice. Once you get past 32 choices it takes more than a word of memory, so it's very space-inefficient. With primes, you can fit 4294967296 different options in a word (well, not that many because not every number is prime, but it's still a hell of a lot more than 32).
I would say that you can only encode 9 flags using primes before you overflow a 32 bit unsigned word, but I just don't know who's joking or not any more :(

Aredna
Mar 17, 2007
Nap Ghost

TSDK posted:

I would say that you can only encode 9 flags using primes before you overflow a 32 bit unsigned word, but I just don't know who's joking or not any more :(

You could let it overflow and just assume you're doing the math mod 2^32. I haven't looked, but I'd imagine you can go quite a ways before you ran into a collision.

casey
Dec 12, 2002

TSDK posted:

I would say that you can only encode 9 flags using primes before you overflow a 32 bit unsigned word, but I just don't know who's joking or not any more :(

you can get 10 if you use -1 as a flag and a signed 32 bit word. Though i guess that would make this a hybrid bit field-prime number encoding scheme.

TSDK
Nov 24, 2003

I got a wooden uploading this one

Aredna posted:

You could let it overflow and just assume you're doing the math mod 2^32. I haven't looked, but I'd imagine you can go quite a ways before you ran into a collision.
Oh god, you're right. I'm in two minds whether just to recoil in horror, or to download a primes list and put it to the test.

:psyduck:

Anyone fancy putting the appropriate loading code in and testing?:
code:
#include <list>
#include <set>
#include < (whatever it is for std::cout, I can't be bothered to look) >

typedef std::list<unsigned int> tPrimeList;
typedef std::set<unsigned int> tTotals;

int main()
{
    tPrimeList primes;
    // Read primes.txt into primes here

    tTotals used;
    unsigned int flag_count = 0;
    unsigned int running_total = 1;
    for ( tPrimeList::iterator prime = primes.begin(),
                               end = primes.end();
          prime != end;
          ++prime )
    {
        running_total *= *prime;
        if ( primes.find( running_total ) != primes.end() ) break;
        primes.insert( running_total );
        flag_count++;
    }

    std::cout <<
        "Can encode " <<
        flag_count <<
        " flags before a collision occurs" <<
        std::endl;
}

zootm
Aug 8, 2006

We used to be better friends.
Caching the prime numbers is memory-intensive. For the best performance profile you should calculate them on-demand each time.

fansipans
Nov 20, 2005

Internets. Serious Business.

zootm posted:

Caching the prime numbers is memory-intensive. For the best performance profile you should calculate them on-demand each time.

This is especially true when you are using PRRBM (Pigeon Relay Ring Buffer Memory) - write times, read times, and serial access - the worst of all worlds!

chocojosh
Jun 9, 2007

D00D.
In all seriousness then, how would you encode N different options that can be on simultaneously? I always assumed the bitmask would work (and I would go so far to say that it is a simple and clear solution that should be efficient enough for small cases, n <= 8 or n <= 32).

Aredna
Mar 17, 2007
Nap Ghost
^^^ A bitwise mask is of course the best solution, but I find this method interesting if for no other reason than I hadn't thought about it before. The encoding works by multiplying all of the chosen primes together which will produce a unique number. If you picked the options represented by primes 2, 3, and 7, then you store 42. You can determine the prime factors of 42 later if you want to know what options were selected.


It looks like you can use any combination of the first 20 prime numbers without a collision. I'm not posting my code either since it will be more than fitting for this thread.

I wonder what the max number of prime numbers you can use would be, assuming you can use any prime number less than 2^32. Obviously it can't be more than 31.

The real question is, what algorithm do you use to decode the number back to base primes? What if you don't know how many or which primes were used other than that they produced no collisions?

Aredna fucked around with this message at 21:03 on Apr 24, 2008

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

chocojosh posted:

In all seriousness then, how would you encode N different options that can be on simultaneously? I always assumed the bitmask would work (and I would go so far to say that it is a simple and clear solution that should be efficient enough for small cases, n <= 8 or n <= 32).

std::vector<bool> AFAIK is almost always implemented as a bitfield that uses proxy objects to modify its 'bool references'. I doubt that the 31 bits you save per bool really add up to anything significant, but it's a simple solution at least.

TSDK
Nov 24, 2003

I got a wooden uploading this one

Aredna posted:

The real question is, what algorithm do you use to decode the number back to base primes? What if you don't know how many or which primes were used other than that they produced no collisions?
You cache it all in a std::map< unsigned int, std::set< unsigned int > >, of course, duh!

;)

casey
Dec 12, 2002

Aredna posted:

The real question is, what algorithm do you use to decode the number back to base primes? What if you don't know how many or which primes were used other than that they produced no collisions?

since this is the coding horrors thread, the answer is easy. you calculate the every possible multiplication of the prime numbers as you've done, but modify that program to generate a header file full of #define's for all the values. Then use a massive switch statement to handle each one. No decoding necessary at runtime!

code:
#define OPTION_ONE_AND_NO_OTHERS 2
#define OPTION_TWO_AND_NO_OTHERS 3
#define OPTION_THREE_AND_NO_OTHERS 5
#define OPTION_ONE_AND_TWO 6
#define OPTION_ONE_AND_THREE 10
#define OPTION_TWO_AND_THREE 15
#define OPTION_ONE_TWO_AND_THREE 30

switch (options)
{
   OPTION_ONE_AND_NO_OTHERS: ...; break;
   OPTION_TWO_AND_NO_OTHERS: ...; break;
   OPTION_THREE_AND_NO_OTHERS: ...; break;
   OPTION_ONE_AND_TWO: ...; break;
   OPTION_ONE_AND_THREE: ...; break;
   OPTION_TWO_AND_THREE: ...; break;
   OPTION_ONE_TWO_AND_THREE: ...; break;
   default: assert(0);  //help me
}

chocojosh
Jun 9, 2007

D00D.

Drx Capio posted:

std::vector<bool> AFAIK is almost always implemented as a bitfield that uses proxy objects to modify its 'bool references'. I doubt that the 31 bits you save per bool really add up to anything significant, but it's a simple solution at least.

Alright, so then you also second the bitmask suggestion (which means I'm not totally crazy).

In C++ you can use the bitset

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

chocojosh posted:

Alright, so then you also second the bitmask suggestion (which means I'm not totally crazy).

In C++ you can use the bitset

No, that's just a viable alternative if you're retarded and don't want to go with the prime factorization solution. Why would you use dynamic memory at runtime when you can quickly run through a sieve of eratosthenes and get some valid, unique flags?

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Drx Capio posted:

std::vector<bool> AFAIK is almost always implemented as a bitfield that uses proxy objects to modify its 'bool references'. I doubt that the 31 bits you save per bool really add up to anything significant, but it's a simple solution at least.

Yes, vector<bool> is a very appropriate topic for the coding horrors thread.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

JoeNotCharles posted:

No, because then you need a whole bit for each individual choice. Once you get past 32 choices it takes more than a word of memory, so it's very space-inefficient. With primes, you can fit 4294967296 different options in a word (well, not that many because not every number is prime, but it's still a hell of a lot more than 32).

I can't tell if youre joking or not, but 32 on/off options leads to 2^32 possible combinations of options, and you can't encode that in less than 2^32 numbers without losing some possible combinations.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

HappyHippo posted:

I can't tell if youre joking or not, but 32 on/off options leads to 2^32 possible combinations of options, and you can't encode that in less than 2^32 numbers without losing some possible combinations.

I was joking about the primes, yes, but when I said "you can't get more than 32 options" that was correct. I wasn't talking about combinations of options, I was talking about individual options.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

JoeNotCharles posted:

I was joking about the primes, yes, but when I said "you can't get more than 32 options" that was correct. I wasn't talking about combinations of options, I was talking about individual options.

Yes of course.

chocojosh
Jun 9, 2007

D00D.

Drx Capio posted:

No, that's just a viable alternative if you're retarded and don't want to go with the prime factorization solution. Why would you use dynamic memory at runtime when you can quickly run through a sieve of eratosthenes and get some valid, unique flags?

For simplicity? Am I missing something here or does it just seem to be much easier to use a vector<bool> (or bitset) and just count the enumerated bits? I honestly don't see what advantage there is to factoring prime numbers.


Also, if you need 32 different options, then wouldn't you need to multiply the first 32 prime numbers together? That multiplication gives 31610054640417607788145206291543662493274686990 (47 digit number). I think that would require a significant amount of memory space..

I'm really lost as to who is joking right now :(

king_kilr
May 25, 2007

chocojosh posted:

For simplicity? Am I missing something here or does it just seem to be much easier to use a vector<bool> (or bitset) and just count the enumerated bits? I honestly don't see what advantage there is to factoring prime numbers.


Also, if you need 32 different options, then wouldn't you need to multiply the first 32 prime numbers together? That multiplication gives 31610054640417607788145206291543662493274686990 (47 digit number). I think that would require a significant amount of memory space..

I'm really lost as to who is joking right now :(

He's not looking for things that multiple together uniquely, it's addition.(I think)

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

HappyHippo posted:

I can't tell if youre joking or not, but 32 on/off options leads to 2^32 possible combinations of options, and you can't encode that in less than 2^32 numbers without losing some possible combinations.
Unless you compress the result


We're through the looking glass here folks

TSDK
Nov 24, 2003

I got a wooden uploading this one

minato posted:

Unless you compress the result
What if, now hear me out guys, what if we use this compression scheme where we represent each prime by a particular power of two. So the i-th prime is encoded as 2^i, and then add these together to represent which primes are factors of the number we're compressing.

By my rough calculations, we should be able to store all of the values we're after up to 31610054640417607788145206291543662493274686990, in a little under 33 bits.

Then when we want to figure out which values are set, we just multiply up the primes and then factor the number as usual. Job done.

minato posted:

We're through the looking glass here folks
And how.

chocojosh
Jun 9, 2007

D00D.

king_kilr posted:

He's not looking for things that multiple together uniquely, it's addition.(I think)

How would you be able to factor primes by addition?

Let's pick the primes 2,3 and 5. If you use addition then 2 + 3 = 5. How would you distinguish 5 from representing 2 + 3 or from just representing 5.

However, using prime numbers you can find a number that is the product of those primes (as every number can be represented as a unique product of primes).


TSDK posted:

What if, now hear me out guys, what if we use this compression scheme where we represent each prime by a particular power of two. So the i-th prime is encoded as 2^i, and then add these together to represent which primes are factors of the number we're compressing.

Yeah, umm, that's called a bitmask :)

Alright, this thread has been good for some laughs.

zootm
Aug 8, 2006

We used to be better friends.
The great thing about the prime factorisation solution is that you can have counted flags. It's like the flag equivalent of turning your amp up to 11.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

zootm posted:

The great thing about the prime factorisation solution is that you can have counted flags. It's like the flag equivalent of turning your amp up to 11.

I'm surprised nobody has mentioned this advantage yet. If I remember my Win32 API correctly, there's a window style called WS_EX_THICKFRAME. But it doesn't let me control the degree of thickness.

code:
DWORD exStyle = WS_EX_THICKFRAME; // thick
DWORD exStyle = WS_EX_THICKFRAME | WS_EX_THICKFRAME; // DAMMIT IT'S NOT THICKER
How restrictive! But if we redefined the styles to use prime numbers, imagine the thickability:

code:
DWORD exStyle = (DWORD) pow(WS_EX_THICKFRAME, 10);
Now THAT'S a thick-rear end window frame that will draw the attention that my awesome application deserves. I mean, come on.

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

Flobbster posted:

I'm surprised nobody has mentioned this advantage yet. If I remember my Win32 API correctly, there's a window style called WS_EX_THICKFRAME. But it doesn't let me control the degree of thickness.

code:
DWORD exStyle = WS_EX_THICKFRAME; // thick
DWORD exStyle = WS_EX_THICKFRAME | WS_EX_THICKFRAME; // DAMMIT IT'S NOT THICKER
How restrictive! But if we redefined the styles to use prime numbers, imagine the thickability:

code:
DWORD exStyle = (DWORD) pow(WS_EX_THICKFRAME, 10);
Now THAT'S a thick-rear end window frame that will draw the attention that my awesome application deserves. I mean, come on.

WS_EX_GIRTHYFRAME will do that too.

rotor
Jun 11, 2001

classic case of pineapple derangement syndrome

Flobbster posted:

I'm surprised nobody has mentioned this advantage yet. If I remember my Win32 API correctly, there's a window style called WS_EX_THICKFRAME. But it doesn't let me control the degree of thickness.

code:
DWORD exStyle = WS_EX_THICKFRAME; // thick
DWORD exStyle = WS_EX_THICKFRAME | WS_EX_THICKFRAME; // DAMMIT IT'S NOT THICKER
How restrictive! But if we redefined the styles to use prime numbers, imagine the thickability:

code:
DWORD exStyle = (DWORD) pow(WS_EX_THICKFRAME, 10);
Now THAT'S a thick-rear end window frame that will draw the attention that my awesome application deserves. I mean, come on.

this thread just took a sharp right heading straight down Hilarious St.

king_kilr
May 25, 2007

chocojosh posted:

How would you be able to factor primes by addition?

Let's pick the primes 2,3 and 5. If you use addition then 2 + 3 = 5. How would you distinguish 5 from representing 2 + 3 or from just representing 5.

However, using prime numbers you can find a number that is the product of those primes (as every number can be represented as a unique product of primes).
What about 1, 4, 7 any addition of these numbers will be a unique sum(unless you need to add the same value multiple times, in which case primes are the only option).

mantaworks
May 6, 2005

by Fragmaster
I fuckin love pow(). It makes me read out the code like a hyperactive 15 year old that just killed Ogre in Tekken 3 after drinking a few gallons of bawls. Hell yes!! POW! Take that!! gently caress yeah MATH RULES WOOO

chocojosh
Jun 9, 2007

D00D.

king_kilr posted:

What about 1, 4, 7 any addition of these numbers will be a unique sum(unless you need to add the same value multiple times, in which case primes are the only option).

A bitmask can do the exact same thing and will use less space ;)

1 2 4 (Decimal)
001 010 100 (Binary)

1 + 2 + 4 = 7
1 + 4 + 7 = 12


Also, king_kilr, what set of numbers would you use if you have 5 numbers that will always be a unique sum.. or 10 numbers.. or 100 numbers...

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Jesus, what is it with you and your fetish for bitmaps?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply