~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_c.h

  • Committer: John Arbash Meinel
  • Date: 2009-10-02 20:32:50 UTC
  • mto: (4679.6.1 2.1-export-c-api)
  • mto: This revision was merged to the branch mainline in revision 4735.
  • Revision ID: john@arbash-meinel.com-20091002203250-q6iv6o2mwjqp4g53
Add __iter__ support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2009 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
 
 
18
#ifndef _STATIC_TUPLE_H_
 
19
#define _STATIC_TUPLE_H_
 
20
#include <Python.h>
 
21
#include <string.h>
 
22
 
 
23
#define STATIC_TUPLE_HAS_HASH 0
 
24
/* Caching the hash adds memory, but allows us to save a little time during
 
25
 * lookups. TIMEIT hash(key) shows it as
 
26
 *  0.108usec w/ hash
 
27
 *  0.160usec w/o hash
 
28
 * Note that the entries themselves are strings, which already cache their
 
29
 * hashes. So while there is a 1.5:1 difference in the time for hash(), it is
 
30
 * already a function which is quite fast. Probably the only reason we might
 
31
 * want to do so, is if we implement a KeyIntern dict that assumes it is
 
32
 * available, and can then drop the 'hash' value from the item pointers. Then
 
33
 * again, if Key_hash() is fast enough, we may not even care about that.
 
34
 */
 
35
 
 
36
/* This defines a single variable-width key.
 
37
 * It is basically the same as a tuple, but
 
38
 * 1) Lighter weight in memory
 
39
 * 2) Only supports strings.
 
40
 * It is mostly used as a helper. Note that Keys() is a similar structure for
 
41
 * lists of Key objects. Its main advantage, though, is that it inlines all of
 
42
 * the Key objects so that you have 1 python object overhead for N Keys, rather
 
43
 * than N objects.
 
44
 */
 
45
 
 
46
#define STATIC_TUPLE_INTERNED_FLAG 0x01
 
47
#define STATIC_TUPLE_ALL_STRING    0x02
 
48
#define STATIC_TUPLE_DID_HASH      0x04
 
49
typedef struct {
 
50
    PyObject_HEAD
 
51
    unsigned char size;
 
52
    unsigned char flags;
 
53
    unsigned char _unused0;
 
54
    unsigned char _unused1;
 
55
    // Note that on 64-bit, we actually have 4-more unused bytes
 
56
    // because items will always be aligned to a 64-bit boundary
 
57
#if STATIC_TUPLE_HAS_HASH
 
58
    long hash;
 
59
#endif
 
60
    PyObject *items[0];
 
61
} StaticTuple;
 
62
extern PyTypeObject StaticTuple_Type;
 
63
 
 
64
typedef struct {
 
65
    PyObject_VAR_HEAD
 
66
    PyObject *table[0];
 
67
} KeyIntern;
 
68
 
 
69
#define StaticTuple_SET_ITEM(key, offset, val) \
 
70
    ((((StaticTuple*)(key))->items[(offset)]) = ((PyObject *)(val)))
 
71
#define StaticTuple_GET_ITEM(key, offset) (((StaticTuple*)key)->items[offset])
 
72
 
 
73
 
 
74
#ifdef STATIC_TUPLE_MODULE
 
75
/* Used when compiling _static_tuple_c.c */
 
76
 
 
77
static StaticTuple * StaticTuple_New(Py_ssize_t);
 
78
static StaticTuple * StaticTuple_Intern(StaticTuple *self);
 
79
#define StaticTuple_CheckExact(op) (Py_TYPE(op) == &StaticTuple_Type)
 
80
 
 
81
#else
 
82
/* Used as the foreign api */
 
83
 
 
84
#include "_import_c_api.h"
 
85
 
 
86
static StaticTuple *(*StaticTuple_New)(Py_ssize_t);
 
87
static StaticTuple *(*StaticTuple_Intern)(StaticTuple *);
 
88
static PyTypeObject *_p_StaticTuple_Type;
 
89
 
 
90
#define StaticTuple_CheckExact(op) (Py_TYPE(op) == _p_StaticTuple_Type)
 
91
static int (*_StaticTuple_CheckExact)(PyObject *);
 
92
 
 
93
 
 
94
/* Return -1 and set exception on error, 0 on success */
 
95
static int
 
96
import_static_tuple_c(void)
 
97
{
 
98
    struct function_description functions[] = {
 
99
        {"StaticTuple_New", (void **)&StaticTuple_New,
 
100
            "StaticTuple *(Py_ssize_t)"},
 
101
        {"StaticTuple_Intern", (void **)&StaticTuple_Intern,
 
102
            "StaticTuple *(StaticTuple *)"},
 
103
        {"_StaticTuple_CheckExact", (void **)&_StaticTuple_CheckExact,
 
104
            "int(PyObject *)"},
 
105
        {NULL}};
 
106
    struct type_description types[] = {
 
107
        {"StaticTuple", &_p_StaticTuple_Type},
 
108
        {NULL}};
 
109
    return _import_extension_module("bzrlib._static_tuple_c",
 
110
        functions, types);
 
111
}
 
112
 
 
113
#endif // !STATIC_TUPLE_MODULE
 
114
#endif // !_STATIC_TUPLE_H_