Skip to content

Instantly share code, notes, and snippets.

@frostiq
Last active December 31, 2015 23:09
Show Gist options
  • Save frostiq/8058109 to your computer and use it in GitHub Desktop.
Save frostiq/8058109 to your computer and use it in GitHub Desktop.
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 20.12.13
* Time: 23.06
* To change this template use File | Settings | File Templates.
*/
interface ITuple extends Cloneable, Comparable<ITuple>{
public int getSize();
public int getSum();
public ITuple clone() throws CloneNotSupportedException;
}
abstract class Tuple implements ITuple{
public static final int size = 0;
public Tuple() {}
public int getSize(){return arr.length;}
public int getSum(){
int s = 0;
for (int a: arr)
s+=a;
return s;
}
@Override
public int compareTo(ITuple o) {
return this.getSum()-o.getSum();
}
@Override
public Tuple clone() throws CloneNotSupportedException {
return (Tuple)super.clone();
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
for (int i = 0; i < arr.length-1; i++) {
b.append(arr[i]);
b.append(", ");
}
return b.append(arr[arr.length-1]).toString();
}
protected Integer[] arr;
}
class Tuple3 extends Tuple{
public static final int size = 3;
public Tuple3(int v1, int v2, int v3) {
arr = new Integer[size];
arr[0] = v1;
arr[1] = v2;
arr[2] = v3;
}
}
class Tuple2 extends Tuple{
public static final int size = 2;
public Tuple2(int v1, int v2) {
arr = new Integer[size];
arr[0] = v1;
arr[1] = v2;
}
}
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 20.12.13
* Time: 18.23
* To change this template use File | Settings | File Templates.
*/
public class Runner {
public static void main(String args[]){
TupleArray<Tuple2> arr1 = new TupleArray(2, Tuple2.class);
arr1.setElem(new Tuple2(1,2), 0);
arr1.setElem(new Tuple2(3,4), 1);
TupleArray<Tuple3> arr3 = new TupleArray(1, Tuple3.class);
arr3.setElem(new Tuple3(5,6,7),0);
for(Object a: arr1)
System.out.println(a);
System.out.println(arr1.maxTuple());
for(Object a: arr3)
System.out.println(a);
}
}
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 20.12.13
* Time: 17.06
* To change this template use File | Settings | File Templates.
*/
import java.lang.reflect.Array;
import java.util.*;
public class TupleArray<T extends ITuple> implements Cloneable, Iterable{
public TupleArray(){}
public TupleArray(int size, Class<T> elementType) {
this.elementType = elementType;
arr = (T[]) Array.newInstance(elementType, size);
}
public T getElem(int i) {
if(0<=i && i<arr.length)
return arr[i];
else
throw new NoSuchElementException();
}
public void setElem(T elem, int i) {
if(0<=i && i<arr.length)
this.arr[i] = elem;
else
throw new NoSuchElementException();
}
public T maxTuple(){
T max = arr[0];
for(int i=1; i<arr.length; ++i)
if (max.compareTo(arr[i])<0) max = arr[i];
return max;
}
@Override
public TupleArray clone() throws CloneNotSupportedException {
TupleArray copy = (TupleArray)super.clone();
for (int i = 0; i < arr.length; i++) {
copy.arr[i] = arr[i].clone();
}
return copy;
}
@Override
public Iterator<T> iterator() {
return new TupleArrayIterator<T>(arr);
}
protected T[] arr;
private Class<T> elementType;
}
class TupleArrayIterator<T extends ITuple> implements Iterator<T> {
private T array[];
private int pos = 0;
public TupleArrayIterator(T anArray[]) {
array = anArray;
}
@Override
public boolean hasNext() {
return pos < array.length;
}
@Override
public T next() throws NoSuchElementException {
if (hasNext())
return array[pos++];
else
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment