Check bounds function in julia?

Hi, I have encountered this checkbounds function which confused me. Could anyone explain a bit what it does?


    a_star_algorithm(g::AbstractGraph{U},  
                    s::Integer,                       
                    t::Integer,                       
                    distmx::AbstractMatrix{T}=LightGraphs.weights(g),
                    heuristic::Function = (u,v) -> zero(T)) where {T, U}

**Arguments**
* `g` : graph object
* `S` : start vertex
* `t` : end vertex
* `distmx` : distance matrix
* `heuristic` : search heuristic function; by default returns zero 
"""
function a_star_algorithm(g::LightGraphs.AbstractGraph{U},  # the g
                          s::Integer,           # the start vertex
                          t::Integer,           # the end vertex
                          distmx::AbstractMatrix{T}=LightGraphs.weights(g),
                          heuristic::Function = (u,v) -> zero(T)) where {T, U}
    checkbounds(distmx, Base.OneTo(nv(g)), Base.OneTo(nv(g)))
    #.....

Did you read the docs?

help?> checkbounds

  checkbounds(Bool, A, I...)

  Return true if the specified indices I are in bounds for the given
  array A. Subtypes of AbstractArray should specialize this method
  if they need to provide custom bounds checking behaviors; however,
  in many cases one can rely on A's indices and checkindex.

  See also checkindex.

  Examples
  ≑≑≑≑≑≑≑≑≑≑

  julia> A = rand(3, 3);
  
  julia> checkbounds(Bool, A, 2)
  true
  
  julia> checkbounds(Bool, A, 3, 4)
  false
  
  julia> checkbounds(Bool, A, 1:3)
  true
  
  julia> checkbounds(Bool, A, 1:3, 2:4)
  false

  ────────────────────────────────────────────

  checkbounds(A, I...)

  Throw an error if the specified indices I are not in bounds for the
  given array A.
1 Like

I read it. It’s just that it seems that the documentation of its usage is a bit different from the code I have seen above. So, I guess I will mainly focus on

checkbounds(A, I...)

  Throw an error if the specified indices I are not in bounds for the
  given array A.

Is it SO?

Yes?

julia> A = [1, 2, 3];

julia> checkbounds(A, 2)

julia> checkbounds(A, 5)
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [5]
2 Likes

Thank you a lot then! So, it is mainly for the dimensions of the array bounds, not the bounds of the array values’ range.

julia> Base.OneTo(2)
Base.OneTo(2)

help?> Base.OneTo()
  Base.OneTo(n)

  Define an AbstractUnitRange that behaves like 1:n, with the added
  distinction that the lower limit is guaranteed (by the type system)
  to be 1.

While we are on this, I was wondering, do you happen to know what else is this Base.OneTo() function used? It’s a little bit strange here…