Skip to content

Commit

Permalink
moved pkg from github.com/osspkg/goppy
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 committed Jul 1, 2024
1 parent 4936418 commit 01bb22c
Show file tree
Hide file tree
Showing 91 changed files with 3,540 additions and 233 deletions.
20 changes: 13 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.db
*.db-journal
*.mmdb
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea
.tools
.idea/
.vscode/
.tools/

coverage.txt
coverage.out

bin/
vendor/
build/
20 changes: 12 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
Expand Down Expand Up @@ -160,10 +159,10 @@ linters-settings:
# If 'custom-order' is 'true', it follows the order of 'sections' option.
# Default: ["standard", "default"]
#sections:
#- standard # Standard section: captures all standard packages.
#- default # Default section: contains all imports that could not be matched to another section type.
#- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
#- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
#- standard # Standard section: captures all standard packages.
#- default # Default section: contains all imports that could not be matched to another section type.
#- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
#- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
# Skip generated files.
# Default: true
skip-generated: true
Expand Down Expand Up @@ -342,10 +341,16 @@ linters-settings:
# Max line length, lines longer will be reported.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option.
# Default: 120.
line-length: 120
line-length: 130
# Tab width in spaces.
# Default: 1
tab-width: 1
staticcheck:
# Deprecated: use the global `run.go` instead.
go: "1.15"
# SAxxxx checks in https://staticcheck.io/docs/configuration/options/#checks
# Default: ["*"]
checks: [ "*", "-SA1019" ]

linters:
disable-all: true
Expand All @@ -362,14 +367,13 @@ linters:
- unused
- prealloc
- durationcheck
# - nolintlint
- staticcheck
- makezero
- nilerr
- errorlint
- bodyclose
- exportloopref
- gci
# - gci
- gosec
# - lll
fast: false
4 changes: 2 additions & 2 deletions .lic.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
author: "Mikhail Knyazhev <markus621@yandex.ru>"
author: "Mikhail Knyazhev <markus621@yandex.com>"
lic_short: "BSD 3-Clause"
lic_file: LICENSE
lic_file: LICENSE
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**Use issues for everything**

- For a small change, just send a PR.
- For bigger changes open an issue for discussion before sending a PR.
- PR should have:
- Test case
- Documentation
- Example (If it makes sense)
- You can also contribute by:
- Reporting issues
- Suggesting new features or enhancements
- Improve/fix documentation
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2019-2024, Mikhail Knyazhev <markus621@yandex.ru>
Copyright (c) 2019-2024, Mikhail Knyazhev <markus621@yandex.com>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
19 changes: 8 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
install:
go install github.com/osspkg/devtool@latest

.PHONY: setup
setup:
devtool setup-lib

.PHONY: lint
lint:
Expand All @@ -15,17 +12,17 @@ lint:
license:
devtool license

.PHONY: build
build:
devtool build --arch=amd64

.PHONY: tests
tests:
devtool test

.PHONY: pre-commite
pre-commite: setup lint build tests

.PHONY: ci
ci: install setup lint build tests
ci: install license lint tests

.PHONY: go_work
go_work:
go work use -r .
go work sync

create_release:
devtool tag
34 changes: 2 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
# Algorithms

Algorithmic calculation methods

[![Coverage Status](https://coveralls.io/repos/github/osspkg/go-algorithms/badge.svg?branch=master)](https://coveralls.io/github/osspkg/go-algorithms?branch=master)
[![Release](https://img.shields.io/github/release/osspkg/go-algorithms.svg?style=flat-square)](https://github.com/osspkg/go-algorithms/releases/latest)
[![Go Report Card](https://goreportcard.com/badge/github.com/osspkg/go-algorithms)](https://goreportcard.com/report/github.com/osspkg/go-algorithms)
[![CI](https://github.com/osspkg/go-algorithms/actions/workflows/ci.yml/badge.svg)](https://github.com/osspkg/go-algorithms/actions/workflows/ci.yml)

## Install

```shell
go get -u go.osspkg.com/algorithms
```

## List

- Graph
- Topological sorting
- [Kahn's Algorithm](graph/kahn/type.go)
- Information compression
- [Reducing numbers](shorten/shorten.go)
- Sorting algorithm
- [Bubble sort](sorts/bubble.go)
- [Cocktail shaker sort](sorts/cocktail.go)
- [Insertion sort](sorts/insertion.go)
- [Merge sort](sorts/merge.go)
- [Selection sort](sorts/selection.go)
- [Heapsort](sorts/heapsort.go)
- Filtering algorithms
- [Bloom filter](filters/bloom/bloom.go)
# goX

## License

BSD-3-Clause License. See the LICENSE file for details
BSD-3-Clause License. See the LICENSE file for details.
13 changes: 13 additions & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Algorithms

Algorithmic calculation methods

## Install

```shell
go get -u go.osspkg.com/x/algorithms
```

## License

BSD-3-Clause License. See the LICENSE file for details
47 changes: 47 additions & 0 deletions algorithms/encoding/base62/base62.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package base62

import "go.osspkg.com/x/algorithms/sorts"

const size = 62

type Base62 struct {
enc []byte
dec map[byte]uint64
}

func New(alphabet string) *Base62 {
if len(alphabet) != size {
panic("encoding alphabet is not 62-bytes long")
}
v := &Base62{
enc: []byte(alphabet),
dec: make(map[byte]uint64, size),
}
for i, b := range v.enc {
v.dec[b] = uint64(i)
}
return v
}

func (v *Base62) Encode(id uint64) string {
result := make([]byte, 0, 11)
for id > 0 {
result = append(result, v.enc[id%size])
id /= size
}
sorts.Reverse(result)
return string(result)
}

func (v *Base62) Decode(data string) uint64 {
var id uint64
for _, b := range []byte(data) {
id = id*size + v.dec[b]
}
return id
}
86 changes: 86 additions & 0 deletions algorithms/encoding/base62/base62_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <[email protected]>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package base62

import (
"fmt"
"math"
"testing"

"go.osspkg.com/x/test"
)

func TestEncode_EncodeDecode(t *testing.T) {
tests := []struct {
name string
id uint64
want string
}{
{name: "Case1", id: 1, want: "p"},
{name: "Case1", id: 2, want: "L"},
{name: "Case1", id: 3, want: "K"},
{name: "Case1", id: 4, want: "G"},
{name: "Case1", id: 5, want: "R"},
{name: "Case1", id: 6, want: "S"},
{name: "Case1", id: 7, want: "u"},
{name: "Case1", id: 8, want: "D"},
{name: "Case1", id: 9, want: "v"},
{name: "Case2", id: 10, want: "o"},
{name: "Case3", id: 100, want: "pH"},
{name: "Case4", id: 1000, want: "PD"},
{name: "Case5", id: 10000, want: "LIn"},
{name: "Case6", id: 100000, want: "c0k"},
{name: "Case7", id: 1000000000, want: "pRmUWP"},
{name: "Case8", id: 999999, want: "Glvp"},
{name: "Case20", id: math.MaxUint64, want: "XNWjtpSoji4"},
}

v := New("0pLKGRSuDvorlO14Pjnd7XgQw9c8YhaIJ5iqtIHy3mWxM6C2TeAbFVBUkZfsNz")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := v.Encode(tt.id)
test.Equal(t, tt.want, h)
id := v.Decode(h)
test.Equal(t, tt.id, id)
})
}
}

func TestEncode_Encode(t *testing.T) {
tests := []struct {
name string
str string
want uint64
}{
{name: "Case1", str: "a", want: 30},
{name: "Case2", str: "aa", want: 1890},
{name: "Case3", str: "aaaaaaaa", want: 107380379795850},
}

v := New("0pLKGRSuDvorlO14Pjnd7XgQw9c8YhaIJ5iqtIHy3mWxM6C2TeAbFVBUkZfsNz")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := v.Decode(tt.str)
fmt.Println(h)
test.Equal(t, tt.want, h)
})
}
}

func Benchmark_base62(b *testing.B) {
v := New("0pLKGRSuDvorlO14Pjnd7XgQw9c8YhaIJ5iqtIHy3mWxM6C2TeAbFVBUkZfsNz")

b.ReportAllocs()
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
v.Decode(v.Encode(math.MaxUint64))
}
})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved.
* Copyright (c) 2019-2024 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand All @@ -8,30 +8,30 @@ package bloom
import (
"testing"

"github.com/stretchr/testify/require"
"go.osspkg.com/x/test"
)

func TestUnit_Bloom(t *testing.T) {
bf, err := New(1000, 0.00001)
require.NoError(t, err)
test.NoError(t, err)

bf.Add([]byte("hello"))
bf.Add([]byte("user"))
bf.Add([]byte("home"))

require.False(t, bf.Contain([]byte("users")))
require.True(t, bf.Contain([]byte("user")))
test.False(t, bf.Contain([]byte("users")))
test.True(t, bf.Contain([]byte("user")))
}

func TestUnit_Bloom2(t *testing.T) {
_, err := New(0, 0.00001)
require.Error(t, err)
test.Error(t, err)

_, err = New(1, 1)
require.Error(t, err)
test.Error(t, err)

_, err = New(1, 0.0001)
require.NoError(t, err)
test.NoError(t, err)
}

func Benchmark_Bloom(b *testing.B) {
Expand Down
5 changes: 5 additions & 0 deletions algorithms/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go.osspkg.com/x/algorithms

go 1.20

replace go.osspkg.com/x/test => ./../test
File renamed without changes.
Loading

0 comments on commit 01bb22c

Please sign in to comment.