-
Notifications
You must be signed in to change notification settings - Fork 0
Test/resource mgt #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release-v2.0
Are you sure you want to change the base?
Changes from all commits
0bada30
62de6f7
fc2254d
2be2710
1767af9
96ebcf9
e72c8fc
7379ae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,15 @@ public class ResourceManager<ResourceType> | |
|
|
||
| public readonly int MaxNumResources; | ||
|
|
||
| public int NumTotalResources => this.IdPool.NumUsedIds; | ||
| public int NumTotalResources => this.Resources.Count; | ||
|
|
||
| // Constructor | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this a docstring comment, i.e., ///, etc |
||
| 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; | ||
|
bacchuongdaungo marked this conversation as resolved.
|
||
| this.MaxNumResources = maxNumResources; | ||
| } | ||
|
|
||
|
|
@@ -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) | ||
|
|
@@ -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. | ||
|
|
||
| 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> | ||
|
|
@@ -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)); | ||
| } | ||
| } | ||
|
|
@@ -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> | ||
|
|
@@ -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; | ||
|
|
@@ -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)) | ||
| { | ||
|
|
@@ -210,11 +283,15 @@ public bool TryGetNextFreeId(out int id) | |
|
|
||
| public SortedSet<int> GetSubset(int startId, int numIds) | ||
| { | ||
| if (numIds <= 0) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.