-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add lazy dropEnd and friends Fixes #306. Utilizes an eager strategy as described in the original issue. Chunks are eagerly evaluated but the overall structure and pointers are kept intact. * Fix `since` version number * Use `foldrChunks` instead of `length` based checks * Add review changes - Pass Int64 to accumulators - Clean up `breakEnd` - Implement lazy version of `dropWhileEnd` - Add dropWhileEnd lazy test * Fix `breakEnd` and `spanEnd` lazyness * Make `dropEnd` lazier * Formatting * Fix lazy `dropEnd` * Add `Deque` module `dropEnd` now uses `Deque` for handling the accumulated bytestrigs * Normalize function names * Return `Maybe (S.ByteString, Deque)` from pops Plus all other review suggestions * Add review changes * Rename `dropElements` to `dropEndBytes` * Add examples + style fixes * Update Data/ByteString/Lazy/Internal/Deque.hs Co-authored-by: Simon Jakobi <[email protected]> * Replace `elemLength` with `byteLength` * Upadate examples Co-authored-by: Simon Jakobi <[email protected]>
- Loading branch information
Showing
5 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{- | | ||
A Deque used for accumulating `S.ByteString`s in the lazy | ||
version of `dropEnd`. | ||
-} | ||
module Data.ByteString.Lazy.Internal.Deque ( | ||
Deque (..), | ||
empty, | ||
null, | ||
cons, | ||
snoc, | ||
popFront, | ||
popRear, | ||
) where | ||
|
||
import qualified Data.ByteString as S | ||
import Data.Int (Int64) | ||
import Prelude hiding (head, length, null) | ||
|
||
-- A `S.ByteString` Deque used as an accumulator for lazy | ||
-- Bytestring operations | ||
data Deque = Deque | ||
{ front :: [S.ByteString] | ||
, rear :: [S.ByteString] | ||
, -- | Total length in bytes | ||
byteLength :: !Int64 | ||
} | ||
|
||
-- An empty Deque | ||
empty :: Deque | ||
empty = Deque [] [] 0 | ||
|
||
-- Is the `Deque` empty? | ||
-- O(1) | ||
null :: Deque -> Bool | ||
null deque = byteLength deque == 0 | ||
|
||
-- Add a `S.ByteString` to the front of the `Deque` | ||
-- O(1) | ||
cons :: S.ByteString -> Deque -> Deque | ||
cons x (Deque fs rs acc) = Deque (x : fs) rs (acc + len x) | ||
|
||
-- Add a `S.ByteString` to the rear of the `Deque` | ||
-- O(1) | ||
snoc :: S.ByteString -> Deque -> Deque | ||
snoc x (Deque fs rs acc) = Deque fs (x : rs) (acc + len x) | ||
|
||
len :: S.ByteString -> Int64 | ||
len x = fromIntegral $ S.length x | ||
|
||
-- Pop a `S.ByteString` from the front of the `Deque` | ||
-- Returns the bytestring and the updated Deque, or Nothing if the Deque is empty | ||
-- O(1) , occasionally O(n) | ||
popFront :: Deque -> Maybe (S.ByteString, Deque) | ||
popFront (Deque [] rs acc) = case reverse rs of | ||
[] -> Nothing | ||
x : xs -> Just (x, Deque xs [] (acc - len x)) | ||
popFront (Deque (x : xs) rs acc) = Just (x, Deque xs rs (acc - len x)) | ||
|
||
-- Pop a `S.ByteString` from the rear of the `Deque` | ||
-- Returns the bytestring and the updated Deque, or Nothing if the Deque is empty | ||
-- O(1) , occasionally O(n) | ||
popRear :: Deque -> Maybe (Deque, S.ByteString) | ||
popRear (Deque fs [] acc) = case reverse fs of | ||
[] -> Nothing | ||
x : xs -> Just (Deque [] xs (acc - len x), x) | ||
popRear (Deque fs (x : xs) acc) = Just (Deque fs xs (acc - len x), x) |
Oops, something went wrong.