Skip to content

Commit

Permalink
Remove bound checks on Series.Elem, Series.Val and series.parseIndexes
Browse files Browse the repository at this point in the history
  • Loading branch information
kniren committed Jan 21, 2017
1 parent ca0a485 commit 30cfdba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
6 changes: 3 additions & 3 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ func (df DataFrame) Ncol() int {
return df.ncols
}

// Col returns the Series with the given column name contained in the DataFrame.
// Col returns a copy of the Series with the given column name contained in the DataFrame.
func (df DataFrame) Col(colname string) series.Series {
if df.Err != nil {
return series.Series{Err: df.Err}
Expand Down Expand Up @@ -1599,7 +1599,7 @@ func (df DataFrame) Maps() []map[string]interface{} {
for i := 0; i < df.nrows; i++ {
m := make(map[string]interface{})
for k, v := range colnames {
val, _ := df.columns[k].Val(i) // Ignoring the error as the index should not be out of bounds
val := df.columns[k].Val(i)
m[v] = val
}
maps[i] = m
Expand All @@ -1610,7 +1610,7 @@ func (df DataFrame) Maps() []map[string]interface{} {
// Return the element on row `r` and column `c`. Will panic if the index is out of
// bounds.
func (df DataFrame) Elem(r, c int) series.Element {
return df.columns[c].Elem(r).Copy()
return df.columns[c].Elem(r)
}

// fixColnames assigns a name to the missing column names and makes it so that the
Expand Down
24 changes: 8 additions & 16 deletions series/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,15 @@ func (s Series) Str() string {
return strings.Join(ret, "\n")
}

// Val returns the value of a series for the given index
func (s Series) Val(i int) (interface{}, error) {
if i >= s.Len() || i < 0 {
return nil, fmt.Errorf("index out of bounds")
}
return s.elements.Elem(i).Val(), nil
// Val returns the value of a series for the given index. Will panic if the index
// is out of bounds.
func (s Series) Val(i int) interface{} {
return s.elements.Elem(i).Val()
}

// Elem returns the element of a series for the given index or nil if the index is
// out of bounds
// Elem returns the element of a series for the given index. Will panic if the
// index is out of bounds.
func (s Series) Elem(i int) Element {
if i >= s.Len() || i < 0 {
return nil
}
return s.elements.Elem(i)
}

Expand All @@ -573,6 +568,8 @@ func (s Series) Addr() []string {
return ret
}

// parseIndexes will parse the given indexes for a given series of length `l`. No
// out of bounds checks is performed.
func parseIndexes(l int, indexes Indexes) ([]int, error) {
var idx []int
switch indexes.(type) {
Expand Down Expand Up @@ -613,11 +610,6 @@ func parseIndexes(l int, indexes Indexes) ([]int, error) {
default:
return nil, fmt.Errorf("indexing error: unknown indexing mode")
}
for i := 0; i < len(idx); i++ {
if idx[i] < 0 || idx[i] > l {
return nil, fmt.Errorf("indexing error: out of bounds")
}
}
return idx, nil
}

Expand Down

0 comments on commit 30cfdba

Please sign in to comment.