Skip to content

Commit

Permalink
Fixed double building.
Browse files Browse the repository at this point in the history
Incorporated performance enhancements from quad-tree-overlapping-struct
  • Loading branch information
DJGosnell committed Nov 29, 2023
1 parent d4a7892 commit 7d0f952
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build, Pack & Publish
on:
push:
branches:
- '*'
- 'master'
tags:
- 'v*'
pull_request:
Expand Down
1 change: 1 addition & 0 deletions src/DtronixCommon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{99E0DD64-D79C-4A3D-A7E8-A2810D212CE5}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
..\.github\workflows\dotnet.yml = ..\.github\workflows\dotnet.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DtronixCommonBenchmarks", "DtronixCommonBenchmarks\DtronixCommonBenchmarks.csproj", "{48AA31E7-0494-4FBA-8CD7-C00BDB0CB9A9}"
Expand Down
93 changes: 74 additions & 19 deletions src/DtronixCommon/Collections/Lists/Lists.tt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace DtronixCommon.Collections.Lists;
/// <summary>
/// Contains the data.
/// </summary>
private <#=config.NumberType#>[]? _data;
public <#=config.NumberType#>[]? Data;

/// <summary>
/// Number of fields which are used in the list. This number is multuplied
Expand Down Expand Up @@ -138,7 +138,7 @@ namespace DtronixCommon.Collections.Lists;
public <#=config.ClassName#>(int fieldCount, int capacity)
{
_numFields = fieldCount;
_data = new <#=config.NumberType#>[capacity];
Data = new <#=config.NumberType#>[capacity];
}

/// <summary>
Expand All @@ -151,7 +151,7 @@ namespace DtronixCommon.Collections.Lists;
public <#=config.NumberType#> Get(int index, int field)
{
Debug.Assert(index >= 0 && index < InternalCount && field >= 0 && field < _numFields);
return _data![index * _numFields + field];
return Data![index * _numFields + field];
}

/// <summary>
Expand All @@ -167,7 +167,7 @@ namespace DtronixCommon.Collections.Lists;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<<#=config.NumberType#>> Get(int index, int fieldStart, int fieldCount)
{
return new ReadOnlySpan<<#=config.NumberType#>>(_data, index * _numFields + fieldStart, fieldCount);
return new ReadOnlySpan<<#=config.NumberType#>>(Data, index * _numFields + fieldStart, fieldCount);
}

/// <summary>
Expand All @@ -190,7 +190,7 @@ namespace DtronixCommon.Collections.Lists;
public void Set(int index, int field, <#=config.NumberType#> value)
{
Debug.Assert(index >= 0 && index < InternalCount && field >= 0 && field < _numFields);
_data![index * _numFields + field] = value;
Data![index * _numFields + field] = value;
}

/// <summary>
Expand All @@ -212,15 +212,15 @@ namespace DtronixCommon.Collections.Lists;

// If the list is full, we need to reallocate the buffer to make room
// for the new element.
if (newPos > _data!.Length)
if (newPos > Data!.Length)
{
// Use double the size for the new capacity.
int newCap = newPos * 2;

// Allocate new array and copy former contents.
var newArray = new <#=config.NumberType#>[newCap];
Array.Copy(_data, newArray, _data.Length);
_data = newArray;
Array.Copy(Data, newArray, Data.Length);
Data = newArray;
}

return InternalCount++;
Expand All @@ -236,22 +236,77 @@ namespace DtronixCommon.Collections.Lists;

// If the list is full, we need to reallocate the buffer to make room
// for the new element.
if (newPos > _data!.Length)
if (newPos > Data!.Length)
{
// Use double the size for the new capacity.
int newCap = newPos * 2;

// Allocate new array and copy former contents.
var newArray = new <#=config.NumberType#>[newCap];
Array.Copy(_data, newArray, _data.Length);
_data = newArray;
Array.Copy(Data, newArray, Data.Length);
Data = newArray;
}

values.CopyTo(_data.AsSpan(InternalCount * _numFields));
values.CopyTo(Data.AsSpan(InternalCount * _numFields));

return InternalCount++;
}

/// <summary>
/// Inserts an element to the back of the list and adds the passed values to the data.
/// </summary>
/// <returns></returns>
public int PushBackCount(ReadOnlySpan<<#=config.NumberType#>> values, int count)
{
int newPos = (InternalCount + count) * _numFields;

// If the list is full, we need to reallocate the buffer to make room
// for the new element.
if (newPos > Data!.Length)
{
// Use double the size for the new capacity.
int newCap = newPos * 2;

// Allocate new array and copy former contents.
var newArray = new <#=config.NumberType#>[newCap];
Array.Copy(Data, newArray, Data.Length);
Data = newArray;
}

values.CopyTo(Data.AsSpan(InternalCount * _numFields));

var id = InternalCount;
InternalCount += count;
return id;
}


/// <summary>
/// Ensures that the list has enough space to accommodate a specified number of additional elements.
/// </summary>
/// <param name="count">The number of additional elements that the list needs to accommodate.</param>
/// <returns>The current count of elements in the list before the operation.</returns>
/// <remarks>
/// If the list does not have enough space, it reallocates the buffer, doubling its size, to make room for the new elements.
/// </remarks>
public void EnsureSpaceAvailable(int count)
{
int newPos = (InternalCount + count) * _numFields;

// If the list is full, we need to reallocate the buffer to make room
// for the new element.
if (newPos > Data!.Length)
{
// Use double the size for the new capacity.
int newCap = newPos * 2;

// Allocate new array and copy former contents.
var newArray = new <#=config.NumberType#>[newCap];
Array.Copy(Data, newArray, Data.Length);
Data = newArray;
}
}

/// <summary>
/// Removes the element at the back of the list.
/// </summary>
Expand All @@ -265,13 +320,13 @@ namespace DtronixCommon.Collections.Lists;
public void Increment(int index, int field)
{
Debug.Assert(index >= 0 && index < InternalCount && field >= 0 && field < _numFields);
_data![index * _numFields + field]++;
Data![index * _numFields + field]++;
}

public void Decrement(int index, int field)
{
Debug.Assert(index >= 0 && index < InternalCount && field >= 0 && field < _numFields);
_data![index * _numFields + field]--;
Data![index * _numFields + field]--;
}

/// <summary>
Expand All @@ -287,7 +342,7 @@ namespace DtronixCommon.Collections.Lists;
int pos = index * _numFields;

// Set the free index to the next free index.
_freeElement = (int)_data![pos];
_freeElement = (int)Data![pos];

// Return the free index.
return index;
Expand All @@ -310,10 +365,10 @@ namespace DtronixCommon.Collections.Lists;
int pos = index * _numFields;

// Set the free index to the next free index.
_freeElement = (int)_data![pos];
_freeElement = (int)Data![pos];

// Return the free index.
values.CopyTo(_data.AsSpan(index * _numFields));
values.CopyTo(Data.AsSpan(index * _numFields));
return index;
}

Expand All @@ -329,7 +384,7 @@ namespace DtronixCommon.Collections.Lists;
{
// Push the element to the free list.
int pos = index * _numFields;
_data![pos] = _freeElement;
Data![pos] = _freeElement;
_freeElement = index;
}

Expand All @@ -338,7 +393,7 @@ namespace DtronixCommon.Collections.Lists;
/// </summary>
public void Dispose()
{
_data = null;
Data = null;
}
}
<#
Expand Down
Loading

0 comments on commit 7d0f952

Please sign in to comment.