Skip to content

Commit

Permalink
2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Jan 2, 2019
1 parent f95ff70 commit 340f4e7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog


#### 2.0.1
#### 2.0.1 & 2.0.2

- More performance improvements

Expand Down
55 changes: 53 additions & 2 deletions benchmarks/Benchmarks.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,59 @@ main =
suite : Benchmark
suite =
describe "Binary"
[ describe "Converters"
[ let
hexString =
String.repeat 8 "F"
in
describe "Converters"
[ benchmark "fromHex" <|
\_ -> Binary.fromHex (String.repeat 512 "F")
\_ -> Binary.fromHex hexString
]

-----------------------------------------
-- BITWISE OPERATORS
-----------------------------------------
, let
bitsA =
Binary.fromBooleans (List.repeat 8 False)

bitsB =
Binary.fromBooleans (List.repeat 8 True)
in
describe "Bitwise Operators"
[ benchmark "and" <| \_ -> Binary.and bitsA bitsB
, benchmark "or" <| \_ -> Binary.or bitsA bitsB
, benchmark "xor" <| \_ -> Binary.xor bitsA bitsB
, benchmark "not" <| \_ -> Binary.not bitsA
]

-----------------------------------------
-- BIT SHIFTING
-----------------------------------------
, let
bits =
Binary.fromIntegers [ 1, 0, 0, 1, 0, 1, 1, 1 ]
in
describe "Bit Shifting"
[ benchmark "shiftLeftBy" <| \_ -> Binary.shiftLeftBy 5 bits
, benchmark "shiftRightBy" <| \_ -> Binary.shiftRightBy 5 bits
, benchmark "shiftRightZfBy" <| \_ -> Binary.shiftRightZfBy 5 bits
, benchmark "rotateLeftBy" <| \_ -> Binary.rotateLeftBy 5 bits
, benchmark "rotateRightBy" <| \_ -> Binary.rotateRightBy 5 bits
]

-----------------------------------------
-- MATHEMATICAL OPERATORS
-----------------------------------------
, let
bitsA =
Binary.fromBooleans (List.repeat 8 False)

bitsB =
Binary.fromBooleans (List.repeat 8 True)
in
describe "Mathematical Operators"
[ benchmark "add" <|
\_ -> Binary.add bitsA bitsB
]
]
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "icidasset/elm-binary",
"summary": "Work with binary data.",
"license": "MIT",
"version": "2.0.1",
"version": "2.0.2",
"exposed-modules": [
"Binary"
],
Expand Down
61 changes: 27 additions & 34 deletions src/Binary.elm
Original file line number Diff line number Diff line change
Expand Up @@ -376,24 +376,13 @@ shiftLeftBy n =
-}
shiftRightBy : Int -> Bits -> Bits
shiftRightBy n =
map (shiftRightBy_ n)
shiftRightBy n (Bits bits) =
case bits of
firstBit :: _ ->
Bits <| List.repeat n firstBit ++ List.take (List.length bits - n) bits


shiftRightBy_ : Int -> List Bool -> List Bool
shiftRightBy_ n bits =
if n > 0 then
case List.head bits of
Just firstBit ->
shiftRightBy_
(n - 1)
(firstBit :: List.take (List.length bits - 1) bits)

Nothing ->
[]

else
bits
[] ->
Bits <| []


{-| Logical right shift.
Expand All @@ -409,13 +398,11 @@ shiftRightBy_ n bits =
-}
shiftRightZfBy : Int -> Bits -> Bits
shiftRightZfBy n =
map
(\bits ->
List.append
(List.repeat n False)
(List.take (max 0 (List.length bits - n)) bits)
)
shiftRightZfBy n (Bits bits) =
List.append
(List.repeat n False)
(List.take (List.length bits - n) bits)
|> Bits


{-| Rotate a binary sequence to the left.
Expand Down Expand Up @@ -692,14 +679,21 @@ ensureSize size (Bits bits) =
-}
makeIsometric : Bits -> Bits -> ( Bits, Bits )
makeIsometric (Bits a) (Bits b) =
makeIsometric a b =
let
size =
max (List.length a) (List.length b)
( widthA, widthB ) =
( width a
, width b
)
in
( ensureSize size (Bits a)
, ensureSize size (Bits b)
)
if widthA == widthB then
( a, b )

else if widthA > widthB then
( a, ensureSize widthA b )

else
( ensureSize widthB a, b )


{-| Get the amount of bits in a binary sequence.
Expand All @@ -715,15 +709,14 @@ width (Bits bits) =



------------------------------------------------------------------------
-----------------------------------------
-- PRIVATE
------------------------------------------------------------------------
-----------------------------------------


{-| A slightly faster alternative for List.concatMap
-}
concatMap : (a -> List b) -> List a -> List b
concatMap fn =
-- A slightly faster alternative for List.concatMap
List.foldr (\a -> List.append (fn a)) []


Expand Down

0 comments on commit 340f4e7

Please sign in to comment.