Last active
August 29, 2015 14:25
-
-
Save cab404/3c04357083bd3712b330 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cab404.fnst; | |
/** | |
* Fenechka scheme | |
* <p/> | |
* Created at 11:19 on 26/07/15 | |
* | |
* @author cab404 | |
*/ | |
public class StandartTemplate { | |
public CharSequence start; | |
/** | |
* Contains knots | |
* 0 - first thread on top, threads swapped | |
* 1 - first thread on top, threads not swapped | |
* 2 - second thread on top, threads swapped | |
* 3 - second thread on top, threads not swapped | |
* We look only at last two bits of byte. | |
**/ | |
public byte[] knots; | |
/** | |
* Used as buffer for calculations | |
*/ | |
private char[] deck; | |
/** | |
* Swaps thread with one next to it in deck | |
*/ | |
private void swap(int thread) { | |
char a = deck[thread]; | |
deck[thread] = deck[thread + 1]; | |
deck[thread + 1] = a; | |
} | |
/** | |
* Creates array of colors of knots. | |
*/ | |
public char[] evaluate(char[] colors) { | |
if (colors.length < knots.length) throw new RuntimeException("Not enough space in colors array."); | |
// Number of threads | |
short f_len = (short) start.length(); | |
// Current shift | |
short sh = 0; | |
// Current knot (absolute) | |
short knot_counter = 0; | |
// Type of shift | |
boolean even = f_len % 2 == 0; | |
if (deck == null || deck.length != f_len) | |
deck = new char[f_len]; | |
for (int i = 0; i < start.length(); i++) | |
deck[i] = start.charAt(i); | |
for (byte knot : knots) { | |
char color = (knot & 2) == 0 ? deck[sh] : deck[sh + 1]; | |
if ((knot & 1) == 0) swap(sh); | |
colors[knot_counter++] = color; | |
// odd | |
/* \ / \ / \ | |
* o o ) | |
* / \ / \ / | |
* ( o o | |
* \ / \ / \ | |
* o o ) | |
*/ | |
// even | |
/* \ / \ / \ / | |
* o o o | |
* / \ / \ / \ | |
* ( o o ) | |
* \ / \ / \ / | |
* o o o | |
* / \ / \ / \ | |
*/ | |
sh += 2; | |
if (even) { | |
sh %= f_len - 1; | |
} else { | |
if (sh >= f_len - 1) sh = (byte) (f_len - sh); | |
} | |
} | |
return colors; | |
} | |
public int getRowWidth(int row) { | |
int length = start.length(); | |
if (length % 2 == 1) | |
return (length - 1) / 2; | |
else | |
return length / 2 - (row % 2); | |
} | |
public int getRow(int knotIndex) { | |
int length = start.length(); | |
int two_even_rows = length - 1; | |
if (length % 2 == 1) | |
return knotIndex / (two_even_rows / 2); | |
else { | |
int row = knotIndex / two_even_rows * 2; | |
if (knotIndex % two_even_rows >= length / 2) row++; | |
return row; | |
} | |
} | |
@Override | |
public String toString() { | |
StringBuilder dst = new StringBuilder("fT:"); | |
dst.append(start.length()).append('>'); | |
for (byte c : knots) | |
dst.append(c); | |
return dst.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment