#ifndef _T_JStringPtrMap
#define _T_JStringPtrMap

/******************************************************************************
 JStringPtrMap.tmpl

	A convenience for maps which store pointers.  May be folded into
	JStringMap rather than being a separate class.

	BASE CLASS = public JStringMap

	Copyright © 1998 by Dustin Laurence.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

 *****************************************************************************/

//Class Header
#include <JStringPtrMap.h>
#include <JStringPtrMapCursor.h>

#include <jAssert.h>

/******************************************************************************
 Constructor

 *****************************************************************************/

template <class V>
JStringPtrMap<V>::JStringPtrMap
	(
	const JBoolean copyKeys
	)
	:
	JStringMap<V*>(copyKeys)
{
}

template <class V>
JStringPtrMap<V>::JStringPtrMap
	(
	const JSize    lgSize,  // = kJDefaultLgMinTableSize
	const JBoolean copyKeys // = kTrue
	)
	:
	JStringMap<V*>(lgSize, copyKeys)
{
}

/******************************************************************************
 Destructor

 *****************************************************************************/

template <class V>
JStringPtrMap<V>::~JStringPtrMap()
{
}

/******************************************************************************
 DeleteElement

 *****************************************************************************/

template <class V>
JBoolean
JStringPtrMap<V>::DeleteElement
	(
	const JCharacter* const key
	)
{
	JHashCursor< JStrValue<V*> >* cursor = GetCursor();
	// We know the hash value does not depend on V, so this is safe
	JStrValue<V*> hashEntry;
	hashEntry.key = key;
	cursor->ResetKey(hashEntry);
	if ( cursor->NextKey() )
		{
		delete cursor->GetValue().value;
		if ( KeysAreCopied() )
			{
			delete[] const_cast<JCharacter*>(cursor->GetValue().key);
			}
		cursor->Remove();
		return kTrue;
		}
	else
		{
		return kFalse;
		}
}

/******************************************************************************
 DeleteElementAsArray

 *****************************************************************************/

template <class V>
JBoolean
JStringPtrMap<V>::DeleteElementAsArray
	(
	const JCharacter* const key
	)
{
	JHashCursor< JStrValue<V*> >* cursor = GetCursor();
	// We know the hash value does not depend on V, so this is safe
	JStrValue<V*> hashEntry;
	hashEntry.key = key;
	cursor->ResetKey(hashEntry);
	if ( cursor->NextKey() )
		{
		delete[] cursor->GetValue().value;
		if ( KeysAreCopied() )
			{
			delete[] const_cast<JCharacter*>(cursor->GetValue().key);
			}
		cursor->Remove();
		return kTrue;
		}
	else
		{
		return kFalse;
		}
}

/******************************************************************************
 DeleteAll

 *****************************************************************************/

template <class V>
void
JStringPtrMap<V>::DeleteAll()
{
	if (GetElementCount() > 0)
		{
		// delete, but don't mark empty to avoid multiple resizing
		JStringPtrMapCursor<V> cursor(this);
		while ( cursor.Next() )
			{
			delete cursor.GetValue();
			if ( KeysAreCopied() )
				{
				delete[] const_cast<JCharacter*>(cursor.GetKey());
				}
			}
		}

	// Mark all at once and possibly resize; we want to call even if the
	// hash table is already empty to zero out the load count
	MarkAllEmpty();
}

/******************************************************************************
 DeleteAllAsArray

 *****************************************************************************/

template <class V>
void
JStringPtrMap<V>::DeleteAllAsArray()
{
	if (GetElementCount() > 0)
		{
		// delete, but don't mark empty to avoid multiple resizing
		JStringPtrMapCursor<V> cursor(this);
		while ( cursor.Next() )
			{
			delete[] cursor.GetValue();
			if ( KeysAreCopied() )
				{
				delete[] const_cast<JCharacter*>(cursor.GetKey());
				}
			}
		}

	// Mark all at once and possibly resize; we want to call even if the
	// hash table is already empty to zero out the load count
	MarkAllEmpty();
}

#endif

// Instantiate the template for the specified type.

#if defined JTemplateType && ! defined JOnlyWantTemplateDefinition
	#define JTemplateName JStringPtrMap
	#include <instantiate_template.h>
	#undef JTemplateName
#endif
