Click here to Skip to main content
15,887,822 members
Articles / Desktop Programming / MFC
Article

Template-Based Helper Classes: An application to Multidimensional Arrays

Rate me:
Please Sign up or sign in to vote.
3.31/5 (13 votes)
8 Mar 2004GPL33 min read 68.6K   466   19   20
An Implementation of Multidimensional Arrays via Template-Based Helper Classes

Introduction

Multidimensional arrays provide a simple reshaping matrix structures. It could be created and initialized as a vector, and read as 3D matrix. However, subscripting is not that easy. Subscripting operators should reduce dimension of entire multi-array. The problem becomes harder when adding region-of-interest scripting facility. Region-of-interest scripting facility allows selecting separate rows, columns, and plates in different orders. More complexities may be added to support reference indexing. A typical solution is helper classes. Boost library has implemented a helper class to multi_array<t, n=""> for reference indexing and named as multi_array_ref<t, n="">. Matrix Template Library (MTL) provided helper classes to the matrix. Two helper classes were developed to emulate row set and column set. Communication between helper classes is made via copy constructors. More helper classes are needed for every new structure. This yields to more tightly coupled classes as many programmers tend to use friend classes.

Problem Statement and Basic Idea

We need more encapsulation of helper classes with original interfacing class. This could be accomplished by adding more template arguments. Classes with different template arguments lead to separate classes. However, it makes the whole class more cohesive and prevent user from using helper classes in a wrong way. In my master thesis I needed a flexible multidimensional array with the following specs.

1. array-like subscripting operators[].
2. matlab-like region of interests subscripting facility.
3. reference-like array to allow editing a set of selected entries.

1. Array-like subscripting operators[]

This piece of code declaresa a new initialized 3D multi-dimensional array.

tensor<double, 3, 1, false> itns3(dblbegin
5, 7, 3, 1, 2,
4, 7, 2, 1, 2,
1, 8, 9, 3, 6, 
1, 3, 5, 7, 8,

1, 7, 3, 8, 2,
1, 7, 3, 8, 2,
1, 7, 3, 8, 2,
1, 7, 3, 8, 2,

5, 7, 3, 1, 2,
4, 7, 2, 1, 2,
1, 8, 9, 3, 6,
1, 3, 5, 7, 8
clend, sizebegin 3, 4, 5 clend);

Where dblbegin, sizebegin, and clend are defined as follows:

#define dblbegin clbegin(double)
#define sizebegin clbegin(_sizetype)
#define clend )

and clbegin macro is defined as:

#define clbegin(T) (commalist<T>(),

Actually cl-prefix stands for comma-list. commalist<T> template class was developed to allow comma-concatenation. I guess comma-concatenation is simpler on gnu-C++.

Now the container itns3 has three dimensions [3, 4, 5] which is 3-plates, 4-rows each, and 5-cols for each row. Now consider using subscripting operator getting first plate, how many dimensions shall this array hold? Yup, tensor<t, 2=""> with two dims. That is why the subscripting is defined as follows:

tensor<T, N-1, D, true> operator[](const _sizetype& i);
const tensor<T, N-1, D, false> operator[](const _sizetype& i) const;

Other implemetations for multi-arrays makes use of helper classes named subset, rows, cols, rowset, colset, ...


Now moving to second facility;

2. Matlab-like region-of-interests subscripting facility

Region of interest (roi) lets you to select first and third rows for all columns. Consider the following:

tensor<double, 2> itns2(dblbegin 
5, 7, 3, 1, 2,
4, 7, 2, 1, 2,
1, 8, 9, 3, 6,
1, 3, 5, 7, 8
clend, sizebegin 4, 5 clend);

Now consider selecting all rows and cols numbered [0, 4, 2, 3]

itns2[all][select(0, 4, 2, 3, -1)]=3.0;

where select, and all are defined as follows:

#define all ixrange()
#define select rangelist<_indextype>

and ixrange() is defined as follows:

typedef range<_indextype> ixrange;

This means subscripting operator should return tensor<t, n=""> and not tensor<t, n-1=""> as discussed earlier.

This means that we need a new helper class presenting the new dimension we iterate on. In simple words, we first select two rows, and the new subscripting shall select three cols. That is why we need the D template parameter! And roi-subscripting operator should be declared as follows:

const tensor<T, N, D+1, false> operator[](const tensor<_indextype, 1> idx) const;
tensor<T, N, D+1, true> operator[](const tensor<_indextype, 1> idx);

Other implemetations make use of friend helper classes, and usually named as rowset and colset.

Lets move to next requirement:

3. Reference-like array to allow editing a set of selected entries

Reference containers capture addresses of selected entries and allow you to modify them. This was usually implemented with helper classes named reference_container, array<smart_ptr<T> >, or ref_array.
A tensor has one of two states if const subscripting operators should return a copy of entries, if not, a reference to entries should be returned. One more boolean template argument is added describing the entire tensor as a reference or not.

Class and Operator Headers

Consider the following class header:

// tensor<typename, # dims, dim to iterate on, being ref or not>
template<class T, _sizetype N=1, _sizetype D=1, _booltype REF=false>
class tensor : public io
then consider the subscripting operator. It should return a lower dimensioned non-reference tensor
const tensor<T, N-1, 1, false> operator[](const _sizetype& i) const

and this returns a lowerdimensioned referenced tensor:

tensor<T, N-1, 1, true> operator[](const _sizetype& i)

Now consider the region-of-interest subscripting operator. It should return a matrix of chosen rows, and next subscripting operator should iterate on the next dimension.

const tensor<T, N, D+1> operator[](const tensor<_indextype, 1> idx) const 

and this is a referenced copy too.

tensor<T, N, D+1, true> operator[](const tensor<_indextype, 1> idx) 

Thank You

Thanks a lot for your time. I just want to clarify that this is a testing version; I hope you enjoy this.

 

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generali ve updated the article Pin
Mo Hossny12-Feb-04 1:16
Mo Hossny12-Feb-04 1:16 
GeneralRe: i ve updated the article Pin
WREY17-Feb-04 3:28
WREY17-Feb-04 3:28 
There are those who read comments to an article, mostly because to them, it represents entertainment. They are not interested in reading an article and constructively analyze it for the benefits it may contain, or make suggestions regarding how it can be improved. Contrast these kinds to those who point out errors (pertaining to the article, or its code), make alternative suggestions, ask questions, or even seek help in resolving difficulties with its sample code. These latter kind can be genuinely viewed as active participants in the author/readership interaction. They are different than those who read the comments of others, purely for the gossip interest they see in it. IOW, to them (the gossip-mongers), its gossip that's going on, instead of constructive interactions.

I can make those statements because I have spent (to date) about an hour and a half, looking over your code, scrutinizing it, and trying to get a factual understanding of what's happening. Because I see it works, that much I know is sound and real. It's the coalescing of the pieces that doesn't come easy. In short, it's a difficult piece of work you have authored that doesn't give up its secrets easily.

It's because of that concerted effort and energy that I've put into reading both your article and sample code, and the difficulty I have sustained, extracting clarity from it, that tells me (based on the kind of comments some have made) those people have NOT invested anywhere near the time and effort to your article, as I have, simply because they would have seen how difficult it is to read and understand. This is NOT to say your article is of poor quality and meaningless substance. Just the opposite!! I have approached it as those scientists who undertook, trying to decode the human genome. No one said it was going to be easy!! Readers of your article and code have to be methodical and resolute (in their pursuit) if they want to see the light at the end of the tunnel. (That's how I know they haven't read your code, because their comments of discovery would have had some similarities to what I have come to know for a fact.)

This is not the end for me! As Churchill said, "It's not even the beginning of the end. It's only the end of the beginning!!"

I shall come back to this page about a month from now, during which time I shall be poring over your codes some more. It should also be interesting to see what other comments will have been added to this page (in the interim) by others; particularly those who are less interested in it for its entertainment value. Notice so far, everybody who have been commenting, have been adding comments to my original post. Nobody has contributed statements of their own, based on their own research and investigation of your code. Nonetheless, they felt qualified to judge my comments without finding out if what I was saying, was true.

Not to mention, it was because of my original comments that prompted you to update and expand the article. We can see from constructive criticism comes constructive benefits.

Wink | ;)

William

Fortes in fide et opere!
GeneralRe: i ve updated the article Pin
Mo Hossny9-Mar-04 3:39
Mo Hossny9-Mar-04 3:39 
GeneralI am holding my vote! Pin
WREY3-Feb-04 3:07
WREY3-Feb-04 3:07 
GeneralRe: I am holding my vote! Pin
Mo Hossny4-Feb-04 21:18
Mo Hossny4-Feb-04 21:18 
GeneralRe: I am holding my vote! Pin
Miguel Lopes9-Feb-04 11:27
Miguel Lopes9-Feb-04 11:27 
GeneralRe: I am holding my vote! Pin
WREY10-Feb-04 7:58
WREY10-Feb-04 7:58 
GeneralRe: I am holding my vote! Pin
Mo Hossny10-Feb-04 21:55
Mo Hossny10-Feb-04 21:55 
GeneralRe: I am holding my vote! Pin
Anonymous10-Feb-04 22:09
Anonymous10-Feb-04 22:09 
GeneralRe: I am holding my vote! Pin
WREY10-Feb-04 22:17
WREY10-Feb-04 22:17 
GeneralRe: I am holding my vote! Pin
Mo Hossny10-Feb-04 21:54
Mo Hossny10-Feb-04 21:54 
GeneralRe: U != You Pin
Marc Brooks11-Feb-04 13:14
Marc Brooks11-Feb-04 13:14 
GeneralRe: U != You Pin
Miguel Lopes11-Feb-04 13:33
Miguel Lopes11-Feb-04 13:33 
GeneralRe: U != You Pin
J Whattam17-Feb-04 11:28
J Whattam17-Feb-04 11:28 
GeneralRe: U != You Pin
Marc Brooks17-Feb-04 14:15
Marc Brooks17-Feb-04 14:15 
GeneralRe: U != You Pin
JonatTJX20-Feb-04 4:43
JonatTJX20-Feb-04 4:43 
GeneralRe: I am holding my vote! Pin
Anonymous10-Feb-04 22:11
Anonymous10-Feb-04 22:11 
GeneralRe: I am holding my vote! Pin
WREY10-Feb-04 22:24
WREY10-Feb-04 22:24 
GeneralRe: I am holding my vote! Pin
Anonymous10-Feb-04 23:51
Anonymous10-Feb-04 23:51 
GeneralRe: I am holding my vote! Pin
WREY11-Feb-04 7:01
WREY11-Feb-04 7:01 

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.