{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wall -fno-warn-tabs #-}

module Data.Sequences.ToolsYj (indexing) where

import Data.MonoTraversable qualified as Mono
import Data.Sequences qualified as Seq
import Data.Map qualified as M

indexing :: (
	Mono.MonoFoldable seq, Mono.Element seq ~ Mono.Element seq',
	Seq.IsSequence seq', Ord (Mono.Element seq),
	Seq.IsSequence seqi, Enum (Mono.Element seqi) ) => seq -> (seq', seqi)
indexing :: forall seq seq' seqi.
(MonoFoldable seq, Element seq ~ Element seq', IsSequence seq',
 Ord (Element seq), IsSequence seqi, Enum (Element seqi)) =>
seq -> (seq', seqi)
indexing (seq -> [Element seq]
forall mono. MonoFoldable mono => mono -> [Element mono]
Mono.otoList -> [Element seq]
xs) = let
	(Map Int (Element seq)
xs', [Int]
is) = Int
-> Map (Element seq) Int
-> [Element seq]
-> (Map Int (Element seq), [Int])
forall a. Ord a => Int -> Map a Int -> [a] -> (Map Int a, [Int])
indexingGo Int
0 Map (Element seq) Int
forall k a. Map k a
M.empty [Element seq]
xs in
	([Element seq'] -> seq'
forall seq. IsSequence seq => [Element seq] -> seq
Seq.fromList ([Element seq'] -> seq') -> [Element seq'] -> seq'
forall a b. (a -> b) -> a -> b
$ Map Int (Element seq') -> [Element seq']
forall k a. Map k a -> [a]
M.elems Map Int (Element seq)
Map Int (Element seq')
xs', [Element seqi] -> seqi
forall seq. IsSequence seq => [Element seq] -> seq
Seq.fromList ([Element seqi] -> seqi) -> [Element seqi] -> seqi
forall a b. (a -> b) -> a -> b
$ Int -> Element seqi
forall a. Enum a => Int -> a
toEnum (Int -> Element seqi) -> [Int] -> [Element seqi]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int]
is)

indexingGo :: Ord a => Int -> M.Map a Int -> [a] -> (M.Map Int a, [Int])
indexingGo :: forall a. Ord a => Int -> Map a Int -> [a] -> (Map Int a, [Int])
indexingGo Int
_ Map a Int
_ [] = (Map Int a
forall k a. Map k a
M.empty, [])
indexingGo Int
idx Map a Int
rvdct (a
x : [a]
xs) = case a -> Map a Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup a
x Map a Int
rvdct of
	Maybe Int
Nothing -> let
		(Map Int a
dct, [Int]
is) = Int -> Map a Int -> [a] -> (Map Int a, [Int])
forall a. Ord a => Int -> Map a Int -> [a] -> (Map Int a, [Int])
indexingGo (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> Int -> Map a Int -> Map a Int
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert a
x Int
idx Map a Int
rvdct) [a]
xs in
		(Int -> a -> Map Int a -> Map Int a
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Int
idx a
x Map Int a
dct, Int
idx Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
is)
	Just Int
i -> let (Map Int a
dct, [Int]
is) = Int -> Map a Int -> [a] -> (Map Int a, [Int])
forall a. Ord a => Int -> Map a Int -> [a] -> (Map Int a, [Int])
indexingGo Int
idx Map a Int
rvdct [a]
xs in (Map Int a
dct, Int
i Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
is)