@@ -21,12 +21,12 @@ struct Bijection{S,T} <: AbstractDict{S,T}
21
21
R = Set {T} ()
22
22
F = Dict {S,T} ()
23
23
G = Dict {T,S} ()
24
- new (D,R,F, G)
24
+ new (D, R, F, G)
25
25
end
26
26
27
27
# private, unsafe constructor
28
- function Bijection {S,T} (D:: Set{S} ,R:: Set{T} ,F:: Dict{S,T} ,G:: Dict{T,S} ) where {S,T}
29
- new (D,R,F, G)
28
+ function Bijection {S,T} (D:: Set{S} , R:: Set{T} , F:: Dict{S,T} , G:: Dict{T,S} ) where {S,T}
29
+ new (D, R, F, G)
30
30
end
31
31
end
32
32
@@ -53,24 +53,44 @@ function Bijection(x::S, y::T) where {S,T}
53
53
end
54
54
55
55
# Convert an `AbstractDict` to a `Bijection`
56
- function Bijection (dict:: AbstractDict{S, T} ) where S where T
56
+ function Bijection (dict:: AbstractDict{S,T} ) where {S,T}
57
+ vals = values (dict)
58
+ if length (dict) != length (unique (vals))
59
+ error (" Repeated value found in dict" )
60
+ end
61
+
57
62
b = Bijection {S,T} ()
58
63
for (k, v) in pairs (dict)
59
64
b[k] = v
60
65
end
61
66
return b
62
67
end
63
- Bijection (b:: Bijection ) = b
68
+
69
+ # Copy constructor
70
+ Bijection (b:: Bijection ) = Bijection (collect (b))
64
71
65
72
# # Convert a list of pairs to a Bijection
66
73
function Bijection (pair_list:: Vector{Pair{S,T}} ) where {S,T}
67
- d = Dict {S,T} (pair_list)
68
- Bijection (d)
74
+ p = unique (pair_list) # remove duplicate pairs
75
+ n = length (p)
76
+
77
+ xs = first .(p)
78
+ ys = last .(p)
79
+ if length (xs) != length (unique (xs)) || length (ys) != length (unique (ys))
80
+ error (" Repeated key or value found in pair list" )
81
+ end
82
+
83
+ b = Bijection {S,T} ()
84
+ for xy in p
85
+ x, y = xy
86
+ b[x] = y
87
+ end
88
+ return b
69
89
end
70
90
71
91
# Decent way to print out a bijection
72
- function show (io:: IO ,b:: Bijection{S,T} ) where {S,T}
73
- print (io," Bijection{$S ,$T } (with $(length (b)) pairs)" )
92
+ function show (io:: IO , b:: Bijection{S,T} ) where {S,T}
93
+ print (io, " Bijection{$S ,$T } (with $(length (b)) pairs)" )
74
94
end
75
95
76
96
display (b:: Bijection ) = (print (" Bijection " ); display (b. f))
@@ -84,11 +104,11 @@ display(b::Bijection) = (print("Bijection "); display(b.f))
84
104
For a `Bijection` `b` use the syntax `b[x]=y` to add `(x,y)` to `b`.
85
105
"""
86
106
function setindex! (b:: Bijection , y, x)
87
- if in (x,b. domain) || in (y,b. range)
107
+ if in (x, b. domain) || in (y, b. range)
88
108
error (" One of x or y already in this Bijection" )
89
109
else
90
- push! (b. domain,x)
91
- push! (b. range,y)
110
+ push! (b. domain, x)
111
+ push! (b. range, y)
92
112
b. f[x] = y
93
113
b. finv[y] = x
94
114
end
@@ -114,17 +134,17 @@ function inverse(b::Bijection, y)
114
134
end
115
135
116
136
# the notation b(y) is a shortcut for inverse(b,y)
117
- (b:: Bijection )(y) = inverse (b,y)
137
+ (b:: Bijection )(y) = inverse (b, y)
118
138
119
139
120
140
# Remove a pair (x,y) from a bijection
121
141
"""
122
- `delete!(b::Bijection,x)` deletes the ordered pair `(x,b[x])` from `b`.
142
+ `delete!(b::Bijection,x)` deletes the ordered pair `(x,b[x])` from `b`.1==
123
143
"""
124
144
function delete! (b:: Bijection , x)
125
145
y = b[x]
126
- delete! (b. domain,x)
127
- delete! (b. range,y)
146
+ delete! (b. domain, x)
147
+ delete! (b. range, y)
128
148
delete! (b. f, x)
129
149
delete! (b. finv, y)
130
150
return b
@@ -163,7 +183,7 @@ domain(b::Bijection) = copy(b.domain)
163
183
image (b:: Bijection ) = copy (b. range)
164
184
165
185
166
- iterate (b:: Bijection{S,T} ,s:: Int ) where {S,T} = iterate (b. f,s)
186
+ iterate (b:: Bijection{S,T} , s:: Int ) where {S,T} = iterate (b. f, s)
167
187
iterate (b:: Bijection{S,T} ) where {S,T} = iterate (b. f)
168
188
169
189
# convert a Bijection into a Dict; probably not useful
0 commit comments