示例#1
0
        /// <summary>
        ///     For every node in the list, pushes the node down in the tree to
        ///     at highest be "filterNode". For example, if World and Sweden was
        ///     passed with Europe as filterNode, Europe and Sweden would be
        ///     returned.
        /// </summary>
        /// <param name="nodes">The list of nodes.</param>
        /// <param name="topGeography">The highest node to return.</param>
        /// <returns>The filtered list.</returns>
        public Geographies FilterAbove(Geography topGeography)
        {
            Geographies result = new Geographies();

            // Build lookup

            Geographies            filterLine = topGeography.GetLine();
            Dictionary <int, bool> lineLookup = new Dictionary <int, bool>();

            foreach (Geography node in filterLine)
            {
                lineLookup[node.Identity] = true;
            }

            // Filter

            foreach (Geography node in this)
            {
                if (lineLookup.ContainsKey(node.Identity))
                {
                    // Above cutoff point, add filter node

                    result.Add(topGeography);
                }
                else
                {
                    result.Add(node);
                }
            }

            return(result);
        }
示例#2
0
        public static Geographies FromSingle(Geography geography)
        {
            Geographies result = new Geographies();

            result.Add(geography);
            return(result);
        }
示例#3
0
        public Geographies RemoveRedundant()
        {
            Dictionary <int, Geography> remaining = new Dictionary <int, Geography>();

            foreach (Geography geo in this)
            {
                remaining[geo.Identity] = geo;
            }

            // O(n^3).

            // For each node, get the node line, and if any other org in collection
            // is a parent of the current org, the take the current org out of the collection.

            foreach (Geography currentNode in this)
            {
                Geographies currentLine = Geography.FromIdentity(currentNode.GeographyId).GetLine();

                foreach (Geography comparedNode in this)
                {
                    if (comparedNode.Identity == currentNode.Identity)
                    {
                        // Do not compare to ourselves
                        continue;
                    }

                    // When comparing to another node, check against all parents of
                    // the current node.

                    for (int index = 0; index < currentLine.Count - 1; index++)
                    {
                        if (currentLine[index].Identity == comparedNode.Identity)
                        {
                            // the compared node is a parent of the current node.
                            // Therefore, the current node is already present in the collection,
                            // as a child of the compared node. It can be safely removed.

                            remaining.Remove(currentNode.GeographyId);
                            break;
                        }
                    }
                }
            }

            // Assemble result

            Geographies result = new Geographies();

            foreach (int nodeId in remaining.Keys)
            {
                result.Add(remaining[nodeId]);
            }

            return(result);
        }
示例#4
0
        public static Geographies FromArray(BasicGeography[] basicArray)
        {
            Geographies result = new Geographies();

            result.Capacity = basicArray.Length * 11 / 10;
            foreach (BasicGeography basic in basicArray)
            {
                result.Add(Geography.FromBasic(basic));
            }

            return(result);
        }
示例#5
0
        public static Geographies LogicalOr(Geographies set1, Geographies set2)
        {
            // If either set is invalid, return the other
            // (a null is different from an empty set)

            if (set1 == null)
            {
                return(set2);
            }

            if (set2 == null)
            {
                return(set1);
            }

            // Build table, eliminating duplicates

            Dictionary <int, Geography> table = new Dictionary <int, Geography>();

            foreach (Geography geography in set1)
            {
                table[geography.Identity] = geography;
            }

            foreach (Geography geography in set2)
            {
                table[geography.Identity] = geography;
            }

            // Assemble result

            Geographies result = new Geographies();

            foreach (Geography geography in table.Values)
            {
                result.Add(geography);
            }

            return(result);
        }