mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Add some doc and switch to the parallel gzip library
Former-commit-id: 2c1187001f989ba3de5895f516d4c8b54d52a4c4
This commit is contained in:
40
pkg/obiutils/array.go
Normal file
40
pkg/obiutils/array.go
Normal file
@ -0,0 +1,40 @@
|
||||
package obiutils
|
||||
|
||||
// Matrix is a generic type representing a matrix.
|
||||
type Matrix[T any] [][]T
|
||||
|
||||
// Make2DArray generates a 2D array of type T with the specified number of rows and columns.
|
||||
//
|
||||
// Data is stored in a contiguous memory block in row-major order.
|
||||
//
|
||||
// Parameters:
|
||||
// - rows: the number of rows in the 2D array.
|
||||
// - cols: the number of columns in the 2D array.
|
||||
//
|
||||
// Return:
|
||||
// - Matrix[T]: the generated 2D array of type T.
|
||||
func Make2DArray[T any](rows, cols int) Matrix[T] {
|
||||
matrix := make(Matrix[T], rows)
|
||||
data := make([]T, cols*rows)
|
||||
for i := 0; i < rows; i++ {
|
||||
matrix[i] = data[i*cols : (i+1)*cols]
|
||||
}
|
||||
return matrix
|
||||
}
|
||||
|
||||
// Row returns the i-th row of the matrix.
|
||||
//
|
||||
// Parameters:
|
||||
//
|
||||
// i - the index of the row to retrieve.
|
||||
//
|
||||
// Return:
|
||||
//
|
||||
// []T - the i-th row of the matrix.
|
||||
func (matrix *Matrix[T]) Column(i int) []T {
|
||||
r := make([]T, len(*matrix))
|
||||
for j := 0; j < len(*matrix); j++ {
|
||||
r[j] = (*matrix)[j][i]
|
||||
}
|
||||
return r
|
||||
}
|
@ -2,7 +2,7 @@ package obiutils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
gzip "github.com/klauspost/pgzip"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
99
pkg/obiutils/set.go
Normal file
99
pkg/obiutils/set.go
Normal file
@ -0,0 +1,99 @@
|
||||
package obiutils
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Set[E comparable] map[E]struct{}
|
||||
|
||||
// MakeSet creates a new Set with the provided values.
|
||||
//
|
||||
// The function takes a variadic parameter `vals` of type `E` which represents the values
|
||||
// that will be added to the Set.
|
||||
//
|
||||
// The function returns a Set of type `Set[E]` which contains the provided values.
|
||||
func MakeSet[E comparable](vals ...E) Set[E] {
|
||||
s := Set[E]{}
|
||||
for _, v := range vals {
|
||||
s[v] = struct{}{}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// NewSet creates a new Set with the given values.
|
||||
//
|
||||
// It takes a variadic parameter of type E, where E is a comparable type.
|
||||
// It returns a pointer to a Set of type E.
|
||||
func NewSet[E comparable](vals ...E) *Set[E] {
|
||||
s := MakeSet[E](vals...)
|
||||
return &s
|
||||
}
|
||||
|
||||
// Add adds the given values to the set.
|
||||
//
|
||||
// It takes a variadic parameter `vals` of type `E`.
|
||||
// There is no return type for this function.
|
||||
func (s Set[E]) Add(vals ...E) {
|
||||
for _, v := range vals {
|
||||
s[v] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Contains checks if the set contains a given element.
|
||||
//
|
||||
// Parameters:
|
||||
// - v: the element to check for presence in the set.
|
||||
//
|
||||
// Returns:
|
||||
// - bool: true if the set contains the given element, false otherwise.
|
||||
func (s Set[E]) Contains(v E) bool {
|
||||
_, ok := s[v]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Members returns a slice of all the elements in the set.
|
||||
//
|
||||
// It does not modify the original set.
|
||||
// It returns a slice of type []E.
|
||||
func (s Set[E]) Members() []E {
|
||||
result := make([]E, 0, len(s))
|
||||
for v := range s {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// String returns a string representation of the set.
|
||||
//
|
||||
// It returns a string representation of the set by formatting the set's members using the fmt.Sprintf function.
|
||||
// The resulting string is then returned.
|
||||
func (s Set[E]) String() string {
|
||||
return fmt.Sprintf("%v", s.Members())
|
||||
}
|
||||
|
||||
// Union returns a new set that is the union of the current set and the specified set.
|
||||
//
|
||||
// Parameters:
|
||||
// - s2: the set to be unioned with the current set.
|
||||
//
|
||||
// Return:
|
||||
// - Set[E]: the resulting set after the union operation.
|
||||
func (s Set[E]) Union(s2 Set[E]) Set[E] {
|
||||
result := MakeSet(s.Members()...)
|
||||
result.Add(s2.Members()...)
|
||||
return result
|
||||
}
|
||||
|
||||
// Intersection returns a new set that contains the common elements between the current set and another set.
|
||||
//
|
||||
// Parameter:
|
||||
// - s2: the other set to compare with.
|
||||
// Return:
|
||||
// - Set[E]: a new set that contains the common elements.
|
||||
func (s Set[E]) Intersection(s2 Set[E]) Set[E] {
|
||||
result := MakeSet[E]()
|
||||
for _, v := range s.Members() {
|
||||
if s2.Contains(v) {
|
||||
result.Add(v)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user