arrays with negative and possitive indexes, fastest access?Elements are integers

I want a way to declare and use arrays with negative and possitive pointers both, without lose any speed
What I am intresting is the fastest access to the elements of these arrays.
The elements are integers.
[217 byte] By [Demokritos] at [2007-11-18 13:38:03]
# 1 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
In C/C++, you can't have array indices that are negative. They are always zero-based. However, if you really need negative indices, you need have to write proxy class which translate the negative indices into zero-based indices. This, of course, leads to reduction of performance.
Kheun at 2007-11-9 0:27:44 >
# 2 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
If you know a lower bound before creating the structure, say -100, you could always add this offset for any array access. This would allow you to use std::vector.

If you don't know the lower bound, you could still accomplish this with a moving lower bound and use a std::deque, which works like a vector, but allows fast pushes onto the front.

If performance isn't critical (and it's usually not), you could use std::map. This would be the easiest to implement, and still plenty fast. Look ups are O(logn) instead of O(1), but unless you're in a tight loop doing calculations, you probably won't notice.

Jeff
jfaust at 2007-11-9 0:28:41 >
# 3 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
Just for the OP's benefit, in case it is unclear, the previous posts are the implementation details which a class might use to accomplish this. However, the interface step hasn't been mentioned, and it appears that really the question is one of getting a certain interface (since otherwise you could just transform the problem into an unsigned index in the first place). Just to fill in the details, then, you want your class to have something like

ArrayMemberType& operator[](signed index)

in order for you to use the array notation. If you put it in your header for inlining, you could conceivably have the class store whatever container is appropriate and a random-access iterator pointing at the 0 element. Then any element access is just a single addition and dereference away, just as it is for the normal array access. For a vector, with contiguous memory guarantee, the iterator is likely a bare pointer, and you should have no differences between the speed of such an implementation and a naked array.
galathaea at 2007-11-9 0:29:44 >
# 4 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
Good point, Galathaea. With numerous ways to achieve this, the interface does become more important. If you find later that the original implementation is too slow (or wrong), you can always reimplement it and you won't need to change any code outside your class.

You'll also want the const version of this to enable hassle-free access:

ArrayMemberType& operator[](int index);
const ArrayMemberType& operator[](int index) const;

Jeff
jfaust at 2007-11-9 0:30:38 >
# 5 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
#define LOW_BOUNDS (-100)
#define HIGH_BOUNDS (100)

/* ... */

int* array_base = (int*)malloc(sizeof(int)*(HIGH_BOUNDS - LOW_BOUNDS + 1));
int* my_int_array = array_base - LOW_BOUNDS;

/* Now you do something like:

int x = 5*my_int_array[-50];

*/

/* Cleanup: */
free(array_base);
KevinHall at 2007-11-9 0:31:45 >
# 6 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
Nice code, Kevin. That's definitely much more efficient.
Kheun at 2007-11-9 0:32:49 >
# 7 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
What do you think about that? It has been written with another friend's help. The elements are integers as I want.

class A
{
private:
SomethingElse x;
int items[10]; // some kind of data
int offset=20; // (10*4)/2
public:

int& operator[](signed int i)
{
return *(items + i<<2 + offset );
}
};

int main (void)
{
A a;
int data = a[-2];
int* d_ptr = &data;
return 0;
}
Demokritos at 2007-11-9 0:33:50 >
# 8 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
Rather than hard-coding the range for your indices, consider using a constructor for specifying the range.
Kheun at 2007-11-9 0:34:44 >
# 9 Re: arrays with negative and possitive indexes, fastest access?Elements are integers
KevinHall your code is exactly what I was looking for!
Thanks very much all of you.
Demokritos at 2007-11-9 0:35:49 >