~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_c.c

  • Committer: John Arbash Meinel
  • Date: 2009-10-07 15:57:25 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-20091007155725-vq1jsr92sk1vmidx
Some code cleanup passes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
177
177
    StaticTuple *self;
178
178
    PyObject *obj = NULL;
179
179
    Py_ssize_t i, len = 0;
180
 
    int is_all_str;
181
180
 
182
181
    if (type != &StaticTuple_Type) {
183
182
        PyErr_SetString(PyExc_TypeError, "we only support creating StaticTuple");
198
197
    if (self == NULL) {
199
198
        return NULL;
200
199
    }
201
 
    is_all_str = 1;
202
200
    for (i = 0; i < len; ++i) {
203
201
        obj = PyTuple_GET_ITEM(args, i);
204
202
        if (!PyString_CheckExact(obj)) {
205
 
            is_all_str = 0;
206
203
            if (!StaticTuple_CheckExact(obj)) {
207
204
                PyErr_SetString(PyExc_TypeError, "StaticTuple.__init__(...)"
208
205
                    " requires that all key bits are strings or StaticTuple.");
214
211
        Py_INCREF(obj);
215
212
        self->items[i] = obj;
216
213
    }
217
 
    if (is_all_str) {
218
 
        self->flags |= STATIC_TUPLE_ALL_STRING;
219
 
    }
220
214
    return (PyObject *)self;
221
215
}
222
216
 
252
246
#endif
253
247
        x = 0x345678L;
254
248
        p = self->items;
255
 
    if (self->flags & STATIC_TUPLE_ALL_STRING
256
 
        && self->flags & STATIC_TUPLE_DID_HASH) {
257
 
        /* If we know that we only reference strings, and we've already
258
 
         * computed the hash one time before, then we know all the strings will
259
 
         * have valid hash entries, and we can just compute, no branching
260
 
         * logic.
261
 
         */
262
 
        while (--len >= 0) {
263
 
            y = ((PyStringObject*)(*p))->ob_shash;
264
 
            x = (x ^ y) * mult;
265
 
            /* the cast might truncate len; that doesn't change hash stability */
266
 
            mult += (long)(82520L + len + len);
267
 
            p++;
268
 
        }
269
 
    } else {
270
 
        while (--len >= 0) {
271
 
            y = PyObject_Hash(*p++);
272
 
            if (y == -1) /* failure */
273
 
                return -1;
274
 
            x = (x ^ y) * mult;
275
 
            /* the cast might truncate len; that doesn't change hash stability */
276
 
            mult += (long)(82520L + len + len);
277
 
        }
 
249
    // TODO: We could set specific flags if we know that, for example, all the
 
250
    //       keys are strings. I haven't seen a real-world benefit to that yet,
 
251
    //       though.
 
252
    while (--len >= 0) {
 
253
        y = PyObject_Hash(*p++);
 
254
        if (y == -1) /* failure */
 
255
            return -1;
 
256
        x = (x ^ y) * mult;
 
257
        /* the cast might truncate len; that doesn't change hash stability */
 
258
        mult += (long)(82520L + len + len);
278
259
    }
279
260
        x += 97531L;
280
261
        if (x == -1)
287
268
    }
288
269
    self->hash = x;
289
270
#endif
290
 
    self->flags |= STATIC_TUPLE_DID_HASH;
291
271
        return x;
292
272
}
293
273
 
638
618
    }
639
619
    // We need to create the empty tuple
640
620
    stuple = (StaticTuple *)StaticTuple_New(0);
641
 
    stuple->flags = STATIC_TUPLE_ALL_STRING;
642
621
    _empty_tuple = StaticTuple_Intern(stuple);
643
622
    assert(_empty_tuple == stuple);
644
623
    // At this point, refcnt is 2: 1 from New(), and 1 from the return from