Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
osmundf committed Sep 16, 2019
0 parents commit fd5a9e6
Show file tree
Hide file tree
Showing 26 changed files with 2,015 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# IDE files
/.idea

# Maven target
/_target

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
717 changes: 717 additions & 0 deletions .idea/codeStyles/Project.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/combinatoric.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 ZoftWhere

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 67 additions & 0 deletions main-java/app/zoftwhere/combinatoric/AbstractPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package app.zoftwhere.combinatoric;

import java.util.Arrays;
import java.util.List;

public abstract class AbstractPermutation<T> {

private final int[] index;

private final int size;

/**
* User PermutationBuilder to initialize a new permutation.
*
* @param index the index array for permutations.
*/
AbstractPermutation(int[] index) {
this.index = index;
this.size = index.length;
}

public int getSize() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public boolean isPresent() {
return size != 0;
}

public int[] getIndex() {
int[] push = new int[size];
System.arraycopy(index, 0, push, 0, size);
return push;
}

public int getIndex(int position) {
return index[position];
}

public abstract List<T> getValue();

public abstract T getValue(int position);

public abstract AbstractPermutation<T> next(int position);

/**
* Helper method for advancing at a give index, and resetting (sorting) trailing positions.
*
* @param index the index array to advance
* @param left the index position to advance
* @param right the replacement position index array index
* @return the index array after advancement
*/
int[] next(int[] index, int left, int right) {
int[] push = new int[size];
System.arraycopy(index, 0, push, 0, size);
push[left] = index[right];
push[right] = index[left];
Arrays.sort(push, left + 1, size);
return push;
}

}
90 changes: 90 additions & 0 deletions main-java/app/zoftwhere/combinatoric/AbstractSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package app.zoftwhere.combinatoric;

public abstract class AbstractSeries<T, L> implements Series<T, L> {

private final T base;

private final int exponent;

private final T increment;

private final L length;

AbstractSeries(T base, T increment, int exponent, L length) {
this.base = base;
this.increment = increment;
this.exponent = exponent;
this.length = length;
}

@Override
public T base() {
return base;
}

@Override
public T increment() {
return increment;
}

@Override
public int exponent() {
return exponent;
}

@Override
public L length() {
return length;
}

protected abstract AbstractSeries<T, L> clone(T base, T increment, int exponent, L length);

protected abstract T convertValue(int value);

protected abstract T convertValue(long value);

protected abstract L convertLength(int length);

protected abstract L convertLength(long length);

public AbstractSeries<T, L> base(int base) {
return clone(convertValue(base), increment(), exponent(), length());
}

public AbstractSeries<T, L> base(long base) {
return clone(convertValue(base), increment(), exponent(), length());
}

public AbstractSeries<T, L> base(T base) {
return clone(base, increment(), exponent(), length());
}

public AbstractSeries<T, L> increment(int increment) {
return clone(base(), convertValue(increment), exponent(), length());
}

public AbstractSeries<T, L> increment(long increment) {
return clone(base(), convertValue(increment), exponent(), length());
}

public AbstractSeries<T, L> increment(T increment) {
return clone(base(), increment, exponent(), length());
}

public AbstractSeries<T, L> exponent(int exponent) {
return clone(base(), increment(), exponent, length());
}

public AbstractSeries<T, L> length(int length) {
return clone(base(), increment(), exponent(), convertLength(length));
}

public AbstractSeries<T, L> length(long length) {
return clone(base(), increment(), exponent(), convertLength(length));
}

public AbstractSeries<T, L> length(L length) {
return clone(base(), increment(), exponent(), length);
}

}
72 changes: 72 additions & 0 deletions main-java/app/zoftwhere/combinatoric/PermutationBasic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app.zoftwhere.combinatoric;

import java.util.List;
import java.util.NoSuchElementException;

class PermutationBasic<T> extends AbstractPermutation<T> {

private final int size;

private final int[] index;

PermutationBasic(int[] index, int size) {
super(index);
this.index = getIndex();
this.size = size;
}

@Override
public List<T> getValue() {
return null;
}

@Override
public T getValue(int position) {
throw new NoSuchElementException();
}


@Override
public PermutationBasic<T> next(int position) {
if (position < 0 || position >= size) {
return new PermutationBasic<>(new int[0], 0);
}

int min = index[position];
int max = Integer.MAX_VALUE;
int swap = position;

for (int i = position + 1; i < size; i++) {
if (index[i] == min + 1) {
int[] push = next(index, position, i);
return new PermutationBasic<>(push, size);
}
if (index[i] > min && index[i] < max) {
swap = i;
max = index[i];
}
}

if (swap == position) {
return new PermutationBasic<>(new int[0], 0);
}

return new PermutationBasic<>(next(index, position, swap), size);
}

@Override
public String toString() {
if (isEmpty()) {
return "[]";
}

StringBuilder builder = new StringBuilder("[");
builder.append(String.format("%d", index[0]));
for (int i = 1; i < size; i++) {
builder.append(String.format(", %d", index[i]));
}

return builder.append("]").toString();
}

}
Loading

0 comments on commit fd5a9e6

Please sign in to comment.