PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/lib/antlr-2.7.5/lib/csharp/src/antlr.collections.impl/BitSet.cs

https://github.com/w4x/boolangstudio
C# | 539 lines | 363 code | 46 blank | 130 comment | 62 complexity | 2a99ff7621e083da0310197fb67311ee MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using ArrayList = System.Collections.ArrayList;
  3. //using CharFormatter = antlr.CharFormatter;
  4. namespace antlr.collections.impl
  5. {
  6. /*ANTLR Translator Generator
  7. * Project led by Terence Parr at http://www.jGuru.com
  8. * Software rights: http://www.antlr.org/license.html
  9. *
  10. * $Id:$
  11. */
  12. //
  13. // ANTLR C# Code Generator by Micheal Jordan
  14. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  15. // Anthony Oguntimehin
  16. //
  17. // With many thanks to Eric V. Smith from the ANTLR list.
  18. //
  19. /*A BitSet to replace java.util.BitSet.
  20. * Primary differences are that most set operators return new sets
  21. * as opposed to oring and anding "in place". Further, a number of
  22. * operations were added. I cannot contain a BitSet because there
  23. * is no way to access the internal bits (which I need for speed)
  24. * and, because it is final, I cannot subclass to add functionality.
  25. * Consider defining set degree. Without access to the bits, I must
  26. * call a method n times to test the ith bit...ack!
  27. *
  28. * Also seems like or() from util is wrong when size of incoming set is bigger
  29. * than this.bits.length.
  30. *
  31. * @author Terence Parr
  32. * @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>
  33. */
  34. public class BitSet : ICloneable
  35. {
  36. protected internal const int BITS = 64; // number of bits / long
  37. protected internal const int NIBBLE = 4;
  38. protected internal const int LOG_BITS = 6; // 2^6 == 64
  39. /*We will often need to do a mod operator (i mod nbits). Its
  40. * turns out that, for powers of two, this mod operation is
  41. * same as (i & (nbits-1)). Since mod is slow, we use a
  42. * precomputed mod mask to do the mod instead.
  43. */
  44. protected internal static readonly int MOD_MASK = BITS - 1;
  45. /*The actual data bits */
  46. protected internal long[] dataBits;
  47. /*Construct a bitset of size one word (64 bits) */
  48. public BitSet() : this(BITS)
  49. {
  50. }
  51. /*Construction from a static array of longs */
  52. public BitSet(long[] bits_)
  53. {
  54. dataBits = bits_;
  55. }
  56. /*Construct a bitset given the size
  57. * @param nbits The size of the bitset in bits
  58. */
  59. public BitSet(int nbits)
  60. {
  61. dataBits = new long[((nbits - 1) >> LOG_BITS) + 1];
  62. }
  63. /*or this element into this set (grow as necessary to accommodate) */
  64. public virtual void add(int el)
  65. {
  66. int n = wordNumber(el);
  67. if (n >= dataBits.Length)
  68. {
  69. growToInclude(el);
  70. }
  71. dataBits[n] |= bitMask(el);
  72. }
  73. public virtual BitSet and(BitSet a)
  74. {
  75. BitSet s = (BitSet) this.Clone();
  76. s.andInPlace(a);
  77. return s;
  78. }
  79. public virtual void andInPlace(BitSet a)
  80. {
  81. int min = (int) (Math.Min(dataBits.Length, a.dataBits.Length));
  82. for (int i = min - 1; i >= 0; i--)
  83. {
  84. dataBits[i] &= a.dataBits[i];
  85. }
  86. // clear all bits in this not present in a (if this bigger than a).
  87. for (int i = min; i < dataBits.Length; i++)
  88. {
  89. dataBits[i] = 0;
  90. }
  91. }
  92. private static long bitMask(int bitNumber)
  93. {
  94. int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS
  95. return 1L << bitPosition;
  96. }
  97. public virtual void clear()
  98. {
  99. for (int i = dataBits.Length - 1; i >= 0; i--)
  100. {
  101. dataBits[i] = 0;
  102. }
  103. }
  104. public virtual void clear(int el)
  105. {
  106. int n = wordNumber(el);
  107. if (n >= dataBits.Length)
  108. {
  109. // grow as necessary to accommodate
  110. growToInclude(el);
  111. }
  112. dataBits[n] &= ~ bitMask(el);
  113. }
  114. public virtual object Clone()
  115. {
  116. BitSet s;
  117. try
  118. {
  119. s = new BitSet();
  120. s.dataBits = new long[dataBits.Length];
  121. Array.Copy(dataBits, 0, s.dataBits, 0, dataBits.Length);
  122. }
  123. catch //(System.Exception e)
  124. {
  125. throw new System.ApplicationException();
  126. }
  127. return s;
  128. }
  129. public virtual int degree()
  130. {
  131. int deg = 0;
  132. for (int i = dataBits.Length - 1; i >= 0; i--)
  133. {
  134. long word = dataBits[i];
  135. if (word != 0L)
  136. {
  137. for (int bit = BITS - 1; bit >= 0; bit--)
  138. {
  139. if ((word & (1L << bit)) != 0)
  140. {
  141. deg++;
  142. }
  143. }
  144. }
  145. }
  146. return deg;
  147. }
  148. override public int GetHashCode()
  149. {
  150. return dataBits.GetHashCode();
  151. }
  152. /*code "inherited" from java.util.BitSet */
  153. override public bool Equals(object obj)
  154. {
  155. if ((obj != null) && (obj is BitSet))
  156. {
  157. BitSet bset = (BitSet) obj;
  158. int n = (int) (System.Math.Min(dataBits.Length, bset.dataBits.Length));
  159. for (int i = n; i-- > 0; )
  160. {
  161. if (dataBits[i] != bset.dataBits[i])
  162. {
  163. return false;
  164. }
  165. }
  166. if (dataBits.Length > n)
  167. {
  168. for (int i = (int) (dataBits.Length); i-- > n; )
  169. {
  170. if (dataBits[i] != 0)
  171. {
  172. return false;
  173. }
  174. }
  175. }
  176. else if (bset.dataBits.Length > n)
  177. {
  178. for (int i = (int) (bset.dataBits.Length); i-- > n; )
  179. {
  180. if (bset.dataBits[i] != 0)
  181. {
  182. return false;
  183. }
  184. }
  185. }
  186. return true;
  187. }
  188. return false;
  189. }
  190. /*
  191. * Grows the set to a larger number of bits.
  192. * @param bit element that must fit in set
  193. */
  194. public virtual void growToInclude(int bit)
  195. {
  196. int newSize = (int) (System.Math.Max(dataBits.Length << 1, numWordsToHold(bit)));
  197. long[] newbits = new long[newSize];
  198. Array.Copy(dataBits, 0, newbits, 0, dataBits.Length);
  199. dataBits = newbits;
  200. }
  201. public virtual bool member(int el)
  202. {
  203. int n = wordNumber(el);
  204. if (n >= dataBits.Length)
  205. return false;
  206. return (dataBits[n] & bitMask(el)) != 0;
  207. }
  208. public virtual bool nil()
  209. {
  210. for (int i = dataBits.Length - 1; i >= 0; i--)
  211. {
  212. if (dataBits[i] != 0)
  213. return false;
  214. }
  215. return true;
  216. }
  217. public virtual BitSet not()
  218. {
  219. BitSet s = (BitSet) this.Clone();
  220. s.notInPlace();
  221. return s;
  222. }
  223. public virtual void notInPlace()
  224. {
  225. for (int i = dataBits.Length - 1; i >= 0; i--)
  226. {
  227. dataBits[i] = ~ dataBits[i];
  228. }
  229. }
  230. /*complement bits in the range 0..maxBit. */
  231. public virtual void notInPlace(int maxBit)
  232. {
  233. notInPlace(0, maxBit);
  234. }
  235. /*complement bits in the range minBit..maxBit.*/
  236. public virtual void notInPlace(int minBit, int maxBit)
  237. {
  238. // make sure that we have room for maxBit
  239. growToInclude(maxBit);
  240. for (int i = minBit; i <= maxBit; i++)
  241. {
  242. int n = wordNumber(i);
  243. dataBits[n] ^= bitMask(i);
  244. }
  245. }
  246. private int numWordsToHold(int el)
  247. {
  248. return (el >> LOG_BITS) + 1;
  249. }
  250. public static BitSet of(int el)
  251. {
  252. BitSet s = new BitSet(el + 1);
  253. s.add(el);
  254. return s;
  255. }
  256. /*return this | a in a new set */
  257. public virtual BitSet or(BitSet a)
  258. {
  259. BitSet s = (BitSet) this.Clone();
  260. s.orInPlace(a);
  261. return s;
  262. }
  263. public virtual void orInPlace(BitSet a)
  264. {
  265. // If this is smaller than a, grow this first
  266. if (a.dataBits.Length > dataBits.Length)
  267. {
  268. setSize((int) (a.dataBits.Length));
  269. }
  270. int min = (int) (System.Math.Min(dataBits.Length, a.dataBits.Length));
  271. for (int i = min - 1; i >= 0; i--)
  272. {
  273. dataBits[i] |= a.dataBits[i];
  274. }
  275. }
  276. // remove this element from this set
  277. public virtual void remove(int el)
  278. {
  279. int n = wordNumber(el);
  280. if (n >= dataBits.Length)
  281. {
  282. growToInclude(el);
  283. }
  284. dataBits[n] &= ~ bitMask(el);
  285. }
  286. /*
  287. * Sets the size of a set.
  288. * @param nwords how many words the new set should be
  289. */
  290. private void setSize(int nwords)
  291. {
  292. long[] newbits = new long[nwords];
  293. int n = (int) (System.Math.Min(nwords, dataBits.Length));
  294. Array.Copy(dataBits, 0, newbits, 0, n);
  295. dataBits = newbits;
  296. }
  297. public virtual int size()
  298. {
  299. return dataBits.Length << LOG_BITS; // num words * bits per word
  300. }
  301. /*return how much space is being used by the dataBits array not
  302. * how many actually have member bits on.
  303. */
  304. public virtual int lengthInLongWords()
  305. {
  306. return dataBits.Length;
  307. }
  308. /*Is this contained within a? */
  309. public virtual bool subset(BitSet a)
  310. {
  311. if (a == null) //(a == null || !(a is BitSet))
  312. return false;
  313. return this.and(a).Equals(this);
  314. }
  315. /*Subtract the elements of 'a' from 'this' in-place.
  316. * Basically, just turn off all bits of 'this' that are in 'a'.
  317. */
  318. public virtual void subtractInPlace(BitSet a)
  319. {
  320. if (a == null)
  321. return ;
  322. // for all words of 'a', turn off corresponding bits of 'this'
  323. for (int i = 0; i < dataBits.Length && i < a.dataBits.Length; i++)
  324. {
  325. dataBits[i] &= ~ a.dataBits[i];
  326. }
  327. }
  328. public virtual int[] toArray()
  329. {
  330. int[] elems = new int[degree()];
  331. int en = 0;
  332. for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)
  333. {
  334. if (member(i))
  335. {
  336. elems[en++] = i;
  337. }
  338. }
  339. return elems;
  340. }
  341. public virtual long[] toPackedArray()
  342. {
  343. return dataBits;
  344. }
  345. override public string ToString()
  346. {
  347. return ToString(",");
  348. }
  349. /*Transform a bit set into a string by formatting each element as an integer
  350. * @separator The string to put in between elements
  351. * @return A commma-separated list of values
  352. */
  353. public virtual string ToString(string separator)
  354. {
  355. string str = "";
  356. for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)
  357. {
  358. if (member(i))
  359. {
  360. if (str.Length > 0)
  361. {
  362. str += separator;
  363. }
  364. str = str + i;
  365. }
  366. }
  367. return str;
  368. }
  369. /*Create a string representation where instead of integer elements, the
  370. * ith element of vocabulary is displayed instead. Vocabulary is a Vector
  371. * of Strings.
  372. * @separator The string to put in between elements
  373. * @return A commma-separated list of character constants.
  374. */
  375. public virtual string ToString(string separator, ArrayList vocabulary)
  376. {
  377. if (vocabulary == null)
  378. {
  379. return ToString(separator);
  380. }
  381. string str = "";
  382. for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)
  383. {
  384. if (member(i))
  385. {
  386. if (str.Length > 0)
  387. {
  388. str += separator;
  389. }
  390. if (i >= vocabulary.Count)
  391. {
  392. str += "<bad element " + i + ">";
  393. }
  394. else if (vocabulary[i] == null)
  395. {
  396. str += "<" + i + ">";
  397. }
  398. else
  399. {
  400. str += (string) vocabulary[i];
  401. }
  402. }
  403. }
  404. return str;
  405. }
  406. /*
  407. * Dump a comma-separated list of the words making up the bit set.
  408. * Split each 64 bit number into two more manageable 32 bit numbers.
  409. * This generates a comma-separated list of C++-like unsigned long constants.
  410. */
  411. public virtual string toStringOfHalfWords()
  412. {
  413. string s = new string("".ToCharArray());
  414. for (int i = 0; i < dataBits.Length; i++)
  415. {
  416. if (i != 0)
  417. s += ", ";
  418. long tmp = dataBits[i];
  419. tmp &= 0xFFFFFFFFL;
  420. s += (tmp + "UL");
  421. s += ", ";
  422. tmp = SupportClass.URShift(dataBits[i], 32);
  423. tmp &= 0xFFFFFFFFL;
  424. s += (tmp + "UL");
  425. }
  426. return s;
  427. }
  428. /*
  429. * Dump a comma-separated list of the words making up the bit set.
  430. * This generates a comma-separated list of Java-like long int constants.
  431. */
  432. public virtual string toStringOfWords()
  433. {
  434. string s = new string("".ToCharArray());
  435. for (int i = 0; i < dataBits.Length; i++)
  436. {
  437. if (i != 0)
  438. s += ", ";
  439. s += (dataBits[i] + "L");
  440. }
  441. return s;
  442. }
  443. /*Print out the bit set but collapse char ranges. */
  444. /* public virtual string toStringWithRanges(string separator, CharFormatter formatter)
  445. {
  446. string str = "";
  447. int[] elems = this.toArray();
  448. if (elems.Length == 0)
  449. {
  450. return "";
  451. }
  452. // look for ranges
  453. int i = 0;
  454. while (i < elems.Length)
  455. {
  456. int lastInRange;
  457. lastInRange = 0;
  458. for (int j = i + 1; j < elems.Length; j++)
  459. {
  460. if (elems[j] != elems[j - 1] + 1)
  461. {
  462. break;
  463. }
  464. lastInRange = j;
  465. }
  466. // found a range
  467. if (str.Length > 0)
  468. {
  469. str += separator;
  470. }
  471. if (lastInRange - i >= 2)
  472. {
  473. str += formatter.literalChar(elems[i]);
  474. str += "..";
  475. str += formatter.literalChar(elems[lastInRange]);
  476. i = lastInRange; // skip past end of range for next range
  477. }
  478. else
  479. {
  480. // no range, just print current char and move on
  481. str += formatter.literalChar(elems[i]);
  482. }
  483. i++;
  484. }
  485. return str;
  486. }
  487. */
  488. private static int wordNumber(int bit)
  489. {
  490. return bit >> LOG_BITS; // bit / BITS
  491. }
  492. }
  493. }