Skip to content

Commit

Permalink
Refactor: Added PathMap C++ ctor/dtor plus minor PathTree refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 3, 2012
1 parent 627266d commit ee7e071
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 235 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/engine.pro
Expand Up @@ -556,7 +556,7 @@ SOURCES += \
portable/src/p_sight.c \
portable/src/p_think.c \
portable/src/p_ticker.c \
portable/src/pathmap.c \
portable/src/pathmap.cpp \
portable/src/pathtree.cpp \
portable/src/pathtreenode.cpp \
portable/src/plane.c \
Expand Down
15 changes: 10 additions & 5 deletions doomsday/engine/portable/include/pathmap.h
Expand Up @@ -65,7 +65,7 @@ typedef struct pathmapfragment_s {
*
* @return The resultant hash key.
*/
typedef ushort (*hashpathfragmentcallback_t)(char const* path, size_t len, char delimiter);
typedef ushort (*hashpathfragmentfunc_t)(char const* path, size_t len, char delimiter);

typedef struct pathmap_s {
char const* path; ///< The mapped path.
Expand All @@ -88,8 +88,13 @@ typedef struct pathmap_s {
///< Head of the linked list of "extra" fragments, in reverse order.
PathMapFragment* extraFragments;

/// Path fragment hashing callback.
hashpathfragmentcallback_t hashFragmentCallback;
/// Fragment hashing function.
hashpathfragmentfunc_t hashFragmentFunc;

#ifdef __cplusplus
pathmap_s(hashpathfragmentfunc_t hashFragmentFunc, char const* path, char delimiter = '/');
~pathmap_s();
#endif
} PathMap;

/**
Expand All @@ -108,8 +113,8 @@ typedef struct pathmap_s {
*
* @return Pointer to "this" instance for caller convenience.
*/
PathMap* PathMap_Initialize2(PathMap* pathMap, hashpathfragmentcallback_t hashFragmentCallback, char const* path, char delimiter);
PathMap* PathMap_Initialize(PathMap* pathMap, hashpathfragmentcallback_t hashFragmentCallback, char const* path/*, delimiter = '/'*/);
PathMap* PathMap_Initialize2(PathMap* pathMap, hashpathfragmentfunc_t hashFragmentFunc, char const* path, char delimiter);
PathMap* PathMap_Initialize(PathMap* pathMap, hashpathfragmentfunc_t hashFragmentFunc, char const* path/*, delimiter = '/'*/);

/**
* Destroy @a pathMap releasing any resources acquired for it.
Expand Down
10 changes: 8 additions & 2 deletions doomsday/engine/portable/include/pathtree.h
Expand Up @@ -145,8 +145,14 @@ class PathTree
/// @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.
NodeType type() const;
inline NodeType type() const
{
return isLeaf()? Leaf : Branch;
}

/// @return Name for this node's path fragment.
ddstring_t const* name() const;
Expand Down Expand Up @@ -181,7 +187,7 @@ class PathTree
* allow for further optimizations elsewhere (in the file system
* for example) -ds
*/
int comparePath(PathMap* candidatePath, int flags) const;
int comparePath(PathMap& candidatePath, int flags) const;

/**
* Composes and/or calculates the length of the composed path for this node.
Expand Down
9 changes: 3 additions & 6 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -926,8 +926,7 @@ int FS1::findAllPaths(char const* rawSearchPattern, int flags, FS1::PathList& fo
// if not already absolute.
F_PrependBasePath(searchPattern, searchPattern);

PathMap patternMap;
PathMap_Initialize(&patternMap, PathTree::hashPathFragment, Str_Text(searchPattern));
PathMap patternMap = PathMap(PathTree::hashPathFragment, Str_Text(searchPattern));

/*
* Check the Zip directory.
Expand All @@ -946,7 +945,7 @@ int FS1::findAllPaths(char const* rawSearchPattern, int flags, FS1::PathList& fo
}
else
{
patternMatched = node.comparePath(&patternMap, PCF_MATCH_FULL);
patternMatched = node.comparePath(patternMap, PCF_MATCH_FULL);
}

if(!patternMatched) continue;
Expand All @@ -957,11 +956,9 @@ int FS1::findAllPaths(char const* rawSearchPattern, int flags, FS1::PathList& fo
filePath = lump.composePath();
}

found.push_back(PathListItem(Str_Text(filePath), node.type() == PathTree::Branch? A_SUBDIR : 0));
found.push_back(PathListItem(Str_Text(filePath), !node.isLeaf()? A_SUBDIR : 0));
}

PathMap_Destroy(&patternMap);

/*
* Check the dir/WAD direcs.
*/
Expand Down
28 changes: 9 additions & 19 deletions doomsday/engine/portable/src/lumpindex.cpp
Expand Up @@ -385,34 +385,24 @@ lumpnum_t LumpIndex::indexForPath(char const* path)
DENG_ASSERT(d->hashMap);

// Perform the search.
bool builtSearchPattern = false;
PathMap searchPattern;

int hash = PathTree::hashPathFragment(path, strlen(path)) % d->hashMap->size();
int idx;
for(idx = (*d->hashMap)[hash].head; idx != -1; idx = (*d->hashMap)[idx].next)
if((*d->hashMap)[hash].head == -1) return -1;

PathMap searchPattern = PathMap(PathTree::hashPathFragment, path);

for(int idx = (*d->hashMap)[hash].head; idx != -1; idx = (*d->hashMap)[idx].next)
{
File1 const& lump = *d->lumps[idx];
PathTree::Node const& node = lump.directoryNode();

// Time to build the pattern?
if(!builtSearchPattern)
{
PathMap_Initialize(&searchPattern, PathTree::hashPathFragment, path);
builtSearchPattern = true;
}
if(!node.comparePath(searchPattern, 0)) continue;

if(node.comparePath(&searchPattern, 0))
{
// This is the lump we are looking for.
break;
}
// This is the lump we are looking for.
return idx;
}

if(builtSearchPattern)
PathMap_Destroy(&searchPattern);

return idx;
return -1;
}

void LumpIndex::print(LumpIndex const& index)
Expand Down

0 comments on commit ee7e071

Please sign in to comment.