fr.univNantes.intcolls.internal
Class Block

java.lang.Object
  extended by fr.univNantes.intcolls.internal.Block
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>

public final class Block
extends java.lang.Object
implements java.lang.Iterable<java.lang.Integer>, java.lang.Cloneable

A block contains a fixed number of 64 bit slots. Each slot is implemented by a long integer.

Warning : the Block class itself is not thread safe and needs to be externally synchronized when being accessed by multiple threads at same time.

Author:
Pierre-Olivier Terrisse

Field Summary
private  int max
          Maximum value in the block
private  int size
          Size of the block (number of values it contains).
(package private)  long[] slots
          Array of 64 bit slots
 
Constructor Summary
Block(int capacity)
          Concstructor of the block
 
Method Summary
 boolean add(int value)
          Adds a new value
 boolean addAll(Block b)
          Add all the values contained in another block
 int ceiling(int e)
          Returns the least element in this set greater than or equal to the given element, or -1 if there is no such element.
 Block clone()
          Clones this Block
 boolean contains(int value)
          Returns true if the block contains a value
 int countBetween(int fromValue, boolean fromInclusive, int toValue, boolean toInclusive)
          Counts values between two values
 int countHigher(int value, boolean isInclusive)
          Counts values strictly higher than a specified value
 int countLower(int value, boolean isInclusive)
          Counts values strictly lower than a specified value
 java.lang.String dump()
          Only for debug purpose
 boolean equals(java.lang.Object o)
          Returns true if this blocks contains same values as another block
 int first()
          Return the first element of the block
 int floor(int e)
          Returns the greatest element in this set less than or equal to the given element, or -1 if there is no such element.
 long getSlotValue(int slotNum)
          Low level method : returns a slot value
 int hashCode()
          Returns a hash code value for the object
 boolean hasHigher(int e)
          Returns true if the block has a higher value
 boolean hasLower(int e)
          Returns true if the block has a lower value
 int higher(int e)
          Returns the least element in this set strictly greater than the given element, or -1 if there is no such element.
 BlockIterator iterator()
          Creates a new iterator on this block
 int last()
          Returns the last element of the block
 int lower(int e)
          Returns the greatest element in this set strictly less than the given element, or -1 if there is no such element.
private  void recomputeMax()
           
 boolean remove(int value)
          Removes an element from the block
 boolean removeAll(Block b)
          Removes all the values contained in another block
 boolean retainAll(Block b)
          Retains only the elements in this block that are contained in the specified block
 int setSlotValue(int slotNum, long slotVal)
          Low level method : sets a slot value
 int size()
          Returns block size
 int sum()
          Computes sum of all elements in the block
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

slots

long[] slots
Array of 64 bit slots


size

private int size
Size of the block (number of values it contains). This value is updated each time the block is modified.


max

private int max
Maximum value in the block

Constructor Detail

Block

public Block(int capacity)
Concstructor of the block

Parameters:
capacity - number of 64 slots
Method Detail

size

public int size()
Returns block size

Returns:
number of values contained in the block

add

public boolean add(int value)
Adds a new value

Parameters:
value - value to add
Returns:
true if the block was really modified

remove

public boolean remove(int value)
Removes an element from the block

Parameters:
value - element to remove (must have 0 <= value < 64 * capacity)
Returns:
true if the block was modified

contains

public boolean contains(int value)
Returns true if the block contains a value

Parameters:
value - value to test
Returns:
true if the value is in the block

hashCode

public int hashCode()
Returns a hash code value for the object

Overrides:
hashCode in class java.lang.Object
Returns:
hash code

equals

public boolean equals(java.lang.Object o)
Returns true if this blocks contains same values as another block

Overrides:
equals in class java.lang.Object
Parameters:
o - another block

clone

public final Block clone()
Clones this Block

Overrides:
clone in class java.lang.Object
Returns:
a copy of this block

sum

public int sum()
Computes sum of all elements in the block

Returns:
the sum of elements

addAll

public boolean addAll(Block b)
Add all the values contained in another block

Parameters:
b - block to add
Returns:
true if the block changed
Throws:
java.lang.IllegalArgumentException - if block b has another capacity

removeAll

public boolean removeAll(Block b)
Removes all the values contained in another block

Parameters:
b - block to remove
Returns:
true if the block changed
Throws:
java.lang.IllegalArgumentException - if block b has another capacity

retainAll

public boolean retainAll(Block b)
Retains only the elements in this block that are contained in the specified block

Parameters:
b - another block
Returns:
true if this block changed

first

public int first()
Return the first element of the block

Returns:
the first element or -1 if this block is empty

last

public int last()
Returns the last element of the block

Returns:
the last element or -1 if this block is empty

recomputeMax

private void recomputeMax()

higher

public int higher(int e)
Returns the least element in this set strictly greater than the given element, or -1 if there is no such element.

Returns:
the least element greater than e, or -1 if there is no such element

hasHigher

public boolean hasHigher(int e)
Returns true if the block has a higher value

Parameters:
e - a value
Returns:
true if the block has a value strictly superior than e

ceiling

public int ceiling(int e)
Returns the least element in this set greater than or equal to the given element, or -1 if there is no such element. the least element greater than or equal to e, or -1 if there is no such element


lower

public int lower(int e)
Returns the greatest element in this set strictly less than the given element, or -1 if there is no such element.

Returns:
the greatest element less than e, or null if there is no such element

hasLower

public boolean hasLower(int e)
Returns true if the block has a lower value

Parameters:
e - a value
Returns:
true if the block has a value strictly inferior than e

floor

public int floor(int e)
Returns the greatest element in this set less than or equal to the given element, or -1 if there is no such element.

Returns:
the greatest element less than or equal to e, or -1 if there is no such element

iterator

public BlockIterator iterator()
Creates a new iterator on this block

Specified by:
iterator in interface java.lang.Iterable<java.lang.Integer>
Returns:
an iterator

countHigher

public int countHigher(int value,
                       boolean isInclusive)
Counts values strictly higher than a specified value

Parameters:
value - a value between 0 and 64 * slots.length - 1
isInclusive - true is the value is inclusive

countLower

public int countLower(int value,
                      boolean isInclusive)
Counts values strictly lower than a specified value

Parameters:
value - a value between 0 and 64 * slots.length - 1
isInclusive - true is the value is inclusive

countBetween

public int countBetween(int fromValue,
                        boolean fromInclusive,
                        int toValue,
                        boolean toInclusive)
Counts values between two values

Parameters:
fromValue - first value
fromInclusive - true if fromValue is inclusive
toValue - last value (not inclusive)
toInclusive - true if toValue is inclusive

getSlotValue

public long getSlotValue(int slotNum)
Low level method : returns a slot value

Parameters:
slotNum - slot number
Returns:
slot value
Throws:
java.lang.ArrayIndexOutOfBoundsException - if slotNum is outsize block bounds

setSlotValue

public int setSlotValue(int slotNum,
                        long slotVal)
Low level method : sets a slot value

Parameters:
slotNum - slot number
slotVal - slot val
Returns:
delta size of the block
Throws:
java.lang.ArrayIndexOutOfBoundsException - if slotNum is outsize block bounds

dump

public java.lang.String dump()
Only for debug purpose