主要问题: 如何使用Julia在已经排序的列表中最快地插入一个项目?
目前,我采取以下方法:
v = [1, 2, 3, 5] #example list
x = 4 #value to insert
index = searchsortedfirst(v, x) #find index at which to insert x
insert!(v, index, x) #insert x at index
奖励问题:如果我同时想确保没有重复怎么办?
你可以使用searchsorted
来获取值所在的索引范围,而不仅仅是第一个索引,然后使用splice!
将该范围内的值替换为新的一组值:
insert_and_dedup!(v::Vector, x) = (splice!(v, searchsorted(v,x), [x]); v)
那是一个很好的一行代码,可以实现你想要的功能。
julia> v = [1, 2, 3, 3, 5];
julia> insert_and_dedup!(v, 4)
6-element Array{Int64,1}:
1
2
3
3
4
5
julia> insert_and_dedup!(v, 3)
5-element Array{Int64,1}:
1
2
3
4
5
这让我认为 splice!
应该处理替换为单个值而不是数组的情况,因此我可能会添加该功能。
struct TimeEvent
date::Dates.DateTime
type::String
end
v = [
TimeEvent(DateTime("2019-03-01"),"data_release")
TimeEvent(DateTime("2019-02-01"),"data_release")
TimeEvent(DateTime("2019-05-01"),"data_release")
]
new_event=TimeEvent(DateTime("2019-04-01"),"data_release")
# Sort Events
sort!(v, by = v -> v.date, rev=true)
# Define Function
sortedStructInsert!(v::Vector, x) = (splice!(v,
searchsorted(v,x,by= v->v.date, rev=true), [x]); v)
# Call function
sortedStructInsert!(v, new_event)
4-element Vector{TimeEvent}:
TimeEvent(DateTime("2019-05-01T00:00:00"), "data_release")
TimeEvent(DateTime("2019-04-01T00:00:00"), "data_release")
TimeEvent(DateTime("2019-03-01T00:00:00"), "data_release")
TimeEvent(DateTime("2019-02-01T00:00:00"), "data_release")
sortedStructInsert!(v::Vector, x,symbol) = (splice!(v,
searchsorted(v,x,by= v->getproperty(v,symbol), rev=true), [x]); v)
sortedStructInsert!(v, new_event, :date)