Re: Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-17 Thread Robin Vickery

On 13/06/07, Richard Davey [EMAIL PROTECTED] wrote:

Hi Robert,

Wednesday, June 13, 2007, 3:15:39 PM, you wrote:

 It's terribly verbose and inefficient...

 ?php

 $filter['flags'] = 0;

 if( $allow_fraction )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
 }

 if( $allow_thousand )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
 }

 if( $allow_scientific )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
 }

?

I don't think it's *terribly* verbose, as it has good sentence structure
to it, but your version is certainly more efficient, hence I've
swapped to that. Any other takers? ;)


lose the conditionals?

$filter['flags'] = 0;
$allow_fraction  $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
$allow_thousand  $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
$allow_scientific  $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;

or keep the conditionals and just do one assignment?

$filter['flags'] = ($allow_fraction ? FILTER_FLAG_ALLOW_FRACTION : 0)
 | ($allow_thousand ? FILTER_FLAG_ALLOW_THOUSAND : 0)
 | ($allow_scientific ? FILTER_FLAG_ALLOW_SCIENTIFIC : 0);

as the perl motto says, there's more than one way to do it.

-robin


-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-17 Thread Nathan Nobbe


admittedly i had to read up quickly on the usage of a *binary pattern*where each
bit in said *pattern* is used to represent the state of some boolean
variable.
wikipedia sufficed http://en.wikipedia.org/wiki/Bitwise_operation#OR
this makes much more sense to me now; as ive seen this technique before
but never
taken in a formal explanation of it.
i hesitate now to suggest what i would do, which would be to define an
object
encapsulating an array of boolean variables and providing acccessors and
mutators for each
respective value it represented. presumably the internal implementation
might be
enhanced using the bitwise OR mechanism discussed in this thread, in which
case
we would still like to know how to best handle the implementation
component of said
object.
but i do very much love my objects :
nice thread,

-nathan




Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-16 Thread tedd

At 3:19 PM +0100 6/13/07, Richard Davey wrote:



 ?php



 $filter['flags'] = 0;


  if( $allow_fraction )

 {

  $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;

 }


  if( $allow_thousand )

 {

  $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;

 }


  if( $allow_scientific )

 {

  $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;

 }


?

I don't think it's *terribly* verbose, as it has good sentence structure
to it, but your version is certainly more efficient, hence I've
swapped to that. Any other takers? ;)


Rich:

How about?

switch (1)
{
case $allow_fraction:
$filter['flags'] = FILTER_FLAG_ALLOW_FRACTION;
break;

case $allow_thousand:
$filter['flags'] = FILTER_FLAG_ALLOW_THOUSAND;
break;

case  $allow_scientific:
$filter['flags'] = FILTER_FLAG_ALLOW_SCIENTIFIC;
break;
}

Would that not work?

Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-13 Thread Richard Davey
Hi Robert,

Wednesday, June 13, 2007, 3:15:39 PM, you wrote:

 It's terribly verbose and inefficient...

 ?php

 $filter['flags'] = 0;

 if( $allow_fraction )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
 }

 if( $allow_thousand )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
 }

 if( $allow_scientific )
 {
 $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
 }

?

I don't think it's *terribly* verbose, as it has good sentence structure
to it, but your version is certainly more efficient, hence I've
swapped to that. Any other takers? ;)

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

Never trust a computer you can't throw out of a window

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-13 Thread Robert Cummings
On Wed, 2007-06-13 at 15:19 +0100, Richard Davey wrote:
 Hi Robert,
 
 Wednesday, June 13, 2007, 3:15:39 PM, you wrote:
 
  It's terribly verbose and inefficient...
 
  ?php
 
  $filter['flags'] = 0;
 
  if( $allow_fraction )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
  }
 
  if( $allow_thousand )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
  }
 
  if( $allow_scientific )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
  }
 
 ?
 
 I don't think it's *terribly* verbose, as it has good sentence structure
 to it, but your version is certainly more efficient, hence I've
 swapped to that. Any other takers? ;)

Personally I hate constants (can't use non-scalar values so why get used
ot them... also they're just another point for name collision) so if it
were my own code I'd do something more like the following:

?php

$GLOBALS['filterFlags'] = array
(
'allowFraction'   = 1  0,
'allowThousand'   = 1  1,
'allowScientific' = 1  2,
);

// then your above code would become:

$filter['flags'] = 0;
foreach( $filterFlags as $name = $bits )
{
if( isset( $$name )  $$name )
{
$filter['flags'] |= $bits;
}
}

?

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: Re[6]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-13 Thread Ford, Mike


 From: Richard Davey [mailto:[EMAIL PROTECTED]
 Sent: Wed 13/06/2007 15:19
 To: PHP List
 
  Hi Robert,
 
 Wednesday, June 13, 2007, 3:15:39 PM, you wrote:
 
  It's terribly verbose and inefficient...
 
  ?php
 
  $filter['flags'] = 0;
 
  if( $allow_fraction )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
  }
 
  if( $allow_thousand )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
  }
 
  if( $allow_scientific )
  {
  $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
  }
 
 ?
 
 I don't think it's *terribly* verbose, as it has good sentence structure
 to it, but your version is certainly more efficient, hence I've
 swapped to that. Any other takers? ;)

Well, I don't know about more efficient but I'd be terribly tempted to express 
it like this:

  $filter['flags'] = $allow_fraction   ? FILTER_FLAG_ALLOW_FRACTION : 0
   | $allow_thousand   ? FILTER_FLAG_ALLOW_THOUSAND : 0
   | $allow_scientific ? FILTER_FLAG_ALLOW_SCIENTIFIC : 0;

Whether you think this is more readable and/or less verbose probably depends on 
personal taste! ;)
 
- 
Mike Ford,  Electronic Information Services Adviser, 
Learning Support Services, Learning  Information Services, 
JG125, James Graham Building, Leeds Metropolitan University, 
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom 
Email: [EMAIL PROTECTED] 
Tel: +44 113 812 4730  Fax:  +44 113 812 3211 



To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm