Click here to Skip to main content
15,925,499 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
AnswerRe: A particular bug-bear of mine. Pin
Ed.Poore3-Jan-07 7:09
Ed.Poore3-Jan-07 7:09 
GeneralRe: A particular bug-bear of mine. Pin
Pete O'Hanlon3-Jan-07 8:36
mvePete O'Hanlon3-Jan-07 8:36 
GeneralRe: A particular bug-bear of mine. Pin
Ed.Poore3-Jan-07 8:50
Ed.Poore3-Jan-07 8:50 
AnswerRe: A particular bug-bear of mine. Pin
Code_Doctor16-Apr-07 18:10
Code_Doctor16-Apr-07 18:10 
QuestionSelection, as if it were text. Pin
Captain See Sharp27-Dec-06 10:01
Captain See Sharp27-Dec-06 10:01 
AnswerRe: Selection, as if it were text. Pin
J. Dunlap28-Dec-06 1:37
J. Dunlap28-Dec-06 1:37 
GeneralRe: Selection, as if it were text. Pin
Captain See Sharp28-Dec-06 8:51
Captain See Sharp28-Dec-06 8:51 
GeneralRe: Selection, as if it were text. Pin
J. Dunlap28-Dec-06 14:58
J. Dunlap28-Dec-06 14:58 
Captain See Sharp wrote:
Thanks for you input.

You're welcome. Smile | :) I'm hoping to make this forum grow and bring some people together who can share design ideas and expertise.

Captain See Sharp wrote:
I'm building a new version of my hex editor control just to let you know.

Cool | :cool: Is there going to be an article about it? I was going to write one myself but have never had the time. I wanted to write a tool that would make it easier to deconstruct binary file formats.

Captain See Sharp wrote:
It was suffecient however in my new version I want it to be able to work with extremely large files. For large files I'm going to make it so it will not load the entire file so it wont use too much memory.

Nice!

Captain See Sharp wrote:
A binary search would be a good idea and I may do that, How would I sort the ranges? Could I just add the MaxValue, MinValue, and Range together and sort by that sum?

Use the MinValue. If you add them together, then you will get indeterminate sort results and you won't be able to just check for overlap with the 2 ranges before and at the insertion location, which defeats the purpose. Implement IComparable<T> on your Range class like this:
int IComparable<Range>CompareTo(Range other)
{
   if(MinValue>other.MinValue)
      return 1;
   if(MinValue<other.MinValue)
     return -1;
   return 0;
}

Then all you have to do is insert them in the correct sorted location as you add them. (I assume you don't modify the ranges after they are created, other than to combine them when they overlap? If you do you will need to update the sort order on modification as well.) You can use binary search, as I mentioned, to find the insertion location, assuming the structure is a List<Range>:

int insertIdx=ranges.BinarySearch(newRange);
if(insertIdx<0) //if there isn't an existing item matching the MinValue in the collection already...
   insertIdx=~insertIdx; //bitwise complement of the insertion index was returned, so complement it again.
//insertIdx is now the location to insert at

Or you can do it this way (Fast custom binary search with comparison built in, and no bitwise complement needed):

int GetInsertIndex(IList<Range> ranges, long minValue)
{
    int left = 0;
    int right = (0 + ranges.Count) - 1;
    while (left <= right)
    {
        int idx = left + ((right - left) >> 1);
        long listVal = ranges[idx].MinValue;
        if (listVal == minValue)
            return idx;
        if (minValue < listVal)
            right = idx - 1;
        else
            left = idx + 1;
    }
    return left;       
}

Then here is an adapted version of my code to insert ranges:

int insertIdx=GetInsertIndex(ranges,range.MinValue);
//if the range at the insertion location overlaps the range being added...
if (insertIdx < ranges.Count && ranges[insertIdx].MinValue <= range.MaxValue)
    ranges[insertIdx] = CombineRanges(range, ranges[insertIdx]); //combine them
else
    ranges.Insert(insertIdx, range); //insert the range at the insertion location

//if the range before the insertion location overlaps...
if (insertIdx > 0 && ranges[insertIdx - 1].MaxValue >= range.MinValue)
{
    //combine the newly inserted range with the one before it
    ranges[insertIdx - 1] = CombineRanges(range, ranges[insertIdx - 1]);
    //remove the newly inserted range because it has been merged
    ranges.RemoveAt(insertIdx);
}

...

private Range CombineRanges(Range change1, Range change2)
{
    return new Range(
        Math.Min(change1.MinValue, change2.MinValue),
        Math.Max(change1.MaxValue, change2.MaxValue)
        );
}




GeneralRe: Selection, as if it were text. Pin
Captain See Sharp28-Dec-06 15:33
Captain See Sharp28-Dec-06 15:33 
GeneralRe: Selection, as if it were text. [modified][modified] Pin
Captain See Sharp3-Jan-07 13:33
Captain See Sharp3-Jan-07 13:33 
QuestionNew Forum Pin
Bradml25-Dec-06 0:13
Bradml25-Dec-06 0:13 
GeneralAwesome! Pin
J. Dunlap24-Dec-06 14:51
J. Dunlap24-Dec-06 14:51 
GeneralFirst Square Post On A New Forum Pin
Bassam Abdul-Baki23-Dec-06 9:04
professionalBassam Abdul-Baki23-Dec-06 9:04 
JokeWhy Discriminate? Pin
CPallini23-Dec-06 23:31
mveCPallini23-Dec-06 23:31 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad26-Dec-06 18:24
professionalPaul Conrad26-Dec-06 18:24 
GeneralRe: First Square Post On A New Forum Pin
User 171649227-Dec-06 1:52
professionalUser 171649227-Dec-06 1:52 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 6:11
professionalPaul Conrad27-Dec-06 6:11 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 7:26
professionalBassam Abdul-Baki27-Dec-06 7:26 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 7:45
professionalPaul Conrad27-Dec-06 7:45 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 9:44
professionalBassam Abdul-Baki27-Dec-06 9:44 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 10:11
professionalPaul Conrad27-Dec-06 10:11 
GeneralRe: First Square Post On A New Forum Pin
User 171649227-Dec-06 12:57
professionalUser 171649227-Dec-06 12:57 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 15:05
professionalPaul Conrad27-Dec-06 15:05 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 2:17
professionalBassam Abdul-Baki27-Dec-06 2:17 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 6:11
professionalPaul Conrad27-Dec-06 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.