13,297,266 members (70,333 online)
alternative version

#### Stats

48.3K views
14 bookmarked
Posted 10 Mar 2003

# A simple range structure

, 10 Mar 2003
 Rate this:
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.

A list of licenses authors might use can be found here

## Share

 Croatia
No Biography provided

## You may also be interested in...

 First Prev Next
 Hmm? Rocky Moore11-Mar-03 18:44 Rocky Moore 11-Mar-03 18:44
 Re: Hmm? Goran Mitrovic12-Mar-03 0:47 Goran Mitrovic 12-Mar-03 0:47
 Veeeeeeeery nice! v.nrg11-Mar-03 10:42 v.nrg 11-Mar-03 10:42
 Last Visit: 31-Dec-99 19:00     Last Update: 15-Dec-17 16:55 Refresh 1