Skip to content

Commit

Permalink
feat: implementing reverse operation (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivancorrales authored Dec 2, 2018
1 parent 172df46 commit 5b30db4
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 0 deletions.
50 changes: 50 additions & 0 deletions benchmark/reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package benchmark

import (
"github.com/wesovilabs/koazee"
"testing"
)

func BenchmarkReverseString10SumLen(b *testing.B) {
b.StopTimer()
input := strings10
for i := 0; i < b.N; i++ {
stream := koazee.
StreamOf(input)
b.StartTimer()
stream.Reverse().Do()
}
}

func BenchmarkReverseString100SumLen(b *testing.B) {
b.StopTimer()
input := strings100
for i := 0; i < b.N; i++ {
stream := koazee.
StreamOf(input)
b.StartTimer()
stream.Reverse().Do()
}
}

func BenchmarkReverseString1000SumLen(b *testing.B) {
b.StopTimer()
input := strings1000
for i := 0; i < b.N; i++ {
stream := koazee.
StreamOf(input)
b.StartTimer()
stream.Reverse().Do()
}
}

func BenchmarkReverseString5000SumLen(b *testing.B) {
b.StopTimer()
input := strings5000
for i := 0; i < b.N; i++ {
stream := koazee.
StreamOf(input)
b.StartTimer()
stream.Reverse().Do()
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/divan/depscheck v0.0.0-20160406124124-bf02ca53aeea // indirect
github.com/golangci/golangci-lint v1.12.3 // indirect
github.com/magiconair/properties v1.7.6
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/olekukonko/tablewriter v0.0.1 // indirect
github.com/stretchr/testify v1.2.2
Expand Down
313 changes: 313 additions & 0 deletions internal/reverse/dispatcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@

package reverse

import (
"reflect"
)


type dispatchFunction func(items reflect.Value)interface{}

var dispatcher = map[string]dispatchFunction{
"string": reverseString,
"*string": reverseStringPtr,
"bool": reverseBool,
"*bool": reverseBoolPtr,
"int": reverseInt,
"*int": reverseIntPtr,
"int8": reverseInt8,
"*int8": reverseInt8Ptr,
"int16": reverseInt16,
"*int16": reverseInt16Ptr,
"int32": reverseInt32,
"*int32": reverseInt32Ptr,
"int64": reverseInt64,
"*int64": reverseInt64Ptr,
"uint": reverseUint,
"*uint": reverseUintPtr,
"uint8": reverseUint8,
"*uint8": reverseUint8Ptr,
"uint16": reverseUint16,
"*uint16": reverseUint16Ptr,
"uint32": reverseUint32,
"*uint32": reverseUint32Ptr,
"uint64": reverseUint64,
"*uint64": reverseUint64Ptr,
"float32": reverseFloat32,
"*float32": reverseFloat32Ptr,
"float64": reverseFloat64,
"*float64": reverseFloat64Ptr,
}

func dispatch(items reflect.Value, itemsType reflect.Type) (bool,interface{}) {
if fnVal, ok := dispatcher[itemsType.String()]; ok {
return true,fnVal(items)
}
return false,nil
}
func reverseString(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]string)
len := len(input)
output := make([]string, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseStringPtr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*string)
len := len(input)
output := make([]*string, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseBool(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]bool)
len := len(input)
output := make([]bool, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseBoolPtr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*bool)
len := len(input)
output := make([]*bool, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseInt(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]int)
len := len(input)
output := make([]int, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseIntPtr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*int)
len := len(input)
output := make([]*int, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseInt8(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]int8)
len := len(input)
output := make([]int8, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseInt8Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*int8)
len := len(input)
output := make([]*int8, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseInt16(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]int16)
len := len(input)
output := make([]int16, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseInt16Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*int16)
len := len(input)
output := make([]*int16, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseInt32(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]int32)
len := len(input)
output := make([]int32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseInt32Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*int32)
len := len(input)
output := make([]*int32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseInt64(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]int64)
len := len(input)
output := make([]int64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseInt64Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*int64)
len := len(input)
output := make([]*int64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseUint(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]uint)
len := len(input)
output := make([]uint, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseUintPtr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*uint)
len := len(input)
output := make([]*uint, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseUint8(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]uint8)
len := len(input)
output := make([]uint8, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseUint8Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*uint8)
len := len(input)
output := make([]*uint8, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseUint16(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]uint16)
len := len(input)
output := make([]uint16, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseUint16Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*uint16)
len := len(input)
output := make([]*uint16, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseUint32(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]uint32)
len := len(input)
output := make([]uint32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseUint32Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*uint32)
len := len(input)
output := make([]*uint32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseUint64(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]uint64)
len := len(input)
output := make([]uint64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseUint64Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*uint64)
len := len(input)
output := make([]*uint64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseFloat32(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]float32)
len := len(input)
output := make([]float32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseFloat32Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*float32)
len := len(input)
output := make([]*float32, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
func reverseFloat64(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]float64)
len := len(input)
output := make([]float64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}

func reverseFloat64Ptr(itemsValue reflect.Value) interface{}{
input := itemsValue.Interface().([]*float64)
len := len(input)
output := make([]*float64, len)
for index := 0; index < (len/2)+1; index++ {
output[index], output[len-1-index] = input[len-1-index], input[index]
}
return output
}
Loading

0 comments on commit 5b30db4

Please sign in to comment.