Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Boolean Filter
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 10th November 18:18
steve laycock
External User
 
Posts: 1
Default Boolean Filter



I had the bright idea of including an integer field in each record of a
table which would be descriptive of the person in the table e.g 1 st bit
set might indicate sex, 2nd bit hair color and so on so an entry with
10010011 might indicate a male with green eyes. I then tried to set a filter
in the table such as 'mask and 3 = 3' to find all males with green eyes.

Sounds OK in theory but I just get a 'type mismatch in filter condition'
error when I try it.
I have tried the obvious fixes such as table.filter := 'mask and
'+intTostr(3) + ' = ' + intTostr(3)

Any ideas ?
Cheers
Steve Laycock
  Reply With Quote


 


2 10th November 18:19
sander martens
External User
 
Posts: 1
Default Boolean Filter



Why don't you just create a table with records like Eyes, Sex etc. and then
run a query over it?

Sander

"Steve Laycock" <slaycock@bigpond.net.au> schreef in bericht
news:YjRxb.31457$aT.24255@news-server.bigpond.net.au...


filter
  Reply With Quote
3 10th November 18:21
denis jeanroy
External User
 
Posts: 1
Default Boolean Filter


I'm not sure that a boolean operation is allowed in an SQL filter.
when your write ... AND ..., a logical AND is expected, like (name='Smith')
AND (city='Tombstone').
You may try the OnFilterRecord event, where a boolean or any other operation
can be done.

Denis
  Reply With Quote
4 20th November 03:40
alton newman
External User
 
Posts: 1
Default Boolean Filter


Sort of sounds like the old mainframe DBMS - Model 204
  Reply With Quote
5 20th November 03:40
henry bartlett
External User
 
Posts: 1
Default Boolean Filter


Replying to "Steve Laycock" since I cannot see his original post


Since I have very limited experience with using Delphi's built-in
databases (I normally "roll my own"), I cannot help with
queries/tables/filters etc but I may be able help with the bit-setting
and bit-reading.

First, this sort of scheme is normally used for binary data, ie things
that are true/false. Eye colour is not one of these since eyes can be
brown, green, grey, blue or any shade between. And hair colour is
similar.

The scheme can be used for values like Eyes-are-blue, Eyes-are-green,
Eyes-are-brown, Gender-is-male etc.

You are limited by the number of bits in the type you are using to
store the data in. Remember 8 bits per byte.

AFIK, currently the 32 bit longword/cardinal type is the largest
available Delphi integer type, so you are limited to a maximum of 32
values.

Using sets with their 128 maximum-values would give you more
flexibility but your database may not support Delphi sets.

In bit-setting, the bits available are all powers of 2: 2^0 (1), 2^1
(2), 2^2 (4), 2^3 (8) to 2^31 for a longword.

The value 3 "overlaps" values 1 and 2, so using it takes up 2 bits of
your integer.

Assuming BitArray is an integer type and ecBlue (blue eyes) is a power
of 2:

To set ecBlue, use BitArray := BitArray or ecBlue.

and to find out if the ecBlue bit is set, use something like

HasBlueEyes := BitArray and ecBlue > 0;


Storing data by bit-setting used to be popular when memory and
disk-space was at a premium but its usefulness is now much less.

As others have suggested, it is much simpler to using separate fields
for the attributes.

--
Henry Bartlett
HABit utilities ( http://www.hotkey.net.au/~hambar/habit/ )

<SNIP>
  Reply With Quote
6 20th November 03:40
bruce roberts
External User
 
Posts: 1
Default Boolean Filter


Two choices each of hair & eye color seems a little limiting to me. ISTM
likely that a bit packed solution is also going to run slower than simply
using a field for each attribute. And if its question of disk space . . . ha ha ha.

aTable.Filter := '[mask] = 3';
  Reply With Quote
7 20th November 03:41
jamie
External User
 
Posts: 1
Default Boolean Filter


you can do that if you wish, it really does not save that much.
in C/C++ you can use BIT:0 , BIT1 etc... to indicate a value in a
struct (record) much like a variant record (UNION in C). etc..
unless they have added since my old version here delphi does not have
a direct aprouch to bit field name tagging. in stead you can use the
logical expressions to set/read the bits in a field .
this is what C/C++ does in the background so its not really saving
you any code other than source code space.
to this in delphi and to make it simple you can write a couple pieces
of code to set/read the bit values..
for example..
Function CheckBitNumber(BitNumber,Bits:integer):boolean;

Begin
Result := (($01 shl BitNumber)and Bits) <> 0;
End;
Procedure SetBitNumber(Var Bits,BitNumer:integer;State:Boolean);
Begin
If State then
Bits :=Bits or ($01 shl BitNumber) else
Bits :=Bits and ($FFFFFFFF-($01 shl BitNumber));
End;
  Reply With Quote
8 20th November 03:41
alanglloyd
External User
 
Posts: 1
Default Boolean Filter


In article <YjRxb.31457$aT.24255@news-server.bigpond.net.au>, "Steve Laycock"
<slaycock@bigpond.net.au> writes:

If you _must_ use that strategy (and I think that you're making things a bit
complicated for little real gain) then you are moving into the area of Delphi
sets.

Sets are collections of ordinal values (each ordinal must be less than 256),
and is normally implemented as a set of enumerated values.

Enumerated values are of a type containing named values which are declared in a
similar manner to an array constant. The names of the enumerated values by
convention have a prefix of the initials of the type name ...

type
TWeekDays = (wdSunday, wdMonday, wdTuesday, wdWednesday, wdThursday,
wdFriday, wdSaturday);

Enumerated values are usually used as constants instead of using "magic
numbers" (ie a number without any obvious meaning).

Delphi allocates values from 0 upwards for the value of each enumerated value,
so wdTuesday would have a value of 2.

A set can be defined as ...

TWeekDaySet = set of TWeekdays;

.... and is assigned by specifying enumerated values between square brackets ...

var
OpenDays : TWeekDaySet;
begin
OpenDays := [wdMonday, wdWednesday, wdFriday];

Sets can be manipulated by various operators and functions such as +, -, >=,
in, Include(), Exclude().

Now to your particular interest.

Sets are implemented by Delphi as bits in a value. Each bit (from LSB == the
first enumerated value) For up to 8 enumerated values in a set they are bits of
a byte, for up to 16 values, bits of a word, etc.

To use a set as the implemented item one must typecast the set ...

var
OpenDaysByte : byte;
begin
OpenDaysByte := byte(OpenDays).

.... would reult in OpenDaysByte having bits 1, 3, and 5 set in the byte, ie a
value of 42. And of course ...

var
SomeByte : byte;
begin
SomeByte := 40;
if (wdWednesday in TOpenDaysSet(SomeByte)) then ..// "true" conditional.

This is of course more coding that specifying what you have worked out as the
equivalent values of setting bits, bur results in code clarity and no magic
numbers.

Enumerated values a very useful and can be used as array indices with the array
type being declared as ...

const
WeekDayStr : array[TWeekDays] of string = ('Sunday', 'Monday', 'Tuesday', etc

.... then ...

OpenDaysStr := '';
for I := Low(TWeekDays) to High(TWekDays) do
if I in OpenDaysSet then
OpenDaysStr := OpenDaysStr + WeekDayStr[i] + ', ';
if Length(OpenDaysStr) > 2 then
SetLength(OpenDaysStr, Length(OpenDaysStr) - 2);

Alan Lloyd
alanglloyd@aol.com
  Reply With Quote
9 20th November 03:41
marcrohloff_ng
External User
 
Posts: 1
Default Boolean Filter


Also any decent modern DB will pack boolean fields into a single field.
(Oracle isn't very modern by this defenition)

Marc
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666