Skip to content

Commit

Permalink
readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Rubin committed Jun 17, 2015
1 parent 19f2895 commit fba4c27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
13 changes: 9 additions & 4 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package primes

import "math"

const (
// Byte is 8 bits
Byte = 8
)

// BitSet is an object that makes working with large numbers of boolean values
// simple and memory efficient
type BitSet []byte
Expand Down Expand Up @@ -41,20 +46,20 @@ func (s BitSet) IsSet(i uint64) bool {
}

func (s BitSet) byteFor(i uint64) (b *byte, mask byte) {
b = &s[i/8]
mask = byte(1) << (i % 8)
b = &s[i/Byte]
mask = byte(1) << (i % Byte)
return
}

// NewBitSet returns a new BitSet big enough to hold at least l values
func NewBitSet(l uint64) BitSet {
numBlocks := uint64(math.Floor(float64(l)/8)) + 1
numBlocks := uint64(math.Floor(float64(l)/Byte)) + 1
return make(BitSet, int(numBlocks))
}

// Max returns the highest value that can be set
func (s BitSet) Max() uint64 {
return s.Len()*8 - 1
return s.Len()*Byte - 1
}

// ListSet returns a set of all enabled indexes
Expand Down
6 changes: 3 additions & 3 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func TestBitSet(t *testing.T) {
So(s.IsSet(11), ShouldBeTrue)
s.Unset(11)
So(s.IsSet(11), ShouldBeFalse)
So(s, ShouldResemble, BitSet{math.MaxUint8, math.MaxUint8 - uint8(math.Pow(float64(2), float64(11%8)))})
So(s, ShouldResemble, BitSet{math.MaxUint8, math.MaxUint8 - uint8(math.Pow(float64(2), float64(11%Byte)))})

So(s.IsSet(3), ShouldBeTrue)
s.Unset(3)
So(s.IsSet(3), ShouldBeFalse)
So(s, ShouldResemble, BitSet{
math.MaxUint8 - uint8(math.Pow(float64(2), float64(3%8))),
math.MaxUint8 - uint8(math.Pow(float64(2), float64(11%8))),
math.MaxUint8 - uint8(math.Pow(float64(2), float64(3%Byte))),
math.MaxUint8 - uint8(math.Pow(float64(2), float64(11%Byte))),
})

So(len(s.ListSet()), ShouldEqual, 14)
Expand Down
8 changes: 4 additions & 4 deletions eratosthenes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ type Eratosthenes struct {
}

// NewEratosthenes returns a new Eratosthenes calculated for all values from 0
// to the nearest multiple of 8 greater than l
// to the nearest byte greater than l
func NewEratosthenes(l uint64) Sieve {
// initialize the sieve with all bits set but unset 0 and 1 (as they are
// not-prime)
s := Eratosthenes{NewBitSet(l).SetAll().Unset(0).Unset(1)}

for i := uint64(2); i <= sqrt(s.Len()*8); i++ {
if !s.IsSet(i) {
for i := uint64(2); i <= sqrt(s.Max()); i++ {
if !s.IsPrime(i) {
continue
}

// i is prime, so disable all multiples of it as prime
for next := 2 * i; next < s.Len()*8; next += i {
for next := 2 * i; next < s.Len()*Byte; next += i {
s.Unset(next)
}
}
Expand Down

0 comments on commit fba4c27

Please sign in to comment.