-
Notifications
You must be signed in to change notification settings - Fork 0
/
orddictparametric.jl
182 lines (152 loc) · 4.09 KB
/
orddictparametric.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
mutable struct OrdDict{K,V} <: AbstractDict{K,V}
items::Vector{Pair{K, V}}
end
# constructors
OrdDict{K,V}() where {K,V} = OrdDict(Pair{K, V}[])
OrdDict(items...) = OrdDict(collect(items))
import Base: getindex, setindex!, get, delete!,
length, iterate,
getproperty, setproperty!,
haskey, isempty,
union,
copy, push!
# iteration definitions
length(a::OrdDict) = length(a.items)
isempty(a::OrdDict) = isempty(a.items)
iterate(a::OrdDict) = iterate(a.items)
iterate(a::OrdDict, i) = iterate(a.items, i)
# example inferred type
# julia> pairs = ["two" => 2, "four" => 4]
# 2-element Vector{Pair{String, Int64}}:
# "two" => 2
# "four" => 4
# julia> dict = OrdDict(pairs)
# OrdDict{String, Int64} with 2 entries:
# "two" => 2
# "four" => 4
# example of dictionary behavior
# julia> item = OrdDict(:row=> "1", :column=> "2")
# OrdDict{Symbol, String} with 2 entries:
# :row => "1"
# :column => "2"
# julia> for (k,v) in item
# println("key: ", k, " value: ", v)
# end
# key: row value: 1
# key: column value: 2
# # Engheim forgot to assign the collection to a variable
# julia> map(first, item)
# ERROR: map is not defined on dictionaries
# Stacktrace:
# [1] error(s::String)
# @ Base ./error.jl:33
# [2] map(f::Function, #unused#::OrdDict{Symbol, String})
# @ Base ./abstractarray.jl:2880
# [3] top-level scope
# @ REPL[8]:1
# julia> items = collect(item)
# 2-element Vector{Pair{Symbol, String}}:
# :row => "1"
# :column => "2"
# julia> map(first, items)
# 2-element Vector{Symbol}:
# :row
# :column
# julia> map(last, items)
# 2-element Vector{String}:
# "1"
# "2"
# need to add parametric type to method signature to match struct
function getindex(a::OrdDict{K, V}, key::K) where {K, V}
for item in a.items
if first(item) == key
return last(item)
end
end
throw(KeyError(key))
end
# alternative multiple dispatch implementation by number - same as original
getindex(a::OrdDict, index::Integer) = a.items[index]
# need to add parametric type to method signature to match struct
function setindex!(a::OrdDict{K, V}, value::V, key::K) where {K, V}
for (i, item) in enumerate(a.items)
if first(item) == key
a.items[i] = key => value
return
end
end
push!(a.items, key => value)
end
# need to add parametric type to method signature to match struct
function push!(a::OrdDict{K, V}, x::Pair{K, V}) where {K, V}
push!(a.items, x)
end
# need this for setproperty! be;ow
push!(a::OrdDict, x::Pair{Symbol, String}) = push!(a.items, x)
function haskey(a::OrdDict{K, V}, key::K) where {K, V}
for item in a.items
if first(item) == key
return true
end
end
false
end
function delete!(a::OrdDict{K, V}, key::K) where {K, V}
for (i, item) in enumerate(a.items)
if first(item) == key
deleteat!(a.items, i)
end
end
a
end
# make it act like a dictionary
# need to keep the OrdDict.items to behave as usual
# only works if explicity in method signature make Key type symbol, since original expects value of type symbol
function getproperty(a::OrdDict, key::Symbol)
if key == :items
getfield(a, :items)
else
a[key]
end
end
# value can be of any type
function setproperty!(a::OrdDict, key::Symbol, value)
if key == :items
setfield!(a, :item, value)
else
a[key] = value
end
end
# use example in OrdDict
# julia> d = OrdDict(:one => "1", :two => "2")
# OrdDict{Symbol, String} with 2 entries:
# :one => "1"
# :two => "2"
# julia> d[:three] = "three"
# "three"
# julia> d.three
# "three"
# julia> d.four = "4"
# "4"
# julia> d.items
# 4-element Vector{Pair{Symbol, String}}:
# :one => "1"
# :two => "2"
# :three => "three"
# :four => "4"
# julia> d[:two]
# "2"
# Example where value is Integer
# julia> d = OrdDict(:one => 1, :two => 2)
# OrdDict{Symbol, Int64} with 2 entries:
# :one => 1
# :two => 2
# julia> d[:three] = 3
# 3
# julia> d[:two]
# 2
# julia> d.items
# 3-element Vector{Pair{Symbol, Int64}}:
# :one => 1
# :two => 2
# :three => 3