-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.jl
220 lines (178 loc) · 4.47 KB
/
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
# Doubly linked list
# Imports from Base have been done already.
export List, ListNode
type ListNode{T}
prev::ListNode{T}
next::ListNode{T}
data::T
ListNode()=(x=new(); x.prev=x; x.next=x; x)
ListNode(p, n, d)=new(p, n, d)
end
# Doubly linked list.
type List{T}
node::ListNode{T}
List()=new(ListNode{T}())
end
function show{T}(io::IO, l::List{T})
print(io, "List{", string(T), "}(")
middle=false
for item in l
if middle
print(io, ", ")
end
show(io, item)
middle=true
end
print(io, ")")
end
# The section titles work through sections of the manual.
#### Iteration
start{T}(l::List{T})=l.node.next
done{T}(l::List{T}, n::ListNode{T})=(n==l.node)
next{T}(l::List{T}, n::ListNode{T})=(n.data, n.next)
immutable type ListIndexIterator{T}
l::List{T}
end
# Returns an iterator over indices.
# Use getindex, setindex! to find the item at this index.
indexed{T}(l::List{T})=ListIndexIterator{T}(l)
start{T}(liter::ListIndexIterator{T})=liter.l.node.next
done{T}(liter::ListIndexIterator{T}, n::ListNode{T})=(n==liter.l.node)
next{T}(liter::ListIndexIterator{T}, n::ListNode{T})=(n, n.next)
#### General Collections
isempty{T}(l::List{T})=(l.node.next==l.node)
function empty!{T}(l::List{T})
l.node.next=l.node
end
function length{T}(l::List{T})
cnt=0
for n in l
cnt+=1
end
cnt
end
# This is supposed to be an integer index, but
# we return the node as an index.
function endof{T}(l::List{T})
l.node.prev
end
#### Iterable Collections
function in{T}(item::T, l::List{T})
for node in l
if isequal(node.data, item)
return true
end
end
false
end
eltype{T}(l::List{T})=T
# Highest index in list for each value in a that is
# a member of the list.
function indexin{T}(a, l::List{T})
highest=zeros(Int, length(a))
for (lidx, d) in enumerate(l)
for (xidx, x) in enumerate(a)
if isequal(x, d)
highest[xidx]=lidx
break
end
end
end
highest
end
first{T}(l::List{T})=l.node.next.data
function last{T}(l::List{T})
l.node.prev.data::T
end
#### Indexable Collections
# Treat the node as an index. It is also what
# is used for the state in iterators.
getindex{T}(l::List{T}, n::ListNode{T})=n.data
function setindex!{T}(l::List{T}, n::ListNode{T}, d::T)
n.data=d
end
#### Dequeues
# Breaking interface expectation to push multiple items
# so that we can return an index of the pushed item.
# Use append! for multiple items.
function push!{T}(l::List{T}, item::T)
toadd=ListNode{T}(l.node.prev, l.node, item)
l.node.prev.next=toadd
l.node.prev=toadd
toadd
end
function pop!{T}(l::List{T})
d=l.node.prev.data
l.node.prev.prev.next=l.node
l.node.prev=l.node.prev.prev
d
end
# Breaking interface expectation because:
# Returns an index to the item instead of the collection.
# Takes only one value at a time. Use prepend! for multiple.
function unshift!{T}(l::List{T}, d)
toadd=ListNode{T}(l.node, l.node.next, d)
l.node.next.prev=toadd
l.node.next=toadd
toadd
end
function shift!{T}(l::List{T})
x=l.node.next.data
l.node.next.next.prev=l.node
l.node.next=l.node.next.next
x
end
# Insert
function insert!{T}(l::List{T}, n::ListNode{T}, d::T)
toadd=ListNode{T}(n.prev, n, d)
n.prev.next=toadd
n.prev=toadd
toadd
end
# Linear in the number of elements.
# Second argument is the state from an iterator.
function deleteat!{T}(l::List{T}, n::ListNode{T})
n.prev.next=n.next
n.next.prev=n.prev
l
end
# Removal of a node, returning the value at that node.
function splice!{T}(l::List{T}, n::ListNode{T})
n.prev.next=n.next
n.next.prev=n.prev
n.data
end
# Replacement of a node.
function splice!{T}(l::List{T}, n::ListNode{T}, d::T)
(d, n.data)=(n.data, d)
d
end
function append!{T}(l::List{T}, items)
for i in items
push!(l, i)
end
end
function prepend!{T}(l::List{T}, items)
node=l.node # Invariant: Add after the node "node."
for i in items
toadd=ListNode{T}(node, node.next, i)
node.next.prev=toadd
node.next=toadd
node=toadd
end
l
end
# Adding find, to find the iterator to a given value.
function find{T}(l::List{T}, d::T)
find(l, l.node, d)
end
function find{T}(l::List{T}, n::ListNode{T}, d::T)
n=n.next
while n!=l.node && n.data!=d
n=n.next
end
if n==l.node
return(nothing)
end
n
end