Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,748 additions
and
1,742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,64 @@ | ||
|
||
package ga | ||
|
||
import ( | ||
// "fmt" | ||
// "fmt" | ||
) | ||
|
||
type Bitset struct { | ||
size int | ||
bits []int | ||
} | ||
|
||
func ( b *Bitset ) Create( size int ) { | ||
func (b *Bitset) Create(size int) { | ||
b.size = size | ||
b.bits = make( []int, size ) | ||
b.bits = make([]int, size) | ||
} | ||
|
||
func ( b *Bitset ) GetSize() int { | ||
func (b *Bitset) GetSize() int { | ||
return b.size | ||
} | ||
|
||
func ( b *Bitset ) Get( index int ) int { | ||
if ( index < b.size ) { | ||
func (b *Bitset) Get(index int) int { | ||
if index < b.size { | ||
return b.bits[index] | ||
} | ||
return -1 | ||
} | ||
|
||
func ( b *Bitset ) GetAll() []int { | ||
func (b *Bitset) GetAll() []int { | ||
return b.bits | ||
} | ||
|
||
func ( b *Bitset ) setImpl( index, value int ) { | ||
func (b *Bitset) setImpl(index, value int) { | ||
b.bits[index] = value | ||
} | ||
|
||
func ( b *Bitset ) Set( index, value int ) bool { | ||
if ( index < b.size ) { | ||
b.setImpl( index, value ) | ||
func (b *Bitset) Set(index, value int) bool { | ||
if index < b.size { | ||
b.setImpl(index, value) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func ( b *Bitset ) SetAll( value int ) { | ||
func (b *Bitset) SetAll(value int) { | ||
for i := 0; i < b.size; i++ { | ||
b.setImpl( i, value ) | ||
b.setImpl(i, value) | ||
} | ||
} | ||
|
||
func ( b *Bitset ) CreateCopy() Bitset { | ||
func (b *Bitset) CreateCopy() Bitset { | ||
newBitset := Bitset{} | ||
newBitset.Create( b.size ) | ||
newBitset.Create(b.size) | ||
for i := 0; i < b.size; i++ { | ||
newBitset.Set( i, b.Get( i ) ) | ||
newBitset.Set(i, b.Get(i)) | ||
} | ||
return newBitset | ||
} | ||
|
||
func ( b *Bitset ) Slice( startingBit, size int ) Bitset { | ||
func (b *Bitset) Slice(startingBit, size int) Bitset { | ||
ret := Bitset{} | ||
ret.Create( size ) | ||
ret.bits = b.bits[startingBit:startingBit + size] | ||
ret.Create(size) | ||
ret.bits = b.bits[startingBit : startingBit+size] | ||
return ret | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,48 @@ | ||
|
||
package ga | ||
|
||
import ( | ||
// "fmt" | ||
// "fmt" | ||
) | ||
|
||
type IBitsetParse interface { | ||
SetFormat( []int ) | ||
Process( *Bitset ) []uint64 | ||
SetFormat([]int) | ||
Process(*Bitset) []uint64 | ||
} | ||
|
||
type bitsetParse struct { | ||
expectedBitsetSize int | ||
format []int | ||
format []int | ||
} | ||
|
||
func CreateBitsetParse() IBitsetParse { | ||
return &bitsetParse{} | ||
} | ||
|
||
func ( bp *bitsetParse ) SetFormat( format []int ) { | ||
func (bp *bitsetParse) SetFormat(format []int) { | ||
bp.expectedBitsetSize = 0 | ||
for _, i := range format { | ||
bp.expectedBitsetSize += i | ||
} | ||
bp.format = format | ||
} | ||
|
||
func ( bp *bitsetParse ) Process( bitset *Bitset ) []uint64 { | ||
func (bp *bitsetParse) Process(bitset *Bitset) []uint64 { | ||
if bitset.GetSize() != bp.expectedBitsetSize { | ||
panic("Input format does not match bitset size") | ||
} | ||
|
||
ret := make( []uint64, len( bp.format ) ) | ||
ret := make([]uint64, len(bp.format)) | ||
runningBits := 0 | ||
for retIndex, numBits := range bp.format { | ||
ret[retIndex] = 0 | ||
|
||
for i:= 0; i < numBits; i++ { | ||
if bitset.Get( i + runningBits ) == 1 { | ||
ret[retIndex] |= ( 1 << uint( i ) ) | ||
for i := 0; i < numBits; i++ { | ||
if bitset.Get(i+runningBits) == 1 { | ||
ret[retIndex] |= (1 << uint(i)) | ||
} | ||
} | ||
|
||
runningBits += numBits | ||
} | ||
return ret | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,108 @@ | ||
|
||
package ga_test | ||
|
||
import ( | ||
"github.com/tomcraven/goga" | ||
. "gopkg.in/check.v1" | ||
"math/rand" | ||
"github.com/tomcraven/goga" | ||
) | ||
|
||
type BitsetParseSuite struct { | ||
bp ga.IBitsetParse | ||
} | ||
func ( s *BitsetParseSuite ) SetUpTest( t *C ) { | ||
|
||
func (s *BitsetParseSuite) SetUpTest(t *C) { | ||
s.bp = ga.CreateBitsetParse() | ||
} | ||
func ( s *BitsetParseSuite ) TearDownTest( t *C ) { | ||
func (s *BitsetParseSuite) TearDownTest(t *C) { | ||
s.bp = nil | ||
} | ||
var _ = Suite( &BitsetParseSuite{} ) | ||
|
||
func ( s *BitsetParseSuite ) TestShouldInstantiate( t *C ) { | ||
var _ = Suite(&BitsetParseSuite{}) | ||
|
||
func (s *BitsetParseSuite) TestShouldInstantiate(t *C) { | ||
// tested as part of suite setup | ||
} | ||
|
||
func ( s *BitsetParseSuite ) TestShouldSetFormat( t *C ) { | ||
s.bp.SetFormat( []int{ 1, 2, 3, 4, 5 } ) | ||
func (s *BitsetParseSuite) TestShouldSetFormat(t *C) { | ||
s.bp.SetFormat([]int{1, 2, 3, 4, 5}) | ||
} | ||
|
||
func ( s *BitsetParseSuite ) TestShouldPanicWithMismatchedFormatAndBitsetSize( t *C ) { | ||
inputFormat := []int { | ||
func (s *BitsetParseSuite) TestShouldPanicWithMismatchedFormatAndBitsetSize(t *C) { | ||
inputFormat := []int{ | ||
1, 5, 3, 9, 10, 1, | ||
} | ||
s.bp.SetFormat( inputFormat ) | ||
s.bp.SetFormat(inputFormat) | ||
|
||
inputBitset := ga.Bitset{} | ||
inputBitset.Create( 1 ) // Size should equal sum of all formats | ||
inputBitset.Create(1) // Size should equal sum of all formats | ||
|
||
t.Assert( func() { s.bp.Process( &inputBitset ) }, Panics, "Input format does not match bitset size" ) | ||
t.Assert(func() { s.bp.Process(&inputBitset) }, Panics, "Input format does not match bitset size") | ||
} | ||
|
||
func ( s *BitsetParseSuite ) TestShouldNotPanicWithCorrectFormatAndBitsetSize( t *C ) { | ||
inputFormat := []int { | ||
rand.Intn( 10 ), | ||
rand.Intn( 10 ), | ||
rand.Intn( 10 ), | ||
rand.Intn( 10 ), | ||
rand.Intn( 10 ), | ||
rand.Intn( 10 ), | ||
func (s *BitsetParseSuite) TestShouldNotPanicWithCorrectFormatAndBitsetSize(t *C) { | ||
inputFormat := []int{ | ||
rand.Intn(10), | ||
rand.Intn(10), | ||
rand.Intn(10), | ||
rand.Intn(10), | ||
rand.Intn(10), | ||
rand.Intn(10), | ||
} | ||
s.bp.SetFormat( inputFormat ) | ||
s.bp.SetFormat(inputFormat) | ||
|
||
bitsetSize := 0 | ||
for _, i := range inputFormat { | ||
bitsetSize += i | ||
} | ||
|
||
inputBitset := ga.Bitset{} | ||
inputBitset.Create( bitsetSize ) // Size should equal sum of all formats | ||
inputBitset.Create(bitsetSize) // Size should equal sum of all formats | ||
|
||
s.bp.Process( &inputBitset ) | ||
s.bp.Process(&inputBitset) | ||
} | ||
|
||
func ( s *BitsetParseSuite ) TestShouldProcessSingleFormat( t *C ) { | ||
inputFormat := []int { | ||
func (s *BitsetParseSuite) TestShouldProcessSingleFormat(t *C) { | ||
inputFormat := []int{ | ||
16, | ||
} | ||
|
||
s.bp.SetFormat( inputFormat ) | ||
s.bp.SetFormat(inputFormat) | ||
|
||
inputBitset := ga.Bitset{} | ||
inputBitset.Create( 16 ) | ||
inputBitset.Create(16) | ||
for i := 0; i < 16; i++ { | ||
inputBitset.Set( i, 1 ) | ||
inputBitset.Set(i, 1) | ||
} | ||
t.Assert( s.bp.Process( &inputBitset ), DeepEquals, []uint64{ 65535 } ) | ||
t.Assert(s.bp.Process(&inputBitset), DeepEquals, []uint64{65535}) | ||
|
||
for i := 0; i < 16; i++ { | ||
inputBitset.Set( i, 0 ) | ||
inputBitset.Set(i, 0) | ||
} | ||
t.Assert( s.bp.Process( &inputBitset ), DeepEquals, []uint64{ 0 } ) | ||
t.Assert(s.bp.Process(&inputBitset), DeepEquals, []uint64{0}) | ||
} | ||
|
||
func ( s *BitsetParseSuite ) TestShouldProcessMultipleFormat( t *C ) { | ||
inputFormat := []int { | ||
func (s *BitsetParseSuite) TestShouldProcessMultipleFormat(t *C) { | ||
inputFormat := []int{ | ||
8, 8, | ||
} | ||
|
||
s.bp.SetFormat( inputFormat ) | ||
s.bp.SetFormat(inputFormat) | ||
|
||
inputBitset := ga.Bitset{} | ||
inputBitset.Create( 16 ) | ||
inputBitset.Create(16) | ||
for i := 0; i < 8; i++ { | ||
inputBitset.Set( i, 1 ) | ||
inputBitset.Set(i, 1) | ||
} | ||
for i := 8; i < 16; i++ { | ||
inputBitset.Set( i, 0 ) | ||
inputBitset.Set(i, 0) | ||
} | ||
t.Assert( s.bp.Process( &inputBitset ), DeepEquals, []uint64{ 255, 0 } ) | ||
t.Assert(s.bp.Process(&inputBitset), DeepEquals, []uint64{255, 0}) | ||
|
||
for i := 0; i < 8; i++ { | ||
inputBitset.Set( i, 0 ) | ||
inputBitset.Set(i, 0) | ||
} | ||
for i := 8; i < 16; i++ { | ||
inputBitset.Set( i, 1 ) | ||
inputBitset.Set(i, 1) | ||
} | ||
t.Assert( s.bp.Process( &inputBitset ), DeepEquals, []uint64{ 0, 255 } ) | ||
} | ||
t.Assert(s.bp.Process(&inputBitset), DeepEquals, []uint64{0, 255}) | ||
} |
Oops, something went wrong.