DATextTable.cpp #1

  • //
  • guest/
  • sam_stafford/
  • deepannotate/
  • DATextTable.cpp
  • View
  • Commits
  • Open Download .zip Download (2 KB)
#include "DATextTable.h"

#include <stdhdrs.h>
#include <strbuf.h>
#include <vararray.h>

class DATextNode
{
public:
	DATextNode( const StrPtr& t ) 
	{ 
		left = right = 0x0; 
		id = 0;
		text.Set( t );
	};
	DATextNode* getNodeFor( const StrPtr& t );
	StrBuf text;
	int id;
	DATextNode* left;
	DATextNode* right;
};

DATextTable::DATextTable(void)
{
	idArray = new VarArray;
	textTree = 0x0;

	//Reserve id 0 to use as handy "null" value.
	idArray->Put( 0x0 );
}

DATextTable::~DATextTable(void)
{
	DATextNode* n;
	for ( int i = 0 ; i < idArray->Count() ; i++ )
	{
		n = (DATextNode*)idArray->Get( i );
		delete n;
	}
	delete idArray;
}

const StrPtr& DATextTable::getText( int id )
{
	if ( id < 1 || id >= idArray->Count() ) return StrRef::Null();

	DATextNode* n = (DATextNode*)idArray->Get( id );
	return n->text;
}

int DATextTable::getId( const StrPtr& text )
{
	DATextNode* n;
	if ( !textTree )
	{
		n = new DATextNode( text );
		indexNode( n );
		textTree = n;
		return n->id;
	}

	n = textTree->getNodeFor( text );
	if ( n->id ) return n->id;

	indexNode( n );
	return n->id;	
}

DATextNode* DATextNode::getNodeFor( const StrPtr& t )
{
	int c = text.XCompare( t );
	if ( c > 0 )
	{
		if ( left ) return left->getNodeFor( t );

		left = new DATextNode( t );
		return left;
	}
	else if ( c < 0 )
	{
		if ( right ) return right->getNodeFor( t );

		right = new DATextNode( t );
		return right;
	}
	else //equal
	{
		return this;
	}
}

void DATextTable::indexNode( DATextNode* n )
{
	n->id = idArray->Count();
	idArray->Put( n );
}
# Change User Description Committed
#1 6297 Sam Stafford Work so far on "deep annotate".
 Been getting a lot of questions on
this lately from other people working on the same thing; might as well
pool efforts.