lacuna / bifurcan

functional, durable data structures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Forked set is modified after baked linear set modification

AlexPl292 opened this issue · comments

Hi, thank you for the great library!
I've found out that the forked set can be modified in case the initial linear set gets any modifications.
This behaviour doesn't reproduce for lists.
Here is the code example:

import io.lacuna.bifurcan.List;
import io.lacuna.bifurcan.Set;

public class Main {
    public static void main(String[] args) {
        Set<Integer> linearSet = new Set<Integer>().linear();
        linearSet.add(1);
        Set<Integer> forkedSet = linearSet.forked();
        linearSet.add(2);

        System.out.println("Linear set: " + linearSet);
        System.out.println("Forked set: " + forkedSet);

        List<Integer> linearList = new List<Integer>().linear();
        linearList.addLast(1);
        List<Integer> forkedList = linearList.forked();
        linearList.addLast(2);

        System.out.println("Linear list: " + linearList);
        System.out.println("Forked list: " + forkedList);
    }
}

And here is the produced output:

Linear set: {1, 2}
Forked set: {1, 2}
Linear list: [1, 2]
Forked list: [1]

I'd expect that the "Forked set" remains as {1}.

Here is the full project code: https://github.com/AlexPl292/distraught-chimera

According to the docs here:
https://lacuna.io/docs/bifurcan/io/lacuna/bifurcan/Set.html#linear--

If ICollection.forked() is called on a linear collection, all references to that linear collection should be discarded.

So the test appears invalid?