cdynset.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. Dynamic set
  3. Copyright (c) 2007-2026 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cdynset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype;
  22. type
  23. TDynSet = array of byte;
  24. PDynSet = ^TDynSet;
  25. { add e to s }
  26. procedure DynSetInclude(var s : TDynSet;e : integer);
  27. { add s to d }
  28. procedure DynSetIncludeSet(var d : TDynSet;const s : TDynSet);
  29. { remove s to d }
  30. procedure DynSetExcludeSet(var d : TDynSet;const s : TDynSet);
  31. { remove e from s }
  32. procedure DynSetExclude(var s : TDynSet;e : integer);
  33. { test if s contains e }
  34. function DynSetIn(const s : TDynSet;e : integer) : boolean;
  35. { d:=s1+s2; }
  36. procedure DynSetUnion(var d : TDynSet;const s1,s2 : TDynSet);
  37. { d:=s1*s2; }
  38. procedure DynSetIntersect(var d : TDynSet;const s1,s2 : TDynSet);
  39. { d:=s1-s2; }
  40. procedure DynSetDiff(var d : TDynSet;const s1,s2 : TDynSet);
  41. { s1<>s2; }
  42. function DynSetNotEqual(const s1,s2 : TDynSet) : boolean;
  43. { output DynSet }
  44. procedure PrintDynSet(var f : text;s : TDynSet);
  45. implementation
  46. uses
  47. cutils;
  48. procedure DynSetInclude(var s : tDynset;e : integer);
  49. var
  50. e8 : Integer;
  51. begin
  52. e8:=e div 8;
  53. if e8>high(s) then
  54. SetLength(s,e8+1);
  55. s[e8]:=s[e8] or (1 shl (e mod 8));
  56. end;
  57. procedure DynSetIncludeSet(var d : tDynset;const s : tDynset);
  58. var
  59. i : integer;
  60. begin
  61. if length(s)>length(d) then
  62. SetLength(d,length(s));
  63. for i:=0 to high(s) do
  64. d[i]:=d[i] or s[i];
  65. end;
  66. procedure DynSetExcludeSet(var d : tDynset;const s : tDynset);
  67. var
  68. i : integer;
  69. begin
  70. if length(s)>length(d) then
  71. SetLength(d,length(s));
  72. for i:=0 to high(s) do
  73. d[i]:=d[i] and not(s[i]);
  74. end;
  75. procedure DynSetExclude(var s : tDynset;e : integer);
  76. var
  77. e8 : Integer;
  78. begin
  79. e8:=e div 8;
  80. if e8<=high(s) then
  81. s[e8]:=s[e8] and not(1 shl (e mod 8));
  82. end;
  83. function DynSetIn(const s : tDynset;e : integer) : boolean;
  84. var
  85. e8 : Integer;
  86. begin
  87. e8:=e div 8;
  88. if e8<=high(s) then
  89. result:=(s[e8] and (1 shl (e mod 8)))<>0
  90. else
  91. result:=false;
  92. end;
  93. procedure DynSetUnion(var d : tDynset;const s1,s2 : tDynset);
  94. var
  95. i : integer;
  96. begin
  97. SetLength(d,max(Length(s1),Length(s2)));
  98. for i:=0 to min(high(s1),high(s2)) do
  99. d[i]:=s1[i] or s2[i];
  100. if high(s1)<high(s2) then
  101. for i:=high(s1)+1 to high(s2) do
  102. d[i]:=s2[i]
  103. else
  104. for i:=high(s2)+1 to high(s1) do
  105. d[i]:=s1[i];
  106. end;
  107. procedure DynSetIntersect(var d : tDynset;const s1,s2 : tDynset);
  108. var
  109. i : integer;
  110. begin
  111. SetLength(d,min(Length(s1),Length(s2)));
  112. for i:=0 to high(d) do
  113. d[i]:=s1[i] and s2[i];
  114. end;
  115. procedure DynSetDiff(var d : tDynset;const s1,s2 : tDynset);
  116. var
  117. i : integer;
  118. begin
  119. SetLength(d,length(s1));
  120. for i:=0 to high(d) do
  121. if i>high(s2) then
  122. d[i]:=s1[i]
  123. else
  124. d[i]:=s1[i] and not(s2[i]);
  125. end;
  126. function DynSetNotEqual(const s1,s2 : tDynset) : boolean;
  127. var
  128. i : integer;
  129. begin
  130. result:=true;
  131. { one set could be larger than the other }
  132. if length(s1)>length(s2) then
  133. begin
  134. for i:=0 to high(s2) do
  135. if s1[i]<>s2[i] then
  136. exit;
  137. { check remaining part being zero }
  138. for i:=length(s2) to high(s1) do
  139. if s1[i]<>0 then
  140. exit;
  141. end
  142. else
  143. begin
  144. for i:=0 to high(s1) do
  145. if s1[i]<>s2[i] then
  146. exit;
  147. { check remaining part being zero }
  148. for i:=length(s1) to high(s2) do
  149. if s2[i]<>0 then
  150. exit;
  151. end;
  152. result:=false;
  153. end;
  154. procedure PrintDynSet(var f : text;s : TDynSet);
  155. var
  156. i : integer;
  157. first : boolean;
  158. begin
  159. first:=true;
  160. for i:=0 to Length(s)*8 do
  161. begin
  162. if DynSetIn(s,i) then
  163. begin
  164. if not(first) then
  165. write(f,',');
  166. write(f,i);
  167. first:=false;
  168. end;
  169. end;
  170. end;
  171. end.