some refactoring (and progress filling in the FOUND_WITH_CHILDREN case in the 'helpInsert' function)
This commit is contained in:
BIN
src/string-set
BIN
src/string-set
Binary file not shown.
@@ -238,6 +238,31 @@ struct
|
|||||||
* - Complete code for FOUND_WITH_CHILDREN case in insert function.
|
* - Complete code for FOUND_WITH_CHILDREN case in insert function.
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
fun insertNewChild (keys, insIdx, insKey, children, constructor) =
|
||||||
|
let
|
||||||
|
val newLength = Vector.length keys + 1
|
||||||
|
val newKeys = Vector.tabulate (newLength, fn idx =>
|
||||||
|
if idx < insIdx then Vector.sub (keys, idx)
|
||||||
|
else if idx > insIdx then Vector.sub (keys, idx - 1)
|
||||||
|
else insKey)
|
||||||
|
|
||||||
|
val newChildren = Vector.tabulate (newLength, fn idx =>
|
||||||
|
if idx < insIdx then Vector.sub (children, idx)
|
||||||
|
else if idx > insIdx then Vector.sub (children, idx - 1)
|
||||||
|
else FOUND)
|
||||||
|
|
||||||
|
in
|
||||||
|
constructor {keys = newKeys, children = newChildren}
|
||||||
|
end
|
||||||
|
|
||||||
|
fun appendNewChild (keys, insKey, children, constructor) =
|
||||||
|
let
|
||||||
|
val newKeys = Vector.concat [keys, Vector.fromList [insKey]]
|
||||||
|
val newChildren = Vector.concat [children, Vector.fromList [FOUND]]
|
||||||
|
in
|
||||||
|
constructor {keys = newKeys, children = newChildren}
|
||||||
|
end
|
||||||
|
|
||||||
fun helpInsert (insKey, keyPos, trie) : t =
|
fun helpInsert (insKey, keyPos, trie) : t =
|
||||||
case trie of
|
case trie of
|
||||||
FOUND =>
|
FOUND =>
|
||||||
@@ -254,21 +279,7 @@ struct
|
|||||||
in
|
in
|
||||||
(case insertBinSearch (findChr, keyPos, keys) of
|
(case insertBinSearch (findChr, keyPos, keys) of
|
||||||
INSERT_NEW_CHILD insIdx =>
|
INSERT_NEW_CHILD insIdx =>
|
||||||
let
|
insertNewChild (keys, insIdx, insKey, children, CHILDREN)
|
||||||
val newLength = Vector.length keys + 1
|
|
||||||
val newKeys = Vector.tabulate (newLength, fn idx =>
|
|
||||||
if idx < insIdx then Vector.sub (keys, idx)
|
|
||||||
else if idx > insIdx then Vector.sub (keys, idx - 1)
|
|
||||||
else insKey)
|
|
||||||
|
|
||||||
val newChildren = Vector.tabulate (newLength, fn idx =>
|
|
||||||
if idx < insIdx then Vector.sub (children, idx)
|
|
||||||
else if idx > insIdx then Vector.sub (children, idx - 1)
|
|
||||||
else FOUND)
|
|
||||||
|
|
||||||
in
|
|
||||||
CHILDREN {keys = newKeys, children = newChildren}
|
|
||||||
end
|
|
||||||
| FOUND_INSERT_POS insIdx =>
|
| FOUND_INSERT_POS insIdx =>
|
||||||
let
|
let
|
||||||
val trieKey = Vector.sub (keys, insIdx)
|
val trieKey = Vector.sub (keys, insIdx)
|
||||||
@@ -373,15 +384,20 @@ struct
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
| APPEND_NEW_CHILD =>
|
| APPEND_NEW_CHILD =>
|
||||||
let
|
appendNewChild (keys, insKey, children, CHILDREN))
|
||||||
val newKeys = Vector.concat [keys, Vector.fromList [insKey]]
|
end
|
||||||
val newChildren = Vector.concat
|
| FOUND_WITH_CHILDREN {keys, children} =>
|
||||||
[children, Vector.fromList [FOUND]]
|
let
|
||||||
in
|
val findChr = String.sub (insKey, keyPos)
|
||||||
CHILDREN {keys = newKeys, children = newChildren}
|
in
|
||||||
end)
|
(case insertBinSearch (findChr, keyPos, keys) of
|
||||||
|
INSERT_NEW_CHILD insIdx =>
|
||||||
|
insertNewChild
|
||||||
|
(keys, insIdx, insKey, children, FOUND_WITH_CHILDREN)
|
||||||
|
| FOUND_INSERT_POS insIdx => raise Empty
|
||||||
|
| APPEND_NEW_CHILD =>
|
||||||
|
appendNewChild (keys, insKey, children, FOUND_WITH_CHILDREN))
|
||||||
end
|
end
|
||||||
| FOUND_WITH_CHILDREN {keys, children} => raise Empty (* todo *)
|
|
||||||
|
|
||||||
fun insert (insKey, trie) =
|
fun insert (insKey, trie) =
|
||||||
if String.size insKey > 0 then helpInsert (insKey, 0, trie) else trie
|
if String.size insKey > 0 then helpInsert (insKey, 0, trie) else trie
|
||||||
|
|||||||
Reference in New Issue
Block a user