| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package slice
- type Slice[T any] []T
- func New[T any](initCap int) Slice[T] {
- return make(Slice[T], 0, initCap)
- }
- func (s *Slice[T]) Append(t T) {
- *s = append(*s, t)
- }
- func (s *Slice[T]) AppendFront(t T) {
- *s = append([]T{t}, *s...)
- }
- func (s *Slice[T]) Get(index int) (t T, ok bool) {
- if index < 0 || index >= s.Len() {
- return
- }
- t = (*s)[index]
- ok = true
- return
- }
- func (s *Slice[T]) Set(index int, t T) (old T, ok bool) {
- if index < 0 || index >= s.Len() {
- return
- }
- old = (*s)[index]
- (*s)[index] = t
- ok = true
- return
- }
- func (s *Slice[T]) Delete(index int) (t T, ok bool) {
- if index < 0 || index >= s.Len() {
- return
- }
- t = (*s)[index]
- *s = append((*s)[:index], (*s)[index+1:]...)
- ok = true
- return
- }
- func (s *Slice[T]) Swap(i, j int) {
- if i < 0 || j < 0 || i >= s.Len() || j >= s.Len() {
- return
- }
- (*s)[i], (*s)[j] = (*s)[j], (*s)[i]
- }
- func (s *Slice[T]) Len() int {
- return len(*s)
- }
- func (s *Slice[T]) Cap() int {
- return cap(*s)
- }
- func (s *Slice[T]) Clear() {
- *s = Slice[T]{}
- }
|