Skip to content

Commit

Permalink
Reworked data to data pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamingRaven committed Jul 17, 2022
1 parent 84c7f62 commit 52176e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
14 changes: 7 additions & 7 deletions dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ type DatasetCompat[L LattigoCompat] interface {

// Dataset abstraction and definition of avaliable methods
type Dataset[D DatasetCompat[L], L LattigoCompat] interface {
Get(i int) (D, error)
Get(i int) (*D, error)
Length() (int, error)
ToJSON() ([]byte, error)
FromJSON(bytes []byte) error
}

// simpleDataset the simplest dataset to show as an example
type simpleDataset[D DatasetCompat[L], L LattigoCompat] struct {
Data []D `json:"data"` // slice of individual examples
Data *[]D `json:"data"` // slice of individual examples
}

// NewSimpleDataset initialises a new dataset object with some testable example data
func NewSimpleDataset[D DatasetCompat[L], L LattigoCompat]() Dataset[D, L] {
return &simpleDataset[D, L]{}
func NewSimpleDataset[D DatasetCompat[L], L LattigoCompat](data *[]D) Dataset[D, L] {
return &simpleDataset[D, L]{Data: data}
}

// Get a specific example from the dataset by index or error if impossible
func (ds *simpleDataset[D, L]) Get(i int) (D, error) {
return ds.Data[i], nil
func (ds *simpleDataset[D, L]) Get(i int) (*D, error) {
return &(*(ds.Data))[i], nil
}

// Length of the dataset so controlling code does not exceed the bounds of this
func (ds *simpleDataset[D, L]) Length() (int, error) {
return len(ds.Data), nil
return len(*(ds.Data)), nil
}

// ToJSON convert dataset internal struct to json bytes
Expand Down
38 changes: 27 additions & 11 deletions dataset/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,60 @@ import (
"fmt"
"testing"

"github.com/ldsec/lattigo/v2/ckks"
"gitlab.com/deepcypher/darklantern/erray"
)

// helper function to generate a dud example slice of slices
func sliceOfSlices[T LattigoCompat](examples, features int) [][]T {
func sliceOfSlices[T LattigoCompat](examples, features int) *[][]T {
slice_slice := make([][]T, examples)
for i := 0; i < examples; i++ {
slice_slice[i] = make([]T, features)
for j := 0; j < features; j++ {
slice_slice[i][j] = T(i * j)
}
}
return slice_slice
return &slice_slice
}

func sliceOfErrays[T LattigoCompat](sos *[][]T) *[]erray.Erray[T] {
soe := make([]erray.Erray[T], len(*sos))
o := erray.NewCKKSErray[float64]()
parms, _ := ckks.NewParametersFromLiteral(ckks.PN12QP109)
o.SetParams(&parms)
for i := 0; i < len(*sos); i++ {
// soe[i] =
fmt.Println((*sos)[i])
}
return &soe
}

func TestSliceOfSlices(t *testing.T) {
ss := sliceOfSlices[float64](10, 20)
for i := 0; i < len(ss); i++ {
if ss[i][1] != float64(i) {
t.Fatal(fmt.Sprintf("slice of slices failed as %v != %v", ss[i], float64(i)))
for i := 0; i < len(*ss); i++ {
if (*ss)[i][1] != float64(i) {
t.Fatal(fmt.Sprintf("slice of slices failed as %v != %v", (*ss)[i], float64(i)))
}
}
}

func TestDatasetInit(t *testing.T) {
sliceOfSlices[float64](10, 20)
ds := NewSimpleDataset[erray.Erray[float64], float64]()
sos := sliceOfSlices[float64](10, 20)
soe := sliceOfErrays(sos)
ds := NewSimpleDataset[erray.Erray[float64], float64](soe)
fmt.Println(ds)
}

func TestDatasetGet(t *testing.T) {
sliceOfSlices[float64](10, 20)
ds := NewSimpleDataset[erray.Erray[float64], float64]()
sos := sliceOfSlices[float64](10, 20)
soe := sliceOfErrays(sos)
ds := NewSimpleDataset[erray.Erray[float64], float64](soe)
fmt.Println(ds)
}

func TestDatasetLen(t *testing.T) {
sliceOfSlices[float64](10, 20)
ds := NewSimpleDataset[erray.Erray[float64], float64]()
sos := sliceOfSlices[float64](10, 20)
soe := sliceOfErrays(sos)
ds := NewSimpleDataset[erray.Erray[float64], float64](soe)
fmt.Println(ds)
}

0 comments on commit 52176e3

Please sign in to comment.