Solved

Enumerated types and sets of - counts and loops

Posted on 2008-01-30
8
6,786 Views
Last Modified: 2012-06-22
:-)
Lets say I have this.

type
  TThing = (tgsOne, tgsTwo, tgsThree);
  TThings = set of TThing;

and then I do

var
  Things : TThings;
begin
  include(Things,tgsOne);

and that is all good, Things now contains tgsOne in the set.
then I do this
include(Things,tgsTwo);

now I want to know how many items are in the set.
showmessage( IntToStr(length(Things)) );
that dosn't want to work.

Also how can I loop the items in my set to see if they are in there/do stuff. Do I have to do
var
  Things : TThings;
  Thing : TThing;
begin
  include(Things,tgsOne);

  For Thing := low(TThing) to high(Thing) do
    If Thing in Things then
     //-- do something

:-)



0
Comment
Question by:wildzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 84 total points
ID: 20781509
Yes You should loop this way
0
 
LVL 10

Author Comment

by:wildzero
ID: 20781582
What about getting a count of the number of items in the set?
0
 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 83 total points
ID: 20781818
Where you have
  //-- Do Something
put in
  Inc(Count);

There are faster ways of doing this.  The problem is that Delphi places sets in an appropriately sized container.  Your set would fit in one byte.  Bigger sets would use 2 or 4 bytes depending on what they need.  If you want a general function to count the number of elements in a set I would do something like the following pseudocode.  Passing an integer will type cast the set to an integer value which will allow it to handle all currently allowable set sizes.  If you need more speed than that you can use the same logic in assemly language.  Let me know if there are questions.
function CountSetItems(SetValue : integer) : byte;
var
  Mask, Count : integer;
 
begin
  Mask := $80000000;
  Count := 0;
  While Mask <> 0 do
    begin
      if SetValue and Mask <> 0 then
        inc(Count);
      Mask := Mask shr 1;
    end;
  Result := Count;
end;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:developmentguru
ID: 20781858
P.S.  the pseudo code I posted has the added advantage that it does not matter what type of set you pass it.  The down side is that it tests for values that cannot exist within a specific set (it is testing for all 32 possibilities - if your set only contains 3...).  It should be possible to create a function where you pass in the type of set and the value and it counts it by looping the appropriate number of times for the set.  I just don't have time to try it tonight, sorry.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 20782724
just wondering what if you do this:
Include(Things, tgsNothing);
will Things contain any elements or will it contain nothing? ;)


sorry couldn't resist:)

ziolko.
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 83 total points
ID: 20784510
a small sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TThing = (tgsOne, tgsTwo, tgsThree);
  TThings = set of TThing;


function getSetCount(aSet : TThings) : integer;
var
  i : TThing;
begin
  result := 0;
  for i := low(TThing) to high(TThing) do
  begin
    if (i in aset) then
      inc(result);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  ThingSet : TThings;
begin
  ThingSet := [tgsOne, tgsTwo];
  showmessage('Items in Set: '+inttostr(getSetCount(ThingSet)));
  ThingSet := ThingSet - [tgsOne];
  showmessage('Items in Set: '+inttostr(getSetCount(ThingSet)));
  ThingSet := ThingSet + [tgsThree];
  showmessage('Items in Set: '+inttostr(getSetCount(ThingSet)));
  ThingSet := ThingSet + [tgsOne];
  showmessage('Items in Set: '+inttostr(getSetCount(ThingSet)));
  ThingSet := [];
  showmessage('Items in Set: '+inttostr(getSetCount(ThingSet)));
end;

end.

meikl ;-)
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21186034
Forced accept.

Computer101
EE Admin
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question