Array-Like Aggregate Type with negative Indices??? 
Author Message
 Array-Like Aggregate Type with negative Indices???

Does anyone know if there is an aggregate data type like
an array that you can specify negative indices for.

A typical array is dimensioned and the first position is 0.
int arrary[10];

I'm looking for something like:

int array[-10..10];

I would like to use a negative number to index my array.  
Any help appreciated.



Mon, 17 Oct 2005 17:28:22 GMT  
 Array-Like Aggregate Type with negative Indices???

Quote:
> Does anyone know if there is an aggregate data type like
> an array that you can specify negative indices for.

> A typical array is dimensioned and the first position is 0.
> int arrary[10];

> I'm looking for something like:

> int array[-10..10];

> I would like to use a negative number to index my array.
> Any help appreciated.

Hi Chris,

Sorry but I don't think there is any such thing. I once needed to implement
something to simulate such an array though, to keep track of positive and
negative numbers in a sorted list. Perhaps an ordered list will work for
you:

#include <afxtempl.h>
CList<int,int> myList(128);
myList.AddHead(intValue);

Good Luck,
jonathan



Mon, 17 Oct 2005 19:09:52 GMT  
 Array-Like Aggregate Type with negative Indices???

Quote:

> Does anyone know if there is an aggregate data type like
> an array that you can specify negative indices for.

> A typical array is dimensioned and the first position is 0.
> int arrary[10];

> I'm looking for something like:

> int array[-10..10];

> I would like to use a negative number to index my array.  
> Any help appreciated.

You can declare a class:
class CDetailArray : public CArray<CRDitem, CRDitem&>
{
public:
        void AddIf( CRDitem& ditem, int index );
        CRDitem& operator []( int index );

Quote:
};

Now you have the ability to use offsets in the override operator []

And you can still access with the real index dv.ElementAt( x )

The best of both worlds.

Best, Dan.

--
if( this == NULL )
    return that;



Thu, 20 Oct 2005 05:25:06 GMT  
 Array-Like Aggregate Type with negative Indices???
There is no base type for this. I'm not familiar enough with STL to know if STL supports
this already, but it wouldn't hurt to look there. Otherwise, you could define your own
class, or even subclass CArray and override the operators, e.g.,

template <class TYPE, class ARG_TYPE> class CMyArray : public CArray<TYPE, ARG_TYPE> {
     public:
           CMyArray(int lower, int upper) : CArray() { lb = lower;
                                                            SetSize(upper - lower + 1); }
           CMyArray() : CArray() { lb = 0; }
           TYPE GetAt(int n) { return CArray::GetAt( n - lb);
           void SetAt(int n, ARG_TYPE p) { CArray::SetAt(n - lb); }
           TYPE & ElementAt(int n) { return CArray::ElementAt( n - lb); }
           void SetAtGrow(int n, ARG_TYPE p) { CArray::SetAtGrow( n - lb, p); }
           TYPE operator[](int n) const { return ...something here... }
            TYPE & operator[](int n) { return ...something here... }
            void InsertAt(int n, TYPE p) { ... something here... }
            void RemoveAt(int n) { ...something here...}
            void InsertAt(int n, CMyArray * pArray) { ... something here... }
            int GetUpperBound() const { return CArray::GetUpperBound() + lb; }
            int GetLowerBound() const { return lb; }  
     protected:
           int lb; // lower bound

Quote:
};

Disclaimer: I'm a little shaky on template syntax, and I left a few things as Exercises
For The Reader, and I think I have all the sign conversions correct, but this captures the
sense of it. But these appear to be the methods you need to override.

An alternative is to embed the array, e.g.,

template<class TYPE, class ARG_TYPE> CMyArray {
      protected:
                     CArray<TYPE, ARG_TYPE> array;
     public:
                      CMyArray(int lower, int upper) {
                                                   lb = lower;
                                                   array.SetSize(upper - lower + 1);
                                                  }
                       CMyArray() { lb = 0; }

                       TYPE & operator[](int n) { return array[ n - lb]; }
                  TYPE operator[](int n) const { return array[ n - lb ]; }
                       TYPE GetAt(int n) { return array.GetAt(n - lb); }
                        void SetAt(int n, ARG_TYPE p) { array.SetAt(n - lb, p); }

Quote:
};

I tend to favor the embedding.                                              

                                        joe

Quote:

>Does anyone know if there is an aggregate data type like
>an array that you can specify negative indices for.

>A typical array is dimensioned and the first position is 0.
>int arrary[10];

>I'm looking for something like:

>int array[-10..10];

>I would like to use a negative number to index my array.  
>Any help appreciated.

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Thu, 20 Oct 2005 07:57:03 GMT  
 Array-Like Aggregate Type with negative Indices???
You can use OLE safe arrays. Try following:

#include <objbase.h>
#include <oleauto.h>
...
 SAFEARRAY* pArray; // array
 SAFEARRAYBOUND bounds[1]; // one element for each dimension
 long i;
 int nElement;
 HRESULT hr;

 // one dimension array with lower bound -5 and 10 elements
 bounds[0].cElements = 10;
 bounds[0].lLbound = -5;

 // the base type is int
 pArray = SafeArrayCreate(VT_INT, 1, &bounds[0]);

 // put data to array
 for(i = -5; i < 5; i++)
 {
  nElement = i;
  hr = SafeArrayPutElement(pArray, &i, &nElement);
 }

 // retrieve data from array
 for(i = -5; i < 5; i++)
  SafeArrayGetElement(pArray, &i, &nElement);

 // destroy array
 SafeArrayDestroy(pArray);

--
Dmitry Davletbaev


Quote:
> Does anyone know if there is an aggregate data type like
> an array that you can specify negative indices for.

> A typical array is dimensioned and the first position is 0.
> int arrary[10];

> I'm looking for something like:

> int array[-10..10];

> I would like to use a negative number to index my array.
> Any help appreciated.



Fri, 21 Oct 2005 16:54:04 GMT  
 Array-Like Aggregate Type with negative Indices???
An easy thing to do would be to derive your own array/vector class
from one of the pre-existing classes (i.e. CArray or vector).  Add
a member variable called something like 'int m_nStartIdx' and then
override the [] operators and any other function that takes an index.
Then all you need to do is offset the index parameter of each of
the functions using your m_nStartIdx member variable so the index is
compatible with the regular base class implementation.

Murrgon

Quote:

> Does anyone know if there is an aggregate data type like
> an array that you can specify negative indices for.

> A typical array is dimensioned and the first position is 0.
> int arrary[10];

> I'm looking for something like:

> int array[-10..10];

> I would like to use a negative number to index my array.  
> Any help appreciated.



Sat, 22 Oct 2005 14:52:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. array with negative index

2. negative array index?

3. Array with variable type of index

4. type of array index

5. Type promotion in array index?

6. What is the type of an array index?

7. Negative indices, is this ok?

8. Negative indices

9. array index of smallest number in array?

10. Using array index in array initialization list's

11. Aggregate types

12. initialising static aggregate array?

 

 
Powered by phpBB® Forum Software