Sims said:
Pete Becker wrote:
.....
Well all that is missing is the fact that they are not in the right order.
A bit more like..
0, 0 <- new 'sub' chunk
E, F
Z, X
0, 0 <- new 'sub' chunk
A, B
C, D
E, F
0, 0 <- new 'sub' chunk
Z, X
A, B
And further more, as I said there is 000's of chunks of data all in
different order mixed up all within each others.
I would create a base class that describes a segment.
struct Node
{
int values[2];
bool operator <( const Node & rhs ) const;
};
struct Segment
{
virtual unsigned Size() const = 0;
const Node * First() const = 0;
const Node * Last() const = 0;
virtual IsBeginnning() const = 0;
virtual const Node & Key() const = 0;
virtual void SetNext( const Segment * ) = 0;
bool Less( const Segment * rhs ) const
{
const Node & KeyLhs = this->Key();
const Node & KeyRhs = rhs->Key();
if ( KeyLhs < KeyRhs )
{
return true;
}
if ( KeyRhs < KeyLhs )
{
return false;
}
return this->IsBeginnning() > rhs->IsBeginnning();
}
};
struct Segment_End : Segment
{
const Segment * m_next;
Segment_End()
: m_next( 0 )
{
}
void SetNext( const Segment * i_next )
{
m_next = i_next;
}
virtual IsBeginnning() const
{
return false;
}
virtual const Node & Key() const
{
return * ( Last() -1 );
}
};
struct Segment_Begin : Segment
{
Segment ** m_pos;
Segment_Begin()
: m_pos( 0 )
{
}
void SetNext( const Segment * i_next )
{
m_next = i_next;
}
virtual IsBeginnning() const
{
return true;
}
virtual const Node & Key() const
{
return * ( First() );
}
};
template <unsigned N>
struct Segment_Basic : Segment_Begin, Segment_End
{
Node m_nodes[ N ];
Segment_Basic( const Node * first, const Node * last )
{
std::copy( first, last, m_nodes );
}
Segment_Basic()
{
// prolly should clear m_nodes
}
unsigned Size() const
{
return N;
}
const Node * First() const
{
return m_nodes;
}
const Node * Last() const
{
return m_nodes + N;
}
};
// -- or if you already have a monster array somewhere
// -- just refer to it
struct Segment_Array : Segment_Begin, Segment_End
{
const Node * m_first;
const Node * m_last;
Segment_Basic( const Node * first, const Node * last )
: m_first( first ), m_last( last )
{
}
unsigned Size() const
{
return m_last - m_first;
}
const Node * First() const
{
return m_first
}
const Node * Last() const
{
return m_last;
}
};
Then you place create a Segment_Array or Segment_Basic object for each
segment followed by inserting a Segment_Begin and Segment_End pointer
into an array (of Segment *) and then sort them by the "Less" function,
then traverse the sorted array and call the "Next" method for each
segment. From there you can trivially traverse the segments and in the
process create a second array that has all the segments spliced together.
I'm sure you could probably do this faster than this and I probably
missed a few things but it's performance probably won't suck too badly.