Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Lots of booleans

Reply
Thread Tools

Lots of booleans

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-28-2005
I have a situation where I have many (more than 32) boolean flags:

var foo=true;
var bar=false;
var baz=false;
// etc.

At various points in the script, these flags may be set or unset.
There is a point where an action is to be taken only if all the flags
are false. I also need to debug this check of all flags - i.e., print
out the value of all 32+ of these flags. I'd like to find something
besides a monstrous conditional - for example, using an integer and
storing these flags as bits in it, except that since there are more
than 32 of these flags an integer will not contain all of them. Any
suggestions would be appreciated.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-28-2005
Christopher Benson-Manica wrote:
> I have a situation where I have many (more than 32) boolean flags:
>
> var foo=true;
> var bar=false;
> var baz=false;
> // etc.
>
> At various points in the script, these flags may be set or unset.
> There is a point where an action is to be taken only if all the flags
> are false. I also need to debug this check of all flags - i.e., print
> out the value of all 32+ of these flags. I'd like to find something
> besides a monstrous conditional - for example, using an integer and
> storing these flags as bits in it, except that since there are more
> than 32 of these flags an integer will not contain all of them. Any
> suggestions would be appreciated.
>


Have you considered an array, if element index is sufficient for
locating the correct value, or an object if you need name:value
pairs?

Checking through all the values or printing them out would only
require a small do..while or for..in loop.

Here's something to play with, you can add as many variables as you
like. No doubt some of the loops can be optimised.

<script type="text/javascript">
var glob = {
foo : true,
bar : true,
baz : true
}

function showGlob(){
var msg = '';
for ( varName in glob ){
msg += '\n' + varName + ' : ' + glob[varName];
}
alert(msg);
}

function checkTrue() {
var x = true;
for ( varName in glob ){
x = ( x && glob[varName]);
}
return x;
}

</script>

<input type="button" value="Show vars"
onclick="showGlob();">
<input type="button" value="Set foo false"
onclick="glob['foo']=false;">
<input type="button" value="Set foo true"
onclick="glob['foo']=true;">
<input type="button" value="Check if all true"
onclick="
(checkTrue())? alert('All are true'):
alert('At least one is false');">


--
Rob
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-29-2005
RobG <> wrote:

> Have you considered an array, if element index is sufficient for
> locating the correct value, or an object if you need name:value
> pairs?


I did consider that, but my thinking was that it would make the code
significantly more cluttered. I may yet do that, since dealing with
this number of boolean flags is a PITA, quite frankly Thanks!

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      04-29-2005
Christopher Benson-Manica wrote:
> RobG <> wrote:
>
>
>> Have you considered an array, if element index is sufficient for
>> locating the correct value, or an object if you need name:value
>> pairs?

>
>
> I did consider that, but my thinking was that it would make the code
> significantly more cluttered. I may yet do that, since dealing with
> this number of boolean flags is a PITA, quite frankly Thanks!
>



I don't see how it clutters your code. You can create a single
object with a single method (and add more if required). 'glob' could
hold anything, if it had only booleans then glob.allTrue is much
simpler- the for..in block needs only one statement. Initialising
the array is barely more code that initialising the same number of
variables, and it's vastly simpler to check if they're all true.

glob = {
foo : true,
bar : true,
baz : true,
dud : null,
str : '',
num : 9
}

// Method allTrue: returns true if all booleans are true
glob.allTrue = function() {
var x = true;
for ( varName in this ) {
if ( 'boolean' == typeof this[varName] ) {
x = ( x && this[varName]);
}
}
return x;
}

To set say foo to true:

glob.foo = true;

To do something if all the booleans are true:

if ( glob.allTrue() ) {
// do something
}


--
Rob
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-29-2005
JRS: In article <d4rckb$n29$>, dated Thu, 28 Apr 2005
19:13:15, seen in news:comp.lang.javascript, Christopher Benson-Manica
<> posted :
>I have a situation where I have many (more than 32) boolean flags:
>
>var foo=true;
>var bar=false;
>var baz=false;
>// etc.
>
>At various points in the script, these flags may be set or unset.
>There is a point where an action is to be taken only if all the flags
>are false. I also need to debug this check of all flags - i.e., print
>out the value of all 32+ of these flags. I'd like to find something
>besides a monstrous conditional - for example, using an integer and
>storing these flags as bits in it, except that since there are more
>than 32 of these flags an integer will not contain all of them. Any
>suggestions would be appreciated.


Put them all in an Object, declared as B = {} or with preset contents.
Test them with a function.

function Any(b) { var j
for (j in b) if (b[j]) return true
return false }

function All(b) { var j
for (j in b) if (!b[j]) return false
return true }

function Umm(b) { var j
for (j in b) if (!b[j]) return true
return false }

function Nun(b) { var j
for (j in b) if (b[j]) return false
return true }

B = {}
B.foo = 0
B.bar = 1
B.xxx = true
x = [Any(B), All(B), Umm(B), Nun(B)]

You may need a similar but different function.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Stephen Chalmers
Guest
Posts: n/a
 
      04-30-2005


Christopher Benson-Manica <> wrote in message
news:d4rckb$n29$...
> I have a situation where I have many (more than 32) boolean flags:
>
> var foo=true;
> var bar=false;
> var baz=false;
> // etc.
>
> At various points in the script, these flags may be set or unset.
> There is a point where an action is to be taken only if all the flags
> are false. I also need to debug this check of all flags - i.e., print
> out the value of all 32+ of these flags. I'd like to find something
> besides a monstrous conditional - for example, using an integer and
> storing these flags as bits in it, except that since there are more
> than 32 of these flags an integer will not contain all of them. Any
> suggestions would be appreciated.
>
> --
> Christopher Benson-Manica | I *should* know what I'm talking about - if I
> ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


To store your flags as bits, just use as many integers as you need in an
array to accomodate all the bits.
You can index the relevant integer containing the desired flag thus:
rray[ flagIndex / 32 ] , then just perform a suitable bitwise operation to
read/manipulate the desired bit.

<SCRIPT type='text/javascript'>

function boolManager(boolCount)
{
this.boolCount=boolCount;
this.boolStore=[ 1 + boolCount/32 ];
}

boolManager.prototype.readBool=function(ind)
{
return this.boolStore[ ind/32 ] & 1<<Math.floor(ind % 32);
}

boolManager.prototype.setBool=function(ind, state)
{
state ? ( this.boolStore[ ind/32 ] |= 1 << ind % 32 )
: ( this.boolStore[ ind/32 ] &= ~( 1 << ind % 32 ) );
}

boolManager.prototype.flipBool=function(ind)
{
this.boolStore[ ind/32 ] ^= 1 << ind % 32
}

boolManager.prototype.listBools=function()
{
for(var i=0; i<this.boolCount; i++) // read & display all flags
document.write('<BR>'+ i + " : " + (this.readBool(i)?"True":"False") );
}

boolManager.prototype.allBoolsFalse=function()
{
var rv;

for(var i=0; i<this.boolCount && !(rv=this.readBool(i)) ; i++)
;

return !rv;
}

boolManager.prototype.allBoolsTrue=function()
{
var rv;

for(var i=0; i<this.boolCount && (rv=this.readBool(i)) ; i++)
;

return rv;
}




// ========= Demonstration Code =======

var boolCount=40, // # of booleans in use
setFlags=[ 6, 13, 26, 27, 34, 38 ], // some arbitrary booleans to be
set
myBools=new boolManager(boolCount);

document.write("Set 6, 13, 26, 27, 34 & 38 <BR><BR>");

for(var i=0; i<setFlags.length; i++) // set some booleans to true
myBools.setBool(setFlags[i], true);

myBools.listBools(); // list results


document.write("<BR><BR>Flip all flags :<BR>");

for(var i=0; i<boolCount; i++) // invert all flags
myBools.flipBool(i);

myBools.listBools();


document.write("<BR><BR>Reset all flags :<BR>");

for(var i=0; i<boolCount; i++) // reset all flags
myBools.setBool(i, false);

myBools.listBools();


document.write("<BR><BR>Test for all false: " +
(myBools.allBoolsFalse()?"Yes":"No") );


document.write("<BR><BR>Set flag 0 true <BR>");

myBools.setBool(0, true);

document.write("<BR>Test for all false: " +
(myBools.allBoolsFalse()?"Yes":"No") );


document.write("<BR><BR>Set all flags true:<BR>");

for(var i=0; i<boolCount; i++) // Set all flags
myBools.setBool(i, true);

document.write("<BR>Test for all true: " +
(myBools.allBoolsTrue()?"Yes":"No") );


document.write("<BR><BR>Set flag 20 false <BR>");

myBools.setBool(20, false);

document.write("<BR>Test for all true: " +
(myBools.allBoolsTrue()?"Yes":"No") );


document.write("<BR><BR>Sorted.");

</SCRIPT>

--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for lots of words in lots of files brad Python 9 06-19-2008 07:59 AM
Downloading lots and lots and lots of files coolneo Perl Misc 9 01-30-2007 02:34 PM
array of booleans zeus2@hotmail.com Java 4 10-01-2005 05:29 PM
g++: integers as booleans, no warning? Martin Herbert Dietze C++ 9 02-18-2005 09:03 AM
Booleans and comparison results Roman Suzi Python 0 06-24-2003 03:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57