/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_simple_set_pyx.pxd

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Interface definition of a class like PySet but without caching the hash.
 
18
 
 
19
This is generally useful when you want to 'intern' objects, etc. Note that this
 
20
differs from Set in that we:
 
21
  1) Don't have all of the .intersection, .difference, etc functions
 
22
  2) Do return the object from the set via queries
 
23
     eg. SimpleSet.add(key) => saved_key and SimpleSet[key] => saved_key
 
24
"""
 
25
 
 
26
from cpython.object cimport PyObject
 
27
 
 
28
 
 
29
cdef public api class SimpleSet [object SimpleSetObject, type SimpleSet_Type]:
 
30
    """A class similar to PySet, but with simpler implementation.
 
31
 
 
32
    The main advantage is that this class uses only 2N memory to store N
 
33
    objects rather than 4N memory. The main trade-off is that we do not cache
 
34
    the hash value of saved objects. As such, it is assumed that computing the
 
35
    hash will be cheap (such as strings or tuples of strings, etc.)
 
36
 
 
37
    This also differs in that you can get back the objects that are stored
 
38
    (like a dict), but we also don't implement the complete list of 'set'
 
39
    operations (difference, intersection, etc).
 
40
    """
 
41
    # Data structure definition:
 
42
    #   This is a basic hash table using open addressing.
 
43
    #       http://en.wikipedia.org/wiki/Open_addressing
 
44
    #   Basically that means we keep an array of pointers to Python objects
 
45
    #   (called a table). Each location in the array is called a 'slot'.
 
46
    #
 
47
    #   An empty slot holds a NULL pointer, a slot where there was an item
 
48
    #   which was then deleted will hold a pointer to _dummy, and a filled slot
 
49
    #   points at the actual object which fills that slot.
 
50
    #
 
51
    #   The table is always a power of two, and the default location where an
 
52
    #   object is inserted is at hash(object) & (table_size - 1)
 
53
    #
 
54
    #   If there is a collision, then we search for another location. The
 
55
    #   specific algorithm is in _lookup. We search until we:
 
56
    #       find the object
 
57
    #       find an equivalent object (by tp_richcompare(obj1, obj2, Py_EQ))
 
58
    #       find a NULL slot
 
59
    #
 
60
    #   When an object is deleted, we set its slot to _dummy. this way we don't
 
61
    #   have to track whether there was a collision, and find the corresponding
 
62
    #   keys. (The collision resolution algorithm makes that nearly impossible
 
63
    #   anyway, because it depends on the upper bits of the hash.)
 
64
    #   The main effect of this, is that if we find _dummy, then we can insert
 
65
    #   an object there, but we have to keep searching until we find NULL to
 
66
    #   know that the object is not present elsewhere.
 
67
 
 
68
    cdef Py_ssize_t _used   # active
 
69
    cdef Py_ssize_t _fill   # active + dummy
 
70
    cdef Py_ssize_t _mask   # Table contains (mask+1) slots, a power of 2
 
71
    cdef PyObject **_table  # Pyrex/Cython doesn't support arrays to 'object'
 
72
                            # so we manage it manually
 
73
 
 
74
    cdef PyObject *_get(self, object key) except? NULL
 
75
    cpdef object add(self, key)
 
76
    cpdef bint discard(self, key) except -1
 
77
    cdef int _insert_clean(self, PyObject *key) except -1
 
78
    cdef Py_ssize_t _resize(self, Py_ssize_t min_unused) except -1
 
79
 
 
80
 
 
81
# TODO: might want to export the C api here, though it is all available from
 
82
#       the class object...
 
83
cdef api SimpleSet SimpleSet_New()
 
84
cdef api object SimpleSet_Add(object self, object key)
 
85
cdef api int SimpleSet_Contains(object self, object key) except -1
 
86
cdef api int SimpleSet_Discard(object self, object key) except -1
 
87
cdef api PyObject *SimpleSet_Get(SimpleSet self, object key) except? NULL
 
88
cdef api Py_ssize_t SimpleSet_Size(object self) except -1
 
89
cdef api int SimpleSet_Next(object self, Py_ssize_t *pos, PyObject **key) except -1