Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Anzahl Elemente in "set of" (https://www.delphipraxis.net/132222-anzahl-elemente-set.html)

himitsu 8. Apr 2009 12:35

Re: Anzahl Elemente in "set of"
 
das mit dem in klappt nur, wenn man über eine Konstante geht und die will auch aktuell gehalten werden.

sowas wäre möglich, aber auch nur, wenn die Elemente durchgängig belegt sind und zwischendurch Keine fehlen.
Delphi-Quellcode:
Count := Ord(High(TFontStyle)) - Ord(Low(TFontStyle)) + 1;

alex517 8. Apr 2009 12:35

Re: Anzahl Elemente in "set of"
 
noch eine Variante :wink:
Delphi-Quellcode:
type
  TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline);
  TFontStyles = set of TFontStyle;

function CountStyles(const AStyles: TFontStyles): integer;
var
  f: TFontStyle;
begin
  Result := 0;
  for f := low(TFontStyle) to high(TFontStyle) do
    if f in AStyles then
      inc(Result);
end;
alex

himitsu 8. Apr 2009 12:44

Re: Anzahl Elemente in "set of"
 
@axel: dafür brauchst du aber auch eine "aktuelle" Variable/Konstante.

deine Funktion + diese Definition (ähnliches gilt auch für die anderen Zählfunktionen) würde hier 8 liefern und nicht 4.
Delphi-Quellcode:
type
  TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7);
  TFontStyles = set of TFontStyle;

x := CountStyles([TFontStyle(0)..TFontStyle(7)]);
// oder
x := CountStyles([fsBold..fsUnderline]);

webcss 8. Apr 2009 12:55

Re: Anzahl Elemente in "set of"
 
evtl. ist RTTI Dein Freund:
Delphi-Quellcode:
function CountElements(ATypeAddress: pointer): integer;
var
  lTypeData: PTypeData;
begin
  lTypeData:= GetTypeData(ATypeAddress);
  Result := lTypeData^.MaxValue;
end;
Der Aufruf dann:
Delphi-Quellcode:
myNumerousElements:= CountElements(TypInfo(TFontStyle));

DevilsCamp 8. Apr 2009 13:16

Re: Anzahl Elemente in "set of"
 
Zitat:

Zitat von Tyrael Y.
Ich benutze folgende Funktion, die halt wie hier beschrieben itteriert.

Delphi-Quellcode:
//  anzahl := GetCountOfSetElements(@mySet, sizeof(mySet));
function GetCountOfSetElements(APointerToSet: Pointer; SizeOfSet: Cardinal): Cardinal;
const C_LOOKUP : packed array[0..15] of Byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
var LByte: Byte;
begin
  Result := 0;

  while SizeOfSet > 0 do
  begin
    LByte := PByte(APointerToSet)^;

    Inc(Result, C_LOOKUP[LByte and $0F]);
    Inc(Result, C_LOOKUP[LByte shr 4]);

    Dec(SizeOfSet);
    Inc(PByte(APointerToSet));
  end;
end;

:thumb:
Das funktioniert selbst bei Sets mit 256 Elementen :) (zumindest unter Lazarus, Delphi kann ich grad nicht testen).
Wäre das nicht was für die Code-Library?

alex517 8. Apr 2009 13:22

Re: Anzahl Elemente in "set of"
 
Zitat:

Zitat von himitsu
@axel: dafür brauchst du aber auch eine "aktuelle" Variable/Konstante.

deine Funktion + diese Definition (ähnliches gilt auch für die anderen Zählfunktionen) würde hier 8 liefern und nicht 4.
Delphi-Quellcode:
type
  TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7);
  TFontStyles = set of TFontStyle;

x := CountStyles([TFontStyle(0)..TFontStyle(7)]);
// oder
x := CountStyles([fsBold..fsUnderline]);

Uups..,
werd ich wohl nochmal nachdenken müssen.:gruebel:
alex

s.h.a.r.k 9. Apr 2009 21:00

Re: Anzahl Elemente in "set of"
 
Zitat:

Zitat von himitsu
das mit dem in klappt nur, wenn man über eine Konstante geht und die will auch aktuell gehalten werden.

sowas wäre möglich, aber auch nur, wenn die Elemente durchgängig belegt sind und zwischendurch Keine fehlen.
Delphi-Quellcode:
Count := Ord(High(TFontStyle)) - Ord(Low(TFontStyle)) + 1;

das wird wohl nicht funktionieren, da man ja auch folgendes machen kann:
Delphi-Quellcode:
// copy & paste =)
type
  TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7);

Satty67 9. Apr 2009 21:09

Re: Anzahl Elemente in "set of"
 
:gruebel: Zumindes <> 0 geht so...
Delphi-Quellcode:
type
  TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline);
  TFontStyles = set of TFontStyle;

procedure Test(const Styles: TFontStyles);
var
  anzahlAttribute: Integer;
begin
  anzahlAttribute := Byte(Styles); // <--- Tata

  case anzahlAttribute of
    0: ShowMessage('STYLES ist leer');
    1: ShowMessage('STYLES hat ein Attribut');
  else
    ShowMessage(Format('STYLES hat %d Attribute', [anzahlAttribute]));
  end;
end;
Zumindest in D5 sind die Bits je nach Listen-Element gesetzt.

himitsu 9. Apr 2009 21:13

Re: Anzahl Elemente in "set of"
 
Zitat:

das wird wohl nicht funktionieren, da man ja auch folgendes machen kann:
drum schrieb ich, wenn zwischendurch nix fehlt!

Delphi-Quellcode:
type
  TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline=15);
  TFontStyles = set of TFontStyle;

procedure Test(const Styles: TFontStyles);
var
  anzahlAttribute: Integer;
begin
  anzahlAttribute := Byte(Styles); // <--- Tata
Tata = Compilerfehler

Satty67 9. Apr 2009 21:14

Re: Anzahl Elemente in "set of"
 
Propier mal die anderen Integertypen.

In D5 kann ich bei Sets bis 8 Elemente so die Bits direkt abfragen.

€: Bei mehr als 8 Elementen muß ich ein Word casten. Also ist an der Adresse ein Bit-Muster abgelegt. Da müsste sich doch mit einem Pointer was machen lassen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:34 Uhr.
Seite 2 von 3     12 3      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz