Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.chocosolver.memory.structure.*;

import java.util.BitSet;

/**
* Super class of all environments !
*/
Expand Down Expand Up @@ -58,9 +60,26 @@ public IStateBitSet makeBitSet(int size) {
}
}

@Override
public IStateBitSet makeBitSet(BitSet initialSet) {
int size = initialSet.size();
if (size < 32) {
return new OneWordS32BitSet(this, initialSet);
} else if (size < 64) {
return new OneWordS64BitSet(this, initialSet);
} else {
return new S64BitSet(this, initialSet);
}
}

@Override
public IStateBitSet makeSparseBitset(int blocksize) {
return new SparseBitSet(this, 64);
return new SparseBitSet(this, blocksize);
}

@Override
public IStateBitSet makeSparseBitset(BitSet initialSet) {
return new SparseBitSet(this, initialSet);
}

/**
Expand Down
29 changes: 27 additions & 2 deletions solver/src/main/java/org/chocosolver/memory/IEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import org.chocosolver.memory.structure.BasicIndexedBipartiteSet;
import org.chocosolver.memory.structure.IOperation;

import java.util.BitSet;


/**
* An interface to ease declaration of backtrackable objects (mostly primitives).
*
* @author Charles Prud'homme, Hadrien Cambazard, Guillaume Rochart
*/
public interface IEnvironment {
public interface IEnvironment {

/**
* Returns the world number.
Expand Down Expand Up @@ -85,6 +87,7 @@ public interface IEnvironment {
/**
* Factory pattern: new StoredFloat objects are created by the environment
* (no initial value is assigned to the backtrackable search)
*
* @return new IStateDouble computed by the environment
*/

Expand All @@ -101,13 +104,15 @@ public interface IEnvironment {

/**
* Factory pattern: new backtrackable long attached to this environment.
*
* @return a backtrackable long
*/
@SuppressWarnings("unused")
IStateLong makeLong();

/**
* Factory pattern: new backtrackable long attached to this environment.
*
* @param init its initial value
* @return a backtrackable long
*/
Expand All @@ -121,6 +126,15 @@ public interface IEnvironment {
*/
IStateBitSet makeBitSet(int size);

/**
* Factory pattern: new IStateBitSet objects are created by the environment
*
* @param initialSet initial set of the IStateBitSet
* @return IStateBitSet
*/
IStateBitSet makeBitSet(BitSet initialSet);


/**
* Factory pattern: new SparseBitSet objects are created by the environment
*
Expand All @@ -129,6 +143,14 @@ public interface IEnvironment {
*/
IStateBitSet makeSparseBitset(int blocksize);

/**
* Factory pattern: new SparseBitSet objects are created by the environment
*
* @param initialSet initial bitset
* @return IStateBitSet
*/
IStateBitSet makeSparseBitset(BitSet initialSet);

/**
* Factory pattern: new IStateIntVector objects are created by the environment
*
Expand Down Expand Up @@ -158,20 +180,23 @@ public interface IEnvironment {

/**
* Save this operation onto the stack of operations to undo on backtrack.
*
* @param operation operation to undo
*/
void save(IOperation operation);

/**
* Save this operation onto the stack of operations to undo on backtrack, at level `worldIndex`.
* @param operation operation to undo
*
* @param operation operation to undo
* @param worldIndex when to apply this
*/
void saveAt(IOperation operation, int worldIndex);

/**
* Return the current time stamp.
* It differs from world index since it never decrements.
*
* @return the timestamp
*/
int getTimeStamp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.chocosolver.memory.IStateBitSet;
import org.chocosolver.memory.IStateInt;

import java.util.BitSet;


public class OneWordS32BitSet implements IStateBitSet {

Expand Down Expand Up @@ -44,6 +46,26 @@ public OneWordS32BitSet(IEnvironment environment) {
word = environment.makeInt(0);
}

/**
* Creates a bit set using the specified long array.
*
* @param environment backtrackable environment
* @param initial the initial long array to use
*/
public OneWordS32BitSet(IEnvironment environment, BitSet initial) {
this(environment, (int) initial.toLongArray()[0]);
}

/**
* Creates a bit set using the specified long array.
*
* @param environment backtrackable environment
* @param initial the initial long array to use
*/
public OneWordS32BitSet(IEnvironment environment, int initial) {
this.word = environment.makeInt(initial);
}

/**
* Checks that index is a valid range of bit indices.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.chocosolver.memory.IStateBitSet;
import org.chocosolver.memory.IStateLong;

import java.util.BitSet;


public class OneWordS64BitSet implements IStateBitSet {

Expand Down Expand Up @@ -44,6 +46,26 @@ public OneWordS64BitSet(IEnvironment environment) {
word = environment.makeLong(0);
}

/**
* Creates a bit set using the specified long array.
*
* @param environment backtrackable environment
* @param initial the initial long array to use
*/
public OneWordS64BitSet(IEnvironment environment, long initial) {
this.word = environment.makeLong(initial);
}

/**
* Creates a bit set using the specified long array.
*
* @param environment backtrackable environment
* @param initial the initial long array to use
*/
public OneWordS64BitSet(IEnvironment environment, BitSet initial) {
this(environment, initial.toLongArray()[0]);
}

/**
* Checks that fromIndex ... toIndex is a valid range of bit indices.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
import org.chocosolver.memory.IStateLong;

import java.lang.reflect.Array;
import java.util.BitSet;


public class S64BitSet implements IStateBitSet {

private final static boolean CHECK = false;

/*
* BitSets are packed into arrays of "words." Currently a word is
* a long, which consists of 64 bits, requiring 6 address bits.
* The choice of word size is determined purely by performance concerns.
*/
* BitSets are packed into arrays of "words." Currently a word is
* a long, which consists of 64 bits, requiring 6 address bits.
* The choice of word size is determined purely by performance concerns.
*/
private final static int ADDRESS_BITS_PER_WORD = 6;
protected final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;

Expand Down Expand Up @@ -75,7 +76,7 @@ private static void requirePositiveIndex(final int index) {
*/
private void checkInvariants() {
assert (wordsInUse.get() == 0 || words[wordsInUse.get() - 1].get() != 0);
assert (wordsInUse.get() >= 0 && wordsInUse.get() <= words.length);
assert (wordsInUse.get() <= words.length);
assert (wordsInUse.get() == words.length || words[wordsInUse.get()].get() == 0);
}

Expand Down Expand Up @@ -127,6 +128,28 @@ public S64BitSet(IEnvironment environment, int nbits) {
initWords(nbits);
}

/**
* Creates a bit set whose initial size is large enough to explicitly
* represent bits with indices in the range <code>0</code> through
* <code>nbits-1</code>. All bits are initially <code>false</code>.
*
* @param environment backtrackable environment
* @param initial the initial bitset
* @throws NegativeArraySizeException if the specified initial size
* is negative.
*/
public S64BitSet(IEnvironment environment, BitSet initial) {
this.environment = environment;
long[] iwords = initial.toLongArray();
this.wordsInUse = environment.makeInt(iwords.length);
this.words = new IStateLong[iwords.length];
for (int i = 0; i < iwords.length; i++) {
this.words[i] = environment.makeLong(iwords[i]);
}
if (CHECK) checkInvariants();
}


private void initWords(int nbits) {
words = new IStateLong[wordIndex(nbits - 1) + 1];
for (int i = 0; i < words.length; i++) words[i] = this.environment.makeLong(0);
Expand Down Expand Up @@ -397,7 +420,7 @@ final public boolean get(final int bitIndex) {
*
* @param fromIndex the index to start checking from (inclusive)
* @return the index of the next set bit, or {@code -1} if there
* is no such bit
* is no such bit
* @throws IndexOutOfBoundsException if the specified index is negative
* @since 1.4
*/
Expand Down Expand Up @@ -470,7 +493,7 @@ public int nextClearBit(int fromIndex) {
*
* @param fromIndex the index to start checking from (inclusive)
* @return the index of the previous set bit, or {@code -1} if there
* is no such bit
* is no such bit
* @throws IndexOutOfBoundsException if the specified index is less
* than {@code -1}
* @since 1.7
Expand Down Expand Up @@ -503,7 +526,7 @@ public int prevSetBit(int fromIndex) {
*
* @param fromIndex the index to start checking from (inclusive)
* @return the index of the previous clear bit, or {@code -1} if there
* is no such bit
* is no such bit
* @throws IndexOutOfBoundsException if the specified index is less
* than {@code -1}
* @since 1.7
Expand Down Expand Up @@ -561,7 +584,7 @@ public boolean isEmpty() {
* <code>BitSet</code>.
*
* @return the number of bits set to <tt>true</tt> in this
* <code>BitSet</code>.
* <code>BitSet</code>.
* @since 1.4
*/
public int cardinality() {
Expand Down
Loading