Package org.openscience.cdk.group
Class Partition
- java.lang.Object
-
- org.openscience.cdk.group.Partition
-
public class Partition extends Object
A partition of a set of integers, such as the discrete partition {{1}, {2}, {3}, {4}} or the unit partition {{1, 2, 3, 4}} or an intermediate like {{1, 2}, {3, 4}}.- Author:
- maclean
- Belongs to CDK module:
- group
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCell(int... elements)
Adds a new cell to the end of the partition containing these elements.void
addCell(Collection<Integer> elements)
Adds a new cell to the end of the partition.void
addSingletonCell(int element)
Add a new singleton cell to the end of the partition containing only this element.void
addToCell(int index, int element)
Add an element to a particular cell.SortedSet<Integer>
copyBlock(int cellIndex)
Creates and returns a copy of the cell at cell index.boolean
equals(Object o)
static Partition
fromString(String strForm)
Parse a string like "[0,2|1,3]" to form the partition; cells are separated by '|' characters and elements within the cell by commas.SortedSet<Integer>
getCell(int cellIndex)
Gets the cell at this index.int
getFirstInCell(int cellIndex)
Gets the first element in the specified cell.int
getIndexOfFirstNonDiscreteCell()
Gets the index of the first cell in the partition that is discrete.int
hashCode()
boolean
inOrder()
Check whether the cells are ordered such that for cells i and j, first(j) > first(i) and last(j) > last(i).boolean
inSameCell(int elementI, int elementJ)
Check that two elements are in the same cell of the partition.void
insertCell(int index, SortedSet<Integer> cell)
Insert a cell into the partition at the specified index.boolean
isDiscrete()
Checks that all the cells are singletons - that is, they only have one element.boolean
isDiscreteCell(int cellIndex)
Check to see if the cell atcellIndex
is discrete - that is, it only has one element.int
numberOfElements()
Calculate the size of the partition as the sum of the sizes of the cells.void
order()
Sort the cells in increasing order.void
removeCell(int index)
Removes the cell at the specified index.Permutation
setAsPermutation(int upTo)
Fill the elements of a permutation from the first element of each cell, up to the pointupTo
.int
size()
Gets the size of the partition, in terms of the number of cells.Partition
splitAfter(int cellIndex, int splitElement)
Splits this partition by taking the cell at cellIndex and making two new cells - the first with the the rest of the elements from that cell and the second with the singleton splitElement.Partition
splitBefore(int cellIndex, int splitElement)
Splits this partition by taking the cell at cellIndex and making two new cells - the first with the singleton splitElement and the second with the rest of the elements from that cell.Permutation
toPermutation()
Converts the whole partition into a permutation.String
toString()
static Partition
unit(int size)
Create a unit partition - in other words, the coarsest possible partition where all the elements are in one cell.
-
-
-
Constructor Detail
-
Partition
public Partition()
Creates a new, empty partition with no cells.
-
Partition
public Partition(Partition other)
Copy constructor to make one partition from another.- Parameters:
other
- the partition to copy
-
Partition
public Partition(int[][] cellData)
Constructor to make a partition from an array of int arrays.- Parameters:
cellData
- the partition to copy
-
-
Method Detail
-
unit
public static Partition unit(int size)
Create a unit partition - in other words, the coarsest possible partition where all the elements are in one cell.- Parameters:
size
- the number of elements- Returns:
- a new Partition with one cell containing all the elements
-
size
public int size()
Gets the size of the partition, in terms of the number of cells.- Returns:
- the number of cells in the partition
-
numberOfElements
public int numberOfElements()
Calculate the size of the partition as the sum of the sizes of the cells.- Returns:
- the number of elements in the partition
-
isDiscrete
public boolean isDiscrete()
Checks that all the cells are singletons - that is, they only have one element. A discrete partition is equivalent to a permutation.- Returns:
- true if all the cells are discrete
-
toPermutation
public Permutation toPermutation()
Converts the whole partition into a permutation.- Returns:
- the partition as a permutation
-
inOrder
public boolean inOrder()
Check whether the cells are ordered such that for cells i and j, first(j) > first(i) and last(j) > last(i).- Returns:
- true if all cells in the partition are ordered
-
getFirstInCell
public int getFirstInCell(int cellIndex)
Gets the first element in the specified cell.- Parameters:
cellIndex
- the cell to use- Returns:
- the first element in this cell
-
getCell
public SortedSet<Integer> getCell(int cellIndex)
Gets the cell at this index.- Parameters:
cellIndex
- the index of the cell to return- Returns:
- the cell at this index
-
splitBefore
public Partition splitBefore(int cellIndex, int splitElement)
Splits this partition by taking the cell at cellIndex and making two new cells - the first with the singleton splitElement and the second with the rest of the elements from that cell.- Parameters:
cellIndex
- the index of the cell to split onsplitElement
- the element to put in its own cell- Returns:
- a new (finer) Partition
-
splitAfter
public Partition splitAfter(int cellIndex, int splitElement)
Splits this partition by taking the cell at cellIndex and making two new cells - the first with the the rest of the elements from that cell and the second with the singleton splitElement.- Parameters:
cellIndex
- the index of the cell to split onsplitElement
- the element to put in its own cell- Returns:
- a new (finer) Partition
-
setAsPermutation
public Permutation setAsPermutation(int upTo)
Fill the elements of a permutation from the first element of each cell, up to the pointupTo
.- Parameters:
upTo
- take values from cells up to this one- Returns:
- the permutation representing the first element of each cell
-
isDiscreteCell
public boolean isDiscreteCell(int cellIndex)
Check to see if the cell atcellIndex
is discrete - that is, it only has one element.- Parameters:
cellIndex
- the index of the cell to check- Returns:
- true of the cell at this index is discrete
-
getIndexOfFirstNonDiscreteCell
public int getIndexOfFirstNonDiscreteCell()
Gets the index of the first cell in the partition that is discrete.- Returns:
- the index of the first discrete cell
-
addSingletonCell
public void addSingletonCell(int element)
Add a new singleton cell to the end of the partition containing only this element.- Parameters:
element
- the element to add in its own cell
-
removeCell
public void removeCell(int index)
Removes the cell at the specified index.- Parameters:
index
- the index of the cell to remove
-
addCell
public void addCell(int... elements)
Adds a new cell to the end of the partition containing these elements.- Parameters:
elements
- the elements to add in a new cell
-
addCell
public void addCell(Collection<Integer> elements)
Adds a new cell to the end of the partition.- Parameters:
elements
- the collection of elements to put in the cell
-
addToCell
public void addToCell(int index, int element)
Add an element to a particular cell.- Parameters:
index
- the index of the cell to add toelement
- the element to add
-
insertCell
public void insertCell(int index, SortedSet<Integer> cell)
Insert a cell into the partition at the specified index.- Parameters:
index
- the index of the cell to addcell
- the cell to add
-
copyBlock
public SortedSet<Integer> copyBlock(int cellIndex)
Creates and returns a copy of the cell at cell index.- Parameters:
cellIndex
- the cell to copy- Returns:
- the copy of the cell
-
order
public void order()
Sort the cells in increasing order.
-
inSameCell
public boolean inSameCell(int elementI, int elementJ)
Check that two elements are in the same cell of the partition.- Parameters:
elementI
- an element in the partitionelementJ
- an element in the partition- Returns:
- true if both elements are in the same cell
-
fromString
public static Partition fromString(String strForm)
Parse a string like "[0,2|1,3]" to form the partition; cells are separated by '|' characters and elements within the cell by commas.- Parameters:
strForm
- the partition in string form- Returns:
- the partition corresponding to the string
- Throws:
IllegalArgumentException
- thrown if the provided strFrom is null or empty
-
-