Home > OP4JS, Quartex Pascal > Emulating sets in Smart Mobile Studio

Emulating sets in Smart Mobile Studio

Note: updated the article to also have code with unlimited set values (see bottom of page)

The Smart Mobile Studio compiler does not support sets. But there are ways to getting around that, as long as you can live with sets that has a maximum range of 32 elements. Here is a snippet from the QTX RTL which is being designed as I type. Here sets are solved as such:


type
TQTXValueSet = Record
  bsValue: Integer;
  function  contains(const aValue:Integer):Boolean;overload;
  Procedure Include(const aValue:Integer);
  Procedure Exclude(const aValue:Integer);
End;

//#############################################################################
// TQTXValueSet
//#############################################################################

function TQTXValueSet.Contains(const aValue:Integer):Boolean;
Begin
  result:=TInteger.getBit(aValue,bsValue);
end;

Procedure TQTXValueSet.Include(const aValue:Integer);
Begin
  TInteger.setBit(aValue,true,bsValue);
end;

Procedure TQTXValueSet.Exclude(const aValue:Integer);
Begin
  TInteger.setBit(aValue,false,bsValue);
end;

You can now do stuff like:

const
  csNone       = 0;
  csCreating   = 1;
  csDestroying = 2;

if not FMySet.contains(csCreating) then
FMySet.include(csCreating);

Neat 🙂

Updated

It strikes me that, while the above method is by far the most cost-effective way to emulate sets – in that it will only consume one integer (4 bytes) of memory, it is neither the most flexible or faster variation on the theme. And in all honesty, worrying about memory consumption in a garbage collected script environment is more of an old habit of native languages than a real-life scenario.

As such, here is a faster version that supports an endless number of values I made using arrays.


TQTXValueSet = Record
  vsData: Array of Integer;
  function  Contains(const aSetValue:Integer):Boolean;
  Procedure Include(const aSetValue:Integer);
  Procedure Exclude(const aSetValue:Integer);
End;

//#############################################################################
// TQTXValueSet
//#############################################################################

function TQTXValueSet.Contains(const aSetValue:Integer):Boolean;
Begin
  result:=False;
  if vsData.count>0 then
  result:=vsData.IndexOf(aSetValue,vsData.Low)>=0;
end;

Procedure TQTXValueSet.Include(const aSetValue:Integer);
begin
  if not Contains(aSetValue) then
  vsData.Add(aSetValue);
end;

Procedure TQTXValueSet.Exclude(const aSetValue:Integer);
var
  mIndex: Integer;
begin
  mIndex:=vsData.IndexOf(aSetValue,vsData.low);
  if mIndex>=0 then
  vsData.delete(mIndex);
end;

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: