| Copyright | (c) Matt Hunzinger 2025 |
|---|---|
| License | BSD-style (see the LICENSE file in the distribution) |
| Maintainer | matt@hunzinger.me |
| Stability | provisional |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Data.SparseVector
Description
Synopsis
- newtype SparseVector a = SparseVector {
- unSparseVector :: Vector (Maybe a)
- empty :: SparseVector a
- insert :: Int -> a -> SparseVector a -> SparseVector a
- lookup :: Int -> SparseVector a -> Maybe a
- delete :: Int -> SparseVector a -> SparseVector a
- mapWithKey :: (Int -> a -> b) -> SparseVector a -> SparseVector b
- mapAccum :: (a -> b -> (a, c)) -> a -> SparseVector b -> (a, SparseVector c)
- intersection :: SparseVector a -> SparseVector b -> SparseVector a
- intersectionWith :: (a -> b -> c) -> SparseVector a -> SparseVector b -> SparseVector c
- intersectionWithKey :: (Int -> a -> b -> c) -> SparseVector a -> SparseVector b -> SparseVector c
- intersectionVec :: SparseVector a -> SparseVector b -> Vector a
- intersectionVecWith :: (a -> b -> c) -> SparseVector a -> SparseVector b -> Vector c
- intersectionVecWithKey :: (Int -> a -> b -> c) -> SparseVector a -> SparseVector b -> Vector c
- fromList :: [(Int, a)] -> SparseVector a
- toList :: SparseVector a -> [Maybe a]
- fromVector :: Vector a -> SparseVector a
- toVector :: SparseVector a -> Vector a
- freeze :: PrimMonad m => MSparseVector (PrimState m) a -> m (SparseVector a)
- thaw :: PrimMonad m => SparseVector a -> m (MSparseVector (PrimState m) a)
Sparse vectors
newtype SparseVector a Source #
Sparse n-dimensional vector.
A sparse vector is defined as a Vector (Maybe a),
where Maybe a is a cell for an element in the sparse vector.
Inserting elements at some dimension n will grow the vector up to n,
using Nothing to create empty cells.
Constructors
| SparseVector | |
Fields
| |
Instances
Construction
empty :: SparseVector a Source #
Empty sparse vector.
Operations
insert :: Int -> a -> SparseVector a -> SparseVector a Source #
Insert an element at a given index into a SparseVector.
Inserting elements at some dimension n will grow the vector up to n,
using Nothing to create empty cells.
>>>insert 0 'a' emptySparseVector {unSparseVector = [Just 'a']}
>>>insert 2 'b' emptySparseVector {unSparseVector = [Nothing,Nothing,Just 'b']}
lookup :: Int -> SparseVector a -> Maybe a Source #
Lookup an element at a given index in a SparseVector.
delete :: Int -> SparseVector a -> SparseVector a Source #
Delete an index from a SparseVector, replacing its cell with Nothing.
mapWithKey :: (Int -> a -> b) -> SparseVector a -> SparseVector b Source #
mapAccum :: (a -> b -> (a, c)) -> a -> SparseVector b -> (a, SparseVector c) Source #
Intersection
intersection :: SparseVector a -> SparseVector b -> SparseVector a Source #
intersectionWith :: (a -> b -> c) -> SparseVector a -> SparseVector b -> SparseVector c Source #
intersectionWithKey :: (Int -> a -> b -> c) -> SparseVector a -> SparseVector b -> SparseVector c Source #
intersectionVec :: SparseVector a -> SparseVector b -> Vector a Source #
intersectionVecWith :: (a -> b -> c) -> SparseVector a -> SparseVector b -> Vector c Source #
intersectionVecWithKey :: (Int -> a -> b -> c) -> SparseVector a -> SparseVector b -> Vector c Source #
Conversion
fromList :: [(Int, a)] -> SparseVector a Source #
toList :: SparseVector a -> [Maybe a] Source #
fromVector :: Vector a -> SparseVector a Source #
toVector :: SparseVector a -> Vector a Source #
Mutations
freeze :: PrimMonad m => MSparseVector (PrimState m) a -> m (SparseVector a) Source #
Freeze a MSparseVector into a SparseVector.
thaw :: PrimMonad m => SparseVector a -> m (MSparseVector (PrimState m) a) Source #
Unfreeze a SparseVector into a MSparseVector.