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
29 changes: 13 additions & 16 deletions src/TMG-Framework/Construct/ConstructTimePeriodFromTimes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with TMG-Framework for XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;

using XTMF2;

namespace TMG.Construct
namespace TMG.Construct;

[Module(Name = "Construct Time Period from Times", Description = "Given the start and end times construct a time period.",
DocumentationLink = "http://tmg.utoronto.ca/doc/2.0")]
public sealed class ConstructTimePeriodFromTimes : BaseFunction<TimePeriod>
{
[Module(Name = "Construct Time Period from Times", Description = "Given the start and end times construct a time period.",
DocumentationLink = "http://tmg.utoronto.ca/doc/2.0")]
public sealed class ConstructTimePeriodFromTimes : BaseFunction<TimePeriod>
{
[Parameter(Index = 0, Name = "Start Time", Required = true, Description = "The time to use as the starting point of the time period (Inclusive).")]
public IFunction<Time> StartTime = null!;
[Parameter(Index = 0, Name = "Start Time", Required = true, Description = "The time to use as the starting point of the time period (Inclusive).")]
public IFunction<Time> StartTime = null!;

[Parameter(Index = 1, Name = "End Time", Required = true, Description = "The time to use as the ending point of the time period (Exclusive).")]
public IFunction<Time> EndTime = null!;
[Parameter(Index = 1, Name = "End Time", Required = true, Description = "The time to use as the ending point of the time period (Exclusive).")]
public IFunction<Time> EndTime = null!;

public override TimePeriod Invoke()
{
return new TimePeriod(StartTime.Invoke(), EndTime.Invoke());
}
public override TimePeriod Invoke()
{
return new TimePeriod(StartTime.Invoke(), EndTime.Invoke());
}
}
9 changes: 3 additions & 6 deletions src/TMG-Framework/Convert/ConvertTimesToTimePeriod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with TMG-Framework for XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;

using XTMF2;

namespace TMG.Convert
{
namespace TMG.Convert;

[Module(Name = "Convert Times to Time Period", Description = "Takes in a start and end time to generate a time period.",
DocumentationLink = "http://tmg.utoronto.ca/doc/2.0")]
public sealed class ConvertTimesToTimePeriod : BaseFunction<(Time start, Time end), TimePeriod>
Expand All @@ -32,4 +30,3 @@ public override TimePeriod Invoke((Time start, Time end) context)
return new TimePeriod(context.start, context.end);
}
}
}
188 changes: 91 additions & 97 deletions src/TMG-Framework/Data/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,128 +16,122 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with TMG-Framework for XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static TMG.Utilities.ExceptionHelper;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace TMG
namespace TMG;

/// <summary>
///
/// </summary>
public sealed class Categories : IEnumerable<int>
{
/// <summary>
///
/// Get the number of elements in the categories.
/// </summary>
public sealed class Categories : IEnumerable<int>
{
/// <summary>
/// Get the number of elements in the categories.
/// </summary>
public int Count => _elements.Count;
public int Count => _elements.Count;

/// <summary>
/// TODO: Update the representation later on to something more efficient
/// </summary>
private readonly List<int> _elements;
/// <summary>
/// TODO: Update the representation later on to something more efficient
/// </summary>
private readonly List<int> _elements;

/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The list of elements to use, values will be sorted, any duplicates will return in failure.</param>
/// <param name="error">The error message if creation fails</param>
/// <returns>True if creation succeeds, false otherwise</returns>
public static bool CreateCategories(List<int> elements,
[NotNullWhen(true)] out Categories? categories,
[NotNullWhen(false)] ref string? error)
/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The list of elements to use, values will be sorted, any duplicates will return in failure.</param>
/// <param name="error">The error message if creation fails</param>
/// <returns>True if creation succeeds, false otherwise</returns>
public static bool CreateCategories(List<int> elements,
[NotNullWhen(true)] out Categories? categories,
[NotNullWhen(false)] ref string? error)
{
elements = elements?.ToList() ?? throw new ArgumentNullException(nameof(elements));
elements.Sort();
for (int i = 1; i < elements.Count; i++)
{
elements = elements?.ToList() ?? throw new ArgumentNullException(nameof(elements));
elements.Sort();
for (int i = 1; i < elements.Count; i++)
if (elements[i - 1] == elements[i])
{
if(elements[i - 1] == elements[i])
{
error = $"Found a duplicate category {elements[i]}!";
categories = null;
return false;
}
error = $"Found a duplicate category {elements[i]}!";
categories = null;
return false;
}
categories = new Categories(elements);
return true;
}
categories = new Categories(elements);
return true;
}

/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The list of elements to use, values will be sorted, any duplicates will return in failure.</param>
/// <param name="error">The error message if creation fails</param>
/// <returns>True if creation succeeds, false otherwise</returns>
public static bool CreateCategories(Span<int> elements,
[NotNullWhen(true)] out Categories? categories,
[NotNullWhen(false)] ref string? error)
/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The list of elements to use, values will be sorted, any duplicates will return in failure.</param>
/// <param name="error">The error message if creation fails</param>
/// <returns>True if creation succeeds, false otherwise</returns>
public static bool CreateCategories(Span<int> elements,
[NotNullWhen(true)] out Categories? categories,
[NotNullWhen(false)] ref string? error)
{
var list = new List<int>(elements.ToArray());
list.Sort();
for (int i = 1; i < list.Count; i++)
{
var list = new List<int>(elements.ToArray());
list.Sort();
for (int i = 1; i < list.Count; i++)
if (list[i - 1] == list[i])
{
if(list[i - 1] == list[i])
{
error = $"Found a duplicate category {list[i]}!";
categories = null;
return false;
}
error = $"Found a duplicate category {list[i]}!";
categories = null;
return false;
}
categories = new Categories(list);
return true;
}
categories = new Categories(list);
return true;
}

/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The sorted list of elements to use</param>
private Categories(List<int> elements)
/// <summary>
/// Create a Categories instance from a list of elements
/// </summary>
/// <param name="elements">The sorted list of elements to use</param>
private Categories(List<int> elements)
{
if (elements == null)
{
if(elements == null)
{
ThrowParameterNull(nameof(elements));
}
_elements = elements;
ThrowParameterNull(nameof(elements));
}
_elements = elements;
}

/// <summary>
/// Gives the flat index of the specified sparse index, or less than 0 if the sparse index is not in the map.
/// </summary>
/// <param name="sparseIndex">The sparse index to look up</param>
/// <returns>Gives the flat index of the specified sparse index, or less than 0 if the sparse index is not in the map.</returns>
public int GetFlatIndex(CategoryIndex sparseIndex)
{
return _elements.BinarySearch(sparseIndex);
}
/// <summary>
/// Gives the flat index of the specified sparse index, or less than 0 if the sparse index is not in the map.
/// </summary>
/// <param name="sparseIndex">The sparse index to look up</param>
/// <returns>Gives the flat index of the specified sparse index, or less than 0 if the sparse index is not in the map.</returns>
public int GetFlatIndex(CategoryIndex sparseIndex)
{
return _elements.BinarySearch(sparseIndex);
}

/// <summary>
///
/// </summary>
/// <param name="flatIndex"></param>
/// <returns></returns>
public CategoryIndex GetSparseIndex(int flatIndex)
/// <summary>
///
/// </summary>
/// <param name="flatIndex"></param>
/// <returns></returns>
public CategoryIndex GetSparseIndex(int flatIndex)
{
if (flatIndex < 0 || flatIndex >= _elements.Count)
{
if(flatIndex < 0 || flatIndex >= _elements.Count)
{
ThrowOutOfRangeException(nameof(flatIndex));
}
return _elements[flatIndex];
ThrowOutOfRangeException(nameof(flatIndex));
}
return _elements[flatIndex];
}

/// <inheritdoc/>
public IEnumerator<int> GetEnumerator()
{
return _elements.GetEnumerator();
}
/// <inheritdoc/>
public IEnumerator<int> GetEnumerator()
{
return _elements.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_elements).GetEnumerator();
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_elements).GetEnumerator();
}
}
Loading
Loading