私はここでちょっとした思考実験に取り組んでいます。実際にここでの生活を楽にしようとしています。私は、とりわけ、ソートされた順序で保持されている要素の配列をいくつか含むデータ構造を扱っています。これらのデータ構造を固定サイズのブロックに割り当てて、メモリ配置を容易にし、(将来) 安定したストレージからの読み書きを容易にします。これまでに取り組んでいるコードは次のとおりです。
#include <limits>
const int NODE_SIZE = 512;
template <typename K, typename D>
class Node {
long next;
short num;
K* keys;
D* data;
public:
Node( int l, int order );
};
// num is calculated by something like this...
num = NODE_SIZE - sizeof( Node<K,D> ) - sizeof( long );
num /= (sizeof( D ) + sizeof( K ));
// Constructor
// Will be called with a placement-new and given a NODE_SIZE
// byte block of memory, aligned at NODE_SIZE
template<typename K, typename D>
Node<K,D>::Node( int n ) : num ( n ), next( 0 ) {
keys = reinterpret_cast<K*>(reinterpret_cast<char*>(&next) +
sizeof( *this ));
int numbytes = num*sizeof(K);
// Make sure we're aligned to a void *.
if ( numbytes % sizeof( void * ) ) {
numbytes = (numbytes / sizeof( void * )+1)*sizeof( void * );
}
// Align to the number of bytes in a void *
data = reinterpret_cast<D*>( reinterpret_cast<char*>(keys)+numbytes);
for( int i=0; i<num; i++ ) keys[i] = std::numeric_limits<K>::max();
}
キーの要素はソートされた順序になっているため、std::vector と std::vector を使用できるようにして、自分で作成する代わりに他の誰かのベクトル挿入コードを使用できるようにしたいと考えています (難しいというわけではありませんが、なぜ車輪を再発明するのですか?)。
また、キーとデータのポインターを設定するよりクリーンな方法はありますか? 任意の支援や提案を歓迎します。