1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/***************************************************************************
copyright : (C) 2006 by David Nolden
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "hashedstring.h"
#include <kdatastream.h>
#include <sstream>
#include <algorithm>
#include <iterator>
#include<ext/hash_set>
#include<set>
#include<algorithm>
//It needs to be measured whether this flag should be turned on or off. It seems just to move the complexity from one position to the other, without any variant being really better.
#define USE_HASHMAP
size_t fastHashString( const TQString& str );
size_t hashStringSafe( const TQString& str ) {
size_t hash = 0;
int len = str.length();
for( int a = 0; a < len; a++ ) {
hash = str[a].tqunicode() + (hash * 17);
}
return hash;
}
size_t HashedString::hashString( const TQString& str )
{
return fastHashString( str );
}
size_t fastHashString( const TQString& str ) {
size_t hash = 0;
if( !str.isEmpty() ) {
const TQChar* curr = str.tqunicode();
const TQChar* end = curr + str.length();
TQChar c;
for(; curr < end ;) {
c = *curr;
hash = c.tqunicode() + ( hash * 17 );
++curr;
}
}
return hash;
}
void HashedString::initHash() {
m_hash = hashString( m_str );
}
class HashedStringSetData : public KShared {
public:
#ifdef USE_HASHMAP
typedef __gnu_cxx::hash_set<HashedString> StringSet;
#else
typedef std::set<HashedString> StringSet; //must be a set, so the set-algorithms work
#endif
StringSet m_files;
mutable bool m_hashValid;
mutable size_t m_hash;
HashedStringSetData() : m_hashValid( false ) {
}
inline void tqinvalidateHash() {
m_hashValid = false;
}
void computeHash() const;
};
void HashedStringSetData::computeHash() const {
int num = 1;
m_hash = 0;
for( StringSet::const_iterator it = m_files.begin(); it != m_files.end(); ++it ) {
num *= 7;
m_hash += num * (*it).hash();
}
m_hashValid = true;
}
HashedStringSet::HashedStringSet() {}
HashedStringSet::~HashedStringSet() {}
HashedStringSet::HashedStringSet( const HashedString& file ) {
insert( file );
}
HashedStringSet::HashedStringSet( const HashedStringSet& rhs ) : m_data( rhs.m_data ) {}
HashedStringSet operator + ( const HashedStringSet& lhs, const HashedStringSet& rhs ) {
HashedStringSet ret = lhs;
ret += rhs;
return ret;
}
int HashedStringSet::size() const {
if( !m_data ) return 0;
return m_data->m_files.size();
}
HashedStringSet& HashedStringSet::operator = ( const HashedStringSet& rhs ) {
m_data = rhs.m_data;
return *this;
}
HashedStringSet& HashedStringSet::operator +=( const HashedStringSet& rhs ) {
if ( !rhs.m_data )
return * this;
#ifndef USE_HASHMAP
KSharedPtr<HashedStringSetData> oldData = m_data;
if( !oldData ) oldData = new HashedStringSetData();
m_data = new HashedStringSetData();
std::set_union( oldData->m_files.begin(), oldData->m_files.end(), rhs.m_data->m_files.begin(), rhs.m_data->m_files.end(), std::insert_iterator<HashedStringSetData::StringSet>( m_data->m_files, m_data->m_files.end() ) );
#else
makeDataPrivate();
m_data->m_files.insert( rhs.m_data->m_files.begin(), rhs.m_data->m_files.end() );
/*HashedStringSetData::StringSet::const_iterator end = rhs.m_data->m_files.end();
HashedStringSetData::StringSet& mySet( m_data->m_files );
for( HashedStringSetData::StringSet::const_iterator it = rhs.m_data->m_files.begin(); it != end; ++it ) {
mySet.insert( *it );
}*/
#endif
return *this;
}
HashedStringSet& HashedStringSet::operator -=( const HashedStringSet& rhs ) {
if( !m_data ) return *this;
if( !rhs.m_data ) return *this;
#ifndef USE_HASHMAP
KSharedPtr<HashedStringSetData> oldData = m_data;
m_data = new HashedStringSetData();
std::set_difference( oldData->m_files.begin(), oldData->m_files.end(), rhs.m_data->m_files.begin(), rhs.m_data->m_files.end(), std::insert_iterator<HashedStringSetData::StringSet>( m_data->m_files, m_data->m_files.end() ) );
#else
makeDataPrivate();
HashedStringSetData::StringSet::const_iterator end = rhs.m_data->m_files.end();
HashedStringSetData::StringSet::const_iterator myEnd = m_data->m_files.end();
HashedStringSetData::StringSet& mySet( m_data->m_files );
for( HashedStringSetData::StringSet::const_iterator it = rhs.m_data->m_files.begin(); it != end; ++it ) {
mySet.erase( *it );
}
#endif
return *this;
}
void HashedStringSet::makeDataPrivate() {
if ( !m_data ) {
m_data = new HashedStringSetData();
return ;
}
if ( m_data->_KShared_count() != 1 )
m_data = new HashedStringSetData( *m_data );
}
bool HashedStringSet::operator[] ( const HashedString& rhs ) const {
//if ( rhs.str() == "*" )
//return true; /// * stands for "any file"
if ( !m_data )
return false;
return m_data->m_files.find( rhs ) != m_data->m_files.end();
}
void HashedStringSet::insert( const HashedString& str ) {
if( str.str().isEmpty() ) return;
makeDataPrivate();
m_data->m_files.insert( str );
m_data->tqinvalidateHash();
}
bool HashedStringSet::operator <= ( const HashedStringSet& rhs ) const {
if ( !m_data )
return true;
if ( m_data->m_files.empty() )
return true;
if ( !rhs.m_data )
return false;
#ifndef USE_HASHMAP
return std::includes( rhs.m_data->m_files.begin(), rhs.m_data->m_files.end(), m_data->m_files.begin(), m_data->m_files.end() );
#else
const HashedStringSetData::StringSet& otherSet( rhs.m_data->m_files );
HashedStringSetData::StringSet::const_iterator end = rhs.m_data->m_files.end();
HashedStringSetData::StringSet::const_iterator myEnd = m_data->m_files.end();
for( HashedStringSetData::StringSet::const_iterator it = m_data->m_files.begin(); it != myEnd; ++it ) {
HashedStringSetData::StringSet::const_iterator i = otherSet.find( *it );
if( i == end ) return false;
}
return true;
#endif
}
bool HashedStringSet::operator == ( const HashedStringSet& rhs ) const {
if( hash() != rhs.hash() ) return false;
bool empty1 = false;
if ( !m_data )
empty1 = true;
else if ( m_data->m_files.empty() )
empty1 = true;
bool empty2 = false;
if ( !rhs.m_data )
empty2 = true;
else if ( rhs.m_data->m_files.empty() )
empty2 = true;
if ( empty1 && empty2 )
return true;
if ( empty1 || empty2 )
return false;
return m_data->m_files == rhs.m_data->m_files;
}
size_t HashedStringSet::hash() const {
if( !m_data ) return 0;
if( !m_data->m_hashValid ) m_data->computeHash();
return m_data->m_hash;
}
void HashedStringSet::read( TQDataStream& stream ) {
bool b;
stream >> b;
if( b ) {
m_data = new HashedStringSetData();
int cnt;
stream >> cnt;
HashedString s;
for( int a = 0; a < cnt; a++ ) {
stream >> s;
m_data->m_files.insert( s );
}
} else {
m_data = 0;
}
}
void HashedStringSet::write( TQDataStream& stream ) const {
bool b = m_data;
stream << b;
if( b ) {
int cnt = m_data->m_files.size();
stream << cnt;
for( HashedStringSetData::StringSet::const_iterator it = m_data->m_files.begin(); it != m_data->m_files.end(); ++it ) {
stream << *it;
}
}
}
std::string HashedStringSet::print() const {
std::ostringstream s;
if( m_data ) {
for( HashedStringSetData::StringSet::const_iterator it = m_data->m_files.begin(); it != m_data->m_files.end(); ++it ) {
s << (*it).str().ascii() << "\n";
}
}
return s.str();
}
TQDataStream& operator << ( TQDataStream& stream, const HashedString& str ) {
stream << str.m_str;
stream << str.m_hash;
return stream;
}
TQDataStream& operator >> ( TQDataStream& stream, HashedString& str ) {
stream >> str.m_str;
stream >> str.m_hash;
return stream;
}
void HashedStringSetGroup::addSet( size_t id, const HashedStringSet& set ) {
if( set.m_data && !set.m_data->m_files.empty() ) {
m_sizeMap[ id ] = set.size();
for( HashedStringSetData::StringSet::const_iterator it = set.m_data->m_files.begin(); it != set.m_data->m_files.end(); ++it ) {
GroupMap::iterator itr = m_map.find( *it );
if( itr == m_map.end() ) {
itr = m_map.insert( std::make_pair( *it, ItemSet() ) ).first;
}
itr->second.insert( id );
}
} else {
m_global.insert( id );
}
}
void HashedStringSetGroup::disableSet( size_t id ) {
m_disabled.insert( id );
}
void HashedStringSetGroup::enableSet( size_t id ) {
m_disabled.erase( id );
}
bool HashedStringSetGroup::isDisabled( size_t id ) const {
return m_disabled.find( id ) != m_disabled.end();
}
void HashedStringSetGroup::removeSet( size_t id ) {
m_disabled.erase( id );
m_global.erase( id );
m_sizeMap.erase( id );
for( GroupMap::iterator it = m_map.begin(); it != m_map.end(); ++it ) {
it->second.erase( id );
}
}
void HashedStringSetGroup::findGroups( HashedStringSet strings, ItemSet& target ) const {
target.clear();
if( !strings.m_data ) {
std::set_difference( m_global.begin(), m_global.end(), m_disabled.begin(), m_disabled.end(), std::insert_iterator<ItemSet>( target, target.end() ) );
return;
}
//This might yet be optimized by sorting the sets according to their size, and starting the intersectioning with the smallest ones.
__gnu_cxx::hash_map<size_t, int> hitCounts;
for( HashedStringSetData::StringSet::const_iterator it = strings.m_data->m_files.begin(); it != strings.m_data->m_files.end(); ++it ) {
GroupMap::const_iterator itr = m_map.find( *it );
if( itr == m_map.end() ) {
//There are no string-sets that include the currently searched for string
continue;
}
for( ItemSet::const_iterator it2 = itr->second.begin(); it2 != itr->second.end(); ++it2 ) {
__gnu_cxx::hash_map<size_t, int>::iterator v = hitCounts.find( *it2 );
if( v != hitCounts.end() ) {
++(*v).second;
} else {
hitCounts[*it2] = 1;
}
}
}
//Now count together all groups that are completely within the given string-set(their hitCount equals their size)
ItemSet found;
for( __gnu_cxx::hash_map<size_t, int>::const_iterator it = hitCounts.begin(); it != hitCounts.end(); ++it ) {
if( (*it).second == (*m_sizeMap.find( (*it).first )).second )
found.insert( (*it).first );
}
std::set_union( found.begin(), found.end(), m_global.begin(), m_global.end(), std::insert_iterator<ItemSet>( target, target.end() ) );
target.swap( found );
target.clear();
std::set_difference( found.begin(), found.end(), m_disabled.begin(), m_disabled.end(), std::insert_iterator<ItemSet>( target, target.end() ) );
}
|