Created
August 23, 2010 15:35
-
-
Save fb55/545725 to your computer and use it in GitHub Desktop.
jsarray-behaviour for java
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
class clone{ | |
private int id; | |
public clone next; | |
public clone previous; | |
private boolean deleted = false; | |
public String value; | |
public int getId(){ | |
return this.id; | |
} | |
public void setInitId(int newId){ | |
if(this.getId() == 0){ | |
this.id = newId; | |
} | |
} | |
public void setId(int newId){ | |
this.previous.innerSibling(newId, this.value); | |
this.delete(); | |
return; | |
} | |
private void createNewSibling(int newId, String val){ | |
clone innerNext = new clone(); | |
innerNext.setInitId(newId); | |
innerNext.next = this.next; | |
innerNext.previous = this; | |
innerNext.value = val; | |
this.next = innerNext; | |
return; | |
} | |
public void innerSibling(int newId, String val){ | |
if ( newId == this.getId() ){ | |
this.innerSibling( (newId+1), val); | |
return; | |
} | |
int nextId = 0; | |
try{ nextId = next.getId(); } | |
catch(Exception e){ nextId = 0; } | |
if ( (nextId == 0) && (newId > this.getId()) ){ | |
this.createNewSibling(newId, val); | |
return; | |
} | |
if (nextId != 0){ | |
if ( (newId > this.getId()) && (newId < nextId) ){ | |
this.createNewSibling(newId, val); | |
return; | |
} | |
if ( (newId > this.getId()) && (newId > nextId) ){ | |
next.innerSibling(newId, val); | |
return; | |
} | |
} | |
if(newId < this.getId()){ | |
int prevId = 0; | |
try{ prevId = previous.getId(); } | |
catch(Exception e){ prevId = 0; } | |
if(prevId != 0){ | |
previous.innerSibling(newId, val); | |
return; | |
} | |
else{ | |
clone innerNext = new clone(); | |
innerNext.setInitId(newId); | |
innerNext.next = this; | |
innerNext.value = val; | |
this.previous = innerNext; | |
return; | |
} | |
} | |
else { | |
next.innerSibling(newId, val); | |
return; | |
} | |
} | |
public void _innerAt(int pos, String val){ | |
int idStore = (this.getId()-1); | |
if(pos == idStore ){ | |
this.createNewSibling(pos, val); | |
return; | |
} | |
if(pos < idStore) { | |
this.previous._innerAt(pos, val); | |
} | |
if(pos > idStore) { | |
this.next._innerAt(pos, val); | |
} | |
} | |
public void delete(){ | |
this.deleted = true; | |
try{this.next.previous = this.previous;} | |
catch(Exception e){ | |
try{this.next.previous = null;} | |
catch(Exception f){} | |
}; | |
try { this.previous.next = this.next; } | |
catch(Exception e){ | |
try {this.previous.next = null;} | |
catch(Exception f) {} | |
}; | |
return; | |
} | |
} |
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
//tries to emulate javascript array behaviour | |
class jsArray { | |
//values | |
private clone arr = new clone(); | |
private int arrLength = 1; | |
//initialiser | |
public void init (){ | |
arr.setInitId(1); | |
return; | |
} | |
//getter | |
public int length(){ return this.arrLength; } | |
public clone firstChild () { | |
return this.arr.next; | |
} | |
public clone nthChild (int n) { | |
clone ret = this.arr.next; | |
for(int i = n-1; i>0; i--){ | |
ret = ret.next; | |
} | |
return ret; | |
} | |
public clone lastChild () { | |
return this.nthChild( (this.length()-1) ); | |
} | |
public clone getById (int mId) { | |
clone cln = this.arr; | |
for(int i = (this.length()-1);i>0;i--){ | |
if(cln.getId() == mId){ return cln; } | |
cln = cln.next; | |
} | |
return new clone(); | |
} | |
public clone getByValue (String val){ | |
clone cln = arr; | |
for(int i = (this.length()-1);i>0;i--){ | |
if(cln.value == val){ return cln; } | |
cln = cln.next; | |
} | |
return new clone(); | |
} | |
//setter | |
public void push(String val){ | |
this.arrLength++; | |
this.arr.innerSibling(this.length(), val); | |
return; | |
} | |
public void innerAt (int pos, String val){ | |
arr._innerAt(pos, val); | |
this.resort(); | |
} | |
public String join(String joiner){ | |
String ret = this.arr.next.value; | |
clone nxt = this.arr.next.next; | |
for(int i=(this.length()-2);i>0;i--){ | |
ret = ret + joiner + nxt.value; | |
nxt = nxt.next; | |
} | |
return ret; | |
} | |
public void resort(){ | |
int id = this.arr.next.getId(); | |
clone nxt = this.arr.next.next; | |
for(int i=(this.length()-2);i>0;i--){ | |
if(nxt.getId()<id){ | |
nxt.setId( nxt.getId() ); | |
} | |
else{ | |
if(nxt.getId() == id){ | |
nxt.setId( (nxt.getId()+1) ); | |
} | |
} | |
nxt = nxt.next; | |
} | |
return; | |
} | |
//for clones | |
public void concat(clone chld){ | |
this.push(chld.value); | |
try { this.concat(chld.next);} | |
catch (Exception e) { return; } | |
} | |
//for jsArrays | |
public void concat(jsArray arri){ | |
//arri.resort(); | |
clone chld = arri.firstChild(); | |
for(int i = arri.length()-1; i>0; i--){ | |
this.push(chld.value); | |
chld = chld.next; | |
} | |
return; | |
} | |
public String[] arrayfy(){ | |
String ret[] = new String[ (this.length()-2) ]; | |
int j=(this.length()-2); | |
clone cln = this.arr.next; | |
for(int i = 0;i<j;i++){ | |
ret[i] = cln.value; | |
cln = cln.next; | |
} | |
return ret; | |
} | |
public void slice (int first, int last, String[] replace){ | |
//nthChild | |
clone one = this.arr.next; | |
for(int i = first-1; i>0; i--){ | |
one = one.next; | |
} | |
//delete it | |
int diff = first - last; | |
while( diff-- >= 0 ) { | |
clone two = one.next; | |
one.delete(); | |
one = two; | |
} | |
int repLen = replace.length; | |
while( repLen-- > 0){ | |
this.innerAt(first, replace[repLen]); | |
} | |
//bug: cant run join afterwards | |
} | |
} |
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
public class cloneWars { | |
public static void main(String args[]){ | |
//create array | |
jsArray jsarr = new jsArray(); | |
jsarr.init(); | |
//fill in some values | |
jsarr.push( "1" ); | |
jsarr.push( "2" ); | |
jsarr.push( "3" ); | |
jsarr.push( "4" ); | |
jsarr.push( "5" ); | |
jsarr.push( "6" ); | |
//return some values | |
System.out.println(jsarr.nthChild(2).value); | |
System.out.println(jsarr.lastChild().value); | |
System.out.println(jsarr.join(", ")); | |
jsarr.resort(); | |
//create a new array | |
jsArray jsarr2 = new jsArray(); | |
jsarr2.init(); | |
jsarr2.push( "6" ); | |
jsarr2.push( "7" ); | |
jsarr2.push( "8" ); | |
jsarr.concat(jsarr2); | |
System.out.println(jsarr.join(", ")); | |
//create new clone | |
clone clone = new clone(); | |
clone.setInitId(23); | |
clone.value = "10"; | |
clone.innerSibling(17,"9"); | |
jsarr.concat(clone); | |
System.out.println(jsarr.join(", ")); | |
//test getters | |
System.out.println(jsarr.getById(6).value); | |
System.out.println(jsarr.getByValue("7").getId()); | |
System.out.println(jsarr.join(", ")); | |
String[] _sliceStore = {"Slice", "Store"}; | |
jsarr.slice(3,5, _sliceStore); | |
System.out.println(jsarr.join(", ")); | |
System.out.println(jsarr.arrayfy()[8]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment