Skip to content

Commit 2019bb2

Browse files
author
Jaro Reinders
committed
Basic haddocks
1 parent 79bbdb4 commit 2019bb2

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/Fleet/Array.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
{-# LANGUAGE MagicHash, UnboxedTuples, UnliftedDatatypes #-}
22
{-# OPTIONS_GHC -Wno-name-shadowing -ddump-simpl -ddump-to-file -dsuppress-all -dno-suppress-type-signatures -dno-typeable-binds #-}
33

4-
module Fleet.Array (Array, fromList, toList, (!), index, set, copy, swap) where
4+
{-|
5+
Module : Fleet.Array
6+
Description : Fleet arrays
7+
Copyright : (c) Jaro Reinders, 2025
8+
License : BSD-3-Clause
9+
Maintainer : jaro.reinders@gmail.com
10+
Stability : experimental
11+
Portability : Portable
12+
13+
This module defines fleet arrays and their basic interface.
14+
-}
15+
module Fleet.Array (Array, fromList, toList, (!), index, set, copy, swap, aseq) where
516

617
import GHC.Exts hiding (fromList, toList)
718
import Data.Tuple (Solo (MkSolo))
@@ -19,6 +30,11 @@ data ArrayData a
1930
instance Show a => Show (Array a) where
2031
show xs = "fromList " ++ show (toList xs)
2132

33+
-- | Sequencing array operations.
34+
aseq :: a -> b -> b
35+
aseq x y = x `seq` lazy y
36+
37+
-- | Convert a list into an array. O(n)
2238
fromList :: [a] -> Array a
2339
fromList xs = DA (runRW# $ \s ->
2440
case newArray# (case length xs of (I# n) -> n) undefined s of { (# s , arr #) ->
@@ -27,6 +43,7 @@ fromList xs = DA (runRW# $ \s ->
2743
go _ _ [] s = s
2844
go arr i (x:xs) s = go arr (i +# 1#) xs (writeArray# arr i x s)
2945

46+
-- | Converting an array into a list. O(n)
3047
toList :: Array a -> [a]
3148
toList (DA v) = runRW# $ \s ->
3249
case copyInternal v s of { (# s, arr #) ->
@@ -41,6 +58,7 @@ toList (DA v) = runRW# $ \s ->
4158
in go 0# s
4259
}
4360

61+
-- | Indexing an array. O(1)
4462
{-# INLINE (!) #-}
4563
(!) :: Array a -> Int -> a
4664
DA v ! I# i = helper v i where
@@ -56,6 +74,9 @@ DA v ! I# i = helper v i where
5674
| isTrue# (i ==# j2) -> helper xs j1
5775
| otherwise -> helper xs i
5876

77+
-- | Indexing an array. O(1)
78+
-- Using the 'Solo' constructor, you can sequence indexing to happen before
79+
-- future updates without having to evaluate the resulting element.
5980
{-# INLINE index #-}
6081
index :: Int -> Array a -> Solo a
6182
index (I# i) (DA v) = helper v i where
@@ -131,10 +152,12 @@ appDiffOp op (DA v) = runRW# $ \s ->
131152
-- DA v''
132153
-- }}}}
133154

155+
-- | Update the array element at a given position to a new value. O(1)
134156
{-# INLINE set #-}
135157
set :: Int -> a -> Array a -> Array a
136158
set (I# i) x = appDiffOp (Set i x)
137159

160+
-- | Swap two elements in an array. O(1)
138161
{-# INLINE swap #-}
139162
swap :: Int -> Int -> Array a -> Array a
140163
swap (I# i) (I# j) = appDiffOp (Swap i j)
@@ -149,6 +172,9 @@ copyInternal v s =
149172
case appOp arr op s of { s -> (# s , arr #)
150173
}}
151174

175+
-- | Copy an array. O(n)
176+
-- This detaches any future updates from old versions of the array.
177+
-- Use this when you know you will be updating a large part of an array.
152178
copy :: Array a -> Array a
153179
copy (DA v) = runRW# $ \s ->
154180
case copyInternal v s of { (# s , arr #) ->

0 commit comments

Comments
 (0)