#ifndef __RA_ARRAY__
#define __RA_ARRAY__
#include <intrin.h>
#define ULONG unsigned long
template <typename T>
class random_access_array{
const ULONG elements;
const ULONG R;
ULONG a;
ULONG c;
T* buffer;
const ULONG nextPowerOf2(ULONG n)const{
if ((n & (n - 1)) == 0)
return n;
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return n + 1;
}
ULONG LCG(ULONG X, ULONG a, ULONG c, ULONG R){
return (a*X + c) % R;
}
ULONG randmax(ULONG max){
return LCG((ULONG) __rdtsc(), 0x43FD43FDUL, 0xC39EC3UL, max);
}
size_t getRealIdx(int index){
ULONG X = 0;
while (index-- >= 0)
while ((X = LCG(X, a, c, R)) >= elements){}
return X;
}
public:
random_access_array(T* buffer, size_t elements) : elements(elements), buffer(buffer), R(nextPowerOf2(elements)){
a = 2 * randmax(R / 2) + 1;
c = 4 * randmax(R / 4) + 1;
}
random_access_array(T* buffer, size_t elements, ULONG a, ULONG c) : elements(elements), buffer(buffer), R(nextPowerOf2(elements)), a(a), c(c){}
T& operator [](size_t i){
return buffer[getRealIdx(i)];
}
T operator [](const size_t i) const{
return buffer[getRealIdx(i)];
}
};
#endif//__RA_ARRAY__
Ejemplo de uso:
#define SIZE 573
random_access_array<int> ra(new int[SIZE], SIZE);
for (int i = 0; i < SIZE; i++)
ra[i] = i;
for (int i = 0; i < SIZE; i++)
cout << ra[i] << endl;
Utilidad real:
#define SIZE sizeof(s) - 1
char s[] = "!eru!akklccr !rza";
random_access_array<char> ra(s, SIZE, 5, 25);
for (int i = 0; i < SIZE; i++)
cout << ra[i];
Saludos :)
Pense que lo harias de manera mas inteligente... No validas la longitud del array dentro del template.
¿Como voy a validar el tamaño del array? Es el usuario quien me lo da.
Si el índice que se pasa es superior al tamaño no importa ya que si te fijas verás que getRealIdx() sólo devuelve un índice dentro del rango.
No habría jamás un buffer overflow.
:P no habia visto la validación.