Skip to content

Commit e928b35

Browse files
Add RingArray module
1 parent 070eccb commit e928b35

4 files changed

Lines changed: 108 additions & 10 deletions

File tree

core/src/Streamly/Data/Array.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ module Streamly.Data.Array
107107
)
108108
where
109109

110-
#include "inline.hs"
111-
112110
import Streamly.Internal.Data.Array
113111
import Streamly.Internal.Data.MutByteArray (Unbox(..), Serialize(..))
114112

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{-# LANGUAGE CPP #-}
2+
-- |
3+
-- Module : Streamly.Data.RingArray
4+
-- Copyright : (c) 2025 Composewell Technologies
5+
--
6+
-- License : BSD3
7+
-- Maintainer : streamly@composewell.com
8+
-- Stability : released
9+
-- Portability : GHC
10+
--
11+
-- This module provides APIs to create and use unboxed, mutable ring arrays of
12+
-- fixed size. Ring arrays are useful to keep a circular buffer or a sliding
13+
-- window of elements.
14+
--
15+
-- RingArrays are of fixed size but there is a way to expand the size of the
16+
-- ring, you can copy the ring to a MutArray, expand the MutArray and the cast
17+
-- it back to RingArray.
18+
--
19+
-- This module is designed to be imported qualified:
20+
--
21+
-- >>> import qualified Streamly.Data.RingArray as Ring
22+
--
23+
-- For ring arrays that work on boxed types, not requiring the 'Unbox'
24+
-- constraint, please refer to "Streamly.Data.RingArray.Generic".
25+
--
26+
-- Please refer to "Streamly.Internal.Data.RingArray" for functions that have
27+
-- not yet been released.
28+
--
29+
30+
module Streamly.Data.RingArray
31+
( RingArray
32+
33+
-- * Construction
34+
, createOfLast
35+
, castMutArray -- XXX this is unsafeFreeze in Array module
36+
, castMutArrayWith
37+
-- , unsafeCastMutArray
38+
-- , unsafeCastMutArrayWith
39+
40+
-- * Moving the Head
41+
, moveForward
42+
, moveReverse
43+
-- , moveBy
44+
45+
-- * In-place Mutation
46+
, insert
47+
, replace
48+
, replace_
49+
, putIndex
50+
, modifyIndex
51+
52+
-- * Random Access
53+
, getIndex
54+
, unsafeGetIndex
55+
, unsafeGetHead
56+
57+
-- * Conversion
58+
, toList
59+
, toMutArray
60+
61+
-- * Streams
62+
, read
63+
, readRev
64+
65+
-- * Unfolds
66+
, reader
67+
, readerRev
68+
69+
-- * Size
70+
, length
71+
, byteLength
72+
73+
-- * Casting
74+
, cast
75+
-- , unsafeCast
76+
, asBytes
77+
, asMutArray
78+
-- , asMutArray_
79+
80+
-- * Folds
81+
-- , foldlM'
82+
, fold
83+
84+
-- * Stream of Rings
85+
, ringsOf
86+
, scanRingsOf
87+
88+
-- * Fast Byte Comparisons
89+
, eqArray
90+
, eqArrayN
91+
92+
) where
93+
94+
import Streamly.Internal.Data.RingArray
95+
import Prelude hiding (read, length)

core/src/Streamly/Internal/Data/RingArray.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ unsafeCastMutArray = unsafeCastMutArrayWith 0
292292
-- the ring head at @index@ position in the array.
293293
--
294294
-- This operation throws an error if the index is not within the array bounds.
295-
-- It returns Nothing if the array cannot be cast into ring because the array
295+
-- It returns 'Nothing' if the array cannot be cast into ring because the array
296296
-- is a slice. In that case clone the array and cast it or stream the array and
297297
-- use 'createOfLast' to create a ring.
298298
--
@@ -306,8 +306,10 @@ castMutArrayWith i arr
306306
| otherwise = Nothing
307307

308308
-- | Cast a MutArray to a ring sharing the same memory without copying. The
309-
-- ring head is at index 0 of the array. Cast fails with Nothing if the array
310-
-- is a slice.
309+
-- ring head is at index 0 of the array. The size of the ring is equal to the
310+
-- MutArray length.
311+
--
312+
-- See 'castMutArrayWith' for failure scenario.
311313
--
312314
-- >>> castMutArray = RingArray.castMutArrayWith 0
313315
--
@@ -832,6 +834,10 @@ asMutArray rb =
832834
, ringHead rb
833835
)
834836

837+
-- | Like 'asMutArray' but does not return the ring head.
838+
--
839+
-- >>> asMutArray_ = fst . RingArray.asMutArray
840+
--
835841
{-# INLINE asMutArray_ #-}
836842
asMutArray_ :: RingArray a -> MutArray a
837843
asMutArray_ rb =

core/streamly-core.cabal

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ library
455455

456456
-- Pre-release modules
457457
-- , Streamly.Data.Pipe
458-
-- , Streamly.Data.RingArray
459458
-- , Streamly.Data.RingArray.Generic
460459
-- , Streamly.Data.IORef
461460
-- , Streamly.Data.List
@@ -466,18 +465,18 @@ library
466465
-- , Streamly.Data.Either.Strict
467466

468467
-- streamly-core released modules in alphabetic order
469-
-- NOTE: these must be added to streamly.cabal as well
470468
, Streamly.Console.Stdio
471469
, Streamly.Control.Exception
472-
, Streamly.Data.MutByteArray
473470
, Streamly.Data.Array
474471
, Streamly.Data.Array.Generic
472+
, Streamly.Data.Fold
475473
, Streamly.Data.MutArray
476474
, Streamly.Data.MutArray.Generic
477-
, Streamly.Data.Fold
478-
, Streamly.Data.Scanl
475+
, Streamly.Data.MutByteArray
479476
, Streamly.Data.Parser
480477
, Streamly.Data.ParserK
478+
, Streamly.Data.RingArray
479+
, Streamly.Data.Scanl
481480
, Streamly.Data.Stream
482481
, Streamly.Data.StreamK
483482
, Streamly.Data.Unfold

0 commit comments

Comments
 (0)