@@ -16,9 +16,8 @@ module Data.Patch.MapWithMove where
17import Data.Patch.Class
17import Data.Patch.Class
20import Control.Lens hiding (from, to)
19import Control.Lens hiding (from, to)
21−import Control.Monad.Trans.State
20+import Data.Align (align)
22import Data.Foldable
21import Data.Foldable
23import Data.Function
22import Data.Function
24import Data.List
23import Data.List
@@ -28,9 +27,9 @@ import Data.Maybe
28#if !MIN_VERSION_base(4,11,0)
27#if !MIN_VERSION_base(4,11,0)
29import Data.Semigroup (Semigroup (..))
28import Data.Semigroup (Semigroup (..))
31import qualified Data.Set as Set
31import qualified Data.Set as Set
32import Data.These (These(..))
32import Data.These (These(..))
35-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
34-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
36-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
35-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
@@ -185,37 +184,36 @@ patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatCha
186-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
185-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
187-- will produce the second 'Map'.
186-- will produce the second 'Map'.
188−patchThatChangesMap :: (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
187+patchThatChangesMap :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
189patchThatChangesMap oldByIndex newByIndex = patch
188patchThatChangesMap oldByIndex newByIndex = patch
190− where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
189+ where invert :: Map k v -> Map v (Set k)
191− (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
190+ invert = Map.fromListWith (<>) . fmap (\(k, v) -> (v, Set.singleton k)) . Map.toList
192− let f k v = do
191+ -- In the places where we use unionDistinct, a non-distinct key indicates a bug in this function
193− remainingValues <- get
192+ unionDistinct :: forall k' v'. Ord k' => Map k' v' -> Map k' v' -> Map k' v'
194− let putRemainingKeys remainingKeys = put $ if Set.null remainingKeys
193+ unionDistinct = Map.unionWith (error "patchThatChangesMap: non-distinct keys")
195− then Map.delete v remainingValues
194+ unionPairDistinct :: (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k))
196− else Map.insert v remainingKeys remainingValues
195+ unionPairDistinct (oldFroms, oldTos) (newFroms, newTos) = (unionDistinct oldFroms newFroms, unionDistinct oldTos newTos)
197− case Map.lookup v remainingValues of
196+ -- Generate patch info for a single value
198− Nothing -> return $ NodeInfo (From_Insert v) $ Just undefined -- There's no existing value we can take
197+ -- Keys that are found in both the old and new sets will not be patched
199− Just fromKs ->
198+ -- Keys that are found in only the old set will be moved to a new position if any are available; otherwise they will be deleted
200− if k `Set.member` fromKs
199+ -- Keys that are found in only the new set will be populated by moving an old key if any are available; otherwise they will be inserted
201− then do
200+ patchSingleValue :: v -> Set k -> Set k -> (Map k (From k v), Map k (To k))
202− putRemainingKeys $ Set.delete k fromKs
201+ patchSingleValue v oldKeys newKeys = foldl' unionPairDistinct mempty $ align (toList $ oldKeys `Set.difference` newKeys) (toList $ newKeys `Set.difference` oldKeys) <&> \case
203− return $ NodeInfo (From_Move k) $ Just undefined -- There's an existing value, and it's here, so no patch necessary
202+ This oldK -> (mempty, Map.singleton oldK Nothing) -- There's nowhere for this value to go, so we know we are deleting it
204− else do
203+ That newK -> (Map.singleton newK $ From_Insert v, mempty) -- There's nowhere fo this value to come from, so we know we are inserting it
205− (fromK, remainingKeys) <- return $
204+ These oldK newK -> (Map.singleton newK $ From_Move oldK, Map.singleton oldK $ Just newK)
206− fromMaybe (error "PatchMapWithMove.patchThatChangesMap: impossible: fromKs was empty") $
205+ -- Run patchSingleValue on a These. Missing old or new sets are considered empty
207− Set.minView fromKs -- There's an existing value, but it's not here; move it here
206+ patchSingleValueThese :: v -> These (Set k) (Set k) -> (Map k (From k v), Map k (To k))
208− putRemainingKeys remainingKeys
207+ patchSingleValueThese v = \case
209− return $ NodeInfo (From_Move fromK) $ Just undefined
208+ This oldKeys -> patchSingleValue v oldKeys mempty
210− Map.traverseWithKey f newByIndex
209+ That newKeys -> patchSingleValue v mempty newKeys
211− unusedOldKeys = fold unusedValuesByValue
210+ These oldKeys newKeys -> patchSingleValue v oldKeys newKeys
212− pointlessMove k = \case
211+ -- Generate froms and tos for all values, then merge them together
213− From_Move k' | k == k' -> True
212+ (froms, tos) = foldl' unionPairDistinct mempty $ Map.mapWithKey patchSingleValueThese $ align (invert oldByIndex) (invert newByIndex)
214− _ -> False
213+ patch = unsafePatchMapWithMove $ align froms tos <&> \case
215− keyWasMoved k = if k `Map.member` oldByIndex && not (k `Set.member` unusedOldKeys)
214+ This from -> NodeInfo from Nothing -- Since we don't have a 'to' record for this key, that must mean it isn't being moved anywhere, so it should be deleted.
216− then Just undefined
215+ That to -> NodeInfo From_Delete to -- Since we don't have a 'from' record for this key, it must be getting deleted
217− else Nothing
216+ These from to -> NodeInfo from to
218− patch = unsafePatchMapWithMove $ Map.filterWithKey (\k -> not . pointlessMove k . _nodeInfo_from) $ Map.mergeWithKey (\k a _ -> Just $ nodeInfoSetTo (keyWasMoved k) a) (Map.mapWithKey $ \k -> nodeInfoSetTo $ keyWasMoved k) (Map.mapWithKey $ \k _ -> NodeInfo From_Delete $ keyWasMoved k) insertsAndMoves oldByIndex
220-- | Change the 'From' value of a 'NodeInfo'
218-- | Change the 'From' value of a 'NodeInfo'
221nodeInfoMapFrom :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
219nodeInfoMapFrom :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v