Skip to content

Commit

Permalink
This is a large update with two significant changes which break the p…
Browse files Browse the repository at this point in the history
…revious API.

Firstly the use of pointers to slices has been greatly reduced with the various xList functions being removed and the test code changed accordingly.

Secondly the VSlice type has been renamed RSlice with a new VSlice introduced which equates to a []reflect.Value, allowing for more flexible approaches to reflected slice handling.
These types retain VList and RList constructer functions.
  • Loading branch information
feyeleanor committed Sep 25, 2011
1 parent 4434bf8 commit bdeeec7
Show file tree
Hide file tree
Showing 42 changed files with 6,257 additions and 5,138 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TARG=github.com/feyeleanor/slices
GOFILES=\
slices.go\
slice.go\
reflected.go\
value.go\
string.go\
uintptr.go\
Expand Down
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
== About ==

Slices provides several implementations of slice-based datatypes.
The Slices package provides implementations of slice-based datatypes for all of Go's basic types, as well as two separate implementations for handling slices in the context of reflection values. Currently there's no documentation but the extensive test suite includes numerous examples of how to use slices to solve common tasks.

The package has its origins in research for the GoLightly virtual machine project but has since taken on a life of its own. I hope you will find it useful.
44 changes: 13 additions & 31 deletions complex128.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"sort"
)

func C128List(n... complex128) *C128Slice {
if len(n) == 0 {
n = make(C128Slice, 0, 0)
}
return (*C128Slice)(&n)
}

type C128Slice []complex128

func (s C128Slice) Len() int { return len(s) }
Expand Down Expand Up @@ -259,28 +252,21 @@ func (s C128Slice) Repeat(count int) C128Slice {
return destination
}

func (s *C128Slice) Flatten() {
// Flatten is a non-op for the C128Slice as they cannot contain nested elements
}

func (s C128Slice) equal(o C128Slice) (r bool) {
switch {
case s == nil: r = o == nil
case s.Len() == o.Len(): r = true
for i, v := range s {
if r = v == o[i]; !r {
return
}
}
if len(s) == len(o) {
r = true
for i, v := range s {
if r = v == o[i]; !r {
return
}
}
}
return
}

func (s C128Slice) Equal(o interface{}) (r bool) {
switch o := o.(type) {
case *C128Slice: r = o != nil && s.equal(*o)
case C128Slice: r = s.equal(o)
case *[]complex128: r = o != nil && s.equal(*o)
case []complex128: r = s.equal(o)
}
return
Expand All @@ -302,15 +288,15 @@ func (s C128Slice) Cdr() (t C128Slice) {

func (s *C128Slice) Rplaca(v interface{}) {
switch {
case s == nil: *s = *C128List(v.(complex128))
case s == nil: *s = C128Slice{v.(complex128)}
case s.Len() == 0: *s = append(*s, v.(complex128))
default: (*s)[0] = v.(complex128)
}
}

func (s *C128Slice) Rplacd(v interface{}) {
if s == nil {
*s = *C128List(v.(complex128))
*s = C128Slice{v.(complex128)}
} else {
ReplaceSlice := func(v C128Slice) {
if l := len(v); l < cap(*s) {
Expand All @@ -326,13 +312,12 @@ func (s *C128Slice) Rplacd(v interface{}) {
}

switch v := v.(type) {
case *C128Slice: ReplaceSlice(*v)
case complex128: (*s)[1] = v
*s = (*s)[:2]
case C128Slice: ReplaceSlice(v)
case *[]complex128: ReplaceSlice(C128Slice(*v))
case []complex128: ReplaceSlice(C128Slice(v))
case nil: *s = (*s)[:1]
default: (*s)[1] = v.(complex128)
*s = (*s)[:2]
default: panic(v)
}
}
}
Expand Down Expand Up @@ -499,10 +484,9 @@ func (s C128Slice) ReplaceIf(f interface{}, r interface{}) {

func (s *C128Slice) Replace(o interface{}) {
switch o := o.(type) {
case complex128: *s = C128Slice{o}
case C128Slice: *s = o
case *C128Slice: *s = *o
case []complex128: *s = C128Slice(o)
case *[]complex128: *s = C128Slice(*o)
default: panic(o)
}
}
Expand Down Expand Up @@ -579,9 +563,7 @@ func (s *C128Slice) Insert(i int, v interface{}) {
copy(n[i + len(v):], (*s)[i:])
*s = n

case *C128Slice: s.Insert(i, *v)
case []complex128: s.Insert(i, C128Slice(v))
case *[]complex128: s.Insert(i, C128Slice(*v))
default: panic(v)
}
}
Loading

0 comments on commit bdeeec7

Please sign in to comment.