2022-01-16 00:21:42 +01:00
|
|
|
package obiseq
|
|
|
|
|
2022-02-21 19:00:23 +01:00
|
|
|
import (
|
2022-08-23 11:06:51 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-02-21 19:00:23 +01:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// BioSequenceSlice represents a collection or a set of BioSequence.
|
|
|
|
//
|
|
|
|
// BioSequenceSlice is used to define BioSequenceBatch
|
|
|
|
// a memory pool of BioSequenceSlice is managed to limit allocations.
|
2022-02-21 19:00:23 +01:00
|
|
|
type BioSequenceSlice []*BioSequence
|
|
|
|
|
|
|
|
var _BioSequenceSlicePool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
bs := make(BioSequenceSlice, 0, 10)
|
|
|
|
return &bs
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// > This function returns a pointer to a new `BioSequenceSlice` object
|
2023-01-25 13:22:56 +01:00
|
|
|
func NewBioSequenceSlice(size ...int) *BioSequenceSlice {
|
|
|
|
slice := _BioSequenceSlicePool.Get().(*BioSequenceSlice)
|
|
|
|
if len(size) > 0 {
|
|
|
|
s := size[0]
|
2023-02-23 23:35:58 +01:00
|
|
|
slice = slice.InsureCapacity(s)
|
2023-01-25 13:22:56 +01:00
|
|
|
(*slice)=(*slice)[0:s]
|
|
|
|
}
|
|
|
|
return slice
|
2022-02-21 19:00:23 +01:00
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// `MakeBioSequenceSlice()` returns a pointer to a new `BioSequenceSlice` struct
|
2023-01-25 13:22:56 +01:00
|
|
|
func MakeBioSequenceSlice(size ...int) BioSequenceSlice {
|
|
|
|
return *NewBioSequenceSlice(size...)
|
2022-02-21 19:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BioSequenceSlice) Recycle() {
|
2022-08-23 11:06:51 +02:00
|
|
|
if s == nil {
|
|
|
|
log.Panicln("Trying too recycle a nil pointer")
|
|
|
|
}
|
2022-02-21 19:00:23 +01:00
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// Code added to potentially limit memory leaks
|
|
|
|
for i := range *s {
|
|
|
|
(*s)[i] = nil
|
|
|
|
}
|
2022-02-21 19:00:23 +01:00
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
*s = (*s)[:0]
|
|
|
|
_BioSequenceSlicePool.Put(s)
|
2022-02-21 19:00:23 +01:00
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// Making sure that the slice has enough capacity to hold the number of elements that are being added
|
|
|
|
// to it.
|
|
|
|
func (s *BioSequenceSlice) InsureCapacity(capacity int) *BioSequenceSlice {
|
|
|
|
var c int
|
|
|
|
if s != nil {
|
|
|
|
c = cap(*s)
|
|
|
|
} else {
|
|
|
|
c = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if c < capacity {
|
|
|
|
sl := make(BioSequenceSlice, 0,capacity)
|
|
|
|
s = &sl
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appending the sequence to the slice.
|
2022-02-21 19:00:23 +01:00
|
|
|
func (s *BioSequenceSlice) Push(sequence *BioSequence) {
|
|
|
|
*s = append(*s, sequence)
|
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// Returning the last element of the slice and removing it from the slice.
|
2022-02-21 19:00:23 +01:00
|
|
|
func (s *BioSequenceSlice) Pop() *BioSequence {
|
|
|
|
_s := (*s)[len(*s)-1]
|
|
|
|
(*s)[len(*s)-1] = nil
|
|
|
|
*s = (*s)[:len(*s)-1]
|
|
|
|
return _s
|
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// Returning the first element of the slice and removing it from the slice.
|
2022-02-21 19:00:23 +01:00
|
|
|
func (s *BioSequenceSlice) Pop0() *BioSequence {
|
|
|
|
_s := (*s)[0]
|
|
|
|
(*s)[0] = nil
|
|
|
|
*s = (*s)[1:]
|
|
|
|
return _s
|
|
|
|
}
|
|
|
|
|
2022-08-23 11:06:51 +02:00
|
|
|
// Test that a slice of sequences contains at least a sequence.
|
2022-02-21 19:00:23 +01:00
|
|
|
func (s BioSequenceSlice) NotEmpty() bool {
|
|
|
|
return len(s) > 0
|
|
|
|
}
|