* prefer HashSet and HashMap to respective std-classes where possible

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40518 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe 2011-02-15 18:52:03 +00:00
parent 07b37bb0a0
commit f5644b8d3b
3 changed files with 83 additions and 35 deletions

View File

@ -0,0 +1,70 @@
/*
* Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
* Distributed under the terms of the MIT License.
*/
#ifndef _PACKAGE__PRIVATE__HASHABLE_STRING_H_
#define _PACKAGE__PRIVATE__HASHABLE_STRING_H_
#include <String.h>
#include <HashString.h>
namespace BPackageKit {
namespace BPrivate {
class HashableString : public BString {
public:
inline HashableString();
inline HashableString(const BString& string);
inline uint32 GetHashCode() const;
inline bool operator!= (const HashableString& other) const;
private:
uint32 fHashCode;
};
inline
HashableString::HashableString()
:
fHashCode(0)
{
}
inline
HashableString::HashableString(const BString& string)
:
BString(string),
fHashCode(string_hash(String()))
{
}
inline uint32
HashableString::GetHashCode() const
{
return fHashCode;
}
inline bool
HashableString::operator!= (const HashableString& other) const
{
return Compare(other) != 0 || fHashCode != other.fHashCode;
}
} // namespace BPrivate
} // namespace BPackageKit
#endif // _PACKAGE__PRIVATE__HASHABLE_STRING_H_

View File

@ -19,12 +19,12 @@
#include <Path.h>
#include <HashMap.h>
#include <HashString.h>
#include <package/hpkg/ErrorOutput.h>
#include <package/hpkg/PackageInfoAttributeValue.h>
#include <package/hpkg/RepositoryContentHandler.h>
#include <package/hpkg/RepositoryReader.h>
#include <package/HashableString.h>
#include <package/PackageInfo.h>
#include <package/RepositoryInfo.h>
@ -32,40 +32,13 @@
namespace BPackageKit {
using BPrivate::HashableString;
using namespace BHPKG;
namespace {
struct HashableString : public BString {
uint32 hashCode;
HashableString()
:
hashCode(0)
{
}
HashableString(const BString& string_)
:
BString(string_),
hashCode(string_hash(String()))
{
}
inline uint32 GetHashCode() const
{
return hashCode;
}
inline bool operator!= (const HashableString& other) const
{
return Compare(other) != 0 || hashCode != other.hashCode;
}
};
typedef ::BPrivate::HashMap<HashableString, BPackageInfo> PackageHashMap;
struct RepositoryContentHandler : BRepositoryContentHandler {

View File

@ -8,13 +8,13 @@
#include <algorithm>
#include <new>
#include <set>
#include <ByteOrder.h>
#include <Message.h>
#include <Path.h>
#include <AutoDeleter.h>
#include <HashSet.h>
#include <package/hpkg/HPKGDefsPrivate.h>
#include <package/hpkg/PackageDataReader.h>
@ -23,6 +23,7 @@
#include <package/hpkg/PackageReader.h>
#include <package/BlockBufferCacheNoLock.h>
#include <package/ChecksumAccessors.h>
#include <package/HashableString.h>
#include <package/RepositoryInfo.h>
@ -34,6 +35,7 @@ namespace BPrivate {
using BPackageKit::BPrivate::GeneralFileChecksumAccessor;
using BPackageKit::BPrivate::HashableString;
namespace {
@ -254,7 +256,8 @@ private:
} // anonymous namespace
struct RepositoryWriterImpl::PackageNameSet : public std::set<BString> {
struct RepositoryWriterImpl::PackageNameSet
: public ::BPrivate::HashSet<HashableString> {
};
@ -281,6 +284,9 @@ RepositoryWriterImpl::Init(const char* fileName)
{
try {
fPackageNames = new PackageNameSet();
status_t result = fPackageNames->InitCheck();
if (result != B_OK)
return result;
return _Init(fileName);
} catch (status_t error) {
return error;
@ -417,9 +423,7 @@ RepositoryWriterImpl::_RegisterCurrentPackageInfo()
}
// reject package with a name that we've seen already
PackageNameSet::const_iterator namePos
= fPackageNames->find(fPackageInfo.Name());
if (namePos != fPackageNames->end()) {
if (fPackageNames->Contains(fPackageInfo.Name())) {
fListener->PrintError("package %s has already been added!\n",
fPackageInfo.Name().String());
return B_NAME_IN_USE;
@ -449,7 +453,8 @@ RepositoryWriterImpl::_RegisterCurrentPackageInfo()
return B_BAD_DATA;
}
fPackageNames->insert(fPackageInfo.Name());
if ((result = fPackageNames->Add(fPackageInfo.Name())) != B_OK)
return result;
RegisterPackageInfo(PackageAttributes(), fPackageInfo);
fPackageCount++;