Skip to content

Commit

Permalink
PathTree: Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 3, 2012
1 parent 28f73cf commit cf94d99
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 68 deletions.
83 changes: 25 additions & 58 deletions doomsday/engine/portable/include/pathtree.h
Expand Up @@ -60,7 +60,7 @@ extern "C" {
* @defgroup pathTreeFlags Path Tree Flags
*/
///@{
#define PDF_ALLOW_DUPLICATE_LEAF 0x1 ///< There can be more than one leaf with a given name.
#define PATHTREE_MULTI_LEAF 0x1 ///< There can be more than one leaf with a given name.
///@}

#ifdef __cplusplus
Expand Down Expand Up @@ -123,8 +123,8 @@ class PathTree
static ushort hashPathFragment(char const* fragment, size_t len, char delimiter = '/');

#if _DEBUG
static void debugPrint(PathTree& pt, char delimiter = '/');
static void debugPrintHashDistribution(PathTree& pt);
static void debugPrint(PathTree& pathtree, char delimiter = '/');
static void debugPrintHashDistribution(PathTree& pathtree);
#endif

/**
Expand All @@ -142,15 +142,14 @@ class PathTree
/// @return PathTree which owns this node.
PathTree& tree() const;

/// @return Parent of this node else @c NULL
/// @return Parent of this node else @c NULL.
Node* parent() const;

/// @return @c true iff this node is a leaf.
bool isLeaf() const;

/// @return Type of this node.
inline NodeType type() const
{
inline NodeType type() const {
return isLeaf()? Leaf : Branch;
}

Expand All @@ -160,22 +159,6 @@ class PathTree
/// @return Hash for this node's path fragment.
ushort hash() const;

/// @return User-specified custom pointer.
void* userPointer() const;

/// @return User-specified custom value.
int userValue() const;

/**
* Sets the user-specified custom pointer.
*/
Node& setUserPointer(void* ptr);

/**
* Sets the user-specified custom pointer.
*/
Node& setUserValue(int value);

/**
* @param candidatePath Mapped search pattern (path).
* @param flags @ref pathComparisonFlags
Expand Down Expand Up @@ -206,6 +189,22 @@ class PathTree
*/
ddstring_t* composePath(ddstring_t* path, int* length, char delimiter = '/') const;

/**
* Sets the user-specified custom pointer.
*/
Node& setUserPointer(void* ptr);

/// @return User-specified custom pointer.
void* userPointer() const;

/**
* Sets the user-specified custom pointer.
*/
Node& setUserValue(int value);

/// @return User-specified custom value.
int userValue() const;

friend class PathTree;
friend struct PathTree::Instance;

Expand Down Expand Up @@ -260,17 +259,6 @@ class PathTree
/**
* Find a single node in the hierarchy.
*
* @note This method essentially amounts to "interface sugar". A convenient
* shorthand of the call tree:
*
* <pre>
* PathMap search;
* PathMap_Initialize(&search, @a flags, @a searchPath, @a delimiter);
* foundNode = PathTree_Search("this", &search, PathTreeNode_ComparePath);
* PathMap_Destroy(&search);
* return foundNode;
* </pre>
*
* @param flags @ref pathComparisonFlags
* @param path Relative or absolute path to be searched for.
* @param delimiter Names in the composed @a path hierarchy are delimited
Expand Down Expand Up @@ -345,7 +333,7 @@ int PathTree_Size(PathTree* pt);
void PathTree_Clear(PathTree* pt);

PathTreeNode* PathTree_Insert2(PathTree* pt, char const* path, char delimiter/*, userData = 0*/);
PathTreeNode* PathTree_Insert(PathTree* pt, char const* path/*, delimiter = '/'*/);
PathTreeNode* PathTree_Insert (PathTree* pt, char const* path/*, delimiter = '/'*/);

/**
* Callback function type for PathTree::Iterate
Expand All @@ -372,29 +360,8 @@ typedef int (*pathtree_iterateconstcallback_t) (PathTreeNode const* node, void*
*/
typedef int (*pathtree_searchcallback_t) (PathTreeNode* node, int flags, PathMap* mappedSearchPath, void* parameters);

/**
* Perform a search of the nodes in the hierarchy making a callback for each.
* Pre-selection of nodes is determined by @a mappedSearchPath. Iteration ends when
* all selected nodes have been visited or a callback returns non-zero.
*
* This method essentially amounts to "interface sugar". A manual search of the
* hierarchy can be performed using nodes pre-selected by the caller or using
* @see PathTree_Iterate().
*
* @param pt PathTree instance.
* @param flags @ref pathComparisonFlags
* @param mappedSearchPath Fragment mapped search path.
* @param callback Callback function ptr. The callback should only return
* a non-zero value when the desired node has been found.
* @param parameters Passed to the callback.
*
* @return @c 0 iff iteration completed wholly.
*/
PathTreeNode* PathTree_Search2(PathTree* pt, int flags, PathMap* mappedSearchPath, pathtree_searchcallback_t callback, void* parameters);
PathTreeNode* PathTree_Search (PathTree* pt, int flags, PathMap* mappedSearchPath, pathtree_searchcallback_t callback/*, parameters = 0*/);

PathTreeNode* PathTree_Find2(PathTree* pt, int flags, char const* path, char delimiter);
PathTreeNode* PathTree_Find(PathTree* pt, int flags, char const* path/*, delimiter = '/'*/);
PathTreeNode* PathTree_Find (PathTree* pt, int flags, char const* path/*, delimiter = '/'*/);

/**
* Iterate over nodes in the hierarchy making a callback for each. Iteration ends
Expand All @@ -419,7 +386,7 @@ int PathTree_Iterate2_Const(PathTree const* pt, int flags, PathTreeNode const* p
int PathTree_Iterate_Const (PathTree const* pt, int flags, PathTreeNode const* parent, ushort hash, pathtree_iterateconstcallback_t callback/*, parameters = 0*/);

ushort PathTree_HashPathFragment2(char const* fragment, size_t len, char delimiter);
ushort PathTree_HashPathFragment(char const* fragment, size_t len/*, delimiter = '/'*/);
ushort PathTree_HashPathFragment (char const* fragment, size_t len/*, delimiter = '/'*/);

#if _DEBUG
void PathTree_DebugPrint(PathTree* pt, char delimiter);
Expand All @@ -436,7 +403,7 @@ void PathTreeNode_SetUserPointer(PathTreeNode* node, void* ptr);
void PathTreeNode_SetUserValue(PathTreeNode* node, int value);
int PathTreeNode_ComparePath(PathTreeNode* node, int flags, PathMap* candidatePath);
ddstring_t* PathTreeNode_ComposePath2(PathTreeNode const* node, ddstring_t* path, int* length, char delimiter);
ddstring_t* PathTreeNode_ComposePath(PathTreeNode const* node, ddstring_t* path, int* length/*, delimiter = '/'*/);
ddstring_t* PathTreeNode_ComposePath (PathTreeNode const* node, ddstring_t* path, int* length/*, delimiter = '/'*/);

#ifdef __cplusplus
} // extern "C"
Expand Down
13 changes: 5 additions & 8 deletions doomsday/engine/portable/src/pathtree.cpp
Expand Up @@ -27,13 +27,10 @@
#include <de/Error>
#include <de/Log>
#include <de/stringpool.h>
#include <de/memory.h>
#if 0
# include "blockset.h"
#endif
#if 0
# include "de_console.h"
# include "de_system.h"
#if _DEBUG
# include "m_misc.h" // For M_NumDigits()
#endif

Expand Down Expand Up @@ -152,7 +149,7 @@ struct PathTree::Instance
if(parent != node->parent()) continue;
if(fragmentId != node->fragmentId()) continue;

if(nodeType == PathTree::Branch || !(flags & PDF_ALLOW_DUPLICATE_LEAF))
if(nodeType == PathTree::Branch || !(flags & PATHTREE_MULTI_LEAF))
return node;
}
}
Expand Down Expand Up @@ -275,7 +272,7 @@ struct PathTree::Instance
#if _DEBUG
if(node.userPointer())
{
LOG_ERROR("Node %p has non-NULL user data.") << (void*)(&node);
LOG_ERROR("Node %p has non-NULL user data.") << de::dintptr(&node);
}
#endif
deleteNode(node);
Expand Down Expand Up @@ -446,7 +443,7 @@ int PathTree::findAllPaths(FoundPaths& found, int flags, char delimiter)
void PathTree::debugPrint(PathTree& pt, char delimiter)
{
LOG_AS("PathTree");
LOG_INFO("Directory [%p]:") << de::dintptr(&pt);
LOG_INFO("[%p]:") << de::dintptr(&pt);
FoundPaths found;
if(pt.findAllPaths(found, 0, delimiter))
{
Expand All @@ -457,7 +454,7 @@ void PathTree::debugPrint(PathTree& pt, char delimiter)
LOG_INFO(" %s") << *i;
}
}
LOG_INFO(" %i %s in directory.") << found.count() << (found.count() == 1? "path" : "paths");
LOG_INFO(" %i unique %s in the tree.") << found.count() << (found.count() == 1? "path" : "paths");
}

#if 0
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/wad.cpp
Expand Up @@ -315,7 +315,7 @@ struct Wad::Instance
Str_Reserve(Str_Init(&absPath), LUMPNAME_T_LASTINDEX + 4/*.lmp*/);

// Intialize the directory.
lumpDirectory = new PathTree(PDF_ALLOW_DUPLICATE_LEAF);
lumpDirectory = new PathTree(PATHTREE_MULTI_LEAF);

// Build our runtime representation from the archived lump directory.
wadlumprecord_t const* arcRecord = arcRecords;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/zip.cpp
Expand Up @@ -383,7 +383,7 @@ struct Zip::Instance
if(entryCount == 0) break;

// Intialize the directory.
lumpDirectory = new PathTree(PDF_ALLOW_DUPLICATE_LEAF);
lumpDirectory = new PathTree(PATHTREE_MULTI_LEAF);
}

// Position the read cursor at the start of the buffered central centralDirectory.
Expand Down

0 comments on commit cf94d99

Please sign in to comment.