Skip to content

Commit

Permalink
Improved code generation. Added IEnumerable implementation to new ent…
Browse files Browse the repository at this point in the history
…ity tables. Added ElementIndexMap to EntityTableSet.
  • Loading branch information
mavimaec committed Dec 5, 2024
1 parent c4fe093 commit 6807524
Show file tree
Hide file tree
Showing 4 changed files with 531 additions and 69 deletions.
19 changes: 17 additions & 2 deletions src/cs/vim/Vim.Format.CodeGen/ObjectModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ private static void WriteEntityTableSet(CodeBuilder cb)
cb.AppendLine("private SerializableEntityTable GetRawTableOrDefault(string tableName)");
cb.AppendLine(" => RawTableMap.TryGetValue(tableName, out var result) ? result : null;");
cb.AppendLine();
cb.AppendLine("public ElementIndexMaps ElementIndexMaps { get; }");
cb.AppendLine();

cb.AppendLine("public EntityTableSet(SerializableEntityTable[] rawTables, string[] stringBuffer)");
cb.AppendLine("public EntityTableSet(SerializableEntityTable[] rawTables, string[] stringBuffer, bool inParallel = true)");
cb.AppendLine("{");
cb.AppendLine("foreach (var rawTable in rawTables)");
cb.AppendLine(" RawTableMap[rawTable.Name] = rawTable;");
Expand All @@ -314,6 +316,9 @@ private static void WriteEntityTableSet(CodeBuilder cb)
cb.AppendLine($" {t.Name}Table = new {t.Name}Table({tmp}, stringBuffer);");
cb.AppendLine();
}
cb.AppendLine("// Initialize element index maps");
cb.AppendLine("ElementIndexMaps = new ElementIndexMaps(this, inParallel);");
cb.AppendLine();
cb.AppendLine("} // EntityTableSet constructor");

cb.AppendLine();
Expand All @@ -322,6 +327,7 @@ private static void WriteEntityTableSet(CodeBuilder cb)
cb.AppendLine($"public {t.Name}Table {t.Name}Table {{ get; }} // can be null");
cb.AppendLine($"public {t.Name} Get{t.Name}(int index) => {t.Name}Table?.Get(index);");
}

cb.AppendLine("} // class EntityTableSet");
cb.AppendLine();

Expand All @@ -334,7 +340,7 @@ private static void WriteEntityTable(CodeBuilder cb, Type t)
var entityFields = t.GetEntityFields().ToArray();
var relationFields = t.GetRelationFields().ToArray();

cb.AppendLine($"public partial class {t.Name}Table : EntityTable_v2");
cb.AppendLine($"public partial class {t.Name}Table : EntityTable_v2, IEnumerable<{t.Name}>");
cb.AppendLine("{");
cb.AppendLine("private readonly EntityTableSet _parentTableSet; // can be null");
cb.AppendLine();
Expand Down Expand Up @@ -413,6 +419,14 @@ private static void WriteEntityTable(CodeBuilder cb, Type t)
cb.AppendLine("return r;");
cb.AppendLine("}");

cb.AppendLine("// Enumerator");
cb.AppendLine("IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();");
cb.AppendLine($"public IEnumerator<{t.Name}> GetEnumerator()");
cb.AppendLine("{");
cb.AppendLine("for (var i = 0; i < RowCount; ++i)");
cb.AppendLine(" yield return Get(i);");
cb.AppendLine("}");

cb.AppendLine($"}} // class {t.Name}Table ");
cb.AppendLine();
}
Expand Down Expand Up @@ -488,6 +502,7 @@ public static void WriteDocument(string file)
cb.AppendLine("// AUTO-GENERATED FILE, DO NOT MODIFY.");
cb.AppendLine("// ReSharper disable All");
cb.AppendLine("using System;");
cb.AppendLine("using System.Collections;");
cb.AppendLine("using System.Collections.Generic;");
cb.AppendLine("using System.Linq;");
cb.AppendLine("using Vim.Math3d;");
Expand Down
37 changes: 25 additions & 12 deletions src/cs/vim/Vim.Format.Core/EntityTable_v2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using Vim.BFast;
using Vim.Util;

namespace Vim.Format
{
public class EntityTable_v2
{
private readonly Dictionary<string, NamedBuffer<int>> _indexColumnMap = new Dictionary<string, NamedBuffer<int>>();
private readonly Dictionary<string, NamedBuffer<int>> _stringColumnMap = new Dictionary<string, NamedBuffer<int>>();
private readonly Dictionary<string, INamedBuffer> _dataColumnMap = new Dictionary<string, INamedBuffer>();
private readonly string[] _stringBuffer;

public Dictionary<string, NamedBuffer<int>> IndexColumns { get; } = new Dictionary<string, NamedBuffer<int>>();
public Dictionary<string, NamedBuffer<int>> StringColumns { get; } = new Dictionary<string, NamedBuffer<int>>();
public Dictionary<string, INamedBuffer> DataColumns { get; } = new Dictionary<string, INamedBuffer>();

/// <summary>
/// The entity table name
/// </summary>
Expand Down Expand Up @@ -39,13 +41,13 @@ public EntityTable_v2(
RowCount = Columns.FirstOrDefault()?.NumElements() ?? 0;

foreach (var column in et.IndexColumns)
_indexColumnMap[column.Name] = column;
IndexColumns[column.Name] = column;

foreach (var column in et.StringColumns)
_stringColumnMap[column.Name] = column;
StringColumns[column.Name] = column;

foreach (var column in et.DataColumns)
_dataColumnMap[column.Name] = column;
DataColumns[column.Name] = column;

_stringBuffer = stringBuffer;
}
Expand All @@ -57,16 +59,27 @@ private static T GetColumnOrDefault<T>(Dictionary<string, T> map, string key, T
/// Returns the index column based on the given column name.
/// </summary>
public int[] GetIndexColumnValues(string columnName)
=> GetColumnOrDefault(_indexColumnMap, columnName)?.GetColumnValues<int>();
=> GetColumnOrDefault(IndexColumns, columnName)?.GetColumnValues<int>();

/// <summary>
/// Returns the string column based on the given column name.
/// </summary>
public string[] GetStringColumnValues(string columnName)
=> GetColumnOrDefault(_stringColumnMap, columnName)
?.GetColumnValues<int>()
?.Select(i => _stringBuffer[i])
.ToArray();
{
var stringIndices = GetColumnOrDefault(StringColumns, columnName)
?.GetColumnValues<int>() ?? Array.Empty<int>();

var strings = new string[stringIndices.Length];

for (var i = 0; i < strings.Length; i++)
{
strings[i] = _stringBuffer == null || _stringBuffer.Length == 0
? "" // Guard against the case where the string buffer is null or empty.
: _stringBuffer.ElementAtOrDefault(stringIndices[i], "");
}

return strings;
}

/// <summary>
/// Returns the data column based on the given column name.
Expand All @@ -78,7 +91,7 @@ public T[] GetDataColumnValues<T>(string columnName) where T : unmanaged
if (!ColumnExtensions.DataColumnTypes.Contains(type))
throw new Exception($"{nameof(GetDataColumnValues)} error - unsupported data column type {type}");

var namedBuffer = GetColumnOrDefault(_dataColumnMap, columnName);
var namedBuffer = GetColumnOrDefault(DataColumns, columnName);
if (namedBuffer == null)
return null;

Expand Down
50 changes: 50 additions & 0 deletions src/cs/vim/Vim.Format/ObjectModel/ElementIndexMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ public ElementIndexMaps(DocumentModel documentModel, bool inParallel = true)
}
}

public ElementIndexMaps(EntityTableSet entityTables, bool inParallel = true)
{
var actions = new Action[]
{
() => FamilyInstanceIndexFromElementIndex = GetElementIndexMap(entityTables.FamilyInstanceTable),
() => FamilyTypeIndexFromElementIndex = GetElementIndexMap(entityTables.FamilyTypeTable),
() => FamilyIndexFromElementIndex = GetElementIndexMap(entityTables.FamilyTable),
() => ViewIndexFromElementIndex = GetElementIndexMap(entityTables.ViewTable),
() => AssemblyIndexFromElementIndex = GetElementIndexMap(entityTables.AssemblyInstanceTable),
() => DesignOptionIndexFromElementIndex = GetElementIndexMap(entityTables.DesignOptionTable),
() => LevelIndexFromElementIndex = GetElementIndexMap(entityTables.LevelTable),
() => PhaseIndexFromElementIndex = GetElementIndexMap(entityTables.PhaseTable),
() => RoomIndexFromElementIndex = GetElementIndexMap(entityTables.RoomTable),
() => ParameterIndicesFromElementIndex = GetElementIndicesMap(entityTables.ParameterTable),
() => SystemIndexFromElementIndex = GetElementIndexMap(entityTables.SystemTable)
};

if (inParallel)
{
Parallel.Invoke(actions);
}
else
{
foreach (var action in actions)
action.Invoke();
}
}

public static readonly string ElementIndexColumnName = ColumnExtensions.GetIndexColumnName(TableNames.Element, nameof(Element));

public static DictionaryOfLists<int, int> GetElementIndicesMap(EntityTable et)
Expand All @@ -70,6 +98,17 @@ public static DictionaryOfLists<int, int> GetElementIndicesMap(EntityTable et)
return indicesMap;
}

public static DictionaryOfLists<int, int> GetElementIndicesMap(EntityTable_v2 et)
{
var indicesMap = new DictionaryOfLists<int, int>();
var elementIndices = et?.IndexColumns[ElementIndexColumnName]?.GetTypedData();
if (elementIndices == null)
return indicesMap;
for (var i = 0; i < elementIndices.Length; ++i)
indicesMap.Add(elementIndices[i], i);
return indicesMap;
}

public static IndexMap GetElementIndexMap(EntityTable et)
{
var indexMap = new IndexMap();
Expand All @@ -80,5 +119,16 @@ public static IndexMap GetElementIndexMap(EntityTable et)
indexMap.TryAdd(elementIndices[i], i);
return indexMap;
}

public static IndexMap GetElementIndexMap(EntityTable_v2 et)
{
var indexMap = new IndexMap();
var elementIndices = et?.IndexColumns[ElementIndexColumnName]?.GetTypedData();
if (elementIndices == null)
return indexMap;
for (var i = 0; i < elementIndices.Length; ++i)
indexMap.TryAdd(elementIndices[i], i);
return indexMap;
}
}
}
Loading

0 comments on commit 6807524

Please sign in to comment.