Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.util;

Set of pairs of ints.

TODO: cleanup method naming, dynamically grow the relation if necessary. I wait with cleanup until there is more code that uses relations.

 
 
 public class IntBitRelation {
    
storage.
 
     private final IntBitSet[] line;

    
Create a new empty releation.

Parameters:
size upper bound for int values stored in the relation
 
     public IntBitRelation(int size) {
          = new IntBitSet[size]; // initialized to null
     }

    
Returns the upper bound for int elements in the relation

Returns:
largest int + 1 allowed in this relation
 
     public int getMax() {
         return .;
     }

    
Adds a new pair to the relation.

Parameters:
left left value of the pair
right right value of the pair
Returns:
false, if the pair was already element of the relation
 
     public boolean add(int leftint right) {
         if ([left] == null) {
             [left] = new IntBitSet();
         } else {
             if ([left].contains(right)) {
                 return false;
             }
         }
 
         [left].add(right);
         return true;
     }

    
Adds a set of pairs.

Parameters:
left left value of all pairs to add
right right values of the pairs to add. If empty, no pair is added
 
     public void add(int leftIntBitSet right) {
         if ([left] == null) {
             [left] = new IntBitSet();
         }
         [left].addAll(right);
     }

    
Adds all element from the relation specified.

Parameters:
right relation to add.
 
     public void addAll(IntBitRelation right) {
         int imax;
 
         max = right.line.length;
         for (i = 0; i < maxi++) {
             if (right.line[i] != null) {
                 add(iright.line[i]);
             }
         }
     }

    
Element test.

Parameters:
left left value of the pair to test.
right right value of the pair to test.
Returns:
true, if (left, right) is element of the relation.
    public boolean contains(int leftint right) {
        if ([left] == null) {
            return false;
        }
        return [left].contains(right);
    }

    
Returns all right values from the relation that have the left value specified.

Parameters:
left left value required
result where to return the result
    public void addRightWhere(int leftIntBitSet result) {
        if ([left] != null) {
            result.addAll([left]);
        }
    }


    
If (x,a) is element of left and (x,b) is element of right, (b,a) is added to this relation.

Parameters:
left left relation
right right relation
    public void composeLeftLeftInv(IntBitRelation leftIntBitRelation right) {
        int iab;
        IntBitSet liri;
        for (i = 0; i < .i++) {
            li = left.line[i];
            ri = right.line[i];
            if (li != null && ri != null) {
                for (a = li.first(); a != -1; a = li.next(a)) {
                    for (b = ri.first(); b != -1; b = ri.next(b)) {
                        add(ba);
                    }
                }
            }
        }
    }

    
If (a,b) is element of left and (b,c) is element of right, then (a,c) is added to this relation.

Parameters:
left left relation
right right relation.
    public void composeRightLeft(IntBitRelation leftIntBitRelation right) {
        int iele;
        IntBitSet li;
        for (i = 0; i < left.line.lengthi++) {
            li = left.line[i];
            if (li != null) {
                for (ele = li.first(); ele != -1; ele = li.next(ele)) {
                    if (right.line[ele] != null) {
                        add(iright.line[ele]);
                    }
                }
            }
        }
    }

    
Comparison.

Parameters:
obj object to compare with
Returns:
true, if obj is a relation with exaclty the the same elements as this relation
    @Override
    public boolean equals(Object obj) {
        IntBitRelation rel;
        int i;
        if (!(obj instanceof IntBitRelation)) {
            return false;
        }
        rel = (IntBitRelationobj;
        if (. != rel.line.length) {
            return false;
        }
        for (i = 0; i < .i++) {
            if (([i] != null) && (rel.line[i] != null)) {
                if (![i].equals(rel.line[i])) {
                    return false;
                }
            } else {
                if (([i] != null) || (rel.line[i] != null)) {
                    return false;
                }
            }
        }
        return true;
    }
    @Override
    public int hashCode() {
        return .;
    }

    
Subset test.

Parameters:
sub relation to compare with.
Returns:
true, if every element of sub is element of this
    public boolean contains(IntBitRelation sub) {
        int i;
        if (. != sub.line.length) {
            return false;
        }
        for (i = 0; i < .i++) {
            if ([i] == null) {
                if (sub.line[i] != null) {
                    return false;
                }
            } else {
                if ((sub.line[i] != null) &&
                    ![i].containsAll(sub.line[i])) {
                    return false;
                }
            }
        }
        return true;
    }

    
Transitive closure.
    public void transitiveClosure() {
        IntBitRelation next;
        while (true) {
            next = new IntBitRelation(getMax());
            next.composeRightLeft(thisthis);
            if (contains(next)) {
                return;
            }
            addAll(next);
        }
    }
    //-------------------------------------------------------------

    
Returns a String representation.

Returns:
string representation
    @Override
    public String toString() {
        StringBuilder buf;
        int i;
        buf = new StringBuilder();
        for (i = 0; i < .i++) {
            if ([i] != null) {
                buf.append(i).append(": ").append([i].toString()).append('\n');
            }
        }
        return buf.toString();
    }
New to GrepCode? Check out our FAQ X