# Range Constructor

By , 22 Sep 2005
Votes of 3 or less require a comment

## Introduction

This is my first article. I've wanted to write something for ages...anyway I have read heaps of articles and haven't seen anything similar...so I hope u enjoy this one .

The article covers a group of static functions that are useful for creating two dimensional arrays from one dimensional arrays. This is used in conjuction with custom button and panel classes to emulate the Microsoft Outlook address book buttons panel. It takes a range/array of objects and converts this array into a two dimensional array depending on the range size allocated (sorry for the long winded explanation, see the background to make more sense).

## Background

I recently had an assignment which involved creating a teledex/electronic address book. I wanted to be able to have buttons on the left side that corresponded to the starting letter of a contact's name. One problem with this was that there needed to be room for 26 buttons. I wanted to be able to get the range displayed on the buttons to expand, when there was not enough room to fit all the buttons.

That is, if I had a range of say {A,B,C,D}, and if I had room for four buttons then it would be :

```A
B
C
D```

If I had room for only two buttons then it would appear:

```A-B
C-D```

For any one who uses Microsoft Outlook 2003, the address book uses this kind of resizing technique. So this library tries to emulate the Outlook address book feature.

## Overview

For the assignment, the range class was using strings, but I thought I would change this to objects, as people might find other uses that I was unaware of, or didn't think of. Also another interesting fact is that the Microsoft uses a strange pattern when the range changes, this class uses a more standard form, but I provided an emulation to be able to change the type of distribution for future upgrades.

### The Range Constructor

This is the important part of the static functions. This constructs a 2D array from a 1D array. The pattern that is used to form the distribution from 1D to 2D is dependent on the emulation range.

```/// Used to calculate a 2 dimensional array from a
/// 1 dimension array, depending on the space allocated.
static public object[][] RangeConstruct(object[] range, int a,
CameronSinge.Range.Range.Distribution dist)
{
int required = range.Length;
int allocated;
object[][] tempRange = new object[a][];
if(a == 0)
{
for(int i = 0; i < tempRange.Length; i++)
{
tempRange[i] = new object[0];
}
return tempRange;
}
if(required <= a)
{
allocated = required;
}
else
{
allocated = a;
}
int[] matrix =
CameronSinge.Range.Range.calculateStaggedMatrix(required,
allocated);
for(int i = 0; i < allocated; i++)
{
tempRange[i] = new object[matrix[i]];
}
int count = 0;
for(int i = 0; i < allocated; i++)
{
for(int j = 0; j < tempRange[i].Length; j++)
{
tempRange[i][j] = range[count];
count++;
}
}
return tempRange;
}```

### The distribution

Okay...now all this time I kept talking about the distribution, but not really explained exactly what I meant. The distrubtion pattern is how the objects are distrubited over a two dimensional array.

The diagram below shows the standard form of distribution and gives some insight on how Microsoft Outlook 2003 distributes the objects.

```/// Creates a stagered matrix/array,
/// used to create the standard type of distrubtion
static public int[] calculateStaggedMatrix(int required, int allocated)
{
int [] staggedMatrix = new int[allocated];
for(int i = 0; i < required; i++)
{
staggedMatrix[i%allocated]++;
}
return staggedMatrix;
}```

## Point of interests

I like the idea of converting 1D arrays into 2D arrays. I can see a lot of uses with it. One future use would be to have similar group of classes that are used to create tournament/team ordering.. (i.e., round-robin events, elimination, double elimination and so on...). If you want to discuss ideas please drop me a line .

## To-Do List

• Make the library more OO - (any suggestions would be great!)
• Add more forms of distribution
• Change the class from using objects to make it generic
• Include a hybrid version of the class that uses the most common letter distribution....

## History

• 23 Sep, 2005 - Included a diagram to explain the distribution easier.

A list of licenses authors might use can be found here

Web Developer
Australia
No Biography provided

 First Prev Next
 A thought Paul Brower 8-Sep-05 4:19
 Re: A thought csinge 8-Sep-05 5:38
 Nice Contribution Marc Clifton 8-Sep-05 2:59
 Last Visit: 31-Dec-99 18:00     Last Update: 8-Dec-13 4:56 Refresh 1