Conversation

Cardinality of a set

(overview of excerpts from articles in conversation)

Newsgroup: borland.public.delphi.objectpascal

# Lines
wrote on 13-Oct-1999:

Calculating the cardinality of a set is an easy task, but
is there any Delphi BUILT-IN function doing the job?
I looked at the Delphi Help and at developers.href.com
site and found nothing about such a function.
Thanks in advance
11
  
Rudy Velthuis replied on 13-Oct-1999:

Jacques Melancon wrote:
  (snip)

What do you mean with the cardinality of a set?
  --
  Rudy Velthuis
  
8
  
Rene Tschaggelar replied on 13-Oct-1999:

a set is a predefined type, just count them.
  Rene
  --
  Ing.Buero R.Tschaggelar - http://www.ibrtses.com
  Jacques Melancon wrote:
16
    
Jacques Melancon replied on 14-Oct-1999:

I know I can count. I want to know if there is a
    BUILT-IN function doing the job for me.
    Jacques
    Rene Tschaggelar wrote:
    (snip)
23
      
Philippe Ranger replied on 14-Oct-1999:

<       I know I can count. I want to know if there is a
      BUILT-IN function doing the job for me.
      (snip)
There is no built-in function and, like you, I think there should be, just
      as there should be sets beyond 256 elements. Many years ago, I wrote a
74
        
Robert Lee replied on 14-Oct-1999:

Philippe Ranger wrote:
        (snip)

Weren't we just here?
        Your "many years ago" is in full flower. That 'loop' instruction has
        got to go. Besides, it would seem that wrapping around the best of the
        "bitsInByte" solutions would be both faster and less cumbersome.
21
        
Jacques Melancon replied on 14-Oct-1999:

Merci Philippe,
        As you say I think it would be usefull if we could have such a function.
        Now, a question about your function. If I declare something like:
        Type
        TDigits = set of 0..9;
33
          
Philippe Ranger replied on 14-Oct-1999:

<           I would call your function like this:
          Cardin := Card( Digits, sizeof(TDigits) );
          or
          Cardin := Card( Digits, 2 );
18
            
Jacques Melancon replied on 14-Oct-1999:

Philippe Ranger <.> wrote in message news:7u55ru$✉forums.borland.com...
            (snip)

Philippe
            1. I know that there is no bit addressing
            2. Both calls are identical in my example ( sizeof(TDigits) IS EQUAL TO 2 ).
            You need at least 2 bytes to store 10 "boolean" values.
36
              
Philippe Ranger replied on 14-Oct-1999:

<               1. I know that there is no bit addressing
              2. Both calls are identical in my example ( sizeof(TDigits) IS EQUAL TO 2 ).
              You need at least 2 bytes to store 10 "boolean" values.
              (snip)
Sizeof() is not a function, Jacques, it's a compiler macro.
26
    
Glynn Owen replied on 13-Oct-1999:

The cardinality of a finite set is the number of elements it has.
    Phillipe has a BASM solution to this problem that he's posted a couple
    of times. Maybe he'll do it again.
    Glynn
    Rudy Velthuis wrote:
19
      
Eddie Shipman replied on 13-Oct-1999:

Wouldn't that be High(set)???
      Glynn Owen <✉gtstrans.com> wrote in message
      news:✉gtstrans.com...
      (snip)


      
26
        
Rudy Velthuis replied on 13-Oct-1999:

Eddie Shipman wrote:
        (snip)
That would be the maximum number of items a set can have. The
        cardinality would be the number of items actually in there.
        The answer is no, there are no built-in functions for this. If
        Philippe posted code, try to find it on
12
        
John Herbster replied on 14-Oct-1999:

Shouldn't that be like ord(high(set)) + 1 ?
        
        
        
        
3
          
Philippe Ranger replied on 14-Oct-1999:

<           Shouldn't that be like ord(high(set)) + 1 ?
          (snip)
Not at all sure. What happens with a set of 14..28?
          PhR
          
9
            
John Herbster replied on 15-Oct-1999:

Philippe, You are are quite correct in seeing a problem with
            "ord(high(set))+1". On a related topic, that I have never seen
            discussed here, is the problem of using other people's definition of
            enumerated types in const statements like ...
            const FieldTypeNames: array [TFieldType] of string[4] = ('Unk', 'A',
23
              
Philippe Ranger replied on 15-Oct-1999:

<               But what is a good way to minimize the effect of adding a member in
              the middle of the range?
              (snip)
Far as I know, none. Whoever does this on a commercial library is a criminal
              (unless the type maps an OS-defined thing, which changed).
11
    
Philippe Ranger replied on 14-Oct-1999:

<     What do you mean with the cardinality of a set?
    (snip)
Guess why my bit counter is named Card.
    PhR
    
9
          
Philippe Ranger replied on 14-Oct-1999:

<           Besides, it would seem that wrapping around the best of the
          "bitsInByte" solutions would be both faster and less cumbersome.
          (snip)
Perhaps, but that involves one call per byte. Feel free to redo Card if you
          want.
11
            
Robert Lee replied on 15-Oct-1999:

Philippe Ranger wrote:
            (snip)

By wrap I meant put the code inside a loop. This is between 1.7 and 3x
            faster depending on the size of the set on a PII, and it uses your
            BitsInByte Nibble algorithm crushed into one pascal expression.
            function card1(var tar; size: integer): integer;
39
              
Philippe Ranger replied on 15-Oct-1999:

<               By wrap I meant put the code inside a loop. This is between 1.7 and 3x
              faster depending on the size of the set on a PII, and it uses your
              BitsInByte Nibble algorithm crushed into one pascal expression.
              (snip)
This is getting embarrassing!
46
                
Robert Lee replied on 15-Oct-1999:

Philippe Ranger wrote:
                (snip)

Actually, I didn't 'work' on it. I just applied my standard tune-ups,
                then looked at what I got (CPU window). It looked good so I quit.
                (snip)
For loops usually have more setup overhead and usually increment a
                counter and one pointer for each array in the loop. The while loop has
57
                  
Philippe Ranger replied on 15-Oct-1999:

<                   I don't. I scan up referened from the back of the array. Thus i goes
                  from -size to -1.
                  (snip)
Got it!
                  <<
39
                    
Robert Lee replied on 15-Oct-1999:

Philippe Ranger wrote:
                    (snip)

Absolute implies memory address. However, many params don't have any
                    memory associated with them as they are values. By using the 'absolute'
                    approach to typecasting them you force them on to the stack and out of a
                    register, making them slower in the process. Upon reflection though,
61
                      
Philippe Ranger replied on 15-Oct-1999:

<<                       From way back, I see absolute as the normal complement for untyped params.
                      Can't say your pointer and assignment look like a convincing improvement.
                      (snip)
<                       Absolute implies memory address. However, many params don't have any
40
                        
Robert Lee replied on 15-Oct-1999:

Philippe Ranger wrote:
                        (snip)

The while loop really isn't optimized at all in the way for loops are.
                        A while loop is handled more like a bent if-then statement.
                        Consequently, you can manually optimize access within the loop by proper
                        use of pointers, indicies etc. But you have to do it all yourself. I
36
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Tue, 26 Nov 2024 09:31:39 UTC
Copyright © 2009-2024
HREF Tools Corp.