Commit e9c57cd6 authored by MARCHE Claude's avatar MARCHE Claude
Browse files

lcp: syntax OK until tests

parent 8619672a
...@@ -31,7 +31,7 @@ Pseudocode: ...@@ -31,7 +31,7 @@ Pseudocode:
ADVANCED ADVANCED
======== ========
...@@ -53,7 +53,7 @@ Typically, the suffixes are not stored explicitly as above but ...@@ -53,7 +53,7 @@ Typically, the suffixes are not stored explicitly as above but
represented as pointers into the original text. The suffixes in a suffix represented as pointers into the original text. The suffixes in a suffix
array are sorted in lexicographical order. This way, occurrences of array are sorted in lexicographical order. This way, occurrences of
repeated substrings in the original text are neighbors in the suffix repeated substrings in the original text are neighbors in the suffix
array. array.
For the above, example (assuming pointers are 0-based integers), the For the above, example (assuming pointers are 0-based integers), the
sorted suffix array is: sorted suffix array is:
...@@ -74,60 +74,56 @@ that it does so correctly. ...@@ -74,60 +74,56 @@ that it does so correctly.
*) *)
module SuffixArray module SuffixArray
use import int.Int
use import array.Array
type suffixArray = { type suffixArray = {
a : map int int; values : array int;
suffixes : map int int; suffixes : array int;
n : int;
} }
let create (a:map int int) (length:int) : suffixArray = let select (s:suffixArray) (i:int) : int = s.suffixes[i]
let s = Array.make 0 length in
for i = 0 to length-1 do s[i] <- i done; use import ref.Refint
sort(s);
{ a = a; let lcp (a:array int) (x y:int) : int =
n = length; let n = a.length in
suffixes = s.elts;
}
let select (s:suffixArray) (int i) : int = s.suffixes[i]
let private_lcp (s:suffixArray) (x y:int) : int =
let n = s.n in
let l = ref 0 in let l = ref 0 in
while x + !l < n && y + !l < n && s.a[x + !l] = a[y + !l] do while x + !l < n && y + !l < n && a[x + !l] = a[y + !l] do
incr l incr l
done; done;
!l !l
let lcp (s:suffixArray) (i:int) : int = let compare (a:array int) (x y:int) : int =
private_lcp s.suffixes[i] s.suffixes[i-1]
let compare (s:suffixArray) (x y:int) : int =
if x = y then 0 else if x = y then 0 else
let l = ref 0 in let n = a.length in
while x + !l < n && y + !l < n && s.a[x + !l] = s.a[y + !l] do let l = lcp a x y in
incr l if x + l = n then -1 else
done; if y + l = n then 1 else
if x + !l = n then -1 else if a[x + l] < a[y + l] then -1 else
if y + !l = n then 1 else if a[x + l] > a[y + l] then 1 else
if s.a[x + !l] < s.a[y + !l] then -1 else
if s.a[x + !l] > s.a[y + !l] then 1 else
absurd absurd
let sort (s:suffixArray) (data : array int) = let sort (a:array int) (data : array int) =
for i = 0 to data.length do for i = 0 to data.length - 1 do
let j = ref i in let j = ref i in
while j > 0 && compare s data[!j-1] data[!j] > 0 do while !j > 0 && compare a data[!j-1] data[!j] > 0 do
let b = !j - 1 in let b = !j - 1 in
let t = data[!j] in let t = data[!j] in
data[!j] <- data[b]; data[!j] <- data[b];
data[b] <- t; data[b] <- t;
decr j decr j
done done
done done
let create (a:array int) : suffixArray =
let n = a.length in
let suf = Array.make 0 n in
for i = 0 to n-1 do suf[i] <- i done;
sort a suf;
{ values = a; suffixes = suf }
let test1 () = let test1 () =
int[] arr = {1,2,2,5}; int[] arr = {1,2,2,5};
...@@ -149,7 +145,11 @@ let test1 () = ...@@ -149,7 +145,11 @@ let test1 () =
end end
module LRS module LRS
let lcp (s:suffixArray) (i:int) : int =
SuffixArray.lcp s s.suffixes[i] s.suffixes[i-1]
private static int solStart = 0; private static int solStart = 0;
private static int solLength = 0; private static int solLength = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment