From 34a0afb242c14988bd601786a6b7ead110fd316d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 28 Jan 2025 23:47:37 -0600 Subject: [PATCH] generic vector instructions done --- src/Instructions/GenericInstructions.hs | 13 +++++++++++++ src/Instructions/StringInstructions.hs | 6 ++---- src/Instructions/VectorIntInstructions.hs | 9 +++++++++ test/Main.hs | 6 +++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index d961b40..cafeb6c 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -313,3 +313,16 @@ instructionVectorReplace state primAccessor vectorAccessor = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps _ -> state + +instructionVectorReplaceFirst :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorReplaceFirst state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps + _ -> state + +instructionVectorRemove :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorRemove state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps + _ -> state + diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 840d7fb..e828677 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -106,8 +106,7 @@ instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) instructionStringSplitOnChar state = state instructionStringReplaceFirstChar :: State -> State -instructionStringReplaceFirstChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs}) = state {_string = replace s1 [c1] [c2] (Just 1) : ss, _char = cs} -instructionStringReplaceFirstChar state = state +instructionStringReplaceFirstChar state = instructionVectorReplaceFirst state char string instructionStringReplaceNChar :: State -> State instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} @@ -125,8 +124,7 @@ instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _ instructionStringRemoveNChar state = state instructionStringRemoveAllChar :: State -> State -instructionStringRemoveAllChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state{_string = replace s1 [c1] "" Nothing : ss, _char = cs} -instructionStringRemoveAllChar state = state +instructionStringRemoveAllChar state = instructionVectorRemove state char string instructionStringOccurrencesOfChar :: State -> State instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 3feffbe..5cf508c 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -53,3 +53,12 @@ instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state i instructionVectorIntSetNth :: State -> State instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt + +instructionVectorIntReplace :: State -> State +instructionVectorIntReplace state = instructionVectorReplace state int vectorInt + +instructionVectorIntReplaceFirst :: State -> State +instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int vectorInt + +instructionVectorIntRemove :: State -> State +instructionVectorIntRemove state = instructionVectorRemove state int vectorInt diff --git a/test/Main.hs b/test/Main.hs index 39abe40..cdfcd88 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -283,4 +283,8 @@ main = do intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState - -- vectorIntTestFunc "instructionVectorInt" + vectorIntTestFunc "instructionVectorIntReplace3" [[0,1,2,99,4,5,99,5,99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState