1
/***************************************************************************
2
copyright : (C) 2006 by David Nolden
3
email : david.nolden.kdevelop@art-master.de
4
***************************************************************************/
6
/***************************************************************************
8
* This program is free software; you can redistribute it and/or modify *
9
* it under the terms of the GNU General Public License as published by *
10
* the Free Software Foundation; either version 2 of the License, or *
11
* (at your option) any later version. *
13
***************************************************************************/
15
#ifndef HASHED_STRING_H
16
#define HASHED_STRING_H
21
#include <qdatastream.h>
23
#include <ext/hash_map>
26
///A simple class that stores a string together with it's appropriate hash-key
30
HashedString() : m_hash(0) {}
32
HashedString(const QString& str) : m_str(str)
37
HashedString(const char* str) : m_str(str)
42
inline size_t hash() const
52
bool operator == (const HashedString& rhs) const
54
if (m_hash != rhs.m_hash)
56
return m_str == rhs.m_str;
59
///Does not compare alphabetically, uses the hash-key for ordering.
60
bool operator < (const HashedString& rhs) const
62
if (m_hash < rhs.m_hash)
64
if (m_hash == rhs.m_hash)
65
return m_str < rhs.m_str;
69
static size_t hashString(const QString& str);
77
friend QDataStream& operator << (QDataStream& stream, const HashedString& str);
78
friend QDataStream& operator >> (QDataStream& stream, HashedString& str);
81
QDataStream& operator << (QDataStream& stream, const HashedString& str);
83
QDataStream& operator >> (QDataStream& stream, HashedString& str);
85
class HashedStringSetData;
86
class HashedStringSetGroup;
88
///This is a reference-counting string-set optimized for fast lookup of hashed strings
96
///Constructs a string-set from one single file
97
HashedStringSet(const HashedString& file);
99
HashedStringSet(const HashedStringSet& rhs);
103
HashedStringSet& operator = (const HashedStringSet& rhs);
104
///@return whether the given file-name was included
105
bool operator[] (const HashedString& rhs) const;
107
void insert(const HashedString& str);
109
HashedStringSet& operator +=(const HashedStringSet&);
111
HashedStringSet& operator -=(const HashedStringSet&);
114
///Returns true if all files that are part of this set are also part of the given set
115
bool operator <= (const HashedStringSet& rhs) const;
117
bool operator == (const HashedStringSet& rhs) const;
119
void read(QDataStream& stream);
120
void write(QDataStream& stream) const;
122
std::string print() const;
126
friend class HashedStringSetGroup;
127
void makeDataPrivate();
128
KSharedPtr<HashedStringSetData> m_data; //this implies some additional cost because KShared's destructor is virtual. Maybe change that by copying KShared without the virtual destructor.
129
friend HashedStringSet operator + (const HashedStringSet& lhs, const HashedStringSet& rhs);
132
HashedStringSet operator + (const HashedStringSet& lhs, const HashedStringSet& rhs);
137
struct hash<HashedString> {
138
size_t operator () (const HashedString& str) const
145
///Used to find all registered HashedStringSet's that contain all strings given to findGroups(..)
146
class HashedStringSetGroup
149
typedef std::set<size_t> ItemSet;
150
void addSet(size_t id, const HashedStringSet& set);
151
void enableSet(size_t id);
152
bool isDisabled(size_t id) const;
153
void disableSet(size_t id);
154
void removeSet(size_t id);
156
//Writes the ids of all registered and not disabled HashedStringSet's that are completely included in the given HashedStringSet efficiently)
157
void findGroups(HashedStringSet strings, ItemSet& target) const;
160
typedef __gnu_cxx::hash_map<HashedString, ItemSet> GroupMap;
161
typedef __gnu_cxx::hash_map<size_t, size_t> SizeMap;