From e6d636cff2fa782911ee8ee06af7555405a4a215 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Mon, 18 Nov 2024 01:35:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D1=82=D1=8B=20=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D1=8F=D1=82=D1=81=D1=8F=20=D0=B2=20Vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 3 ++- lab3/lab3.cabal | 3 +++ lab3/package.yaml | 1 + lab3/src/Lib.hs | 17 ++++++++++------- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 5c45c6c..a82cd5b 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -1,6 +1,7 @@ module Main (main) where import Codec.Picture +import qualified Data.Vector.Unboxed as VU import Lib caesarShift :: Int @@ -15,7 +16,7 @@ main = do let encryptedText = encryptCaesar alphabet caesarShift inputText putStrLn $ take 30 encryptedText let encryptedTextBits = textToBits encryptedText - putStrLn $ concat (take 30 (map show encryptedTextBits)) + putStrLn $ concat (take 30 (map show (VU.toList encryptedTextBits))) let encryptedTextFromBits = bitsToText encryptedTextBits putStrLn $ take 30 encryptedTextFromBits let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits diff --git a/lab3/lab3.cabal b/lab3/lab3.cabal index d0ffb76..7294e2b 100644 --- a/lab3/lab3.cabal +++ b/lab3/lab3.cabal @@ -36,6 +36,7 @@ library build-depends: JuicyPixels , base >=4.7 && <5 + , vector default-language: Haskell2010 executable lab3-exe @@ -51,6 +52,7 @@ executable lab3-exe JuicyPixels , base >=4.7 && <5 , lab3 + , vector default-language: Haskell2010 test-suite lab3-test @@ -67,4 +69,5 @@ test-suite lab3-test JuicyPixels , base >=4.7 && <5 , lab3 + , vector default-language: Haskell2010 diff --git a/lab3/package.yaml b/lab3/package.yaml index ea1a099..9d8962e 100644 --- a/lab3/package.yaml +++ b/lab3/package.yaml @@ -22,6 +22,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 - JuicyPixels +- vector ghc-options: - -Wall diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index 5a7cfbe..8a3f3db 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -9,6 +9,7 @@ module Lib import Data.Char (ord, chr) import Data.Bits (testBit) +import qualified Data.Vector.Unboxed as VU createAlphabetFromText :: String -> [Char] createAlphabetFromText [] = [] @@ -35,13 +36,15 @@ decryptCaesar alphabet shift = where alphabetLength = length alphabet -textToBits :: String -> [Int] -textToBits = concatMap charToBits - where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ] +textToBits :: String -> VU.Vector Int +textToBits text = VU.fromList $ concatMap charToBits text + where + charToBits c = [if testBit (ord c) i then 1 else 0 | i <- [7,6..0]] -bitsToText :: [Int] -> String -bitsToText [] = [] -bitsToText bits = (chr $ bitsToInt (take 8 bits)) : bitsToText (drop 8 bits) +bitsToText :: VU.Vector Int -> String +bitsToText bits + | VU.null bits = [] + | otherwise = (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits) where bitsToInt charBits = - sum [bit * (2 ^ index) | (bit, index) <- zip charBits [7 :: Int, 6 .. 0]] \ No newline at end of file + sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]] \ No newline at end of file