RosettaCodeData/Task/Burrows-Wheeler-transform/Zkl/burrows-wheeler-transform-1.zkl
2023-07-01 13:44:08 -04:00

17 lines
689 B
Text

class BurrowsWheelerTransform{
fcn init(chr="$"){ var special=chr; }
fcn encode(str){
_assert_(not str.holds(special), "String cannot contain char \"%s\"".fmt(special) );
str=str.append(special);
str.len().pump(List().merge,'wrap(n){ String(str[n,*],str[0,n]) })
.pump(String,T("get",-1)); // last char of each "permutation"
}
fcn decode(str){
table:=List.createLong(str.len(),""); // ("",""..), mutable
do(str.len()){
foreach n in (str.len()){ table[n]=str[n] + table[n] }
table.sort();
} // --> ("$dogwood","d$dogwoo","dogwood$",...)
table.filter1("%s*".fmt(special).glob)[1,*]; // str[0]==$, often first element
}
}