You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/mutable.md
+15-4Lines changed: 15 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,18 @@
1
+
```@meta
2
+
DocTestSetup = quote
3
+
using Bijections
4
+
end
5
+
```
6
+
1
7
# [Bijections for Mutable Structures](@id mutable)
2
8
3
9
## Mutating keys/values can lead to corruption
4
10
5
11
The safest use of a `Bijection` is when the keys and values are immutable.
6
12
If a mutable key or value in a `Bijection` is altered, the bijective property
7
13
can be compromised. Here is an example:
8
-
```
14
+
15
+
```jldoctest
9
16
julia> b = Bijection{Int, Vector{Int}}();
10
17
11
18
julia> b[1] = [1,2,3]
@@ -42,7 +49,8 @@ In case none of these is a viable option, we provide the following additional al
42
49
## Keys/values as objects
43
50
44
51
The issue in the example presented above is that distinct Julia objects may be equal, but not the same object. For example:
45
-
```
52
+
53
+
```jldoctest
46
54
julia> v = [1,2,3];
47
55
48
56
julia> w = [1,2,3];
@@ -55,13 +63,16 @@ false
55
63
```
56
64
57
65
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:
58
-
```
66
+
67
+
```julia
59
68
Bijection{K, V, IdDict{K,V}, IdDict{V,K}}()
60
69
```
70
+
61
71
where `K` is the type of the keys and `V` is the type of the values.
62
72
63
73
For example:
64
-
```
74
+
75
+
```jldoctest
65
76
julia> b = Bijection{Vector{Int}, String, IdDict{Vector{Int},String}, IdDict{String,Vector{Int}}}();
0 commit comments