[ Home ] | [ Concepts ] | [ Tutorial ] | [ API ] | [ Download ] | [ Resources ] | [ Credits ] |
Position
that intuitively plays the role of the "cell" in the linked list. This means that we can add an item after a given Position
, we can remove a Position
, we can get the first and the last Position
of a sequence and get the Position
that is positionned after a given Position
. This interface has only two features: it knows which element it contains and also knows which collection it belongs to. It has no possibility to know its successor (i.e. no method getNext()
) or predecessor. Only the sequence knows this information. It is possible to implement such an interface using a sequence implemented with a doubly linked list (in this case, the Position is implemented by the cell class). As an option, we provide also an implementation of this interface using an underlying array. In this case, the Position
contains the element plus the index where it is stored, the array contains only Positions
.
seq
(we do not know how it was implemented).Positions
pos1 and pos2 visit the sequence. We swap elements if they are in the wrong order. (this piece of code comes from bubble sort algorithm).pos1 = seq.first(); while(pos1!=seq.last()){ pos2=seq.after(pos1); if( pos1.element()> pos2.element()){ Integer tmp = pos1.element(); seq.replace(pos1,pos2.element()); seq.replace(pos2,tmp); } }This example has the same efficiency for both of the possible implementations of the framework.
MyString
that extends a String
and has two properties positionInList1
and positionInList2
.
public static void removeFirstThreeOfL1FromL2(Sequencesequence1, Sequence sequence2){ Position pos=sequence1.first(); for(int i=0;i<3;i++){ MyString s=sequence1.element(pos); sequence2.delete(s.getPositionInList2()); pos=sequence1.after(pos); } } public void positionEfficiencyExample(){ MyString s1=new MyString("1"); MyString s2=new MyString("2"); MyString s3=new MyString("3"); MyString s4=new MyString("4"); MyString s5=new MyString("5"); MyString s6=new MyString("6"); Sequence seq1 = new ArraySequence (); s1.setPositionInList1(seq1.insert(s1)); s2.setPositionInList1(seq1.insert(s2)); s3.setPositionInList1(seq1.insert(s3)); s4.setPositionInList1(seq1.insert(s4)); s5.setPositionInList1(seq1.insert(s5)); s6.setPositionInList1(seq1.insert(s6)); Sequence seq2 = new LinkedSequence (); s1.setPositionInList2(seq2.insert(s1)); s6.setPositionInList2(seq2.insert(s6)); s5.setPositionInList2(seq2.insert(s5)); s4.setPositionInList2(seq2.insert(s4)); s2.setPositionInList2(seq2.insert(s2)); s3.setPositionInList2(seq2.insert(s3)); removeFirstThreeOfL1FromL2(seq1,seq2); }
java.util.List
interface.
public static void bubbleSortArrayLike(ListWe can without any problem use our data structure, since it implements this interface.list){ int size = list.size(); for (int pass=0; pass < size ; pass++){ for(int index = 0; index< size-(pass+1);index++){ if(list.get(index)>list.get(index+1)){ Integer tmp = list.get(index); list.set(index,list.get(index+1)); list.set(index+1,tmp); } } } }
Listseq1 = new ArraySequence (); seq1.add(10); seq1.add(20); seq1.add(30); seq1.add(40); seq1.add(1); bubbleSortArrayLike(seq1);
Sequences are iterable. So each method that returns a Sequence can be iterated.
For instance, the method g.vertices()
:
ListPrevious Tutorial Next> vertices = g.vertices(); for(Position v : vertices){ if(! bufferedVertices.contains(v)){ buffer.add(v);