Esempio n. 1
0
 public static InputMap <TId, TValue> Filter <TId, TValue>(this InputMap <TId, TValue> source,
                                                           Func <TId, bool> perdicate)
 {
     return(source.Source
            .Where(kvPair => perdicate(kvPair.Key))
            .ToInputMap());
 }
Esempio n. 2
0
 public static InputMap <TTarget, TValue> ApplyMapping <TTarget, TSource, TValue>(
     this InputMapping <TSource, TTarget> inputMapping,
     InputMap <TSource, TValue> inputSource,
     Func <TValue[], TValue> merge)
 {
     return(inputMapping.Mappings
            .Select(kvPair => {
         var targetId = kvPair.Key;
         var sourceIds = kvPair.Value;
         var sources = new List <Func <TValue> >();
         foreach (var sourceId in sourceIds)
         {
             Func <TValue> source;
             if (inputSource.Source.TryGetValue(sourceId, out source))
             {
                 sources.Add(source);
             }
         }
         if (sources.Count > 0)
         {
             var mergedSources = ImperoCore.MergePollFns(merge, sources);
             return new KeyValuePair <TTarget, Func <TValue> >(targetId, mergedSources);
         }
         return new KeyValuePair <TTarget, Func <TValue> >(targetId, null);
     })
            .Where(kvPair => kvPair.Value != null)
            .ToDictionary()
            .ToInputMap());
 }
Esempio n. 3
0
 /// <summary>
 ///     Adapts all poll functions in the InputMap providing a new adapter for each poll function.
 ///     Useful if you're adapters are stateful and store state per poll function.
 /// </summary>
 public static InputMap <TInputId, TDest> Adapt <TInputId, TSource, TDest>(
     this InputMap <TInputId, TSource> inputMap, Func <Func <TSource, TDest> > adapterProvider)
 {
     return(inputMap.Source
            .ChangeValues(pollFn => Adapt(pollFn, adapterProvider()))
            .ToInputMap());
 }
Esempio n. 4
0
 /// <summary>
 ///     Adapts all poll functions in the InputMap
 /// </summary>
 public static InputMap <TInputId, TDest> Adapt <TInputId, TSource, TDest>(
     this InputMap <TInputId, TSource> inputMap, Func <TInputId, Func <TSource>, Func <TDest> > adaptInput)
 {
     return(inputMap.Source.Select(sourceEntry => {
         TInputId pollKey = sourceEntry.Key;
         Func <TDest> adaptedPollFn = adaptInput(sourceEntry.Key, sourceEntry.Value);
         return new KeyValuePair <TInputId, Func <TDest> >(pollKey, adaptedPollFn);
     }).ToInputMap());
 }
Esempio n. 5
0
        public static Func <TValue> Merge <TId, TValue>(this InputMap <TId, TValue> inputMap,
                                                        Func <TValue[], TValue> merge,
                                                        params TId[] ids)
        {
            var pollFns = inputMap.Source
                          .Where(pollPair => ids.Contains(pollPair.Key))
                          .Select(pollPair => pollPair.Value);

            return(MergePollFns(merge, pollFns));
        }
Esempio n. 6
0
        public static InputMap <TInputId, TValue> Adapt <TInputId, TValue>(this InputMap <TInputId, TValue> inputMap,
                                                                           TInputId id, Func <TValue, TValue> adapter)
        {
            Func <TValue> pollFn;

            if (inputMap.Source.TryGetValue(id, out pollFn))
            {
                inputMap = inputMap
                           .Source
                           .SetItem(id, pollFn.Adapt(adapter))
                           .ToInputMap();
            }
            return(inputMap);
        }
Esempio n. 7
0
        public static InputMap <TId, TValue> FillEmptyValues <TId, TValue>(this InputMap <TId, TValue> inputMap,
                                                                           IEnumerable <TId> ids,
                                                                           Func <TValue> identityPollFn)
        {
            var map = inputMap.Source;

            foreach (var id in ids)
            {
                if (!map.ContainsKey(id))
                {
                    //Debug.LogWarning("Filling empty action " + id);
                    map = map.Add(id, identityPollFn);
                }
            }
            return(map.ToInputMap());
        }
Esempio n. 8
0
 /// <summary>
 ///     Change all the id's inside an InputMap and return a new InputMap with all Id's changed.
 /// </summary>
 public static InputMap <TDestId, TValue> ChangeId <TSourceId, TDestId, TValue>(
     this InputMap <TSourceId, TValue> inputMap, Func <TSourceId, TDestId> idConverter)
 {
     return(inputMap.Source.ChangeKeys(idConverter).ToInputMap());
 }
Esempio n. 9
0
 public static InputMap <TInputId, TValue> Update <TInputId, TValue>(this InputMap <TInputId, TValue> inputMap,
                                                                     TInputId id, Func <TValue> pollFn)
 {
     return(inputMap.Source.SetItem(id, pollFn).ToInputMap());
 }
Esempio n. 10
0
 /// <summary>
 ///     Replaces the dictionary inside the input map with a dictionary that has fast and garbage-less lookup.
 ///     Warning, works only for Enum types.
 /// </summary>
 public static InputMap <TInputId, TValue> Optimize <TInputId, TValue>(this InputMap <TInputId, TValue> inputMap)
     where TInputId : struct, IComparable, IConvertible, IFormattable
 {
     return(new InputMap <TInputId, TValue>(inputMap.Source.ToFastImmutableEnumDictionary()));
 }