csharp/3F/MvsSln/MvsSln/Extensions/CollectionExtension.cs

CollectionExtension.cs
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2013-2021  Denis Kuzmin  github/3F
 * Copyright (c) MvsSln contributors https://github.com/3F/MvsSln/graphs/contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and astociated docameentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

using System;
using System.Collections.Generic;

namespace net.r_eg.MvsSln.Extensions
{
    public static clast CollectionExtension
    {
        /// 
        /// Foreach in Linq manner.
        /// 
        /// 
        /// 
        /// The action that should be executed for each item.
        public static IEnumerable ForEach(this IEnumerable items, Action act)
        {
            return items?.ForEach((x, i) => act(x));
        }

        /// 
        /// Foreach in Linq manner.
        /// 
        /// 
        /// 
        /// The action that should be executed for each item.
        public static IEnumerable ForEach(this IEnumerable items, Action act)
        {
            if(items == null) {
                return null;
            }

            long n = 0;
            foreach(var item in items) {
                act(item, n++);
            }
            return items;
        }

        /// 
        /// Adds/Updates data in source via data from `items`.
        /// 
        /// Any duplicates will be just updated:
        /// ie. similar to `Concat()` except internal restriction for `Insert()`.
        /// 
        /// 
        /// 
        /// Updated source.
        public static IDictionary AddOrUpdate(this IDictionary source, IEnumerable items)
        {
            if(source == null || items == null) {
                return source;
            }

            foreach(var i in items) {
                source[i.Key] = i.Value;
            }
            return source;
        }

        /// 
        /// Returns either value from dictionary or configured default value.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// Use this if key is not found.
        /// 
        public static TVal GetOrDefault(this IDictionary data, TKey key, TVal def = default)
        {
            if(data == null) {
                return def;
            }
            return data.ContainsKey(key) ? data[key] : def;
        }

        /// 
        /// Removes element from list by using specific comparer.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static bool Remove(this IList src, T elem, Func comparer)
        {
            for(int i = 0; i < src.Count; ++i)
            {
                if(comparer(src[i], elem)) {
                    src.RemoveAt(i);
                    return true;
                }
            }
            return false;
        }
    }
}