jset.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by Jonas Maebe,
  4. members of the Free Pascal development team.
  5. This file implements support infrastructure for sets under the JVM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  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.
  11. **********************************************************************}
  12. function FpcBitSet.clone: JLObject;
  13. begin
  14. result:=inherited;
  15. end;
  16. function FpcBitSet.add(elem: jint): FpcBitSet;
  17. begin
  18. &set(elem);
  19. result:=self;
  20. end;
  21. function FpcBitSet.addAll(s: FpcBitSet): FpcBitSet;
  22. begin
  23. &or(s);
  24. result:=self;
  25. end;
  26. function FpcBitSet.remove(elem: jint): FpcBitSet;
  27. begin
  28. clear(elem);
  29. result:=self;
  30. end;
  31. function FpcBitSet.removeAll(s: FpcBitSet): FpcBitSet;
  32. begin
  33. andnot(s);
  34. result:=self;
  35. end;
  36. function FpcBitSet.retainAll(s: FpcBitSet): FpcBitSet;
  37. begin
  38. &and(s);
  39. result:=self;
  40. end;
  41. function FpcBitSet.contains(elem: jint): boolean;
  42. begin
  43. result:=get(elem);
  44. end;
  45. function FpcBitSet.containsAll(s: FpcBitSet): boolean;
  46. var
  47. tmp: FpcBitSet;
  48. begin
  49. tmp:=FpcBitSet(clone);
  50. tmp.&and(s);
  51. result:=tmp.equals(s);
  52. end;
  53. function FpcBitSet.symdif(s: FpcBitSet): FpcBitSet;
  54. begin
  55. s.&xor(s);
  56. result:=self;
  57. end;
  58. class function FpcBitSet.range(start, stop: jint): FpcBitSet;
  59. begin
  60. result:=FpcBitSet.create(stop);
  61. result.&set(start,stop+1);
  62. end;
  63. class function FpcBitSet.&of(elem: jint): FpcBitSet;
  64. begin
  65. result:=FpcBitSet.create(elem);
  66. result.&set(elem);
  67. end;
  68. procedure fpc_bitset_copy(const src: FpcBitSet; dst: FpcBitSet); compilerproc;
  69. begin
  70. dst.clear();
  71. dst.&or(src);
  72. end;
  73. procedure fpc_enumset_copy(const src: JUEnumSet; dst: JUEnumSet); compilerproc;
  74. begin
  75. dst.clear();
  76. dst.addAll(src);
  77. end;
  78. function fpc_enumset_symdif(const set1, set2: JUEnumSet): JUEnumSet; compilerproc;
  79. var
  80. tmp: JUEnumSet;
  81. begin
  82. { (set1 + set 2) - (set1 * set2) }
  83. result:=JUEnumSet(set1.clone);
  84. result.addAll(set2);
  85. tmp:=JUEnumSet(set1.clone);
  86. tmp.retainAll(set2);
  87. result.removeAll(tmp);
  88. end;
  89. function fpc_bitset_from_string(const s: unicodestring): FpcBitSet; compilerproc;
  90. var
  91. i, bits: longint;
  92. wc: word;
  93. begin
  94. { all bits are encoded in the string characters }
  95. result:=FpcBitSet.Create(cardinal(length(s)+15) div 16);
  96. for i:=1 to length(s) do
  97. begin
  98. wc:=word(s[i]);
  99. if wc=0 then
  100. continue;
  101. for bits:=15 downto 0 do
  102. if (wc and (1 shl bits)) <> 0 then
  103. result.&set((i-1)*16+15-bits);
  104. end;
  105. end;
  106. function fpc_enumset_from_string(dummy: FpcEnumValueObtainable; const s: unicodestring): JUEnumSet; compilerproc;
  107. var
  108. i, bits: longint;
  109. wc: word;
  110. begin
  111. { all bits are encoded in the string characters }
  112. result:=JUEnumSet.noneOf(JLObject(dummy).getClass);
  113. for i:=1 to length(s) do
  114. begin
  115. wc:=word(s[i]);
  116. if wc=0 then
  117. continue;
  118. for bits:=15 downto 0 do
  119. if (wc and (1 shl bits)) <> 0 then
  120. result.add(dummy.fpcGenericValueOf((i-1)*16+15-bits));
  121. end;
  122. end;
  123. function fpc_enumset_to_int(const s: JUEnumSet; setbase, setsize: longint): jint; compilerproc;
  124. var
  125. it: JUIterator;
  126. ele: FpcEnumValueObtainable;
  127. val: longint;
  128. begin
  129. it:=s.iterator;
  130. result:=0;
  131. setsize:=setsize*8;
  132. while it.hasNext do
  133. begin
  134. ele:=FpcEnumValueObtainable(it.next);
  135. val:=ele.fpcOrdinal-setbase;
  136. result:=result or (1 shl (setsize-val));
  137. end;
  138. end;
  139. function fpc_enumset_to_long(const s: JUEnumSet; setbase, setsize: longint): jlong; compilerproc;
  140. var
  141. it: JUIterator;
  142. ele: FpcEnumValueObtainable;
  143. val: longint;
  144. begin
  145. it:=s.iterator;
  146. result:=0;
  147. setsize:=setsize*8;
  148. while it.hasNext do
  149. begin
  150. ele:=FpcEnumValueObtainable(it.next);
  151. val:=ele.fpcOrdinal-setbase;
  152. result:=result or (1 shl (setsize-val));
  153. end;
  154. end;
  155. function fpc_bitset_to_int(const s: FpcBitSet; setbase, setsize: longint): jint; compilerproc;
  156. var
  157. i, val: longint;
  158. begin
  159. result:=0;
  160. setsize:=setsize*8;
  161. i:=s.nextSetBit(0);
  162. while i>=0 do
  163. begin
  164. val:=i-setbase;
  165. result:=result or (1 shl (setsize-val));
  166. i:=s.nextSetBit(i+1);
  167. end;
  168. end;
  169. function fpc_bitset_to_long(const s: FpcBitSet; setbase, setsize: longint): jlong; compilerproc;
  170. var
  171. i, val: longint;
  172. begin
  173. result:=0;
  174. setsize:=setsize*8;
  175. i:=s.nextSetBit(0);
  176. while i>=0 do
  177. begin
  178. val:=i-setbase;
  179. result:=result or (1 shl (setsize-val));
  180. i:=s.nextSetBit(i+1);
  181. end;
  182. end;
  183. function fpc_int_to_bitset(const val: jint; setbase, setsize: jint): FpcBitSet; compilerproc;
  184. var
  185. i, setval: jint;
  186. begin
  187. result:=FpcBitSet.create;
  188. if val<>0 then
  189. begin
  190. setsize:=setsize*8;
  191. for i:=0 to setsize-1 do
  192. if (val and (jint(1) shl (setsize-i)))<>0 then
  193. result.&set(i+setbase);
  194. end;
  195. end;
  196. function fpc_long_to_bitset(const val: jint; setbase, setsize: jint): FpcBitSet; compilerproc;
  197. var
  198. i, setval: jint;
  199. begin
  200. result:=FpcBitSet.create;
  201. if val<>0 then
  202. begin
  203. setsize:=setsize*8;
  204. for i:=0 to setsize-1 do
  205. if (val and (jlong(1) shl (setsize-i)))<>0 then
  206. result.&set(i+setbase);
  207. end;
  208. end;
  209. function fpc_enumset_to_bitset(const val: JUEnumSet; fromsetbase, tosetbase: jint): FpcBitSet; compilerproc;
  210. var
  211. it: JUIterator;
  212. ele: FpcEnumValueObtainable;
  213. i: longint;
  214. begin
  215. result:=FpcBitSet.Create;
  216. it:=val.iterator;
  217. while it.hasNext do
  218. begin
  219. ele:=FpcEnumValueObtainable(it.next);
  220. i:=ele.fpcOrdinal-fromsetbase;
  221. result.&set(i+tosetbase);
  222. end;
  223. end;
  224. function fpc_bitset_to_bitset(const s: FpcBitSet; fromsetbase, tosetbase: jint): FpcBitSet; compilerproc;
  225. var
  226. i, val: longint;
  227. begin
  228. result:=FpcBitSet.create;
  229. i:=s.nextSetBit(0);
  230. while i>=0 do
  231. begin
  232. val:=i-fromsetbase;
  233. result.&set(val+tosetbase);
  234. i:=s.nextSetBit(i+1);
  235. end;
  236. end;