2
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
3
* For full copyright and license information please refer to doc/license.txt.
9
* Allows navigation of a word via permutation tree: exposes the permutations
10
* of a string as a tree
14
var items : List<String>;
16
private function new( items : List<String> )
22
* Provide a string for a permutation which will permutate all orders of the
23
* characters in the string.
25
* @param string [in] the characters which to permute.
26
* NOTE: Characters are all treated as unique, thus providing a character
27
* twice will result in two passes on that branch. This is very hard
28
* to avoid without suffering horrible performance loss.
30
static public function withString( string : String )
32
var items = new List<String>();
33
for( i in 0...string.length )
34
items.add( string.charAt(i) );
35
return new PermutationTree( items );
39
* Obtain the root node of the tree. Multiple calls to this function may
40
* return different objects. The object returned should be considered
41
* invalid if the owning tree changes.
43
public function root() : PermutationIterator
45
return new TreePermutationIterator( new TreePermutationIteratorInt( null, items, 0 ) );
50
* Build the iterator out of the virtual node items. That was how it was built
51
* first, and since there is no present need to optimize this it will just stay
54
private class TreePermutationIterator implements PermutationIterator
56
var current : TreePermutationIteratorInt;
58
public function new( start : TreePermutationIteratorInt )
63
public function childFirstStep() : Bool
65
if( current.hasFirstChild() )
67
current = current.firstChild();
70
if( current.hasNextSibling() )
72
current = current.nextSibling();
76
while( current.hasParent() )
78
current = current.parent();
79
if( current.hasNextSibling() )
81
current = current.nextSibling();
89
public function stringToHere() : String { return current.stringToHere(); }
92
* @return [out] firstChild, or null if none
94
public function toFirstChild() : Bool { current = current.firstChild(); return current != null; }
95
public function hasFirstChild() : Bool { return current.hasFirstChild(); }
98
* @return [out] next sbiling, or null if none
100
public function toNextSibling() : Bool { current = current.nextSibling(); return current != null; }
101
public function hasNextSibling() : Bool { return current.hasNextSibling(); }
104
* @return [out] parent, or null if none
106
public function toParent() : Bool { current = current.parent(); return current != null; }
107
public function hasParent() : Bool { return current.hasParent(); }
111
private class TreePermutationIteratorInt
113
var parentN : TreePermutationIteratorInt;
114
var set : List<String>; //rotation such that "at" is always at front
115
var at : Int; //how far into this set we are
117
public function new( parent : TreePermutationIteratorInt, set : List<String>, at : Int )
119
this.parentN = parent;
124
public function stringToHere() : String
126
var ret = (parentN == null) ? "" : parentN.stringToHere();
131
public function firstChild() : TreePermutationIteratorInt
133
if( !hasFirstChild() )
136
var cset = ListUtil.clone( set );
137
cset.pop(); //drop out item
138
return new TreePermutationIteratorInt( this, cset, 0 );
141
public function hasFirstChild() : Bool
143
return set.length > 1;
146
public function nextSibling() : TreePermutationIteratorInt
148
if( !hasNextSibling() )
151
//NOTE: This clone could be avoided if we use Array's instead of List's
152
var sset = ListUtil.clone( set );
157
return new TreePermutationIteratorInt( parentN, sset, at+1 );
160
public function hasNextSibling() : Bool
162
return (at+1) < set.length;
165
public function parent() : TreePermutationIteratorInt
170
public function hasParent() : Bool
172
return parentN != null;