~ubuntu-branches/ubuntu/trusty/umbrello/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/interfaces/hashedstring.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-03-19 12:12:50 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20140319121250-a7nvktn0cp3k9iny
Tags: 4:4.12.90-0ubuntu1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
   copyright            : (C) 2006 by David Nolden
 
3
   email                : david.nolden.kdevelop@art-master.de
 
4
***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
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.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
 
 
15
#ifndef HASHED_STRING_H
 
16
#define HASHED_STRING_H
 
17
 
 
18
#include "ast.h"
 
19
 
 
20
#include <QString>
 
21
#include <qdatastream.h>
 
22
#include <set>
 
23
#include <ext/hash_map>
 
24
#include <string>
 
25
 
 
26
///A simple class that stores a string together with it's appropriate hash-key
 
27
class HashedString
 
28
{
 
29
public:
 
30
    HashedString() : m_hash(0) {}
 
31
 
 
32
    HashedString(const QString& str) : m_str(str)
 
33
    {
 
34
        initHash();
 
35
    }
 
36
 
 
37
    HashedString(const char* str) : m_str(str)
 
38
    {
 
39
        initHash();
 
40
    }
 
41
 
 
42
    inline size_t hash() const
 
43
    {
 
44
        return m_hash;
 
45
    }
 
46
 
 
47
    QString str() const
 
48
    {
 
49
        return m_str;
 
50
    }
 
51
 
 
52
    bool operator == (const HashedString& rhs) const
 
53
    {
 
54
        if (m_hash != rhs.m_hash)
 
55
            return false;
 
56
        return m_str == rhs.m_str;
 
57
    }
 
58
 
 
59
    ///Does not compare alphabetically, uses the hash-key for ordering.
 
60
    bool operator < (const HashedString& rhs) const
 
61
    {
 
62
        if (m_hash < rhs.m_hash)
 
63
            return true;
 
64
        if (m_hash == rhs.m_hash)
 
65
            return m_str < rhs.m_str;
 
66
        return false;
 
67
    }
 
68
 
 
69
    static size_t hashString(const QString& str);
 
70
 
 
71
private:
 
72
    void initHash();
 
73
 
 
74
    QString m_str;
 
75
    size_t m_hash;
 
76
 
 
77
    friend QDataStream& operator << (QDataStream& stream, const HashedString& str);
 
78
    friend QDataStream& operator >> (QDataStream& stream, HashedString& str);
 
79
};
 
80
 
 
81
QDataStream& operator << (QDataStream& stream, const HashedString& str);
 
82
 
 
83
QDataStream& operator >> (QDataStream& stream, HashedString& str);
 
84
 
 
85
class HashedStringSetData;
 
86
class HashedStringSetGroup;
 
87
 
 
88
///This is a reference-counting string-set optimized for fast lookup of hashed strings
 
89
class HashedStringSet
 
90
{
 
91
public:
 
92
    HashedStringSet();
 
93
 
 
94
    ~HashedStringSet();
 
95
 
 
96
    ///Constructs a string-set from one single file
 
97
    HashedStringSet(const HashedString& file);
 
98
 
 
99
    HashedStringSet(const HashedStringSet& rhs);
 
100
 
 
101
    int size() const;
 
102
 
 
103
    HashedStringSet& operator = (const HashedStringSet& rhs);
 
104
    ///@return whether the given file-name was included
 
105
    bool operator[] (const HashedString& rhs) const;
 
106
 
 
107
    void insert(const HashedString& str);
 
108
 
 
109
    HashedStringSet& operator +=(const HashedStringSet&);
 
110
 
 
111
    HashedStringSet& operator -=(const HashedStringSet&);
 
112
 
 
113
    ///intersection-test
 
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;
 
116
 
 
117
    bool operator == (const HashedStringSet& rhs) const;
 
118
 
 
119
    void read(QDataStream& stream);
 
120
    void write(QDataStream& stream) const;
 
121
 
 
122
    std::string print() const;
 
123
 
 
124
    size_t hash() const;
 
125
private:
 
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);
 
130
};
 
131
 
 
132
HashedStringSet operator + (const HashedStringSet& lhs, const HashedStringSet& rhs);
 
133
 
 
134
namespace __gnu_cxx
 
135
{
 
136
template<>
 
137
struct hash<HashedString> {
 
138
    size_t operator () (const HashedString& str) const
 
139
    {
 
140
        return str.hash();
 
141
    }
 
142
};
 
143
}
 
144
 
 
145
///Used to find all registered HashedStringSet's that contain all strings given to findGroups(..)
 
146
class HashedStringSetGroup
 
147
{
 
148
public:
 
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);
 
155
 
 
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;
 
158
 
 
159
private:
 
160
    typedef __gnu_cxx::hash_map<HashedString, ItemSet> GroupMap;
 
161
    typedef __gnu_cxx::hash_map<size_t, size_t> SizeMap;
 
162
    GroupMap m_map;
 
163
    SizeMap m_sizeMap;
 
164
    ItemSet m_disabled;
 
165
    ItemSet m_global;
 
166
};
 
167
#endif