Skip to content
Ranges and iterators utility library.
C#
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Benchmarks
Examples
Source
.gitattributes
.gitignore
Changelog.md
LICENSE.md
PublishRelease.txt
README.md
RangeIt.sln
appveyor.yml

README.md

RangeIt

NuGet Package License CodeFactor

Ranges and Iterators

Contains a collection of helper methods (Range.Ints() and Range.Iota()) for generating ranges of arbitrary type. Adds also iterators for common collections.

Supported Platforms

  • .Net Framework >= 4.5
  • .NET Core >= 1.0
  • Windows 8 / 8.1 / 10 / UWP
  • ASP.NET Core >= 1.0
  • Xamarin Android | Xamarin iOS
  • Windows Phone 8.1

Chat Room

Do you have a question or suggestion?

Gitter

Or do you want to report a bug?

Build Status

Branch Status Description
master Build status branch master This branch tracks all stable releases.
dev Build status branch dev This branch tracks the current and possibly unstable development.

Getting Started

Install the latest release by running the following command

PM> Install-Package RangeIt

or with the Package Management in Visual Studio and search for "rangeit".

Each release will also be published here.

Ranges

Ranges Usage Examples

using RangeIt.Ranges;

Range<int> intRange = Range.Ints(10);
// intRange == { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

// with start value
Range<int> intRange = Range.Ints(5, 10);
// intRange == { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }

// with step value
Range<int> intRange = Range.IntsWithStep(10, 2);
// intRange == { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }

// with start value / with step value
Range<int> intRange = Range.Ints(5, 10, 2);
// intRange == { 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 }

// ----------------------------

// from - to (exclusive)
Range<int> intRange = Range.Iota(5, 20);
// intRange == { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }

// from - to (exclusive) / with step value
Range<int> intRange = Range.Iota(5, 20, 2);
// intRange == { 5, 7, 9, 11, 13, 15, 17, 19 }

// ----------------------------

Range<string> strRange = Range.Iota("hello", (s) => s + "a", (s) => s?.Length == 10);
// strRange = { "hello", "helloa", "helloaa", "helloaaa", "helloaaaa", "helloaaaaa" }

// ----------------------------

// Transform

Range<int> intRange = Range.Ints(10);
// intRange == { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

intRange = intRange.Transform(Range.MultiplyBy(2));
// or
intRange |= Range.MultiplyBy(2);
// or
intRange = Range.Ints(10) | Range.Multiply(2);

// intRange == { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }

// or custom transform
// intRange = Range.Transform(custom Func<T, T>);

// ----------------------------

// Filter

Range<int> intRange = Range.Ints(10);
// intRange == { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

intRange = intRange.Filter(Range.IsEven());
// or
intRange |= Range.IsEven();
// or
intRange = Range.Ints(10) | Range.IsEven();

// intRange == { 2, 4, 6, 8, 10 }

// or custom filter
// intRange = Range.Filter(custom Func<T, bool>);

// ----------------------------

// looping
foreach (var value in intRange)
{
    Console.WriteLine(value);
}

// ----------------------------

// ToString()
// intRange == { 5, 7, 9, 11, 13, 15, 17, 19 }
string range = intRange.ToString();
// range == "5,7,9,11,13,15,17,19"

// or ToString(string separator)
// intRange == { 5, 7, 9, 11, 13, 15, 17, 19 }
string range = intRange.ToString(" - ");
// range == "5 - 7 - 9 - 11 - 13 - 15 - 17 - 19"

There are many more overloads, especially for Range.Iota().

Iterators

Supported Collections - Benchmarks

Collection Iterator ConstIterator
T[] Iterator<T> ConstIterator<T>
KeyValuePair<T, U>[] Iterator<T, U> ConstIterator<T, U>
List<T> Iterator<T> ConstIterator<T>
Collection<T> Iterator<T> ConstIterator<T>
Dictionary<T, U> Iterator<T, U> ConstIterator<T, U>
ConcurrentDictionary<T, U> Iterator<T, U> ConstIterator<T, U>
ReadOnlyCollection<T> xxx ConstIterator<T>
ReadOnlyDictionary<T, U> xxx ConstIterator<T, U>

Iterators Usage Examples

using RangeIt.Iterators;

var list = new List<int> { 1, 2, 3, 4, 5 };
Iterator<int> it = list.Begin();

// Looping forward
while (it++)
    Console.WriteLine(it.Current);

// not necessary after looping completely forward
it = list.End();

// Looping backward
while (--it)
    Console.WriteLine(it.Current);

// change second element
// list = { 1, 2, 3, 4, 5 }
it = list.Begin();

it = it + 2;
it.Current = 8;
// list = { 1, 8, 3, 4, 5 }

it = list.Begin();

// Using iterator like an enumerator
foreach (var val in it)
    Console.WriteLine(val);

// ---------------------------
// ---------------------------
// const iterator
ConstIterator<int> it = list.ConstBegin();

// Looping forward
while (it++)
    Console.WriteLine(it.Current);

// Looping backward
while (it--)
    Console.WriteLine(it.Current);

it = list.ConstBegin() + 2;

// changing second element not possible,
// since it is a const iterator,
// this won't compile
// it.Current = 7;

it = list.ConstBegin();

// Using iterator like an enumerator
foreach (var val in it)
    Console.WriteLine(val);

License

The MIT License (MIT)

Copyright (c) 2016 - 2017 Henrik Fröhling

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You can’t perform that action at this time.