src/LinkedLists.jl

Wed, 22 Dec 2021 11:14:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:14:38 +0200
changeset 35
d881275c6564
parent 4
59fd17a3cea0
permissions
-rw-r--r--

Add metaprogramming tools and fast multidimensional loops.

0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ####################################################################
3
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
2 # Immutable linked lists (different from the mutable lists of
0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 # https://github.com/ChrisRackauckas/LinkedLists.jl)
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 ####################################################################
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5
4
59fd17a3cea0 Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
6 __precompile__()
59fd17a3cea0 Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
7
0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 module LinkedLists
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
3
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
10 using DelimitedFiles
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
11
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
12 using ..StructTools
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
13
0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 ##############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 # Our exports
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 ##############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 export LinkedListEntry,
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 LinkedList,
3
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
20 unfold_linked_list,
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
21 write_log
0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 #############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 # Data types
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 #############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 struct LinkedListEntry{T}
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 value :: T
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 next :: Union{LinkedListEntry{T},Nothing}
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 LinkedList{T} = Union{LinkedListEntry{T},Nothing}
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 ############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 # Functions
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 ############
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 function Base.iterate(list::LinkedList{T}) where T
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 return Base.iterate(list, list)
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 function Base.iterate(list::LinkedList{T}, tail::Nothing) where T
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 return nothing
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 function Base.iterate(list::LinkedList{T}, tail::LinkedListEntry{T}) where T
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 return tail.value, tail.next
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 # Return the items in the list with the tail first
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 function unfold_linked_list(list::LinkedList{T}) where T
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 res = []
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 for value ∈ list
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 push!(res, value)
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 return reverse(res)
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 end
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58
3
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
59 # Write out a a “log” of LinkedList of IterableStructs as a delimited file
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
60 function write_log(filename::String, log::LinkedList{T}, comment::String) where T <: IterableStruct
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
61 open(filename, "w") do io
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
62 print(io, comment)
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
63 writedlm(io, [String.(fieldnames(T))])
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
64 writedlm(io, unfold_linked_list(log))
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
65 end
0
888dfd34d24a Initialise
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 end
3
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
67
ec9084e97e46 Add write_log to LinkedLists
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
68 end

mercurial