Re: Providing a no-overhead way for a contained class to access its
container?
PeteOlcott schrieb:
Here is what I mean. For this specific use it is about the highest
performance (space and time) possible. A Large number of Contained
elements can be read in and written to disk as a single block read or
block write. Also all of the extra memory allocation overhead that
would normally be associated with the individual Contained elements
has been reduced to making two large allocations.
#define uint8 unsigned char
#define uint32 unsigned int
typedef unsigned char uint8;
typedef unsigned int uint32;
ContainedClass {
uint32 Offset;
bool operator<(const ContainedClass& CC);
}
ContainerClass {
uint32 Length; // All ContainedClass elements are the same Length
std::vector<uint8> Bytes;
std::vector<ContainedClass> Contained;
uint8& operator[](uint32 N){ return Bytes[N]; };
void sort(){ std::sort(Contained.begin(), Contained.end()) };
} Container;
This code doesn't compile, but assuming a working version of this code,
you could use std::sort with a functor with a pointer to its contained
class (untested! code):
struct ContainedLess
{
ContainedLess(const ContainerClass* container_) :
container(container_) {}
bool operator()(const ContainedClass& c1, const ContainedClass& c2)
const
{
// compare c1 and c2, use member 'container'
// to access container class
const uint8* p1 = &container->Bytes[c1.Offset];
const uint8* p2 = &container->Bytes[c2.Offset];
const uint8* end = p1 + container->Length;
for (; p1 != end; ++p1, ++p2)
{
// ...
}
}
private:
const ContainerClass* container;
};
void ContainerClass::sort()
{
std::sort(Contained.begin(), Contained.end(), ContainedLess(this));
}
It also would be a good idea to make some members of the classes private.
bool ContainedClass::operator<(const ContainedClass& CC)
{
uint32 Last = this->Offset + Container.Length;
for (int N = this->Offset, M = CC.Offset; N < Last; N++, M++)
if (Container[N] < Container[M])
return true;
else if (Container[N] > Container[M])
return false;
return false; // They must be Equal, thus Not(LessThan)
}
--
Thomas