Uint Ranges
Documentation Link: Here -> UintRanges
Tutorial: Managing and Querying Unsigned Integer Ranges
1. Introduction to UintRange
The UintRange interface captures a range of unsigned integers using a start and end property. This is handy when representing intervals or spans of values.
2. Sorting and Merging Ranges
To sort a list of ranges and merge adjacent or overlapping ones:
const ranges = UintRangeArray.From([
{ start: 10n, end: 20n },
{ start: 5n, end: 12n },
{ start: 21n, end: 25n }
]);
ranges.sortAndMerge();
console.log(ranges); // Expected: [{ start: 5n, end: 25n }]3. Searching Within Ranges
To search for a specific ID within a list of ranges and return its index and a boolean indicating if it was found:
const idToSearch = 15n;
const [index, isFound] = ragnes.search(idToSearch)
console.log(`Index: ${index}, Found: ${isFound}`);4. Inverting Ranges
To invert a list of ranges between a minimum and maximum ID:
5. Removing One Range From Another
To remove one range from another and also get the removed part:
6. Checking for Overlaps
To determine if there are overlaps within a list of ranges:
Conclusion
The functions provided offer a comprehensive toolkit for managing and querying unsigned integer ranges. Whether you're checking for overlaps, inverting ranges, or removing specific integers from a range, you now have the tools to do it efficiently and systematically.
Last updated