![]() |
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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:17 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