diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..e9d2163 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +@Workiva/skreams diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..eb00815 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,38 @@ +name: "Tests" + +on: + pull_request: + push: + branches: + - 'master' + tags: + - '*' + +permissions: + pull-requests: write + contents: read + id-token: write + +jobs: + Tests: + runs-on: ubuntu-latest + strategy: + matrix: + go: [ '1.15', 'stable' ] + name: Tests on Go ${{ matrix.go }} + steps: + - name: Checkout Repo + uses: actions/checkout@v3.3.0 + with: + path: go/src/github.com/Workiva/go-datastructures + + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go }} + + - name: Run Tests + timeout-minutes: 10 + run: | + cd go/src/github.com/Workiva/go-datastructures + go test ./... diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c4cb4f4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: go - -go: - - tip - -before_install: go get golang.org/x/tools/cmd/cover - -install: -- go mod vendor - -script: go test -race -v -cover ./... - -env: -- GOMAXPROCS=8 GORACE="halt_on_error=1" - -notifications: - email: false diff --git a/sort/sort.go b/sort/sort.go index 3b949d5..0d7ce59 100644 --- a/sort/sort.go +++ b/sort/sort.go @@ -27,8 +27,11 @@ func MultithreadedSortComparators(comparators Comparators) Comparators { var wg sync.WaitGroup numCPU := int64(runtime.NumCPU()) - if numCPU%2 == 1 { // single core machine - numCPU++ + if numCPU == 1 { // single core machine + numCPU = 2 + } else { + // otherwise this algo only works with a power of two + numCPU = int64(prevPowerOfTwo(uint64(numCPU))) } chunks := chunk(toBeSorted, numCPU) @@ -70,3 +73,13 @@ func chunk(comparators Comparators, numParts int64) []Comparators { } return parts } + +func prevPowerOfTwo(x uint64) uint64 { + x = x | (x >> 1) + x = x | (x >> 2) + x = x | (x >> 4) + x = x | (x >> 8) + x = x | (x >> 16) + x = x | (x >> 32) + return x - (x >> 1) +}