Skip to content

Commit

Permalink
LumpIndex|Refactor: Added firstIndexForPath()
Browse files Browse the repository at this point in the history
Locate the first path in the index which matches the search term.
  • Loading branch information
danij-deng committed Nov 26, 2012
1 parent ccce424 commit 2fe7927
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
7 changes: 5 additions & 2 deletions doomsday/engine/include/filesys/lumpindex.h
Expand Up @@ -74,8 +74,11 @@ class LumpIndex
/// @return @c true iff @a lumpNum can be interpreted as a valid lump index.
bool isValidIndex(lumpnum_t lumpNum) const;

/// @return Index associated with the last lump with variable-length @a path if found else @c -1
lumpnum_t indexForPath(Uri const& path) const;
/// Returns the index associated to the last lump matching @a path; otherwise @c -1.
lumpnum_t lastIndexForPath(Path const& path) const;

/// Returns the index associated to the first lump matching @a path; otherwise @c -1.
lumpnum_t firstIndexForPath(Path const &path) const;

/**
* Lookup a file at specific offset in the index.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/src/filesys/fs_main.cpp
Expand Up @@ -244,7 +244,7 @@ struct FS1::Instance
}

// First check the Zip lump index.
lumpnum_t lumpNum = zipFileIndex.indexForPath(de::Uri(path, RC_NULL));
lumpnum_t lumpNum = zipFileIndex.lastIndexForPath(path);
if(lumpNum >= 0)
{
return &zipFileIndex.lump(lumpNum);
Expand Down Expand Up @@ -731,7 +731,7 @@ lumpnum_t FS1::lumpNumForName(String name)
}

// Perform the search.
return d->primaryIndex.indexForPath(de::Uri(name, RC_NULL));
return d->primaryIndex.lastIndexForPath(Path(name));
}

void FS1::releaseFile(de::File1& file)
Expand Down
34 changes: 30 additions & 4 deletions doomsday/engine/src/filesys/lumpindex.cpp
Expand Up @@ -371,9 +371,9 @@ bool LumpIndex::catalogues(File1& file)
return false;
}

lumpnum_t LumpIndex::indexForPath(de::Uri const& search) const
lumpnum_t LumpIndex::lastIndexForPath(Path const& path) const
{
if(search.isEmpty() || d->lumps.empty()) return -1;
if(path.isEmpty() || d->lumps.empty()) return -1;

// We may need to prune path-duplicate lumps.
d->pruneDuplicates();
Expand All @@ -383,15 +383,15 @@ lumpnum_t LumpIndex::indexForPath(de::Uri const& search) const
DENG_ASSERT(d->hashMap);

// Perform the search.
ushort hash = search.path().lastSegment().hash() % d->hashMap->size();
ushort hash = path.lastSegment().hash() % d->hashMap->size();
if((*d->hashMap)[hash].head == -1) return -1;

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();

if(node.comparePath(search.path(), 0)) continue;
if(node.comparePath(path, 0)) continue;

// This is the lump we are looking for.
return idx;
Expand All @@ -400,6 +400,32 @@ lumpnum_t LumpIndex::indexForPath(de::Uri const& search) const
return -1;
}

/// @todo Make use of the hash!
lumpnum_t LumpIndex::firstIndexForPath(Path const &path) const
{
if(path.isEmpty() || d->lumps.empty()) return -1;

// We may need to prune path-duplicate lumps.
d->pruneDuplicates();

// We may need to rebuild the path hash map.
d->buildHashMap();
DENG_ASSERT(d->hashMap);

// Perform the search.
for(lumpnum_t idx = 0; idx < d->lumps.size(); ++idx)
{
File1 const& lump = *d->lumps[idx];
PathTree::Node const& node = lump.directoryNode();

if(node.comparePath(path, 0)) continue;

// This is the lump we are looking for.
return idx;
}
return -1;
}

void LumpIndex::print(LumpIndex const& index)
{
int const numRecords = index.size();
Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/src/filesys/metafile.cpp
Expand Up @@ -29,6 +29,8 @@
#include "resource/wad.h"
#include "resource/zip.h"

#include <de/Path>

namespace de {

struct MetaFile::Instance
Expand Down Expand Up @@ -166,7 +168,7 @@ static lumpnum_t lumpNumForIdentityKey(LumpIndex const& lumpIndex, String idKey)
name += ".lmp";
}

lumpnum_t lumpNum = lumpIndex.indexForPath(de::Uri(name, RC_NULL));
lumpnum_t lumpNum = lumpIndex.lastIndexForPath(Path(name));
if(lumpNum < 0) return -1;

// Check the condition.
Expand Down

0 comments on commit 2fe7927

Please sign in to comment.