Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-11

Forin feature with tBits

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dariusz Mazur
If try to apply forin feature to tBits.
 But I have two problems
1. function FindFirstBit has bug
try:
  List := TEnumBits.Create(10);
  List.bits[1]:=true;
  List.bits[6]:=true;
  writeln(list.findFirstBit(true));
  writeln(list.findNextBit);
  writeln(list.findNextBit);

should return -1, but exception received

2. findfirstbit is not multithread aware (because state is per object, not local)

Should I prepare patch?
Whats about used names?

Darek

Reply by email

Alexander Klenin
to Dariusz Mazur

2009/11/7 Dariusz Mazur :

If try to apply forin feature to tBits. But I have two problems 1. function FindFirstBit has bug

[skipped]

2. findfirstbit is not multithread aware (because state is per object, not local)

Yes, this is a good example why separate iterators are preferable to the ones built into container.

Should I prepare patch? Whats about used names?

IMHO your names (EnumSet/EnumClear) are ok.
Obviously, the implementation should not call FindFirstBit, but copy it's logic. To avoid code duplication,

I think you can to it the other way around -- let FindFirstBit create a enumerator and store it inside the TBits instance. It would be even better to drop FindFirstBit/FindNextBit altogether, but I do not know how large compatibility problem it might cause.

[skipped most of code]

Show quoted text 13 lines
function tEnumBits.enumSet:tBitEnumerator; begin  result:=tBitEnumerator.create(self,true); end; function tEnumBits.enumClear:tBitEnumerator; begin  result:=tBitEnumerator.create(self,false); end; operator enumerator (AEnumerator: TBitEnumerator): TBitEnumerator; begin  Result := AEnumerator end;

IMHO, this code demonstrates an inconvenience of the current for..in implementation.
The last "operator enumerator" declaration is clearly redundant. I have two alternative suggestions here:
1) Let for..in accept the enumerator instead of the container as the second argument.
2) Define the trivial "operator enumerator" as above for some basic type, e.g. TEnumerator,
maybe in Classes.pp or even System unit. After that, one can inherit TBitEnumerator from TEnumerator to avoid the need for "operator enumerator" declaration.

Paul Ishenin, what do you think?

Alexander S. Klenin

Reply by email

Paul Ishenin
to Alexander Klenin
Alexander Klenin wrote 14 lines
IMHO, this code demonstrates an inconvenience of the current for..in implementation. The last "operator enumerator" declaration is clearly redundant. I have two alternative suggestions here: 1) Let for..in accept the enumerator instead of the container as the second argument. 2) Define the trivial "operator enumerator" as above for some basic type, e.g. TEnumerator, maybe in Classes.pp or even System unit. After that, one can inherit TBitEnumerator from TEnumerator to avoid the need for "operator enumerator" declaration. Paul Ishenin, what do you think?

I think that it will be nice to have some extension to select paticular enumerator for the for-in loop but it is not yet available. And I think that your first suggestion will add undeeded ambiguity to the compiler since the expression can be a container and enumerator at the same moment.

Best regards,
Paul Ishenin.

Reply by email

Dariusz Mazur
to Alexander Klenin: 2009/11/7 Dariusz Mazur : [skipped] Yes, this is a good example why separate iterators…

Alexander Klenin pisze:

Show quoted text 29 lines
2009/11/7 Dariusz Mazur : > If try to apply forin feature to tBits. > But I have two problems > 1. function FindFirstBit has bug > [skipped] > 2. findfirstbit is not multithread aware (because state is per object, not > local) > Yes, this is a good example why separate iterators are preferable to the ones built into container. > Should I prepare patch? > Whats about used names? > IMHO your names (EnumSet/EnumClear) are ok. Obviously, the implementation should not call FindFirstBit, but copy it's logic. To avoid code duplication, I think you can to it the other way around -- let FindFirstBit create a enumerator and store it inside the TBits instance.

I'm not sure, but if this solution is not multithread aware too?

Show quoted text 35 lines
It would be even better to drop FindFirstBit/FindNextBit altogether, but I do not know how large compatibility problem it might cause. [skipped most of code] > function tEnumBits.enumSet:tBitEnumerator; > begin > result:=tBitEnumerator.create(self,true); > end; > function tEnumBits.enumClear:tBitEnumerator; > begin > result:=tBitEnumerator.create(self,false); > end; > > operator enumerator (AEnumerator: TBitEnumerator): TBitEnumerator; > begin > Result := AEnumerator > end; > IMHO, this code demonstrates an inconvenience of the current for..in implementation. The last "operator enumerator" declaration is clearly redundant. I have two alternative suggestions here: 1) Let for..in accept the enumerator instead of the container as the second argument. 2) Define the trivial "operator enumerator" as above for some basic type, e.g. TEnumerator, maybe in Classes.pp or even System unit. After that, one can inherit TBitEnumerator from TEnumerator to avoid the need for "operator enumerator" declaration.

I've thinking about second solution. Now enumerator is passed through operator and its better, when always is the same.
For me it should be done in sysutils, similar as Exception

Darek

Reply by email

Alexander Klenin
to Dariusz Mazur
Dariusz Mazur wrote 8 lines
Alexander Klenin pisze: > Obviously, the implementation should not call FindFirstBit, > but copy it's logic. To avoid code duplication, > I think you can to it the other way around -- let FindFirstBit > create a enumerator and store it inside the TBits instance. > I'm not sure, but if this solution is not multithread aware too?

Yes, as I wrote below, the best solution would be to drop the current interface...

> It would be even better to drop FindFirstBit/FindNextBit altogether, > but I do not know how large compatibility problem it might cause.

... but _if_ the backwards compatibility is deemed important enough, my suggestion will allow to maintain it with the minimal code duplication.

Alexander S. Klenin

Reply by email