-
Notifications
You must be signed in to change notification settings - Fork 244
/
mutable_list.jl
250 lines (224 loc) · 5.95 KB
/
mutable_list.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
mutable struct ListNode{T}
data::T
prev::ListNode{T}
next::ListNode{T}
function ListNode{T}() where T
node = new{T}()
node.next = node
node.prev = node
return node
end
function ListNode{T}(data) where T
node = new{T}(data)
return node
end
end
mutable struct MutableLinkedList{T}
len::Int
node::ListNode{T}
function MutableLinkedList{T}() where T
l = new{T}()
l.len = 0
l.node = ListNode{T}()
l.node.next = l.node
l.node.prev = l.node
return l
end
end
MutableLinkedList() = MutableLinkedList{Any}()
function MutableLinkedList{T}(elts...) where T
l = MutableLinkedList{T}()
for elt in elts
push!(l, elt)
end
return l
end
Base.iterate(l::MutableLinkedList) = l.len == 0 ? nothing : (l.node.next.data, l.node.next.next)
Base.iterate(l::MutableLinkedList, n::ListNode) = n === l.node ? nothing : (n.data, n.next)
Base.isempty(l::MutableLinkedList) = l.len == 0
Base.length(l::MutableLinkedList) = l.len
Base.collect(l::MutableLinkedList{T}) where T = T[x for x in l]
Base.eltype(::Type{<:MutableLinkedList{T}}) where T = T
Base.lastindex(l::MutableLinkedList) = l.len
function Base.first(l::MutableLinkedList)
isempty(l) && throw(ArgumentError("List is empty"))
return l.node.next.data
end
function Base.last(l::MutableLinkedList)
isempty(l) && throw(ArgumentError("List is empty"))
return l.node.prev.data
end
Base.:(==)(l1::MutableLinkedList{T}, l2::MutableLinkedList{S}) where {T,S} = false
function Base.:(==)(l1::MutableLinkedList{T}, l2::MutableLinkedList{T}) where T
length(l1) == length(l2) || return false
for (i, j) in zip(l1, l2)
i == j || return false
end
return true
end
function Base.map(f::Base.Callable, l::MutableLinkedList{T}) where T
if isempty(l) && f isa Function
S = Core.Compiler.return_type(f, (T,))
return MutableLinkedList{S}()
elseif isempty(l) && f isa Type
return MutableLinkedList{f}()
else
S = typeof(f(first(l)))
l2 = MutableLinkedList{S}()
for h in l
el = f(h)
if el isa S
push!(l2, el)
else
R = typejoin(S, typeof(el))
l2 = MutableLinkedList{R}(collect(l2)...)
push!(l2, el)
end
end
return l2
end
end
function Base.filter(f::Function, l::MutableLinkedList{T}) where T
l2 = MutableLinkedList{T}()
for h in l
if f(h)
push!(l2, h)
end
end
return l2
end
function Base.reverse(l::MutableLinkedList{T}) where T
l2 = MutableLinkedList{T}()
for h in l
pushfirst!(l2, h)
end
return l2
end
function Base.copy(l::MutableLinkedList{T}) where T
l2 = MutableLinkedList{T}()
for h in l
push!(l2, h)
end
return l2
end
function Base.getindex(l::MutableLinkedList, idx::Int)
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx))
node = l.node
for i in 1:idx
node = node.next
end
return node.data
end
function Base.getindex(l::MutableLinkedList{T}, r::UnitRange) where T
@boundscheck 0 < first(r) < last(r) <= l.len || throw(BoundsError(l, r))
l2 = MutableLinkedList{T}()
node = l.node
for i in 1:first(r)
node = node.next
end
len = length(r)
for j in 1:len
push!(l2, node.data)
node = node.next
end
l2.len = len
return l2
end
function Base.setindex!(l::MutableLinkedList{T}, data, idx::Int) where T
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx))
node = l.node
for i in 1:idx
node = node.next
end
node.data = convert(T, data)
return l
end
function Base.append!(l1::MutableLinkedList{T}, l2::MutableLinkedList{T}) where T
l1.node.prev.next = l2.node.next # l1's last's next is now l2's first
l2.node.prev.next = l1.node # l2's last's next is now l1.node
l1.len += length(l2)
return l1
end
function Base.append!(l::MutableLinkedList, elts...)
for elt in elts
push!(l, elt)
end
return l
end
function Base.delete!(l::MutableLinkedList, idx::Int)
@boundscheck 0 < idx <= l.len || throw(BoundsError(l, idx))
node = l.node
for i = 1:idx
node = node.next
end
prev = node.prev
next = node.next
prev.next = next
next.prev = prev
l.len -= 1
return l
end
function Base.delete!(l::MutableLinkedList, r::UnitRange)
@boundscheck 0 < first(r) < last(r) <= l.len || throw(BoundsError(l, r))
node = l.node
for i in 1:first(r)
node = node.next
end
prev = node.prev
len = length(r)
for j in 1:len
node = node.next
end
next = node
prev.next = next
next.prev = prev
l.len -= len
return l
end
function Base.push!(l::MutableLinkedList{T}, data) where T
oldlast = l.node.prev
node = ListNode{T}(data)
node.next = l.node
node.prev = oldlast
l.node.prev = node
oldlast.next = node
l.len += 1
return l
end
function Base.pushfirst!(l::MutableLinkedList{T}, data) where T
oldfirst = l.node.next
node = ListNode{T}(data)
node.prev = l.node
node.next = oldfirst
l.node.next = node
oldfirst.prev = node
l.len += 1
return l
end
function Base.pop!(l::MutableLinkedList)
isempty(l) && throw(ArgumentError("List must be non-empty"))
last = l.node.prev.prev
data = l.node.prev.data
last.next = l.node
l.node.prev = last
l.len -= 1
return data
end
function Base.popfirst!(l::MutableLinkedList)
isempty(l) && throw(ArgumentError("List must be non-empty"))
first = l.node.next.next
data = l.node.next.data
first.prev = l.node
l.node.next = first
l.len -= 1
return data
end
function Base.show(io::IO, node::ListNode)
x = node.data
print(io, "$(typeof(node))($x)")
end
function Base.show(io::IO, l::MutableLinkedList)
print(io, typeof(l), '(')
join(io, l, ", ")
print(io, ')')
end