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
19
#include<qdatastream.h>
20
#include<ksharedptr.h>
22
#include <ext/hash_map>
25
///A simple class that stores a string together with it's appropriate hash-key
28
HashedString() : m_hash( 0 ) {}
30
HashedString( const QString& str ) : m_str( str ) {
34
HashedString( const char* str ) : m_str( str ) {
38
inline size_t hash() const {
46
bool operator == ( const HashedString& rhs ) const {
47
if ( m_hash != rhs.m_hash )
49
return m_str == rhs.m_str;
52
///Does not compare alphabetically, uses the hash-key for ordering.
53
bool operator < ( const HashedString& rhs ) const {
54
if ( m_hash < rhs.m_hash )
56
if ( m_hash == rhs.m_hash )
57
return m_str < rhs.m_str;
61
static size_t hashString( const QString& str );
69
friend QDataStream& operator << ( QDataStream& stream, const HashedString& str );
70
friend QDataStream& operator >> ( QDataStream& stream, HashedString& str );
73
QDataStream& operator << ( QDataStream& stream, const HashedString& str );
75
QDataStream& operator >> ( QDataStream& stream, HashedString& str );
77
class HashedStringSetData;
78
class HashedStringSetGroup;
80
///This is a reference-counting string-set optimized for fast lookup of hashed strings
81
class HashedStringSet {
87
///Constructs a string-set from one single file
88
HashedStringSet( const HashedString& file );
90
HashedStringSet( const HashedStringSet& rhs );
94
HashedStringSet& operator = ( const HashedStringSet& rhs );
95
///@return whether the given file-name was included
96
bool operator[] ( const HashedString& rhs ) const;
98
void insert( const HashedString& str );
100
HashedStringSet& operator +=( const HashedStringSet& );
102
HashedStringSet& operator -=( const HashedStringSet& );
105
///Returns true if all files that are part of this set are also part of the given set
106
bool operator <= ( const HashedStringSet& rhs ) const;
108
bool operator == ( const HashedStringSet& rhs ) const;
110
void read( QDataStream& stream );
111
void write( QDataStream& stream ) const;
113
std::string print() const;
117
friend class HashedStringSetGroup;
118
void makeDataPrivate();
119
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.
120
friend HashedStringSet operator + ( const HashedStringSet& lhs, const HashedStringSet& rhs );
123
HashedStringSet operator + ( const HashedStringSet& lhs, const HashedStringSet& rhs );
125
namespace __gnu_cxx {
127
struct hash<HashedString> {
128
size_t operator () ( const HashedString& str ) const {
134
///Used to find all registered HashedStringSet's that contain all strings given to findGroups(..)
135
class HashedStringSetGroup {
137
typedef std::set<size_t> ItemSet;
138
void addSet( size_t id, const HashedStringSet& set );
139
void enableSet( size_t id );
140
bool isDisabled( size_t id ) const;
141
void disableSet( size_t id );
142
void removeSet( size_t id );
144
//Writes the ids of all registered and not disabled HashedStringSet's that are completely included in the given HashedStringSet efficiently)
145
void findGroups( HashedStringSet strings, ItemSet& target ) const;
148
typedef __gnu_cxx::hash_map<HashedString, ItemSet> GroupMap;
149
typedef __gnu_cxx::hash_map<size_t, size_t> SizeMap;