On Fri, May 29, 2015 at 6:25 PM, Seth <[email protected]> wrote:
> Thank you, Páll and Yichao. To clarify:
>
> The function is a Dijkstra shortest-paths calculation. The function can take
> an optional array of distances, but should be able to function without one
> (in which case the edge distances are assumed to be 1.0). I don't really
> want to have to default to an array of NxN floats for the default case,
> since that will cause very large graphs (say, 1 billion vertices) to use
> tons of memory for no real reason.
>
> My thought was that if I could somehow allow some other type in "edge_dists"
> that would indicate "use the defaults", I could then test for that and use
> (hardcoded) defaults if it's set, otherwise the data will be an array of
> floats that represent edge distances.
>
> Is there a better approach?

Not sure if this is the best way but I would try sth like the following.

```
julia> type Dummy <: AbstractArray{Float64, 2}
      end

julia> @inline getindex(::Dummy, ::Int, ::Int) = 1.0
getindex (generic function with 164 methods)

julia> f(a::AbstractArray{Float64, 2}=Dummy()) = a[1, 2]
f (generic function with 2 methods)

julia> @code_typed f()
1-element Array{Any,1}:
:($(Expr(:lambda, Any[], Any[Any[],Any[],Any[],Any[]], :(begin  # none, line 1:
       return 1.0
   end::Float64))))

julia> @code_typed f([1. 2.; 3. 4.])
1-element Array{Any,1}:
:($(Expr(:lambda, Any[:a],
Any[Any[],Any[Any[:a,Array{Float64,2},0]],Any[],Any[]]
, :(begin  # none, line 1:
       return (top(arrayref))(a::Array{Float64,2},1,2)::Float64
   end::Float64))))

julia> f()
1.0

julia> f([1. 2.; 3. 4.])
2.0
```


>
> Thanks,
>
> S.
>
>
>
> On Friday, May 29, 2015 at 2:33:00 PM UTC-7, Yichao Yu wrote:
>>
>> On Fri, May 29, 2015 at 3:50 PM, Seth <[email protected]> wrote:
>> > I have an function that takes an optional (potentially very large)
>> > matrix
>> > (AbstractArray{Float64,2}). I'd like to create a parameter to the
>> > function
>> > that's EITHER this array, or a boolean (false). If I define
>> >
>> > edge_dists::Union(Bool, AbstractArray{Float64,2}) = false
>> >
>> >
>> >
>> > in my function declaration, would that cause significant performance
>> > penalties when accessing the parameter?
>>
>> The function will specialize on (the type of) this parameter so there
>> shouldn't be a big penalty. If you need to do something different when
>> this parameter is missing and if the two cases shares a lot of code,
>> this might not be a bad idea (although IMHO ::Union(Void,
>> AbstractArray{Float64, 2})=nothing might be more intuitive)

Reply via email to