A RingList
is a list of distinct values that is
unchanged by rotation. These can be created by giving a list of values
or a one-dimensional array of values:
julia> using RingLists
julia> a = RingList(1,2,3,4);
julia> b = RingList([2,3,4,1]);
julia> a==b
true
julia> println(a)
[ 1 → 2 → 3 → 4 → 1 ]
Note the repeat of element 1
in the output showing that the list wraps around.
In this list, a
stands for a RingList
.
length(a)
gives the number of elements held in theRingList
.keys(a)
returns an iterator of the elements ina
.haskey(a,x)
checks ifx
is an element of theRingList
.Vector(a)
returns a one-dimensional array of the elements ina
.Set(a)
returns the elements ofa
(as an unordered collection).collect(a)
returns the elements ofa
in an ordered list.copy(a)
makes an independent copy ofa
.shuffle(a)
returns a newRingList
with the same elements asa
but in randomized order.next(a,x)
returns the next element afterx
ina
; alsoa[x]
.previous(a,x)
returns the elementy
witha[y]==x
; alsoa(y)
.first(a)
returns an element ofa
that is, if possible, the smallest element ofa
. Callfirst(a,false)
to ignore trying to start at the smallest element. Fails ifa
is empty.delete!(a,x)
removesx
from the collection linking together its predecessor and successor.insert!(a,x)
inserts the elementa
into theRingList
. No guarantee where it will end up.insertbefore!(a,x,y)
insertsx
intoa
beforey
.insertafter!(a,x,y)
insertsx
intoa
aftery
. For example:
> a = RingList(1,2,3)
[ 1 → 2 → 3 → 1 ]
julia> insertafter!(a,99,2)
julia> a
[ 1 → 2 → 99 → 3 → 1 ]
reverse(a)
returns a newRingList
with the elements reversed.
julia> a = RingList(1,2,3,4,5)
[ 1 → 2 → 3 → 4 → 5 → 1 ]
julia> b = reverse(a)
[ 1 → 5 → 4 → 3 → 2 → 1 ]
firsts(a)
returns a newRingList
built by taking thefirst
member of each element ofa
.
julia> a
[ (19, 20) → (20, 25) → (25, 31) → (31, 27) → (27, 19) → (19, 20) ]
julia> firsts(a)
[ 19 → 20 → 25 → 31 → 27 → 19 ]
RingList
elements can be iterated:
julia> a = RingList(1,2,3,4,5)
[ 1 → 2 → 3 → 4 → 5 → 1 ]
julia> for x in a
println(x)
end
1
2
3
4
5