473,513 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lots of booleans

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.
Jul 23 '05 #1
5 1503
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
Jul 23 '05 #2
RobG <rg***@iinet.net.auau> 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.
Jul 23 '05 #3
Christopher Benson-Manica wrote:
RobG <rg***@iinet.net.auau> 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
Jul 23 '05 #4
JRS: In article <d4**********@chessie.cirr.com>, dated Thu, 28 Apr 2005
19:13:15, seen in news:comp.lang.javascript, Christopher Benson-Manica
<at***@nospam.cyberspace.org> 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.
Jul 23 '05 #5


Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message
news:d4**********@chessie.cirr.com...
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


Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1837
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends,...
4
1598
by: Thomas Rast | last post by:
Hello everyone My scenario is somewhat involved, so if you're in a hurry, here's an abstract: I have a daemon that needs about 80MB of RAM to build its internal data structures, but can pack...
10
1756
by: AlexS | last post by:
Hi, I wonder if anybody can comment if what I see is normal in FW 1.1 and how to avoid this. I have .Net assembly, which creates literally thousands of temporary strings and other objects...
4
1435
by: Dr. Know | last post by:
I seem to be having an unresolvable problem with an ASP page (darned typeless scripting engines) and JET datatypes. Don't ask why we are using JET, let's just say it has something to do with...
13
1595
by: Dune | last post by:
How do I compare a boolean with Nothing? I can't say "If boolVar Is Nothing" because a boolean is not a reference type, but "If boolVar = Nothing" always seems to return true, even if the...
6
21998
by: chech | last post by:
Possible to have array of booleans? Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim obj() As Object = {b1, b2, b3} dim v As Object For Each v In obj v = True Next This does not work. ...
1
1064
by: John Dann | last post by:
This question is just for my education as there are some simple and obvious workarounds, but... I have an array of Booleans that I need to export to a text file. Currently the Boolean values are...
8
1386
by: mathieu | last post by:
Hi, I was trying to make an entry in a std::map be stored more efficiently. The struct contained 3 booleans discribing it's property, so I am trying to make it as compact as possible. Using a...
2
3995
by: SiJP | last post by:
Hi, My vb.net project sends an Input object to a webservice and retrieves a Results object. The webservice is maintained by a third party, and is pretty huge. I am using the following...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.