This file is indexed.

/usr/lib/gdesklets/utils/Trie.py is in gdesklets 0.36.1-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
_DELIMITER = "delimiter"
_MATCH_ONE = "?"
_MATCH_MANY = "*"


#
# Class for the node of a compressed trie.
#
class _TrieNode(object):

    __slots__ = ('__body', '__children', '__values')


    def __init__(self):
 
        # the body elements; since the trie is compressed, there may be more
        # than one
        self.__body = []

        # the children of the node
        self.__children = {}

        # the values stored in the node
        self.__values = []


    def _set_body(self, body): self.__body = body[:]
    def _set_children(self, children): self.__children = children.copy()
    def _set_values(self, values): self.__values = values[:]


    #
    # Inserts a new string into the trie.
    #
    def insert(self, data, value):
        
        # if the node is empty, just fill it
        if (not self.__body):
            self._set_body(data)
            self.__values.append(value)
            return

        index = 0
        length = len(self.__body)

        # check how far the elements match
        while (index < length):
            # split up node if elements don't match
            if (data[index] != self.__body[index]):
                ext_node = _TrieNode()
                ext_node._set_body(self.__body[index:])
                ext_node._set_children(self.__children)
                ext_node._set_values(self.__values)

                new_node = _TrieNode()
                new_node.insert(data[index:], value)
                
                self.__children = {self.__body[index]: ext_node,
                                   data[index]: new_node}
                self.__body = self.__body[:index]
                self.__values = []
                return
            #end if
            index += 1
        #end while

        # use this node if the given string exactly matches the body string
        if (index == len(data)):
            self.__values.append(value)
            return

        # insert node
        node = self.__children.get(data[index])
        if (node):
            # insert into existing node
            node.insert(data[index:], value)
        else:
            # create a new node
            new_node = _TrieNode()
            new_node.insert(data[index:], value)
            self.__children[data[index]] = new_node


    #
    # Retrieves the values which are stored for the given string. The string
    # may contain the wildcards ? (match any character) and * (match zero or
    # more characters).
    #
    # FIXME: clean up
    #
    def retrieve(self, data, case_sensitive):

        index = 0
        length = len(self.__body)

        values = []
        while (index < length):
            if (case_sensitive):
                next = data[index]
                char = self.__body[index]
            else:
                next = data[index].lower()
                char = self.__body[index].lower()
            
            if (next == _MATCH_ONE and char != _DELIMITER):
                index += 1
            elif (next == _MATCH_ONE):
                return []
            
            # the MANY wildcard can be translated to ONE wildcards
            elif (next == _MATCH_MANY):
                prefix = data[:index]
                suffix = data[index + 1:]
                values += self.retrieve(prefix + suffix, case_sensitive)
                values += self.retrieve(prefix + [_MATCH_ONE, _MATCH_MANY] +
                                        suffix, case_sensitive)
                return values
            
            elif (next != char):
                return []
            
            else:
                index += 1
        #end while

        if (index >= len(data)): return self.__values

        next = data[index]
        if (next in (_MATCH_ONE, _MATCH_MANY)):
            for node in self.__children.values():
                values += node.retrieve(data[index:], case_sensitive)
            return values
                    
        else:
            node1 = self.__children.get(next)
            if (not case_sensitive): node2 = self.__children.get(next.lower())
            else: node2 = None
            if (not case_sensitive): node3 = self.__children.get(next.upper())
            else: node3 = None

            if (node1 or node2 or node3):
                l1 = []
                l2 = []
                l3 = []
                if (node1):
                    l1 = node1.retrieve(data[index:], case_sensitive)
                if (node2):
                    l2 = node2.retrieve(data[index:], case_sensitive)
                if (node3):
                    l3 = node3.retrieve(data[index:], case_sensitive)

                return values + l1 + l2 + l3
            else:
                return values + self.__values


    #
    # Recursive method for removing entries. If it return True, then the node
    # could be removed successfully, otherwise the node is still in use.
    #
    def remove(self, key, value):

        # recursively check the children, if the key matches
        if (len(key) > len(self.__body) and
               key[:len(self.__body)] == self.__body):
            key = key[len(self.__body):]
            next = key[0]
            child = self.__children.get(next)
            if (child):
                to_remove = child.remove(key, value)
                if (to_remove):
                    del self.__children[next]
                
        # remove the value from this node
        else:
            try:
                self.__values.remove(value)
            except ValueError:
                pass

        # if the node has become empty, then we can remove it
        if (not self.__values and not self.__children):
            return True
        else:
            return False


    def get_size(self):

        size = 1
        for c in self.__children.values():
            size += c.get_size()

        return size



#
# Class for a trie.
#
class Trie:

    __slots__ = ('__root', '__case_sensitive')

    def __init__(self):

        self.__root = _TrieNode()
        self.__case_sensitive = True

        # table for storing all indices of a value: value -> [indices]
        self.__index_table = {}


    #
    # Sets the case sensitivity for searches in the trie.
    #
    def set_case_sensitive(self, value): self.__case_sensitive = value
    

    #
    # Inserts a value into the trie for the given list of key elements.
    # A list element is usually a letter of a string which is to be indexed.
    #
    def insert(self, key, value):

        key = [_DELIMITER] + key + [_DELIMITER]
        self.__root.insert(key, value)

        if (not value in self.__index_table): self.__index_table[value] = []
        self.__index_table[value].append(key)


    #
    # Retrieves the stored value for the given list of key elements. A list
    # element is usually a letter of an indexed string.
    #
    def retrieve(self, key):

        key = [_DELIMITER] + key + [_DELIMITER]
        values = self.__root.retrieve(key, self.__case_sensitive)

        if len(values) <= 1:
            return values

        # remove duplicates
        values.sort()
        new = []
        current = None
        for v in values:
            if (v != current):
                new.append(v)
                current = v
        #end for
        return new


    #
    # Removes the given value from the trie.
    #
    def remove(self, value):

        indices = self.__index_table.get(value, [])
        for index in indices:
            self.__root.remove(index, value)


    def get_size(self):

        return self.__root.get_size()




if (__name__ == "__main__"):

    text = """
    This is just a plain text which we are going to index. There are lots of
    words in it, so this will be some kind of stress test for our trie
    implementation. Let's see how well this will work with compressed tries.
    """

    import sys
    if '.' not in sys.path: sys.path.append('.')
    if '..' not in sys.path: sys.path.append('..')


    def stats():

        from libdesklets.system.gtop import proc_mem, proc_time
        from os import getpid

        pid = getpid()

        print 'CPU time: %ds\tMemory usage: %0.2f' % (proc_time(pid).utime / 100,
                                                      proc_mem(pid).resident / (1024.0**2),)
    

    stats()

    keys = text.split() #open('/usr/share/dict/words').read().split()#[:1000]

    stats()

    t = Trie()
    for word in keys:
        t.insert(list(word), word)


    stats()
    
    for key in keys:
        t.retrieve(list(key))

    stats()


    a = t.retrieve(list("w?ll*"))
    print a, len(a)

    for word in text.split():
        t.remove(word)
        print t.get_size()

    for word in keys:
        t.insert(list(word), word)
        print t.get_size()

    a = t.retrieve(list("*"))
    print a, len(a)

    stats()