Skip to content

Commit 7d3a25c

Browse files
committed
Added composition
1 parent 4812a10 commit 7d3a25c

File tree

5 files changed

+94
-33
lines changed

5 files changed

+94
-33
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Bijections"
22
uuid = "e2ed5e7c-b2de-5872-ae92-c73ca462fb04"
3-
version = "0.1.5"
3+
version = "0.1.6"
44

55
[compat]
66
julia = "1"

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,21 @@ contains no pairs:
218218
julia> isempty(b)
219219
false
220220
```
221+
222+
223+
## Composition
224+
225+
Given two `Bijection`s `a` and `b`, their composition `c = a*b` is a new
226+
`Bijection` with the property that `c[x] = a[b[x]]` for all `x` in the
227+
domain of `b`.
228+
229+
```
230+
julia> a = Bijection{Int,Int}(); a[1] = 10; a[2] = 20;
231+
232+
julia> b = Bijection{String,Int}(); b["hi"] = 1; b["bye"] = 2;
233+
234+
julia> c = a * b;
235+
236+
julia> c["hi"]
237+
10
238+
```

src/Bijections.jl

+1
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,6 @@ function get(b::Bijection, key, default)
199199
end
200200

201201
include("inversion.jl")
202+
include("composition.jl")
202203

203204
end # end of module Bijections

src/composition.jl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Base.(*)
2+
3+
"""
4+
(*)(a::Bijection{A,B}, b::Bijection{B,C})::Bijection{A,C} where {A,B,C}
5+
6+
The result of `a * b` is a new `Bijection` `c` such that `c[x]` is `a[b[x]]` for `x`
7+
in the domain of `b`. This function throws an error is `domain(a)` is not the same
8+
as the `image` of `b`.
9+
"""
10+
function (*)(a::Bijection{B,A}, b::Bijection{C,B})::Bijection{C,A} where {A,B,C}
11+
# check that the domain of a equals the image of B
12+
if domain(a) != image(b)
13+
error("Domain/Image mismatch")
14+
end
15+
16+
c = Bijection{C,A}()
17+
for x in domain(b)
18+
c[x] = a[b[x]]
19+
end
20+
21+
return c
22+
end

test/runtests.jl

+52-32
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
11
using Test
22
using Bijections
33

4-
b = Bijection(3,"Hello")
5-
@test b[3] == "Hello"
6-
@test b("Hello") == 3
7-
@test haskey(b, 3)
8-
@test !haskey(b, 4)
9-
@test !haskey(b, "Hello")
4+
@testset "Basics" begin
5+
b = Bijection(3, "Hello")
6+
@test b[3] == "Hello"
7+
@test b("Hello") == 3
8+
@test haskey(b, 3)
9+
@test !haskey(b, 4)
10+
@test !haskey(b, "Hello")
1011

11-
b[2] = "Bye"
12-
@test b[2] == "Bye"
13-
@test b("Bye") == 2
14-
@test inverse(b,"Bye") == 2
12+
b[2] = "Bye"
13+
@test b[2] == "Bye"
14+
@test b("Bye") == 2
15+
@test inverse(b, "Bye") == 2
1516

1617

17-
@test domain(b) == Set([2,3])
18-
@test image(b) == Set(["Bye", "Hello"])
18+
@test domain(b) == Set([2, 3])
19+
@test image(b) == Set(["Bye", "Hello"])
1920

20-
@test length(b) == 2
21-
@test !isempty(b)
21+
@test length(b) == 2
22+
@test !isempty(b)
2223

2324

24-
bb = inv(b)
25-
@test bb["Bye"] == 2
26-
bb["xx"] = 4
27-
@test length(bb) != length(b)
25+
bb = inv(b)
26+
@test bb["Bye"] == 2
27+
bb["xx"] = 4
28+
@test length(bb) != length(b)
2829

29-
bb = active_inv(b)
30-
@test bb["Bye"] == 2
31-
b[0] = "Ciao"
32-
@test bb["Ciao"] == 0
33-
@test inv(bb) == b
30+
bb = active_inv(b)
31+
@test bb["Bye"] == 2
32+
b[0] = "Ciao"
33+
@test bb["Ciao"] == 0
34+
@test inv(bb) == b
3435

35-
# iteration test
36-
dom_list = [x for (x,y) in b]
37-
@test Set(dom_list) == domain(b)
3836

39-
# conversion to a Dict
40-
d = Dict(b)
41-
@test d[0] == b[0]
42-
@test Bijection(d) == b
37+
# iteration test
38+
dom_list = [x for (x, y) in b]
39+
@test Set(dom_list) == domain(b)
4340

44-
# check pair constructor
45-
@test Bijection(collect(b)) == b
41+
42+
# conversion to a Dict
43+
d = Dict(b)
44+
@test d[0] == b[0]
45+
@test Bijection(d) == b
46+
47+
48+
# check pair constructor
49+
@test Bijection(collect(b)) == b
50+
end
51+
52+
# check composition
53+
@testset "Composition" begin
54+
a = Bijection{Int,Int}()
55+
a[1] = 10
56+
a[2] = 20
57+
58+
b = Bijection{String,Int}()
59+
b["hi"] = 1
60+
b["bye"] = 2
61+
62+
c = a * b
63+
@test c["hi"] == 10
64+
65+
end

0 commit comments

Comments
 (0)