Source code: joelib/util/JOEBitVec.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: JOEBitVec.java,v $
3 // Purpose: BitSet14 extensions.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.17 $
8 // $Date: 2003/08/22 15:56:21 $
9 // $Author: wegner $
10 // Original Author: ???, OpenEye Scientific Software
11 // Original Version: babel 2.0a1
12 //
13 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 ///////////////////////////////////////////////////////////////////////////////
24 package joelib.util;
25
26 import java.io.*;
27
28 /*==========================================================================*
29 * IMPORTS
30 *========================================================================== */
31 import java.util.*;
32
33 import org.apache.log4j.*;
34
35
36 /*==========================================================================*
37 * CLASS DECLARATION
38 *========================================================================== */
39
40 /**
41 * BitSet14 extensions.
42 *
43 * @author wegnerj
44 * @license GPL
45 * @cvsversion $Revision: 1.17 $, $Date: 2003/08/22 15:56:21 $
46 */
47 public class JOEBitVec extends BitSet14 implements java.io.Serializable
48 {
49 //~ Static fields/initializers /////////////////////////////////////////////
50
51 /*-------------------------------------------------------------------------*
52 * public static member variables
53 *-------------------------------------------------------------------------*/
54
55 /**
56 * Obtain a suitable logger.
57 */
58 private static Category logger = Category.getInstance(
59 "joelib.util.JOEBitVec");
60
61 //~ Constructors ///////////////////////////////////////////////////////////
62
63 /*-------------------------------------------------------------------------*
64 * public member variables
65 *------------------------------------------------------------------------- */
66
67 //private int _size;
68
69 /**
70 * {@link java.util.Vector} of <tt>int[1]</tt>
71 */
72
73 //private Vector _set;
74
75 /*-------------------------------------------------------------------------*
76 * constructor
77 *------------------------------------------------------------------------- */
78 public JOEBitVec()
79 {
80 super();
81
82 //_set.resize(STARTWORDS);
83 //_size=_set.size();
84 //clear();
85 }
86
87 /**
88 * Constructor for the JOEBitVec object
89 *
90 * @param bits Description of the Parameter
91 */
92 public JOEBitVec(int bits)
93 {
94 super(bits);
95
96 //_set.resize(bits/SETWORD);
97 //_size=_set.size();
98 //clear();
99 }
100
101 /**
102 * Constructor for the JOEBitVec object
103 *
104 * @param bv Description of the Parameter
105 */
106 public JOEBitVec(final JOEBitVec bv)
107 {
108 clear();
109 super.or(bv);
110 }
111
112 //~ Methods ////////////////////////////////////////////////////////////////
113
114 /**
115 * Clears the bits from the specified fromIndex(inclusive) to the specified
116 * toIndex(inclusive) to <tt>false</tt>.
117 *
118 * @param bitIndex The new bitOff value
119 */
120 public void setBitOff(int bitIndex)
121 {
122 super.clear(bitIndex);
123 }
124
125 /*-------------------------------------------------------------------------*
126 * public methods
127 *------------------------------------------------------------------------- */
128
129 /**
130 * Sets the bits from the specified fromIndex(inclusive) to the specified
131 * toIndex(inclusive) to <tt>false</tt>.
132 *
133 * @param bitIndex The new bitOn value
134 */
135 public void setBitOn(int bitIndex)
136 {
137 super.set(bitIndex);
138 }
139
140 /**
141 * Clears the bits from the specified fromIndex(inclusive) to the specified
142 * toIndex(inclusive) to <tt>false</tt>.
143 *
144 * @param from index of the first bit to be
145 * cleared.
146 * @param to index after the last bit to be
147 * cleared.
148 */
149 public void setRangeOff(int from, int to)
150 {
151 super.clear(from, to + 1);
152 }
153
154 /**
155 * Sets the bits from the specified fromIndex(inclusive) to the specified
156 * toIndex(inclusive) to <tt>false</tt>.
157 *
158 * @param from index of the first bit to be
159 * cleared.
160 * @param to index after the last bit to be
161 * cleared.
162 */
163 public void setRangeOn(int from, int to)
164 {
165 super.set(from, to + 1);
166 }
167
168 /**
169 * Description of the Method
170 *
171 * @return Description of the Return Value
172 */
173 public final int endBit()
174 {
175 return (-1);
176 }
177
178 /* OEBitVec &OEBitVec::operator+= (OEBitVec &bv)
179 *{
180 *int old_size = _size;
181 *Resize(_size*SETWORD+bv._size*SETWORD);
182 *for (int i = 0;i < bv._size;i++) _set[i+old_size] = bv._set[i];
183 *return(*this);
184 *} */
185
186 // public JOEBitVec addSet (JOEBitVec bv)
187 // {
188 //
189 // }
190
191 /**
192 * Description of the Method
193 *
194 * @param bv Description of the Parameter
195 * @return Description of the Return Value
196 */
197 public JOEBitVec and(JOEBitVec bv)
198 {
199 JOEBitVec tmp = (JOEBitVec) bv.clone();
200
201 tmp.andSet(this);
202
203 return tmp;
204 }
205
206 /**
207 * Description of the Method
208 *
209 * @param bv1 Description of the Parameter
210 * @param bv2 Description of the Parameter
211 * @return Description of the Return Value
212 */
213 public static JOEBitVec and(JOEBitVec bv1, JOEBitVec bv2)
214 {
215 JOEBitVec tmp = (JOEBitVec) bv1.clone();
216
217 tmp.andSet(bv2);
218
219 return tmp;
220 }
221
222 /**
223 * Description of the Method
224 *
225 * @param bv Description of the Parameter
226 * @return Description of the Return Value
227 */
228 public JOEBitVec andSet(JOEBitVec bv)
229 {
230 super.and(bv);
231
232 return this;
233 }
234
235 /**
236 * Returns the index of the first bit that is set to <tt>true</tt>. If no
237 * such bit exists then -1 is returned.
238 *
239 * @return the index of the next set bit.
240 * @throws IndexOutOfBoundsException if the specified index is negative.
241 */
242 public int firstBit()
243 {
244 return nextBit(-1);
245
246 //(get(0) ? 0 : nextBit(-1));
247 }
248
249 /* void OEBitVec::Fold(int nbits)
250 *{
251 *int nwords = nbits/SETWORD;
252 *if (_size < nwords)
253 *{
254 *_set.resize(nwords);
255 *_size = nwords;
256 *return;
257 *}
258 *int i,idx = nwords;
259 *for (i = 0,idx=nwords;idx < _size;idx++)
260 *{
261 *_set[i] |= _set[idx];
262 *if (i+1 < nwords) i++;
263 *else i = 0;
264 *}
265 *_set.resize(nwords);
266 *_size = nwords;
267 *} */
268
269 /**
270 * Description of the Method
271 *
272 * @param nbits Description of the Parameter
273 */
274 public void fold(int nbits)
275 {
276 // logger.error("Don't know what to do ...");
277 System.out.println("Don't know what to do ...");
278
279 // int nwords = nbits/SETWORD;
280 //
281 // if (_size < nwords)
282 // {
283 // _set.resize(nwords);
284 // _size = nwords;
285 // return;
286 // }
287 //
288 // int i,idx = nwords;
289 // for (i = 0,idx=nwords;idx < _size;idx++)
290 // {
291 // _set[i] |= _set[idx];
292 // if (i+1 < nwords) i++;
293 // else i = 0;
294 // }
295 // _set.resize(nwords);
296 // _size = nwords;
297 }
298
299 /**
300 * Returns the index of the first bit that is set to <tt>true</tt> that
301 * occurs on or after the specified starting index. If no such bit exists
302 * then -1 is returned. To iterate over the <tt>true</tt> bits in a
303 * <tt>BitSet14</tt>, use the following loop: for(int i=bs.nextSetBit(0);
304 * i>=0; i=bs.nextSetBit(i+1)) { // operate on index i here }
305 *
306 * @param last the index to start checking from
307 * (inclusive).
308 * @return the index of the next set bit.
309 * @throws IndexOutOfBoundsException if the specified index is negative.
310 */
311 public final int nextBit(int last)
312 {
313 return nextSetBit(last + 1);
314 }
315
316 /**
317 * Description of the Method
318 *
319 * @return Description of the Return Value
320 */
321 public int countBits()
322 {
323 return super.cardinality();
324 }
325
326 /**
327 * Description of the Method
328 *
329 * @return Description of the Return Value
330 */
331 public final boolean empty()
332 {
333 return (isEmpty());
334 }
335
336 /**
337 * Description of the Method
338 *
339 * @param bit Description of the Parameter
340 * @return Description of the Return Value
341 */
342 public boolean bitIsOn(int bit)
343 {
344 return get(bit);
345 }
346
347 //public boolean Resize(int)
348 //{
349 //}
350
351 /**
352 * Description of the Method
353 *
354 * @param bit Description of the Parameter
355 * @return Description of the Return Value
356 */
357 public boolean bitIsSet(int bit)
358 {
359 return get(bit);
360 }
361
362 public void fromBoolArray(boolean[] boolArray)
363 {
364 for (int i = 0; i < boolArray.length; i++)
365 {
366 if (boolArray[i])
367 {
368 super.set(i);
369 }
370 else
371 {
372 super.clear(i);
373 }
374 }
375 }
376
377 /**
378 * Description of the Method
379 *
380 * @param intArray Description of the Parameter
381 */
382 public void fromIntArray(int[] intArray)
383 {
384 // System.out.println("fromIntArr:");
385 for (int i = 0; i < intArray.length; i++)
386 {
387 setBitOn(intArray[i]);
388
389 // System.out.print(""+intArray[i]+" ");
390 }
391
392 // System.out.println("");
393 }
394
395 /**
396 * Reads this bit vector from a <tt>String</tt>.
397 * e.g. [0 10 15 23]. It's a list of all set bits in this
398 * bit vector, which are separated by a space character.
399 * The bit vector is enclosed by two brackets.
400 *
401 * @param s the string representation of set bits enclosed by []-brackets
402 */
403 public void fromString(String s)
404 {
405 clear();
406
407 StringTokenizer st = new StringTokenizer(s, " \t\n");
408 String stmp;
409
410 while (st.hasMoreTokens())
411 {
412 stmp = st.nextToken();
413
414 if (stmp.equals("["))
415 {
416 continue;
417 }
418 else if (stmp.equals("]"))
419 {
420 break;
421 }
422
423 setBitOn(Integer.parseInt(stmp));
424 }
425 }
426
427 /**
428 * @param v Description of the Parameter
429 */
430 public void fromVectorWithIntArray(Vector v)
431 {
432 int[] itmp;
433
434 for (int i = 0; i < v.size(); i++)
435 {
436 itmp = (int[]) v.get(i);
437 setBitOn(itmp[0]);
438 }
439
440 if (logger.isDebugEnabled())
441 {
442 //StringBuffer sb = new StringBuffer();
443 //sb.append("fromVecInt");
444 for (int i = 0; i < v.size(); i++)
445 {
446 itmp = (int[]) v.get(i);
447
448 //sb.append(itmp[0]);
449 //sb.append(' ');
450 }
451
452 //logger.debug(sb.toString());
453 }
454 }
455
456 /**
457 * Description of the Method
458 */
459 public void negate()
460 {
461 flip(0, size());
462 }
463
464 /**
465 * Description of the Method
466 *
467 * @param bv Description of the Parameter
468 * @return Description of the Return Value
469 */
470 public JOEBitVec or(JOEBitVec bv)
471 {
472 JOEBitVec tmp = (JOEBitVec) bv.clone();
473
474 tmp.orSet(this);
475
476 return tmp;
477 }
478
479 /**
480 * Description of the Method
481 *
482 * @param bv1 Description of the Parameter
483 * @param bv2 Description of the Parameter
484 * @return Description of the Return Value
485 */
486 public static JOEBitVec or(JOEBitVec bv1, JOEBitVec bv2)
487 {
488 JOEBitVec tmp = (JOEBitVec) bv1.clone();
489
490 tmp.orSet(bv2);
491
492 return tmp;
493 }
494
495 /**
496 * Description of the Method
497 *
498 * @param bv Description of the Parameter
499 * @return Description of the Return Value
500 */
501 public JOEBitVec orSet(JOEBitVec bv)
502 {
503 super.or(bv);
504
505 return this;
506 }
507
508 /**
509 * Description of the Method
510 *
511 * @param bv Description of the Parameter
512 * @return Description of the Return Value
513 */
514 public JOEBitVec set(final JOEBitVec bv)
515 {
516 clear();
517 super.or(bv);
518
519 return this;
520 }
521
522 /**
523 * Description of the Method
524 *
525 * @param bv Description of the Parameter
526 * @return Description of the Return Value
527 */
528 public JOEBitVec subSet(JOEBitVec bv)
529 {
530 JOEBitVec tmp = (JOEBitVec) bv.clone();
531
532 tmp.negate();
533 super.and(tmp);
534
535 return this;
536 }
537
538 public boolean[] toBoolArr(int to)
539 {
540 boolean[] boolArray;
541 boolArray = new boolean[to];
542
543 for (int i = 0; i < to; i++)
544 {
545 boolArray[i] = get(i);
546 }
547
548 return boolArray;
549 }
550
551 public boolean[] toBoolArray()
552 {
553 return toBoolArr(this.size());
554 }
555
556 public int[] toIntArray()
557 {
558 int[] array;
559 array = new int[countBits()];
560
561 // System.out.println("bits "+countBits());
562 int index = 0;
563
564 for (int i = nextBit(-1); i != -1; i = nextSetBit(i + 1))
565 {
566 array[index] = i;
567 index++;
568
569 // System.out.print(" "+i);
570 }
571
572 // System.out.println("");
573 return array;
574 }
575
576 /**
577 * @param v Description of the Parameter
578 */
579 public void toVectorWithIntArray(Vector v)
580 {
581 v.clear();
582 v.ensureCapacity(countBits());
583
584 // System.out.println("bits "+countBits());
585 for (int i = nextBit(-1); i != -1; i = nextSetBit(i + 1))
586 {
587 int[] itmp = new int[]{i};
588 v.add(itmp);
589
590 // System.out.print(" "+i);
591 }
592
593 // System.out.println("");
594 }
595
596 /**
597 * Description of the Method
598 *
599 * @param bv Description of the Parameter
600 * @return Description of the Return Value
601 */
602 public JOEBitVec xor(JOEBitVec bv)
603 {
604 JOEBitVec tmp = (JOEBitVec) bv.clone();
605
606 tmp.xorSet(this);
607
608 return tmp;
609 }
610
611 /**
612 * Description of the Method
613 *
614 * @param bv1 Description of the Parameter
615 * @param bv2 Description of the Parameter
616 * @return Description of the Return Value
617 */
618 public static JOEBitVec xor(JOEBitVec bv1, JOEBitVec bv2)
619 {
620 JOEBitVec tmp = (JOEBitVec) bv1.clone();
621
622 tmp.xorSet(bv2);
623
624 return tmp;
625 }
626
627 /**
628 * Description of the Method
629 *
630 * @param bv Description of the Parameter
631 * @return Description of the Return Value
632 */
633 public JOEBitVec sub(JOEBitVec bv)
634 {
635 JOEBitVec tmp = (JOEBitVec) bv.clone();
636
637 tmp.subSet(this);
638
639 return tmp;
640 }
641
642 /**
643 * Description of the Method
644 *
645 * @param bv1 Description of the Parameter
646 * @param bv2 Description of the Parameter
647 * @return Description of the Return Value
648 */
649 public static JOEBitVec sub(JOEBitVec bv1, JOEBitVec bv2)
650 {
651 JOEBitVec tmp = (JOEBitVec) bv1.clone();
652
653 tmp.subSet(bv2);
654
655 return tmp;
656 }
657
658 /**
659 * Returns the number of bits which are set in this <tt>BitSet14</tt> AND
660 * the <tt>BitSet14</tt> b.
661 *
662 * @param b Description of the Parameter
663 * @return Description of the Return Value
664 */
665 public int andCount(BitSet14 b)
666 {
667 BitSet14 a = (BitSet14) this.clone();
668 a.and(b);
669
670 return a.cardinality();
671 }
672
673 /**
674 * Description of the Method
675 *
676 * @param is Description of the Parameter
677 * @exception IOException Description of the Exception
678 */
679 public void in(InputStream is) throws IOException
680 {
681 LineNumberReader ln = new LineNumberReader(new InputStreamReader(is));
682 String line;
683
684 for (;;)
685 {
686 line = ln.readLine();
687
688 if (line == null)
689 {
690 break;
691 }
692
693 fromString(line);
694 }
695 }
696
697 /**
698 * Description of the Method
699 *
700 * @param os Description of the Parameter
701 */
702 public void out(OutputStream os)
703 {
704 PrintStream ps = new PrintStream(os);
705
706 ps.println(this.toString());
707 }
708
709 /**
710 * Returns the tanimoto similarity coefficient between this <tt>BitSet14</tt>
711 * with the <tt>BitSet14</tt> b.
712 *
713 * @param b Description of the Parameter
714 * @return Description of the Return Value
715 */
716 public double tanimoto(BitSet14 b)
717 {
718 int ab = andCount(b);
719 double tanimoto = (double) ab / (double) ((this.cardinality() +
720 b.cardinality()) - ab);
721
722 return tanimoto;
723 }
724
725 /**
726 * Writes this bit vector to a <tt>String</tt>.
727 * e.g. [0 10 15 23]. It's a list of all set bits in this
728 * bit vector, which are separated by a space character.
729 * The bit vector is enclosed by two brackets.
730 *
731 * @return Description of the Return Value
732 */
733 public String toString()
734 {
735 StringBuffer sb = new StringBuffer("[ ");
736
737 for (int i = nextBit(-1); i != -1; i = nextSetBit(i + 1))
738 {
739 sb.append(i);
740 sb.append(' ');
741 }
742
743 sb.append("]");
744
745 return sb.toString();
746 }
747
748 /**
749 * Description of the Method
750 *
751 * @param bv Description of the Parameter
752 * @return Description of the Return Value
753 */
754 public JOEBitVec xorSet(JOEBitVec bv)
755 {
756 super.xor(bv);
757
758 return this;
759 }
760 }
761 ///////////////////////////////////////////////////////////////////////////////
762 // END OF FILE.
763 ///////////////////////////////////////////////////////////////////////////////