Skip to content

Commit 8427ff8

Browse files
ddobrevtritao
authored andcommitted
Optimized the walking of the managed AST.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 760caf4 commit 8427ff8

14 files changed

+191
-155
lines changed

src/AST/ClassExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ public static Class GetInterface(this Class @class)
184184
Class @interface = null;
185185
if (specialization == null)
186186
{
187-
@interface = @class.Namespace.Classes.Find(
187+
@interface = @class.Namespace.Classes.FirstOrDefault(
188188
c => c.OriginalClass == @class && c.IsInterface);
189189
}
190190
else
191191
{
192192
Class template = specialization.TemplatedDecl.TemplatedClass;
193-
Class templatedInterface = @class.Namespace.Classes.Find(
193+
Class templatedInterface = @class.Namespace.Classes.FirstOrDefault(
194194
c => c.OriginalClass == template && c.IsInterface);
195195
if (templatedInterface != null)
196196
@interface = templatedInterface.Specializations.FirstOrDefault(

src/AST/DeclIterator.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/AST/DeclarationsList.cs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
4+
namespace CppSharp.AST
5+
{
6+
public class DeclarationsList : ObservableCollection<Declaration>
7+
{
8+
public IEnumerable<Namespace> Namespaces => OfType<Namespace>(Kind.Namespace);
9+
10+
public IEnumerable<Enumeration> Enums => OfType<Enumeration>(Kind.Enum);
11+
12+
public IEnumerable<Function> Functions => OfType<Function>(Kind.Function);
13+
14+
public IEnumerable<Class> Classes => OfType<Class>(Kind.Class);
15+
16+
public IEnumerable<Template> Templates => OfType<Template>(Kind.Template);
17+
18+
public IEnumerable<TypedefNameDecl> Typedefs => OfType<TypedefNameDecl>(Kind.Typedef);
19+
20+
public IEnumerable<Variable> Variables => OfType<Variable>(Kind.Variable);
21+
22+
public IEnumerable<Event> Events => OfType<Event>(Kind.Event);
23+
24+
public void AddRange(IEnumerable<Declaration> declarations)
25+
{
26+
foreach (Declaration declaration in declarations)
27+
{
28+
Add(declaration);
29+
}
30+
}
31+
32+
protected override void InsertItem(int index, Declaration item)
33+
{
34+
Kind kind = GetKind(item);
35+
int offset = GetOffset(kind);
36+
if (GetStart(kind) < index && index < offset)
37+
{
38+
base.InsertItem(index, item);
39+
}
40+
else
41+
{
42+
base.InsertItem(offset, item);
43+
}
44+
for (Kind i = kind; i <= Kind.Event; i++)
45+
{
46+
if (offsets.ContainsKey(i))
47+
{
48+
offsets[i]++;
49+
}
50+
}
51+
}
52+
53+
protected override void RemoveItem(int index)
54+
{
55+
base.RemoveItem(index);
56+
for (Kind i = Kind.Namespace; i <= Kind.Event; i++)
57+
{
58+
if (offsets.ContainsKey(i) && index < offsets[i])
59+
{
60+
offsets[i]--;
61+
}
62+
}
63+
}
64+
65+
private IEnumerable<T> OfType<T>(Kind kind) where T : Declaration
66+
{
67+
if (!offsets.ContainsKey(kind))
68+
{
69+
yield break;
70+
}
71+
int offset = offsets[kind];
72+
for (int i = GetStart(kind); i < offset; i++)
73+
{
74+
yield return (T) this[i];
75+
}
76+
}
77+
78+
private static Kind GetKind(Declaration item)
79+
{
80+
if (item is Namespace)
81+
return Kind.Namespace;
82+
if (item is Enumeration)
83+
return Kind.Enum;
84+
if (item is Function)
85+
return Kind.Function;
86+
if (item is Class)
87+
return Kind.Class;
88+
if (item is Template)
89+
return Kind.Template;
90+
if (item is TypedefNameDecl)
91+
return Kind.Typedef;
92+
if (item is Variable)
93+
return Kind.Variable;
94+
if (item is Friend)
95+
return Kind.Friend;
96+
if (item is Event)
97+
return Kind.Event;
98+
throw new System.ArgumentOutOfRangeException(nameof(item),
99+
"Unsupported type of declaration.");
100+
}
101+
102+
private int GetOffset(Kind kind)
103+
{
104+
if (!offsets.ContainsKey(kind))
105+
{
106+
for (Kind i = kind - 1; i >= Kind.Namespace; i--)
107+
{
108+
if (offsets.ContainsKey(i))
109+
{
110+
return offsets[kind] = offsets[i];
111+
}
112+
}
113+
offsets[kind] = 0;
114+
}
115+
return offsets[kind];
116+
}
117+
118+
private int GetStart(Kind kind)
119+
{
120+
for (Kind i = kind - 1; i >= Kind.Namespace; i--)
121+
{
122+
if (offsets.ContainsKey(i))
123+
{
124+
return offsets[i];
125+
}
126+
}
127+
return 0;
128+
}
129+
130+
private Dictionary<Kind, int> offsets = new Dictionary<Kind, int>();
131+
132+
private enum Kind
133+
{
134+
Namespace,
135+
Enum,
136+
Function,
137+
Class,
138+
Template,
139+
Typedef,
140+
Variable,
141+
Friend,
142+
Event
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)