![]() |
Anzahl Elemente in "set of"
Hallo,
ich habe in einem Programm ein "set of"-Type deklariert. Beispiel:
Delphi-Quellcode:
Meine Frage ist nun wie ich herausfinden kann, wieviele Attribute(?) nun in dem set of enthalten sind?
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline); TFontStyles = set of TFontStyle; procedure Test(const Styles: TFontStyles); var anzahlAttribute: Integer; begin anzahlAttribute := Length(Styles); // <--- Compile-Error case anzahlAttribute of 0: ShowMessage('STYLES ist leer'); 1: ShowMessage('STYLES hat ein Attribut'); else ShowMessage(Format('STYLES hat %d Attribute', [anzahlAttribute])); end; end; |
Re: Anzahl Elemente in "set of"
soweit ich weiß sind ja max. 256 elemente möglich, aber das bringt uns hierbei auch nur bedingt weiter.
eine möglichkeit wäre es, eigenständig darüber zu iterrieren und von mir aus einen counter zu erhöhen, aber das klingt mir eher nach einer quick & dirty variante...
Delphi-Quellcode:
klar ist mir schon auch, dass das bei max. 256 elementen nicht all zu lange dauert, aber es muss doch auch anders gehen.
procedure Test(const Styles: TFontStyles);
var i : Integer; len : Integer; begin { ... } len := 0; for value in Styles do inc(len); { ... } end; |
Re: Anzahl Elemente in "set of"
AFAIK ist das nicht möglich. Ich hatte so eine ähnliche Frage schon mal gestellt und da ist in der diskussion herausgekommen das dies nicht sicher möglich ist, vor allem wenn man nummerierte Enums mit "Nummernlücken" verwendet.
Delphi-Quellcode:
type TMyEnum (Enum1 = 4, Enum2 = 12, Enum3 = 122);
|
Re: Anzahl Elemente in "set of"
Anders als durchitterieren geht nicht. Du musst halt irgendwie die (binäre) Quersumme bilden. Und dafür gibt es keinen mir bekannten Intel-OpCode. Eine mögliche Delphi-Funktion kann auch nur eine For-Schleife verwenden.
![]()
Code:
Aber ob dies zwingend notwendig ist? Wie das Fischlein schon sagte, sind 256 Elemente nicht soooo viel.
UInt8 bitcount8(UInt8 c)
{ const UInt8 lookup[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; UInt8 result; result = lookup[c&0xF]; result += lookup[(c>>4)&0xF]; return result; } |
Re: Anzahl Elemente in "set of"
Zitat:
Seit wann ist das denn in Delphi möglich :gruebel: EDIT: @ThreadStarter Hast du schonmal High() ausprobiert? EDIT2: Ok, das geht nur bei Enums - und nicht bei Set of Enums .. MfG :mrgreen: |
Re: Anzahl Elemente in "set of"
|
Re: Anzahl Elemente in "set of"
Zitat:
Zitat:
|
Re: Anzahl Elemente in "set of"
Zitat:
|
Re: Anzahl Elemente in "set of"
probier mal das hier
Delphi-Quellcode:
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline); TFontStyles = set of TFontStyle; const FS = [fsBold, fsItalic, fsUnderline]; function CntAttr( Fnts: TFontStyles ): Byte; var i: Integer; begin Result := 0; for i := 0 to $FF do if TFontStyle(i) in Fnts then inc( Result ); end; procedure TForm1.FormCreate(Sender: TObject); begin ShowMessage( IntToStr( CntAttr( FS ) ) ); // zeig mir 3 an :) end; |
Re: Anzahl Elemente in "set of"
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; |
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;
|
Re: Anzahl Elemente in "set of"
noch eine Variante :wink:
Delphi-Quellcode:
alex
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; |
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]); |
Re: Anzahl Elemente in "set of"
evtl. ist RTTI Dein Freund:
Delphi-Quellcode:
Der Aufruf dann:
function CountElements(ATypeAddress: pointer): integer;
var lTypeData: PTypeData; begin lTypeData:= GetTypeData(ATypeAddress); Result := lTypeData^.MaxValue; end;
Delphi-Quellcode:
myNumerousElements:= CountElements(TypInfo(TFontStyle));
|
Re: Anzahl Elemente in "set of"
Zitat:
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? |
Re: Anzahl Elemente in "set of"
Zitat:
werd ich wohl nochmal nachdenken müssen.:gruebel: alex |
Re: Anzahl Elemente in "set of"
Zitat:
Delphi-Quellcode:
// copy & paste =)
type TFontStyle = (fsBold=0, fsItalic=1, fsStrikeOut=6, fsUnderline=7); |
Re: Anzahl Elemente in "set of"
:gruebel: Zumindes <> 0 geht so...
Delphi-Quellcode:
Zumindest in D5 sind die Bits je nach Listen-Element gesetzt.
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; |
Re: Anzahl Elemente in "set of"
Zitat:
Delphi-Quellcode:
Tata = Compilerfehler
type
TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline=15); TFontStyles = set of TFontStyle; procedure Test(const Styles: TFontStyles); var anzahlAttribute: Integer; begin anzahlAttribute := Byte(Styles); // <--- Tata |
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. |
Re: Anzahl Elemente in "set of"
Beim Casten mußt du einfach nur einen Typen gleicher Größe haben ... also einem, der dann SizeOf(TFontStyles) entspricht.
das kann auch ein Record oder StaticArray sein.
Delphi-Quellcode:
Type T = Array[0..31] of Byte;
P = ^T; Count := 0; For i := 0 to SizeOf(Typ) do Count := Count + ZähleBitsInByte(P(@TypVar)[i]); |
Re: Anzahl Elemente in "set of"
Ja, mit einem Array of Byte geht es.
Aber wird dann wohl nicht wie erhofft einfacher auf die Werte zuzugreifen. Gesetzte Bit's müssen ja immer noch gezählt werden. |
Re: Anzahl Elemente in "set of"
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:00 Uhr. |
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 by Thomas Breitkreuz