|
| 1 | +# SmallVectors |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +A module that defines small (mutable and immutable) vectors with a maximum length. Externally they have a dynamic/runtime length, but internally they are backed by a statically sized vector. This makes it so that operations can be performed faster because they can remain on the stack, but it provides some more convenience compared to StaticArrays.jl where the length is encoded in the type. |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +For example: |
| 10 | +```julia |
| 11 | +using NDTensors.SmallVectors |
| 12 | + |
| 13 | +mv = MSmallVector{10}([1, 2, 3]) # Mutable vector with length 3, maximum length 10 |
| 14 | +push!(mv, 4) |
| 15 | +mv[2] = 12 |
| 16 | +sort!(mv; rev=true) |
| 17 | + |
| 18 | +v = SmallVector{10}([1, 2, 3]) # Immutable vector with length 3, maximum length 10 |
| 19 | +v = SmallVectors.push(v, 4) |
| 20 | +v = SmallVectors.setindex(v, 12, 2) |
| 21 | +v = SmallVectors.sort(v; rev=true) |
| 22 | +``` |
| 23 | +This also has the advantage that you can efficiently store collections of `SmallVector`/`MSmallVector` that have different runtime lengths, as long as they have the same maximum length. |
| 24 | + |
| 25 | +## List of functionality |
| 26 | + |
| 27 | +`SmallVector` and `MSmallVector` are subtypes of `AbstractVector` and therefore can be used in `Base` `AbstractVector` functions, though `SmallVector` will fail for mutating functions like `setindex!` because it is immutable. |
| 28 | + |
| 29 | +`MSmallVector` has specialized implementations of `Base` functions that involve resizing such as: |
| 30 | +- `resize!` |
| 31 | +- `push!` |
| 32 | +- `pushfirst!` |
| 33 | +- `pop!` |
| 34 | +- `popfirst!` |
| 35 | +- `append!` |
| 36 | +- `prepend!` |
| 37 | +- `insert!` |
| 38 | +- `deleteat!` |
| 39 | +which are guaranteed to not realocate memory, and instead just use the memory buffer that already exists, unlike Base's `Vector` which may have to reallocate memory depending on the operation. However, they will error if they involve operations that resize beyond the maximum length of the `MSmallVector`, which you can access with `SmallVectors.maxlength(v)`. |
| 40 | + |
| 41 | +In addition, `SmallVector` and `MSmallVector` implement basic non-mutating operations such as: |
| 42 | +- `SmallVectors.setindex` |
| 43 | +, non-mutating resizing operations: |
| 44 | +- `SmallVector.resize` |
| 45 | +- `SmallVector.push` |
| 46 | +- `SmallVector.pushfirst` |
| 47 | +- `SmallVector.pop` |
| 48 | +- `SmallVector.popfirst` |
| 49 | +- `SmallVector.append` |
| 50 | +- `SmallVector.prepend` |
| 51 | +- `SmallVector.insert` |
| 52 | +- `SmallVector.deleteat` |
| 53 | +which output a new vector. In addition, it implements: |
| 54 | +- `SmallVectors.circshift` |
| 55 | +- `sort` (overloaded from `Base`). |
| 56 | + |
| 57 | +Finally, it provides some new helpful functions that are not in `Base`: |
| 58 | +- `SmallVectors.insertsorted[!]` |
| 59 | +- `SmallVectors.insertsortedunique[!]` |
| 60 | +- `SmallVectors.mergesorted[!]` |
| 61 | +- `SmallVectors.mergesortedunique[!]` |
| 62 | + |
| 63 | +## TODO |
| 64 | + |
| 65 | +Add specialized overloads for: |
| 66 | +- `splice[!]` |
| 67 | +- `union[!]` (`∪`) |
| 68 | +- `intersect[!]` (`∩`) |
| 69 | +- `setdiff[!]` |
| 70 | +- `symdiff[!]` |
| 71 | +- `unique[!]` |
| 72 | + |
| 73 | +Please let us know if there are other operations that would warrant specialized implmentations for `AbstractSmallVector`. |
0 commit comments