jset.inc 6.6 KB

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