Advertisement
Guest User

Untitled

a guest
May 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. private boolean[] bits;
  2.  
  3. public BitVector and(BitVector anotherVector) {
  4.  
  5. if (this == anotherVector) return (BitVector) clone();
  6.  
  7. if (size() != anotherVector.size())
  8. throw new IllegalArgumentException("Incompatible sizes: size="+size()+", other.size()="+anotherVector.size());
  9.  
  10. BitVector intersectionVector = new BitVector(size());
  11. for (int i = 0; i < size(); i++){
  12. if (get(i) & anotherVector.get(i))
  13. intersectionVector.set(i);
  14. }
  15.  
  16. return intersectionVector;
  17. }
  18.  
  19.  
  20. public Object clone() {
  21.  
  22. BitVector clone = null;
  23. try {
  24. clone = (BitVector) super.clone();
  25. } catch (CloneNotSupportedException e) {
  26. e.printStackTrace();
  27. }
  28.  
  29. if (this.bits != null) clone.bits = this.bits.clone();
  30. return clone;
  31. }
  32.  
  33. public void set(int index){
  34. bits[index] = true;
  35. }
  36.  
  37. public boolean get(int index){
  38. return bits[index];
  39. }
  40.  
  41. public int size() {
  42. return this.bits.length;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement