Skip to content

Commit 94836c5

Browse files
committed
Better implemented hash and ==. Closes #77
1 parent 47d8ccf commit 94836c5

File tree

4 files changed

+131
-33
lines changed

4 files changed

+131
-33
lines changed

src/componentarray.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ Base.propertynames(x::ComponentVector) = propertynames(indexmap(getaxes(x)[1]))
284284

285285
Base.keys(x::ComponentVector) = keys(indexmap(getaxes(x)[1]))
286286

287-
Base.hash(x::ComponentArray, h::UInt) = hash(getdata(x), h)
287+
Base.hash(x::ComponentArray, h::UInt) = hash(keys(x), hash(getdata(x), h))
288+
289+
Base.:(==)(x::ComponentArray, y::ComponentArray) = getdata(x)==getdata(y) && getaxes(x)==getaxes(y)
290+
Base.:(==)(x::ComponentArray, y::AbstractArray) = getdata(x)==y && keys(x)==keys(y) # For equality with LabelledArrays
291+
Base.:(==)(x::AbstractArray, y::ComponentArray) = y==x
288292

289293
"""
290294
valkeys(x::ComponentVector)

test/Manifest.toml

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,98 @@
11
# This file is machine-generated - editing it directly is not advised
22

3+
[[ArgTools]]
4+
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
5+
6+
[[ArrayInterface]]
7+
deps = ["IfElse", "LinearAlgebra", "Requires", "SparseArrays", "Static"]
8+
git-tree-sha1 = "2fbfa5f372352f92191b63976d070dc7195f47a4"
9+
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
10+
version = "3.1.7"
11+
12+
[[Artifacts]]
13+
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
14+
315
[[Base64]]
416
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
517

618
[[CommonSubexpressions]]
719
deps = ["Test"]
8-
git-tree-sha1 = "efdaf19ab11c7889334ca247ff4c9f7c322817b0"
20+
git-tree-sha1 = "34aa50efad19a788db0cb2eb44d149942f64816a"
921
uuid = "bbf7d656-a473-5ed7-a52c-81e309532950"
10-
version = "0.2.0"
22+
version = "0.2.1"
1123

1224
[[CompilerSupportLibraries_jll]]
13-
deps = ["Libdl", "Pkg"]
14-
git-tree-sha1 = "7c4f882c41faa72118841185afc58a2eb00ef612"
25+
deps = ["Artifacts", "Libdl"]
1526
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
16-
version = "0.3.3+0"
1727

1828
[[ConstructionBase]]
19-
git-tree-sha1 = "a2a6a5fea4d6f730ec4c18a76d27ec10e8ec1c50"
29+
deps = ["LinearAlgebra"]
30+
git-tree-sha1 = "48920211c95a6da1914a06c44ec94be70e84ffff"
2031
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
21-
version = "1.0.0"
32+
version = "1.1.0"
2233

2334
[[Dates]]
2435
deps = ["Printf"]
2536
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
2637

2738
[[DiffResults]]
2839
deps = ["StaticArrays"]
29-
git-tree-sha1 = "da24935df8e0c6cf28de340b958f6aac88eaa0cc"
40+
git-tree-sha1 = "c18e98cba888c6c25d1c3b048e4b3380ca956805"
3041
uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
31-
version = "1.0.2"
42+
version = "1.0.3"
3243

3344
[[DiffRules]]
3445
deps = ["NaNMath", "Random", "SpecialFunctions"]
35-
git-tree-sha1 = "eb0c34204c8410888844ada5359ac8b96292cfd1"
46+
git-tree-sha1 = "214c3fcac57755cfda163d91c58893a8723f93e9"
3647
uuid = "b552c78f-8df3-52c6-915a-8e097449b14b"
37-
version = "1.0.1"
48+
version = "1.0.2"
3849

39-
[[Distributed]]
40-
deps = ["Random", "Serialization", "Sockets"]
41-
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
50+
[[Downloads]]
51+
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
52+
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
4253

4354
[[ForwardDiff]]
4455
deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "NaNMath", "Random", "SpecialFunctions", "StaticArrays"]
4556
git-tree-sha1 = "869540e4367122fbffaace383a5bdc34d6e5e5ac"
4657
uuid = "f6369f11-7733-5829-9624-2563aa707210"
4758
version = "0.10.10"
4859

60+
[[IfElse]]
61+
git-tree-sha1 = "28e837ff3e7a6c3cdb252ce49fb412c8eb3caeef"
62+
uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
63+
version = "0.1.0"
64+
4965
[[InteractiveUtils]]
5066
deps = ["Markdown"]
5167
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
5268

69+
[[JLLWrappers]]
70+
git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0"
71+
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
72+
version = "1.2.0"
73+
74+
[[LabelledArrays]]
75+
deps = ["ArrayInterface", "LinearAlgebra", "MacroTools", "StaticArrays"]
76+
git-tree-sha1 = "df09e970c816637191ef8794ef5c5c9f8950db88"
77+
uuid = "2ee39098-c373-598a-b85f-a56591580800"
78+
version = "1.6.0"
79+
80+
[[LibCURL]]
81+
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
82+
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
83+
84+
[[LibCURL_jll]]
85+
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
86+
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
87+
5388
[[LibGit2]]
54-
deps = ["Printf"]
89+
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
5590
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
5691

92+
[[LibSSH2_jll]]
93+
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
94+
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
95+
5796
[[Libdl]]
5897
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
5998

@@ -64,42 +103,64 @@ uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
64103
[[Logging]]
65104
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
66105

106+
[[MacroTools]]
107+
deps = ["Markdown", "Random"]
108+
git-tree-sha1 = "6a8a2a625ab0dea913aba95c11370589e0239ff0"
109+
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
110+
version = "0.5.6"
111+
67112
[[Markdown]]
68113
deps = ["Base64"]
69114
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
70115

116+
[[MbedTLS_jll]]
117+
deps = ["Artifacts", "Libdl"]
118+
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
119+
120+
[[MozillaCACerts_jll]]
121+
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
122+
71123
[[NaNMath]]
72-
git-tree-sha1 = "928b8ca9b2791081dc71a51c55347c27c618760f"
124+
git-tree-sha1 = "bfe47e760d60b82b66b61d2d44128b62e3a369fb"
73125
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
74-
version = "0.3.3"
126+
version = "0.3.5"
127+
128+
[[NetworkOptions]]
129+
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
75130

76131
[[OffsetArrays]]
77132
git-tree-sha1 = "930db8ef90483570107f2396b1ffc6680f08e8b7"
78133
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
79134
version = "1.0.4"
80135

81136
[[OpenSpecFun_jll]]
82-
deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"]
83-
git-tree-sha1 = "d51c416559217d974a1113522d5919235ae67a87"
137+
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"]
138+
git-tree-sha1 = "9db77584158d0ab52307f8c04f8e7c08ca76b5b3"
84139
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
85-
version = "0.5.3+3"
140+
version = "0.5.3+4"
86141

87142
[[Pkg]]
88-
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
143+
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs"]
89144
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
90145

91146
[[Printf]]
92147
deps = ["Unicode"]
93148
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
94149

95150
[[REPL]]
96-
deps = ["InteractiveUtils", "Markdown", "Sockets"]
151+
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
97152
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
98153

99154
[[Random]]
100155
deps = ["Serialization"]
101156
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
102157

158+
[[Requires]]
159+
deps = ["UUIDs"]
160+
git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621"
161+
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
162+
version = "1.1.3"
163+
103164
[[SHA]]
104165
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
105166

@@ -115,9 +176,15 @@ uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
115176

116177
[[SpecialFunctions]]
117178
deps = ["OpenSpecFun_jll"]
118-
git-tree-sha1 = "e19b98acb182567bcb7b75bb5d9eedf3a3b5ec6c"
179+
git-tree-sha1 = "d8d8b8a9f4119829410ecd706da4cc8594a1e020"
119180
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
120-
version = "0.10.0"
181+
version = "0.10.3"
182+
183+
[[Static]]
184+
deps = ["IfElse"]
185+
git-tree-sha1 = "ddec5466a1d2d7e58adf9a427ba69763661aacf6"
186+
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
187+
version = "0.2.4"
121188

122189
[[StaticArrays]]
123190
deps = ["LinearAlgebra", "Random", "Statistics"]
@@ -129,8 +196,16 @@ version = "0.12.3"
129196
deps = ["LinearAlgebra", "SparseArrays"]
130197
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
131198

199+
[[TOML]]
200+
deps = ["Dates"]
201+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
202+
203+
[[Tar]]
204+
deps = ["ArgTools", "SHA"]
205+
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
206+
132207
[[Test]]
133-
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
208+
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
134209
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
135210

136211
[[UUIDs]]
@@ -145,3 +220,11 @@ deps = ["ConstructionBase", "LinearAlgebra", "Random"]
145220
git-tree-sha1 = "a061dada333813818aa7454f93c63a5cab6ea981"
146221
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
147222
version = "1.3.0"
223+
224+
[[Zlib_jll]]
225+
deps = ["Libdl"]
226+
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
227+
228+
[[nghttp2_jll]]
229+
deps = ["Artifacts", "Libdl"]
230+
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
3+
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
34
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
45
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
56
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

test/runtests.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ComponentArrays
22
using ForwardDiff
3+
using LabelledArrays
34
using LinearAlgebra
45
using StaticArrays
56
using OffsetArrays
@@ -108,9 +109,18 @@ end
108109
@test keys(ca) == (:a, :b, :c)
109110
@test valkeys(ca) == Val.((:a, :b, :c))
110111

111-
@test ca == getdata(ca)
112-
@test hash(ca) == hash(getdata(ca))
113-
@test hash(ca, zero(UInt)) == hash(getdata(ca), zero(UInt))
112+
@test ca != getdata(ca)
113+
@test getdata(ca) != ca
114+
@test hash(ca) != hash(getdata(ca))
115+
@test hash(ca, zero(UInt)) != hash(getdata(ca), zero(UInt))
116+
117+
ab = ComponentArray(a=1, b=2)
118+
xy = ComponentArray(x=1, y=2)
119+
@test ab != xy
120+
@test hash(ab) != hash(xy)
121+
@test hash(ab, zero(UInt)) != hash(xy, zero(UInt))
122+
123+
@test ab == LVector(a=1, b=2)
114124
end
115125

116126
@testset "Get" begin
@@ -229,7 +239,7 @@ end
229239

230240
A = ComponentArray(zeros(Int,4,4), Axis(x=1:4), Axis(x=1:4))
231241
A[1,:] .= 1
232-
@test A[1,:] == ones(Int,4)
242+
@test A[1,:] == ComponentVector(x=ones(Int,4))
233243
end
234244

235245
@testset "Similar" begin
@@ -258,7 +268,7 @@ end
258268
temp = deepcopy(ca)
259269
@test eltype(Float32.(ca)) == Float32
260270
@test ca .* ca' == cmat
261-
@test 1 .* (ca .+ ca) == ComponentArray(a .+ a)
271+
@test 1 .* (ca .+ ca) == ComponentArray(a .+ a, getaxes(ca))
262272
@test typeof(ca .+ cmat) == typeof(cmat)
263273
@test getaxes(false .* ca .* ca') == (ax, ax)
264274
@test getaxes(false .* ca' .* ca) == (ax, ax)
@@ -313,7 +323,7 @@ end
313323
@test ca * ca' == collect(cmat)
314324
@test ca * ca' == a * a'
315325
@test ca' * ca == a' * a
316-
@test cmat * ca == cmat * a
326+
@test cmat * ca == ComponentArray(cmat * a, getaxes(ca))
317327
@test cmat' * ca isa AbstractArray
318328
@test a' * ca isa Number
319329
@test cmat'' == cmat
@@ -333,7 +343,7 @@ end
333343
@test ca * transpose(ca) == collect(cmat)
334344
@test ca * transpose(ca) == a * transpose(a)
335345
@test transpose(ca) * ca == transpose(a) * a
336-
@test cmat * ca == cmat * a
346+
@test ca' * cmat == ComponentArray(a' * getdata(cmat), getaxes(ca))
337347
@test transpose(transpose(cmat)) == cmat
338348
@test transpose(transpose(ca)) == ca
339349
@test transpose(ca.c) * cmat[:c,:c] * ca.c isa Number

0 commit comments

Comments
 (0)