Package dynarray contains data structures for Dynamic Arrays.
DynArray works like regular arrays except for the following differences:
- Indexing is one based.
- An index can hold no value at all.
- Memory usage depends on count of values stored not the index count.
- When storing values beyond the highest index, length grows automatically instead of seeing index out of bounds.
- Inserting into or deleting from the beginning or middle of the array takes O(log n) time instead of O(n) time.
- Accessing a random array element takes O(log n) time instead of O(1) time.
Cursor instances can speed up array access if reading and writing to the same area of the DynArray. DynArray instances are not safe to use with multiple goroutines.
package main
import (
"fmt"
"github.com/keep94/dynarray"
)
func main() {
var dyn dynarray.DynArray[string]
dyn.Set(1, "hello")
dyn.Set(3, "goodbye")
for i := 1; i <= dyn.Len(); i++ {
fmt.Println(dyn.GetOrElse(i, "novalue"))
}
// Prints:
//
// hello
// novalue
// goodbye
}More documentation and examples can be found here.