1

Is there a use case for storing index ranges when talking about a potentially huge list.

Let's say with a list of millions of records. These will be analysed and a sublist of indexes will be reported to the user. Rather than listing out a massive list of indexes it would be obviously more legible to present;

Identified Rows: 10, 21, 10000-30000, 700000... etc to the user.

Now I can obviously create this string from the array of indexes but I'm wondering if it would also be more memory efficient to create the list in this format (and not creating a massive list of indexes in memory). Or is it not worth the processing overhead?

List intList = new List{1,2,3,4,5,6,7...};

vs

List strList = new List{"1-3000","3002","4000-5000"...};

To apply this I would imagine creating a List and when adding an item update/add to the list as necessary. Would require quite a bit of converting strings to int and vice-versa I think which is where this process may not be worth it.

Let me know if this isn't clear enough and I can potentially explain further.

UPDATE

I quite like Patrick Hofman's solution below using a list of ranges. What would be really cool would be to extend this so that .add(int) would modify the list of ranges correctly. I think this would be quite complicated though, correct?

3
  • 2
    Of course it is more memory efficient if you have a List<string>{"1-9999999"} than one which contains all these numbers Commented Apr 12, 2018 at 15:33
  • 1
    What you're describing is called a sparse matrix and has long been used where most items in a collection are the same. If your matrix contains integers it would be even better to store integers that constantly converting to and from strings. Commented Apr 12, 2018 at 15:39
  • You could use a Dictionary, or just KVP, with a start index and end index as integers. Then there are no conversions, only reading the pairs and pushing the values into your display strings. Then you will just need some validation logic to ensure your ranges don't overlap if you wish to avoid that. Commented Apr 12, 2018 at 16:01

1 Answer 1

4

I would opt to create a list of ranges. Depending on the number of singles in it, it might be more or less efficient:

public struct Range
{
    public Range(int from, int to)
    {
        this.From = from;
        this.To = to;
    }

    public int From { get; }
    public int To { get; }

    public static implicit operator Range(int v)
    {
        return new Range(v, v);
    }
}

You can use it like this:

List<Range> l = new List<Range>{ 1, 2, 3, new Range(5, 3000) };
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. It would be cool to extend this so that .add(7) would modify the list as such to create a range in place of an int if required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.