コード例 #1
0
ファイル: MapHelper.cs プロジェクト: tmaierhofer/grgen
        /// <summary>
        /// Creates a new list representing an array,
        /// containing all values from the given dictionary representing a map <paramref name="map"/> from int to some values.
        /// </summary>
        /// <param name="map">A dictionary representing a map.</param>
        /// <returns>A new list containing all values from <paramref name="map"/>.</returns>
        public static IList MapAsArray(IDictionary map, IGraphModel model)
        {
            int max = 0;

            foreach (int i in map.Keys)
            {
                if (i < 0)
                {
                    throw new Exception("MapAsArray does not support negative indices");
                }
                max = Math.Max(max, i);
            }

            Type keyType;
            Type valueType;

            ContainerHelper.GetDictionaryTypes(map, out keyType, out valueType);
            IList newList = NewList(valueType, max + 1); // yep, if the dict contains max int, contiguous 8GB are needed

            // Add all values of dictionary representing map to new dictionary representing set
            for (int i = 0; i < max + 1; ++i)
            {
                if (map.Contains(i))
                {
                    newList.Add(map[i]);
                }
                else
                {
                    newList.Add(TypesHelper.DefaultValue(valueType.Name, model));
                }
            }

            return(newList);
        }