Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sources/ClangSharp/Cursors/Decls/Decl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ private protected Decl(CXCursor handle, CXCursorKind expectedCursorKind, CX_Decl
_attrs = LazyList.Create<Attr>(Handle.NumAttrs, (i) => TranslationUnit.GetOrCreate<Attr>(Handle.GetAttr(unchecked((uint)i))));
_body = new ValueLazy<Stmt?>(() => !Handle.Body.IsNull ? TranslationUnit.GetOrCreate<Stmt>(Handle.Body) : null);
_canonicalDecl = new ValueLazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.CanonicalCursor));
_decls = LazyList.Create<Decl>(Handle.NumDecls, (i) => TranslationUnit.GetOrCreate<Decl>(Handle.GetDecl(unchecked((uint)i))));
_decls = LazyList.Create<Decl>(Handle.NumDecls, (i, previousDecl) => {
if (previousDecl is null)
{
return TranslationUnit.GetOrCreate<Decl>(Handle.GetDecl(unchecked((uint)i)));
}
else
{
return previousDecl.NextDeclInContext;
}
});
_describedTemplate = new ValueLazy<TemplateDecl?>(() => {
var describedTemplate = Handle.DescribedTemplate;
return describedTemplate.IsNull ? null : TranslationUnit.GetOrCreate<TemplateDecl>(describedTemplate);
Expand Down
10 changes: 10 additions & 0 deletions sources/ClangSharp/LazyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ internal static class LazyList
return new LazyList<T>(count, valueFactory);
}

public static LazyList<T> Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(int count, Func<int, T?, T> valueFactory)
where T : class
{
if (count <= 0)
{
return LazyList<T>.Empty;
}
return new LazyList<T>(count, valueFactory);
}

public static LazyList<T, TBase> Create<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TBase>(LazyList<TBase> list, int skip = -1, int take = -1)
where T : class, TBase
where TBase : class
Expand Down
38 changes: 20 additions & 18 deletions sources/ClangSharp/LazyList`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ internal sealed class LazyList<[DynamicallyAccessedMembers(DynamicallyAccessedMe
where T : class
{
internal readonly T[] _items;
internal readonly Func<int, T> _valueFactory;
internal readonly Func<int, T>? _valueFactory;
internal readonly Func<int, T?, T>? _valueFactoryWithPreviousValue;

public static readonly LazyList<T> Empty = new LazyList<T>(0, _ => null!);

public LazyList(int count, Func<int, T> valueFactory)
{
_items = (count <= 0) ? [] : new T[count];
_valueFactory = valueFactory;
_valueFactoryWithPreviousValue = null;
}

public LazyList(int count, Func<int, T?, T> valueFactoryWithPreviousValue)
{
_items = (count <= 0) ? [] : new T[count];
_valueFactory = null;
_valueFactoryWithPreviousValue = valueFactoryWithPreviousValue;
}

public T this[int index]
Expand All @@ -30,7 +39,14 @@ public T this[int index]

if (item is null)
{
item = _valueFactory(index);
if (_valueFactoryWithPreviousValue is not null)
{
item = _valueFactoryWithPreviousValue(index, index == 0 ? null : _items[index - 1]);
}
else
{
item = _valueFactory!.Invoke(index);
}
items[index] = item;
}

Expand All @@ -56,14 +72,7 @@ public void CopyTo(T[] array, int arrayIndex)

for (var i = 0; i < items.Length; i++)
{
var currentItem = items[i];

if (currentItem is null)
{
currentItem = _valueFactory(i);
items[i] = currentItem;
}

var currentItem = this[i];
array[arrayIndex + i] = currentItem;
}
}
Expand All @@ -76,14 +85,7 @@ public int IndexOf(T item)

for (var i = 0; i < items.Length; i++)
{
var currentItem = items[i];

if (currentItem is null)
{
currentItem = _valueFactory(i);
items[i] = currentItem;
}

var currentItem = this[i];
if (EqualityComparer<T>.Default.Equals(currentItem, item))
{
return i;
Expand Down
32 changes: 14 additions & 18 deletions sources/ClangSharp/LazyList`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal sealed class LazyList<T, [DynamicallyAccessedMembers(DynamicallyAccesse
where TBase : class
{
internal readonly TBase[] _items;
internal readonly Func<int, TBase> _valueFactory;
internal readonly Func<int, TBase>? _valueFactory;
internal readonly Func<int, TBase?, TBase>? _valueFactoryWithPreviousValue;

private readonly int _start;
private readonly int _count;
Expand All @@ -24,6 +25,7 @@ public LazyList(LazyList<TBase> list, int skip = -1, int take = -1)

_items = list._items;
_valueFactory = list._valueFactory;
_valueFactoryWithPreviousValue = list._valueFactoryWithPreviousValue;

_start = skip;
_count = take;
Expand All @@ -38,7 +40,15 @@ public T this[int index]

if (item is null)
{
item = _valueFactory(index + _start);
var idx = index + _start;
if (_valueFactoryWithPreviousValue is not null)
{
item = _valueFactoryWithPreviousValue(idx, idx == 0 ? null : _items[idx - 1]);
}
else
{
item = _valueFactory!.Invoke(idx);
}
items[index] = item;
}

Expand All @@ -64,14 +74,7 @@ public void CopyTo(T[] array, int arrayIndex)

for (var i = 0; i < _count; i++)
{
var currentItem = items[i];

if (currentItem is null)
{
currentItem = _valueFactory(i + _start);
items[i] = currentItem;
}

var currentItem = this[i];
array[arrayIndex + i] = (T)currentItem;
}
}
Expand All @@ -84,14 +87,7 @@ public int IndexOf(T item)

for (var i = 0; i < items.Length; i++)
{
var currentItem = items[i];

if (currentItem is null)
{
currentItem = _valueFactory(i + _start);
items[i] = currentItem;
}

var currentItem = this[i];
if (EqualityComparer<T>.Default.Equals((T)currentItem, item))
{
return i;
Expand Down