https://github.com/murphyne/iterationlimits
Collection of methods to limit potentially infinite loops
https://github.com/murphyne/iterationlimits
infinite-loop
Last synced: about 1 month ago
JSON representation
Collection of methods to limit potentially infinite loops
- Host: GitHub
- URL: https://github.com/murphyne/iterationlimits
- Owner: murphyne
- License: mit
- Created: 2022-12-05T09:25:16.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-13T19:30:29.000Z (over 2 years ago)
- Last Synced: 2025-01-14T15:20:55.882Z (over 1 year ago)
- Topics: infinite-loop
- Language: C#
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# IterationLimits
This is a small library to help you limit potentially infinite loops in your code.
## Features
The `Limits` class provides two ways to limit iterations:
- `LimitCount` to limit the number of iterations.
- `LimitTime` to limit the time duration of iterations.
The `Limits` class supports three types of iterations:
- `Func` to limit based on condition.
- `IEnumerator` to limit enumerator.
- `IEnumerable` to limit enumerable.
The `LimitTime` method provides three overloads to measure time:
- Use the value of `DateTime.Now` by omitting the 3rd argument.
- Use the value of custom `Func` by providing argument of this type.
- Use the value of custom `Func` by providing argument of this type.
## Examples
> [!NOTE]
> More examples are available in `IterationLimitsExamples` project.
`LimitCount(Func condition, int limit)`
```csharp
Func condition = () => true;
Func conditionLimited = Limits.LimitCount(condition, 10);
while (conditionLimited.Invoke()) {}
```
`LimitTime(IEnumerator enumerator, TimeSpan limit)`
```csharp
IEnumerator enumerator = GetEnumerator();
IEnumerator enumeratorLimited = Limits.LimitTime(enumerator, TimeSpan.FromSeconds(1));
while (enumeratorLimited.MoveNext()) {}
```
`LimitTime(IEnumerable enumerable, TimeSpan limit, Func measure)`
```csharp
IEnumerable enumerable = GetEnumerable();
IEnumerable enumerableLimited = Limits.LimitTime(enumerable, TimeSpan.FromSeconds(1), () => DateTime.Now);
foreach (var item in enumerableLimited) {}
```