nOptimalStatesSignal ::
  (Ord a,Vec.Storage vec [Idx.State],Show (vec [Idx.State]),Show (vec Bool),
   Vec.Singleton vec,Show (vec a),Show a,
   Arith.Sum a,
   Vec.Zipper vec,
   Vec.Walker vec,
   Vec.Storage vec a,RealFloat a,
   Vec.Storage vec Bool) =>
  Balance.StateForcing ->
  Type.InterpolationOfAllStates node vec a ->
   Sig.UTSignal vec [Idx.State]
genOptimalStatesSignal statForcing interpolation =
  Map.foldlWithKey' (flip (Sig.zipWith . f)) emptyIndexSignal optStates
  where optStates = findOptimalObjectiveStates statForcing interpolation

        f st a b = a ++ (if b then [st] else [])

        time = Record.getTime
               $ Type.reqsAndDofsSignalsOfState
               $ ModUt.findMinElem interpolation

        emptyIndexSignal = Sig.untype $ Sig.map (const []) time


-- TODO test bauen::
-- TC (Data [0.0,0.3333333333333333,0.6666666666666666,1.0,1.25,1.5,1.75,2.0])
-- genOptimalTime (Sig.fromList [[Idx.State 0],[Idx.State 1, Idx.State 1, Idx.State 1, Idx.State 0],[Idx.State 1]]) (Sig.fromList [0,1,2]) :: Sig.TSignal [] Double


genOptimalStatesSignal ::
  (Ord a,Vec.Storage vec [Idx.State],Show (vec [Idx.State]),Show (vec Bool),
   Vec.Singleton vec,Show (vec a),Show a,
   Arith.Sum a,
   Vec.Zipper vec,
   Vec.Walker vec,
   Vec.Storage vec a,RealFloat a,
   Vec.Storage vec Bool) =>
  Balance.StateForcing ->
  Type.InterpolationOfAllStates node vec a ->
   Sig.UTSignal vec [Idx.State]
genOptimalStatesSignal statForcing interpolation =
  Map.foldlWithKey' (flip (Sig.zipWith . f)) emptyIndexSignal optStates
  where optStates = findOptimalObjectiveStates statForcing interpolation

        f st a b = a ++ (if b then [st] else [])

        time = Record.getTime
               $ Type.reqsAndDofsSignalsOfState
               $ ModUt.findMinElem interpolation

        emptyIndexSignal = Sig.untype $ Sig.map (const []) time


-- TODO test bauen::
-- TC (Data [0.0,0.3333333333333333,0.6666666666666666,1.0,1.25,1.5,1.75,2.0])
-- genOptimalTime (Sig.fromList [[Idx.State 0],[Idx.State 1, Idx.State 1, Idx.State 1, Idx.State 0],[Idx.State 1]]) (Sig.fromList [0,1,2]) :: Sig.TSignal [] Double
genOptimalStatesSignal statForcing interpolation =
  Map.foldlWithKey' (flip (Sig.zipWith . f)) emptyIndexSignal optStates
  where optStates = findOptimalObjectiveStates statForcing interpolation

        f st a b = a ++ (if b then [st] else [])

        time = Record.getTime
               $ Type.reqsAndDofsSignalsOfState
               $ ModUt.findMinElem interpolation

        emptyIndexSignal = Sig.untype $ Sig.map (const []) time


-- TODO test bauen::
-- TC (Data [0.0,0.3333333333333333,0.6666666666666666,1.0,1.25,1.5,1.75,2.0])
-- genOptimalTime (Sig.fromList [[Idx.State 0],[Idx.State 1, Idx.State 1, Idx.State 1, Idx.State 0],[Idx.State 1]]) (Sig.fromList [0,1,2]) :: Sig.TSignal [] Double