Last active
July 8, 2022 18:37
-
-
Save Demuirgos/c31aa22aa72d2a050c40a4ef6c89cf5c to your computer and use it in GitHub Desktop.
DirectTrees Vs InvertedTrees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type metadata = string | |
type NodeBody = { | |
Id : int | |
Parent : int | |
Metadata : string | |
} | |
and InvertedNode = | |
| IChild of NodeBody | |
with static member TransferItem itemID fromID toID state = | |
state |> List.map ( | |
function | |
| IChild(data) as item when data.Id = itemID -> | |
if(data.Parent = fromID) | |
then IChild { data with Parent = toID } | |
else item | |
| _ as e -> e | |
) | |
static member TransferItems fromID toID state = | |
state |> List.map ( | |
function | |
| IChild(data) as item -> | |
if(data.Parent = fromID) | |
then IChild { data with Parent = toID } | |
else item | |
| _ as e -> e | |
) | |
type NormalNode = | |
| NRoot of int * Map<int, NormalNode> | |
| NChild of metadata | |
with static member TransferItem itemID fromID toID state = | |
match Map.tryFind fromID state, Map.tryFind toID state with | |
| Some(NRoot(s_id, s_items)), Some(NRoot(t_id, t_items)) -> | |
match Map.tryFind itemID s_items with | |
| Some(item) -> | |
state | |
|> Map.add fromID | |
(NRoot ( | |
s_id, | |
Map.remove itemID s_items | |
)) | |
|> Map.add toID | |
(NRoot ( | |
t_id, | |
Map.add itemID item t_items | |
)) | |
| _ -> state | |
| _ -> state | |
static member TransferItems fromID toID state = | |
match Map.tryFind fromID state, Map.tryFind toID state with | |
| Some(NRoot(s_id, s_items)), Some(NRoot(t_id, t_items)) -> | |
state | |
|> Map.add fromID | |
(NRoot ( | |
s_id, | |
Map.empty | |
)) | |
|> Map.add toID | |
(NRoot ( | |
t_id, | |
s_items | |
)) | |
| _ -> state | |
let ModeNormal = | |
Seq.initInfinite (fun i -> | |
let id = i * 4 | |
id, NRoot(id, ( | |
[ | |
id + 1, NChild (sprintf "Hat%d" id) | |
id + 2, NChild (sprintf "Shirt%d" id) | |
id + 3, NChild (sprintf "Pants%d" id) | |
] |> Map.ofList | |
)) | |
) | |
|> Seq.take 2 | |
|> Map.ofSeq | |
let ModeInverted = | |
Seq.initInfinite (fun i -> | |
let id = i * 4 | |
let root = id | |
[ | |
IChild { | |
Id = id + 1 | |
Parent = root | |
Metadata = (sprintf "Hat%d" id) | |
} | |
IChild { | |
Id = id + 2 | |
Parent = root | |
Metadata = (sprintf "Shirt%d" id) | |
} | |
IChild { | |
Id = id + 3 | |
Parent = root | |
Metadata = (sprintf "Pants%d" id) | |
} | |
] | |
) | |
|> Seq.take 2 | |
|> Seq.concat | |
|> List.ofSeq | |
ModeInverted | |
|> InvertedNode.TransferItems 0 4 | |
|> InvertedNode.TransferItem 1 4 0 | |
|> (printfn "%A") | |
ModeNormal | |
|> NormalNode.TransferItems 0 4 | |
|> NormalNode.TransferItem 1 4 0 | |
|> (printfn "%A") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment