Archive
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;
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012