Click here to Skip to main content
Licence 
First Posted 10 Mar 2003
Views 42,445
Bookmarked 14 times

A simple range structure

By | 10 Mar 2003 | Article
As its name suggests, it consists of two fields, the start and the end, which define inclusive bounds of a range the structure represents.

Introduction

In this article I'll present a simple data structure I had a need for. As its name suggests, it consists of two fields, the start and the end, which define inclusive bounds of a range the structure represents. The relation between bounds could exist as follows:

  • The end is greater than the start
  • The end is equal as the start meaning the range is consisted of only one representative
  • The end is one less than the start meaning the range is empty

The structure contains following members:

Constructors

  • Range(int start, int end) - constructs a range of given bounds
  • Range(ICollection col) - setup range to collection's lower and upper bounds

Properties

  • int start - get or set lower bound
  • int end - get or set upper bound
  • int mid - get center point
  • int [] - obtain an element by a zero-based index
  • int Count - get a number of elements
  • bool Empty - returns true if range has no elements

Functions

  • void Set(int start, int end) - redefines a range
  • bool Between(int value) - returns true if value is within current range
  • int Saturate(int value) - forces value between current bounds
  • void Offset(int o) - moves range by o
  • void Resize(int s) - moves upper bound by s

Range structure is declared as an ICollection, meaning, it could be used as a simple sequential number generator. For example:

foreach(int i in new Range(1, 10)) Console.Out.WriteLine(i);

Since the structure is consisted of only two fields which are mostly accessed in pair, I believe it was a reasonable choice to declare it as a value type.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Goran Mitrovic



Croatia Croatia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHmm? PinmemberRocky Moore17:44 11 Mar '03  
AnswerRe: Hmm? PinmemberGoran Mitrovic23:47 11 Mar '03  
GeneralVeeeeeeeery nice! Pinmemberv.nrg9:42 11 Mar '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Mar 2003
Article Copyright 2003 by Goran Mitrovic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid