Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions docs/src/mutable.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
```@meta
DocTestSetup = quote
using Bijections
end
```

# [Bijections for Mutable Structures](@id mutable)

## Mutating keys/values can lead to corruption

The safest use of a `Bijection` is when the keys and values are immutable.
If a mutable key or value in a `Bijection` is altered, the bijective property
can be compromised. Here is an example:
```

```jldoctest
julia> b = Bijection{Int, Vector{Int}}();

julia> b[1] = [1,2,3]
Expand Down Expand Up @@ -42,7 +49,8 @@ In case none of these is a viable option, we provide the following additional al
## Keys/values as objects

The issue in the example presented above is that distinct Julia objects may be equal, but not the same object. For example:
```

```jldoctest
julia> v = [1,2,3];

julia> w = [1,2,3];
Expand All @@ -55,13 +63,16 @@ false
```

We may wish to create a `Bijection` in which the keys or values are permitted to be equal, but are distinct objects. Julia's `IdDict` is a variation of `Dict` in which keys/values are considered different if they are distinct object (even if they hold the same data). To replicate this behavior in a `Bijection` use this longer form constructor:
```

```julia
Bijection{K, V, IdDict{K,V}, IdDict{V,K}}()
```

where `K` is the type of the keys and `V` is the type of the values.

For example:
```

```jldoctest
julia> b = Bijection{Vector{Int}, String, IdDict{Vector{Int},String}, IdDict{String,Vector{Int}}}();

julia> b[ [1,2,3] ] = "alpha";
Expand Down
33 changes: 21 additions & 12 deletions docs/src/operations.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
```@meta
DocTestSetup = quote
using Bijections
end
```

# Operations


Expand All @@ -9,12 +15,14 @@ There are two functions that take a `Bijection` and return a new
[`inv`](@ref) and [`active_inv`](@ref).

### Independent inverse: `inv`

Given a `Bijection` `b`, calling `inv(b)` creates a new `Bijection`
that is the inverse of `b`. The new `Bijection` is completely independent
of the original, `b`. Changes to one do not affect the other:
```

```jldoctest
julia> b = Bijection{Int,String}()
Bijection Dict{Int64, String}()
Bijection{Int64, String, Dict{Int64, String}, Dict{String, Int64}}()

julia> b[1] = "alpha"
"alpha"
Expand All @@ -23,7 +31,7 @@ julia> b[2] = "beta"
"beta"

julia> bb = inv(b)
Bijection Dict{String, Int64} with 2 entries:
Bijection{String, Int64, Dict{String, Int64}, Dict{Int64, String}} with 2 entries:
"alpha" => 1
"beta" => 2

Expand All @@ -38,6 +46,7 @@ julia> b[3] = "gamma"

julia> bb["gamma"]
ERROR: KeyError: key "gamma" not found
[...]
```

### Active inverse: `active_inv`
Expand All @@ -47,9 +56,9 @@ case the original and the inverse are actively tied together.
That is, modification of one immediately affects the other.
The two `Bijection`s remain inverses no matter how either is modified.

```
```jldoctest
julia> b = Bijection{Int,String}()
Bijection Dict{Int64, String}()
Bijection{Int64, String, Dict{Int64, String}, Dict{String, Int64}}()

julia> b[1] = "alpha"
"alpha"
Expand All @@ -58,7 +67,7 @@ julia> b[2] = "beta"
"beta"

julia> bb = active_inv(b)
Bijection Dict{String, Int64} with 2 entries:
Bijection{String, Int64, Dict{String, Int64}, Dict{Int64, String}} with 2 entries:
"alpha" => 1
"beta" => 2

Expand All @@ -73,10 +82,10 @@ julia> bb["gamma"]

`Bijection`s can be used in a `for` statement just like Julia
dictionaries:
```
julia> for (x,y) in b; println("$x --> $y"); end
2 --> beta
3 --> gamma
1 --> alpha
```

```@repl example
# The order of iteration is not guaranteed, so this can't be a doctest # hide
Comment thread
goerz marked this conversation as resolved.
using Bijections # hide
b = Bijection(1 => "alpha", 2 => "beta", 3 => "gamma");
for (x, y) in b; println("$x --> $y"); end
```
Loading