コード例 #1
0
        private SymMapBase Visit(SymMapBase m, SymMapBaseTraversal.Visit visit)
        {
            if (m_out != null)
            {
                m_out.WriteLine("visiting {0}", m.TypeName);
            }

            if (m_visited.ContainsKey(m))
            {
                //throw new InvalidOperationException(m.TypeName);
                m_visited[m] += 1;
                //Console.WriteLine(
                //    "cycle detected: map {0}, {1} visits", m.TypeName, m_visited[m]
                //    );
            }
            else
            {
                m_visited[m] = 1;
            }

            if (m.TypeName == m_name)
            {
                m_result.Add(m);
            }
            return(m);
        }
コード例 #2
0
ファイル: SymMapBase.cs プロジェクト: forki/aardvark.base
 /// <summary>
 /// Creates a shallow copy of the supplied map, but uses entries
 /// in the supplied override dictionary instead of map entries
 /// where they exist.
 /// </summary>
 public SymMapBase(SymMapBase map, SymbolDict<object> overrides)
     : this(map.m_typeName,
            overrides != null ? overrides.Copy() : new SymbolDict<object>())
 {
     if (overrides != null)
     {
         foreach (var item in map.m_ht)
         {
             if (overrides.Contains(item.Key)) continue;
             m_ht[item.Key] = item.Value;
         }
     }
     else
     {
         foreach (var item in map.m_ht)
             m_ht[item.Key] = item.Value;
     }
 }
コード例 #3
0
 public SymMapBase(SymMapBase map)
     : this(map.m_typeName, map.m_ht.Copy())
 {
 }
コード例 #4
0
        public SymMapBase Traverse(SymMapBase map)
        {
            // choose visitor
            SymMapBaseVisitor visitor = OnDefaultVisit;

            if (map.TypeName != null && m_perTypenameVisitors.ContainsKey(map.TypeName))
            {
                visitor = m_perTypenameVisitors[map.TypeName];
            }

            // pre visit
            if ((m_visit & Visit.Pre) != 0)
            {
                if (visitor != null)
                {
                    if (m_mode == Mode.Modifying)
                    {
                        map = visitor(map, Visit.Pre);
                    }
                    else
                    {
                        SymMapBase tmp = visitor(map, Visit.Pre);
                        if (tmp != map)
                        {
                            throw new ArgumentException(
                                      "The node returned by a non-modifying pre-visit is " +
                                      "different from the node that has been visited. " +
                                      "This makes no sense!"
                                      );
                        }
                    }
                }
            }

            // traverse children
            if (m_mode == Mode.Modifying)
            {
                SymbolDict <object> tmp = new SymbolDict <object>();
                foreach (var e in map.MapItems)
                {
                    tmp[e.Key] = e.Value;
                }

                foreach (var e in tmp)
                {
                    object o = e.Value;
                    if (o is SymMapBase)
                    {
                        map[e.Key] = Traverse((SymMapBase)o);
                    }
                    else if (o is SymMapBase[])
                    {
                        SymMapBase[] array = (SymMapBase[])o;
                        for (int i = 0; i < array.Length; i++)
                        {
                            array[i] = Traverse(array[i]);
                        }
                    }
                    else if (o is List <SymMapBase> )
                    {
                        List <SymMapBase> list = (List <SymMapBase>)o;
                        for (int i = 0; i < list.Count; i++)
                        {
                            list[i] = Traverse(list[i]);
                        }
                    }
                    else if (o is Dict <string, SymMapBase> )
                    {
                        var dict = new Dict <string, SymMapBase>();
                        foreach (var kvp in (Dict <string, SymMapBase>)o)
                        {
                            dict[kvp.Key] = Traverse(kvp.Value);
                        }
                        map[e.Key] = dict;
                    }
                    else if (o is SymbolDict <SymMapBase> )
                    {
                        var dict = new SymbolDict <SymMapBase>();
                        foreach (var kvp in (SymbolDict <SymMapBase>)o)
                        {
                            dict[kvp.Key] = Traverse(kvp.Value);
                        }
                        map[e.Key] = dict;
                    }
                    else if (o is DictSet <SymMapBase> )
                    {
                        var set = new DictSet <SymMapBase>();
                        foreach (var m in (DictSet <SymMapBase>)o)
                        {
                            set.Add(Traverse(m));
                        }
                        map[e.Key] = set;
                    }
                }
            }
            else
            {
                foreach (var e in map.MapItems)
                {
                    object o = e.Value;
                    if (o is SymMapBase)
                    {
                        Traverse((SymMapBase)o);
                    }
                    else if (o is SymMapBase[])
                    {
                        foreach (var m in (SymMapBase[])o)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is List <SymMapBase> )
                    {
                        foreach (var m in (List <SymMapBase>)o)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is Dict <string, SymMapBase> )
                    {
                        foreach (var m in ((Dict <string, SymMapBase>)o).Values)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is SymbolDict <SymMapBase> )
                    {
                        foreach (var m in ((SymbolDict <SymMapBase>)o).Values)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is DictSet <SymMapBase> )
                    {
                        foreach (var m in (DictSet <SymMapBase>)o)
                        {
                            Traverse(m);
                        }
                    }
                }
            }

            // post visit
            if ((m_visit & Visit.Post) != 0)
            {
                if (visitor != null)
                {
                    if (m_mode == Mode.Modifying)
                    {
                        map = visitor(map, Visit.Post);
                    }
                    else
                    {
                        var tmp = visitor(map, Visit.Post);
                        if (tmp != map)
                        {
                            throw new ArgumentException(
                                      "The node returned by a non-modifying post-visit is " +
                                      "different from the node that has been visited. " +
                                      "This makes no sense!"
                                      );
                        }
                    }
                }
            }

            return(map);
        }
コード例 #5
0
 public SymMapBaseCollectionTraversal(SymMapBase root, TextWriter debugOutput)
 {
     m_root = root;
     m_out  = debugOutput;
 }
コード例 #6
0
 public SymMapBaseCollectionTraversal(SymMapBase root)
 {
     m_root = root;
 }
コード例 #7
0
 public static List <SymMapBase> Collect(SymMapBase root, string typenameToCollect)
 {
     return(new SymMapBaseCollectionTraversal(root).Collect(typenameToCollect));
 }