1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Definition of a class that is similar to Set with some small changes."""
19
cdef extern from "python-compat.h":
22
cdef extern from "Python.h":
23
ctypedef unsigned long size_t
24
ctypedef long (*hashfunc)(PyObject*) except -1
25
ctypedef object (*richcmpfunc)(PyObject *, PyObject *, int)
26
ctypedef int (*visitproc)(PyObject *, void *)
27
ctypedef int (*traverseproc)(PyObject *, visitproc, void *)
29
void Py_INCREF(PyObject *)
30
void Py_DECREF(PyObject *)
31
ctypedef struct PyTypeObject:
33
richcmpfunc tp_richcompare
34
traverseproc tp_traverse
36
PyTypeObject *Py_TYPE(PyObject *)
37
# Note: *Don't* use hash(), Pyrex 0.9.8.5 thinks it returns an 'int', and
38
# thus silently truncates to 32-bits on 64-bit machines.
39
long PyObject_Hash(PyObject *) except -1
41
void *PyMem_Malloc(size_t nbytes)
42
void PyMem_Free(void *)
43
void memset(void *, int, size_t)
46
# Dummy is an object used to mark nodes that have been deleted. Since
47
# collisions require us to move a node to an alternative location, if we just
48
# set an entry to NULL on delete, we won't find any relocated nodes.
49
# We have to use _dummy_obj because we need to keep a refcount to it, but we
50
# also use _dummy as a pointer, because it avoids having to put <PyObject*> all
52
cdef object _dummy_obj
55
_dummy = <PyObject *>_dummy_obj
58
cdef object _NotImplemented
59
_NotImplemented = NotImplemented
62
cdef int _is_equal(PyObject *this, long this_hash, PyObject *other) except -1:
67
other_hash = PyObject_Hash(other)
68
if other_hash != this_hash:
71
# This implements a subset of the PyObject_RichCompareBool functionality.
73
# 1) Doesn't try to do anything with old-style classes
74
# 2) Assumes that both objects have a tp_richcompare implementation, and
75
# that if that is not enough to compare equal, then they are not
76
# equal. (It doesn't try to cast them both to some intermediate form
77
# that would compare equal.)
78
res = Py_TYPE(this).tp_richcompare(this, other, Py_EQ)
79
if res is _NotImplemented:
80
res = Py_TYPE(other).tp_richcompare(other, this, Py_EQ)
81
if res is _NotImplemented:
88
cdef public api class SimpleSet [object SimpleSetObject, type SimpleSet_Type]:
89
"""This class can be used to track canonical forms for objects.
91
It is similar in function to the interned dictionary that is used by
94
1) It assumes that hash(obj) is cheap, so does not need to inline a copy
96
2) It only stores one reference to the object, rather than 2 (key vs
99
As such, it uses 1/3rd the amount of memory to store a pointer to the
102
# Attributes are defined in the .pxd file
103
DEF DEFAULT_SIZE=1024
106
cdef Py_ssize_t size, n_bytes
109
self._mask = size - 1
112
n_bytes = sizeof(PyObject*) * size;
113
self._table = <PyObject **>PyMem_Malloc(n_bytes)
114
if self._table == NULL:
116
memset(self._table, 0, n_bytes)
118
def __dealloc__(self):
119
if self._table != NULL:
120
PyMem_Free(self._table)
135
def _memory_size(self):
136
"""Return the number of bytes of memory consumed by this class."""
137
return sizeof(self) + (sizeof(PyObject*)*(self._mask + 1))
142
def _test_lookup(self, key):
145
slot = _lookup(self, key)
148
elif slot[0] == _dummy:
151
res = <object>slot[0]
152
return <int>(slot - self._table), res
154
def __contains__(self, key):
155
"""Is key present in this SimpleSet."""
158
slot = _lookup(self, key)
159
if slot[0] == NULL or slot[0] == _dummy:
163
cdef PyObject *_get(self, object key) except? NULL:
164
"""Return the object (or nothing) define at the given location."""
167
slot = _lookup(self, key)
168
if slot[0] == NULL or slot[0] == _dummy:
172
def __getitem__(self, key):
173
"""Return a stored item that is equivalent to key."""
174
cdef PyObject *py_val
176
py_val = self._get(key)
178
raise KeyError("Key %s is not present" % key)
179
val = <object>(py_val)
182
cdef int _insert_clean(self, PyObject *key) except -1:
183
"""Insert a key into self.table.
185
This is only meant to be used during times like '_resize',
186
as it makes a lot of assuptions about keys not already being present,
187
and there being no dummy entries.
189
cdef size_t i, n_lookup
191
cdef PyObject **table, **slot
197
the_hash = PyObject_Hash(key)
199
for n_lookup from 0 <= n_lookup <= <size_t>mask: # Don't loop forever
200
slot = &table[i & mask]
203
self._fill = self._fill + 1
204
self._used = self._used + 1
207
raise RuntimeError('ran out of slots.')
209
def _py_resize(self, min_used):
210
"""Do not use this directly, it is only exposed for testing."""
211
return self._resize(min_used)
213
cdef Py_ssize_t _resize(self, Py_ssize_t min_used) except -1:
214
"""Resize the internal table.
216
The final table will be big enough to hold at least min_used entries.
217
We will copy the data from the existing table over, leaving out dummy
220
:return: The new size of the internal table
222
cdef Py_ssize_t new_size, n_bytes, remaining
223
cdef PyObject **new_table, **old_table, **slot
225
new_size = DEFAULT_SIZE
226
while new_size <= min_used and new_size > 0:
227
new_size = new_size << 1
228
# We rolled over our signed size field
231
# Even if min_used == self._mask + 1, and we aren't changing the actual
232
# size, we will still run the algorithm so that dummy entries are
235
# if new_size < self._used:
236
# raise RuntimeError('cannot shrink SimpleSet to something'
237
# ' smaller than the number of used slots.')
238
n_bytes = sizeof(PyObject*) * new_size;
239
new_table = <PyObject **>PyMem_Malloc(n_bytes)
240
if new_table == NULL:
243
old_table = self._table
244
self._table = new_table
245
memset(self._table, 0, n_bytes)
246
self._mask = new_size - 1
248
remaining = self._fill
251
# Moving everything to the other table is refcount neutral, so we don't
255
if slot[0] == NULL: # unused slot
257
elif slot[0] == _dummy: # dummy slot
258
remaining = remaining - 1
260
remaining = remaining - 1
261
self._insert_clean(slot[0])
263
PyMem_Free(old_table)
267
"""Similar to set.add(), start tracking this key.
269
There is one small difference, which is that we return the object that
270
is stored at the given location. (which is closer to the
271
dict.setdefault() functionality.)
273
return self._add(key)
275
cdef object _add(self, key):
276
cdef PyObject **slot, *py_key
279
py_key = <PyObject *>key
280
if (Py_TYPE(py_key).tp_richcompare == NULL
281
or Py_TYPE(py_key).tp_hash == NULL):
282
raise TypeError('Types added to SimpleSet must implement'
283
' both tp_richcompare and tp_hash')
285
# We need at least one empty slot
286
assert self._used < self._mask
287
slot = _lookup(self, key)
288
if (slot[0] == NULL):
290
self._fill = self._fill + 1
291
self._used = self._used + 1
294
elif (slot[0] == _dummy):
296
self._used = self._used + 1
299
# No else: clause. If _lookup returns a pointer to
300
# a live object, then we already have a value at this location.
301
retval = <object>(slot[0])
302
# PySet and PyDict use a 2-3rds full algorithm, we'll follow suit
303
if added and (self._fill * 3) >= ((self._mask + 1) * 2):
304
# However, we always work for a load factor of 2:1
305
self._resize(self._used * 2)
306
# Even if we resized and ended up moving retval into a different slot,
307
# it is still the value that is held at the slot equivalent to 'key',
308
# so we can still return it
311
def discard(self, key):
312
"""Remove key from the set, whether it exists or not.
314
:return: False if the item did not exist, True if it did
316
if self._discard(key):
320
cdef int _discard(self, key) except -1:
321
cdef PyObject **slot, *py_key
323
slot = _lookup(self, key)
324
if slot[0] == NULL or slot[0] == _dummy:
326
self._used = self._used - 1
329
# PySet uses the heuristic: If more than 1/5 are dummies, then resize
331
# if ((so->_fill - so->_used) * 5 < so->mask)
332
# However, we are planning on using this as an interning structure, in
333
# which we will be putting a lot of objects. And we expect that large
334
# groups of them are going to have the same lifetime.
335
# Dummy entries hurt a little bit because they cause the lookup to keep
336
# searching, but resizing is also rather expensive
337
# For now, we'll just use their algorithm, but we may want to revisit
339
if ((self._fill - self._used) * 5 > self._mask):
340
self._resize(self._used * 2)
344
return _SimpleSet_iterator(self)
347
cdef class _SimpleSet_iterator:
348
"""Iterator over the SimpleSet structure."""
352
cdef Py_ssize_t _used # track if things have been mutated while iterating
353
cdef Py_ssize_t len # number of entries left
355
def __init__(self, obj):
358
self._used = self.set._used
359
self.len = self.set._used
365
cdef Py_ssize_t mask, i
370
if self.set._used != self._used:
371
# Force this exception to continue to be raised
373
raise RuntimeError("Set size changed during iteration")
374
if not SimpleSet_Next(self.set, &self.pos, &key):
378
the_key = <object>key # INCREF
379
self.len = self.len - 1
382
def __length_hint__(self):
383
if self.set is not None and self._used == self.set._used:
389
cdef api SimpleSet SimpleSet_New():
390
"""Create a new SimpleSet object."""
394
cdef SimpleSet _check_self(object self):
395
"""Check that the parameter is not None.
397
Pyrex/Cython will do type checking, but only to ensure that an object is
398
either the right type or None. You can say "object foo not None" for pure
399
python functions, but not for C functions.
400
So this is just a helper for all the apis that need to do the check.
402
cdef SimpleSet true_self
404
raise TypeError('self must not be None')
409
cdef PyObject **_lookup(SimpleSet self, object key) except NULL:
410
"""Find the slot where 'key' would fit.
412
This is the same as a dicts 'lookup' function.
414
:param key: An object we are looking up
415
:param hash: The hash for key
416
:return: The location in self.table where key should be put.
417
location == NULL is an exception, but (*location) == NULL just
418
indicates the slot is empty and can be used.
420
# This uses Quadratic Probing:
421
# http://en.wikipedia.org/wiki/Quadratic_probing
423
# This leads to probe locations at:
426
# h2 = h0 + 3 = h1 + 1 + 1
427
# h3 = h0 + 6 = h2 + 1 + 2
428
# h4 = h0 + 10 = h2 + 1 + 3
429
# Note that all of these are '& mask', but that is computed *after* the
431
# This differs from the algorithm used by Set and Dict. Which, effectively,
432
# use double-hashing, and a step size that starts large, but dwindles to
433
# stepping one-by-one.
434
# This gives more 'locality' in that if you have a collision at offset X,
435
# the first fallback is X+1, which is fast to check. However, that means
436
# that an object w/ hash X+1 will also check there, and then X+2 next.
437
# However, for objects with differing hashes, their chains are different.
438
# The former checks X, X+1, X+3, ... the latter checks X+1, X+2, X+4, ...
439
# So different hashes diverge quickly.
440
# A bigger problem is that we *only* ever use the lowest bits of the hash
441
# So all integers (x + SIZE*N) will resolve into the same bucket, and all
442
# use the same collision resolution. We may want to try to find a way to
443
# incorporate the upper bits of the hash with quadratic probing. (For
444
# example, X, X+1, X+3+some_upper_bits, X+6+more_upper_bits, etc.)
445
cdef size_t i, n_lookup
448
cdef PyObject **table, **slot, *cur, **free_slot, *py_key
450
py_key = <PyObject *>key
451
# Note: avoid using hash(obj) because of a bug w/ pyrex 0.9.8.5 and 64-bit
452
# (it treats hash() as returning an 'int' rather than a 'long')
453
key_hash = PyObject_Hash(py_key)
458
for n_lookup from 0 <= n_lookup <= <size_t>mask: # Don't loop forever
459
slot = &table[i & mask]
463
if free_slot != NULL:
464
# Did we find an earlier _dummy entry?
469
# Found an exact pointer to the key
472
if free_slot == NULL:
474
elif _is_equal(py_key, key_hash, cur):
475
# Both py_key and cur belong in this slot, return it
478
raise AssertionError('should never get here')
481
cdef api PyObject **_SimpleSet_Lookup(object self, object key) except NULL:
482
"""Find the slot where 'key' would fit.
484
This is the same as a dicts 'lookup' function. This is a private
485
api because mutating what you get without maintaing the other invariants
488
:param key: An object we are looking up
489
:param hash: The hash for key
490
:return: The location in self._table where key should be put
491
should never be NULL, but may reference a NULL (PyObject*)
493
return _lookup(_check_self(self), key)
496
cdef api object SimpleSet_Add(object self, object key):
497
"""Add a key to the SimpleSet (set).
499
:param self: The SimpleSet to add the key to.
500
:param key: The key to be added. If the key is already present,
501
self will not be modified
502
:return: The current key stored at the location defined by 'key'.
503
This may be the same object, or it may be an equivalent object.
504
(consider dict.setdefault(key, key))
506
return _check_self(self)._add(key)
509
cdef api int SimpleSet_Contains(object self, object key) except -1:
510
"""Is key present in self?"""
511
return (key in _check_self(self))
514
cdef api int SimpleSet_Discard(object self, object key) except -1:
515
"""Remove the object referenced at location 'key'.
517
:param self: The SimpleSet being modified
518
:param key: The key we are checking on
519
:return: 1 if there was an object present, 0 if there was not, and -1 on
522
return _check_self(self)._discard(key)
525
cdef api PyObject *SimpleSet_Get(SimpleSet self, object key) except? NULL:
526
"""Get a pointer to the object present at location 'key'.
528
This returns an object which is equal to key which was previously added to
529
self. This returns a borrowed reference, as it may also return NULL if no
530
value is present at that location.
532
:param key: The value we are looking for
533
:return: The object present at that location
535
return _check_self(self)._get(key)
538
cdef api Py_ssize_t SimpleSet_Size(object self) except -1:
539
"""Get the number of active entries in 'self'"""
540
return _check_self(self)._used
543
cdef api int SimpleSet_Next(object self, Py_ssize_t *pos,
544
PyObject **key) except -1:
545
"""Walk over items in a SimpleSet.
547
:param pos: should be initialized to 0 by the caller, and will be updated
549
:param key: Will return a borrowed reference to key
550
:return: 0 if nothing left, 1 if we are returning a new value
552
cdef Py_ssize_t i, mask
553
cdef SimpleSet true_self
554
cdef PyObject **table
555
true_self = _check_self(self)
559
mask = true_self._mask
560
table= true_self._table
561
while (i <= mask and (table[i] == NULL or table[i] == _dummy)):
571
cdef int SimpleSet_traverse(SimpleSet self, visitproc visit,
572
void *arg) except -1:
573
"""This is an implementation of 'tp_traverse' that hits the whole table.
575
Cython/Pyrex don't seem to let you define a tp_traverse, and they only
576
define one for you if you have an 'object' attribute. Since they don't
577
support C arrays of objects, we access the PyObject * directly.
580
cdef PyObject *next_key
584
while SimpleSet_Next(self, &pos, &next_key):
585
ret = visit(next_key, arg)
590
# It is a little bit ugly to do this, but it works, and means that Meliae can
591
# dump the total memory consumed by all child objects.
592
(<PyTypeObject *>SimpleSet).tp_traverse = <traverseproc>SimpleSet_traverse