Skip to content
Open
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
Binary file modified docs/images/association_arrows-techvidvan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/association_multiplicity_annotations-vertabelo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/association_multiplicity_concept-techvidvan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/class_notation-tutorialspoint.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/uml_arrows-stack_overflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions src/EStimLibrary/Core/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ public class ResourceManager<ResourceType>

public readonly int MaxNumResources;

public int NumTotalResources => this.IdPool.NumUsedIds;
public int NumTotalResources => this.Resources.Count;
Comment thread
bacchuongdaungo marked this conversation as resolved.

// Constructor
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a docstring comment, i.e., ///, etc
Also, include docstring comments for all classes, properties, fields, and methods

public ResourceManager(int baseId = 0, int initialNumResourceIds = 0,
int maxNumResources = Constants.POS_INFINITY)
{
this.IdPool = new(baseId, initialNumResourceIds);
this.Resources = new();
if (maxNumResources < 0) maxNumResources = Constants.POS_INFINITY;
Comment thread
bacchuongdaungo marked this conversation as resolved.
this.MaxNumResources = maxNumResources;
}

Expand All @@ -29,7 +31,10 @@ public bool IsValidResourceId(int globalId)
{
// TODO: does Used check validity again?
return this.IdPool.IsValidId(globalId) &&
this.IdPool.IsUsed(globalId);
this.IdPool.IsUsed(globalId) &&
this.Resources.ContainsKey(globalId);
//return this.IdPool.IsValidId(globalId) &&
// this.IdPool.IsUsed(globalId);
}

public bool TryGetNextAvailableId(out int globalId)
Expand All @@ -40,9 +45,11 @@ public bool TryGetNextAvailableId(out int globalId)
while (!this.IdPool.TryGetNextFreeId(out globalId))
{
// If max capacity already used, return failure.
//if (this.IdPool.NumUsedIds >= this.MaxNumResources)
if (!Utils.IsWithinUpperBound(this.IdPool.NumUsedIds,
this.MaxNumResources))
this.MaxNumResources, inclusive: false))
{
globalId = default;
return false;
}
// Else increment the number of IDs in the pool and try again.
Expand Down
117 changes: 97 additions & 20 deletions src/EStimLibrary/Core/ReusableIdPool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EStimLibrary.Core;
using System.ComponentModel.DataAnnotations;

namespace EStimLibrary.Core;


/// <summary>
Expand All @@ -18,48 +20,101 @@ public class ReusableIdPool
// Private fields should never be accessed or manipulated directly. Use
// corresponding properties.

// The base ID value for this pool. All IDs in the pool will be in a
// consecutive block including and after this value, up to NumIds IDs.
// Guaranteed to be >= 0 at all times after construction.
/// <summary>
/// The base ID value for this pool. All IDs in the pool will be in a
/// consecutive block including and after this value, up to NumIds IDs.
/// Guaranteed to be >= 0 at all times after construction.
/// </summary>
private int _baseId;
// The number of IDs in this pool. Can be changed via class methods.
// Guaranteed to be >= 0 at all times after construction.
/// <summary>
/// The number of IDs in this pool. Can be changed via class methods.
/// Guaranteed to be >= 0 at all times after construction.
/// </summary>
private int _numIds;

// Protective BaseId and NumIds properties for the private fields.
// Ensures limits when setting the values.
/// <summary>
/// The lowest ID in this pool. Cannot be below 0.
/// </summary>
public int BaseId
{
get => this._baseId;
protected set => this._baseId = Math.Max(MIN_BASE_ID, value);
}
/// <summary>
/// The number of IDs in this pool. Cannot be below 0.
/// </summary>
public int NumIds
{
get => this._numIds;
protected set => this._numIds = Math.Max(MIN_NUM_IDS, value);
//protected set => this._numIds = Math.Max(MIN_NUM_IDS, value);
protected set
{
long maxAllowableNumIds = (long)Constants.POS_INFINITY - (long)this.BaseId + 1;
int minAllowableNumIds = MIN_NUM_IDS;

if (this.UsedIds != null)
{
if (this.UsedIds.Count > 0)
{
minAllowableNumIds = this.UsedIds.Max - this.BaseId + 1;
}
}

if (value < minAllowableNumIds) { this._numIds = minAllowableNumIds; }
else if (value > maxAllowableNumIds) { this._numIds = (int)maxAllowableNumIds; }
else this._numIds = value;
}
}

// Total ID pool, generated based on BaseId and NumIds
/// <summary>
/// Set of IDs in this pool, generated based on BaseId and NumIds:
/// [BaseId, BaseId+NumIds)
/// Empty if no IDs currently in this pool.
/// </summary>
public SortedSet<int> Ids
{
get
{
// Return an empty set if no IDs in this pool.
if (this.NumIds == 0)
{
return new SortedSet<int>();
}

// Ensure that baseId + numIds - 1 does not exceed Constants.POS_INFINITY
if ((long)this.BaseId + (long)this.NumIds - 1 > Constants.POS_INFINITY)
{
this.NumIds = Math.Min(NumIds - BaseId, NumIds);
}
// Return all IDs in the [BaseId, BaseId+NumIds) range.
return new(Enumerable.Range(this.BaseId, this.NumIds));
}
}

// Set of used IDs and its count. UsedIds is the only set manipulated.
/// <summary>
/// The number of IDs in this pool that are currently marked as used.
/// </summary>
public int NumUsedIds { get => this.UsedIds.Count; }
/// <summary>
/// Set of used IDs. UsedIds is the only set manipulated.
/// </summary>
public SortedSet<int> UsedIds { get; protected set; }

// Set of free IDs and its count.
/// <summary>
/// The number of IDs in this pool that are currently marked as free, or
/// unsused.
/// </summary>
public int NumFreeIds { get => this.FreeIds.Count; }
/// <summary>
/// Set of free, or unused, IDs.
/// </summary>
public SortedSet<int> FreeIds
{
get
{
// Return all except the used IDs.
// Return all IDs except the used IDs.
return new(this.Ids.Except(this.UsedIds));
}
}
Expand All @@ -71,19 +126,17 @@ public SortedSet<int> FreeIds
/// Defaults to 0 if a negative number is given. This value cannot be
/// changed after construction.</param>
/// <param name="numIds">The number of IDs in this pool. Defaults to 0 if a
/// negative number is given. Can be changed after construction.</param>
/// negative number is given. Can be changed after construction. Will be
/// bounded by ((max possible integer value) - baseId + 1).</param>
public ReusableIdPool(int baseId, int numIds)
{
// Store the base ID. Property setter handles defaulting to 0.
// Ensure BaseId is not negative
this.BaseId = baseId;

// Store the number of IDs in this pool. Property setter handles
// defaulting to 0.
this.NumIds = numIds;

// Initialize set of used IDs to empty. This is the only set manually
// that is method-editable not auto-generated.
this.UsedIds = new();
// Initialize UsedIds
this.UsedIds = new SortedSet<int>();
}

/// <summary>
Expand All @@ -100,12 +153,27 @@ public ReusableIdPool()
this.UsedIds = new();
}

/// <summary>
/// Change the number of IDs in this pool by a specified amount.
/// </summary>
/// <param name="increment">The positive or negavtive integer number of IDs
/// to add or remove from the current pool. Cannot icnrement to beyond the
/// delta between BaseId and the max possible integer value. Cannot remove
/// IDs beyond the highest currently used ID.</param>
/// <returns>The resulting NumIds value.</returns>
public int IncrementNumIds(int increment)
{
this.NumIds += increment;
return this.NumIds;
}

/// <summary>
/// Overwrite the number of IDs in this pool to a new specified amount.
/// </summary>
/// <param name="newMax">The new number of IDs to set in this pool. Will be
/// bounded by the delta between BaseId and the max possible integer value,
/// and the highest current used ID.</param>
/// <returns>The resulting NumIds value.</returns>
public int ResetNumIds(int newMax)
{
this.NumIds = newMax;
Expand Down Expand Up @@ -162,6 +230,11 @@ public bool UseId(int globalId)
/// </returns>
public bool FreeId(int globalId)
{
// Checks if the globalId is in range of the pool IDs
if (!this.IsValidId(globalId))
{
return false;
}
// If ID currently used, return bool success of the removal operation.
if (this.IsUsed(globalId))
{
Expand Down Expand Up @@ -210,11 +283,15 @@ public bool TryGetNextFreeId(out int id)

public SortedSet<int> GetSubset(int startId, int numIds)
{
if (numIds <= 0)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good case to think about: is it already covered by the way the rest of the method is carried out? Or was the GetViewBetween(x, x) giving you errors?

{
return new SortedSet<int>();
}
if (this.IsValidId(startId))
{
// Find the *inclusive* end ID bound.
int endId = Math.Min(this.BaseId + startId + numIds,
this.BaseId + this.NumIds) - 1;
int endId = Math.Min(startId + numIds,
this.BaseId + this.NumIds) - 1;
return this.Ids.GetViewBetween(startId, endId);
}
// Return empty set if invalid ID request.
Expand Down
2 changes: 1 addition & 1 deletion src/EStimLibrary/Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static bool IsWithinUpperBound(double value, double upperBound,
bool inclusive = true)
{
return upperBound == Constants.POS_INFINITY ||
(inclusive) ? value <= upperBound : value < upperBound;
(inclusive ? value <= upperBound : value < upperBound);
}

/// <summary>
Expand Down
Loading