Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit ae6b88a

Browse files
committed
usage of bool
1 parent 68aaaea commit ae6b88a

File tree

1 file changed

+25
-54
lines changed

1 file changed

+25
-54
lines changed

arraymap.c

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,24 @@ static PyTypeObject FAMVType;
2929
static PyTypeObject FAMType;
3030
static PyObject *NonUniqueError;
3131

32-
3332
// The main storage "table" is an array of TableElement
3433
typedef struct TableElement{
3534
Py_ssize_t keys_pos;
3635
Py_hash_t hash;
3736
} TableElement;
3837

39-
4038
// Table configuration; experimentation shows that these values work well:
4139
# define LOAD 0.9
4240
# define SCAN 16
4341

4442
const static size_t UCS4_SIZE = sizeof(Py_UCS4);
4543

46-
4744
// Partial, two-argument version of PyUnicode_FromKindAndData for consistent templating with bytes version.
4845
static inline PyObject*
4946
PyUnicode_FromUCS4AndData(const void *buffer, Py_ssize_t size) {
5047
return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, size);
5148
}
5249

53-
5450
typedef enum KeysArrayType{
5551
KAT_LIST = 0, // must be falsy
5652

@@ -85,27 +81,22 @@ typedef enum KeysArrayType{
8581
KAT_DTps,
8682
KAT_DTfs,
8783
KAT_DTas,
88-
8984
} KeysArrayType;
9085

91-
9286
NPY_DATETIMEUNIT
9387
dt_unit_from_array(PyArrayObject* a) {
9488
// This is based on get_datetime_metadata_from_dtype in the NumPy source, but that function is private. This does not check that the dytpe is of the appropriate type.
9589
PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyArray_DESCR(a)->c_metadata)->meta);
9690
return dma->base;
9791
}
9892

99-
10093
NPY_DATETIMEUNIT
10194
dt_unit_from_scalar(PyDatetimeScalarObject* dts) {
10295
// Based on convert_pyobject_to_datetime and related usage in datetime.c
10396
PyArray_DatetimeMetaData* dma = &(dts->obmeta);
10497
return dma->base;
10598
}
10699

107-
108-
109100
KeysArrayType
110101
at_to_kat(int array_t, PyArrayObject* a) {
111102
switch (array_t) {
@@ -178,8 +169,7 @@ at_to_kat(int array_t, PyArrayObject* a) {
178169
}
179170
}
180171

181-
182-
// To determine when we can use direct array lookups, this function return 1 if we match, 0 if we do not match. Given a keys array type and the kind of lookup key, return true only for the largest KAT types.s
172+
// To determine when we can use direct array lookups, this function return 1 if we match, 0 if we do not match. Given a keys array type and the kind of lookup key, return true only for the largest KAT types.
183173
int
184174
kat_is_kind(KeysArrayType kat, char kind) {
185175
switch (kat) {
@@ -226,52 +216,52 @@ kat_is_kind(KeysArrayType kat, char kind) {
226216
}
227217

228218
// Given a KAT, determine if it matches a NumPy dt64 unit.
229-
int
219+
bool
230220
kat_is_datetime_unit(KeysArrayType kat, NPY_DATETIMEUNIT unit) {
231221
switch (kat) {
232222
case KAT_DTY:
233-
if (unit == NPY_FR_Y ) {return 1;}
223+
if (unit == NPY_FR_Y ) {return true;}
234224
break;
235225
case KAT_DTM:
236-
if (unit == NPY_FR_M ) {return 1;}
226+
if (unit == NPY_FR_M ) {return true;}
237227
break;
238228
case KAT_DTW:
239-
if (unit == NPY_FR_W ) {return 1;}
229+
if (unit == NPY_FR_W ) {return true;}
240230
break;
241231
case KAT_DTD:
242-
if (unit == NPY_FR_D ) {return 1;}
232+
if (unit == NPY_FR_D ) {return true;}
243233
break;
244234
case KAT_DTh:
245-
if (unit == NPY_FR_h ) {return 1;}
235+
if (unit == NPY_FR_h ) {return true;}
246236
break;
247237
case KAT_DTm:
248-
if (unit == NPY_FR_m ) {return 1;}
238+
if (unit == NPY_FR_m ) {return true;}
249239
break;
250240
case KAT_DTs:
251-
if (unit == NPY_FR_s ) {return 1;}
241+
if (unit == NPY_FR_s ) {return true;}
252242
break;
253243
case KAT_DTms:
254-
if (unit == NPY_FR_ms) {return 1;}
244+
if (unit == NPY_FR_ms) {return true;}
255245
break;
256246
case KAT_DTus:
257-
if (unit == NPY_FR_us) {return 1;}
247+
if (unit == NPY_FR_us) {return true;}
258248
break;
259249
case KAT_DTns:
260-
if (unit == NPY_FR_ns) {return 1;}
250+
if (unit == NPY_FR_ns) {return true;}
261251
break;
262252
case KAT_DTps:
263-
if (unit == NPY_FR_ps) {return 1;}
253+
if (unit == NPY_FR_ps) {return true;}
264254
break;
265255
case KAT_DTfs:
266-
if (unit == NPY_FR_fs) {return 1;}
256+
if (unit == NPY_FR_fs) {return true;}
267257
break;
268258
case KAT_DTas:
269-
if (unit == NPY_FR_as) {return 1;}
259+
if (unit == NPY_FR_as) {return true;}
270260
break;
271261
default: // non dt64 KATs
272-
return 0;
262+
return false;
273263
}
274-
return 0;
264+
return false;
275265
}
276266

277267
typedef struct FAMObject{
@@ -284,14 +274,12 @@ typedef struct FAMObject{
284274
Py_UCS4* key_buffer;
285275
} FAMObject;
286276

287-
288277
typedef enum ViewKind{
289278
ITEMS,
290279
KEYS,
291280
VALUES,
292281
} ViewKind;
293282

294-
295283
// Return the end pointer, or the pointer to the location after the last valid character. The end pointer minus the start pointer is the number of characters. For an empty string, all characters are NULL, and the start pointer and end pointer should be equal. NOTE: would like to use strchr(str, '\0') instead of this routine, but some buffers might not have a null terminator and stread by full to the the dt_size.
296284
static inline Py_UCS4*
297285
ucs4_get_end_p(Py_UCS4* p_start, Py_ssize_t dt_size) {
@@ -303,7 +291,6 @@ ucs4_get_end_p(Py_UCS4* p_start, Py_ssize_t dt_size) {
303291
return p_start;
304292
}
305293

306-
307294
static inline char*
308295
char_get_end_p(char* p_start, Py_ssize_t dt_size) {
309296
for (char* p = p_start + dt_size - 1; p >= p_start; p--) {
@@ -314,7 +301,6 @@ char_get_end_p(char* p_start, Py_ssize_t dt_size) {
314301
return p_start;
315302
}
316303

317-
318304
// This masks the input with INT64_MAX, which removes the MSB; we then cast to an int64; the range is now between 0 and INT64_MAX. We then use the MSB of the original value; if set, we negate the number, producing negative values for the upper half of the uint64 range. Note that we only need to check for hash -1 in this branch.
319305
static inline Py_hash_t
320306
uint_to_hash(npy_uint64 v) {
@@ -337,7 +323,6 @@ int_to_hash(npy_int64 v) {
337323
return hash;
338324
}
339325

340-
341326
// This is a adapted from https://github.com/python/cpython/blob/ba65a065cf07a7a9f53be61057a090f7311a5ad7/Python/pyhash.c#L92
342327
#define HASH_MODULUS (((size_t)1 << 61) - 1)
343328
#define HASH_BITS 61
@@ -379,7 +364,6 @@ double_to_hash(double v)
379364
return (Py_hash_t)x;
380365
}
381366

382-
383367
// The `str` arg is a pointer to a C-array of Py_UCS4; we will only read `len` characters from this. This is a "djb2" hash algorithm.
384368
static inline Py_hash_t
385369
unicode_to_hash(Py_UCS4 *str, Py_ssize_t len) {
@@ -395,7 +379,6 @@ unicode_to_hash(Py_UCS4 *str, Py_ssize_t len) {
395379
return hash;
396380
}
397381

398-
399382
static inline Py_hash_t
400383
string_to_hash(char *str, Py_ssize_t len) {
401384
char* p = str;
@@ -410,17 +393,14 @@ string_to_hash(char *str, Py_ssize_t len) {
410393
return hash;
411394
}
412395

413-
414396
//------------------------------------------------------------------------------
415397
// the global int_cache is shared among all instances
416398

417399
static PyObject *int_cache = NULL;
418400

419-
420401
// NOTE: this used to be a Py_ssize_t, which can be 32 bits on some machines and might easily overflow with a few very large indices. Using an explicit 64-bit int seems safer
421402
static npy_int64 key_count_global = 0;
422403

423-
424404
// Fill the int_cache up to size_needed with PyObject ints; `size` is not the key_count_global.
425405
static int
426406
int_cache_fill(Py_ssize_t size_needed)
@@ -446,7 +426,6 @@ int_cache_fill(Py_ssize_t size_needed)
446426
return 0;
447427
}
448428

449-
450429
// Given the current key_count_global, remove cache elements only if the key_count is less than the the current size of the int_cache.
451430
void
452431
int_cache_remove(Py_ssize_t key_count)
@@ -460,7 +439,6 @@ int_cache_remove(Py_ssize_t key_count)
460439
}
461440
}
462441

463-
464442
//------------------------------------------------------------------------------
465443
// FrozenAutoMapIterator functions
466444

@@ -469,7 +447,7 @@ typedef struct FAMIObject {
469447
FAMObject *fam;
470448
PyArrayObject* keys_array;
471449
ViewKind kind;
472-
int reversed;
450+
bool reversed;
473451
Py_ssize_t index; // current index state, mutated in-place
474452
} FAMIObject;
475453

@@ -481,15 +459,13 @@ fami_dealloc(FAMIObject *self)
481459
PyObject_Del((PyObject *)self);
482460
}
483461

484-
485462
static FAMIObject *
486463
fami_iter(FAMIObject *self)
487464
{
488465
Py_INCREF(self);
489466
return self;
490467
}
491468

492-
493469
// For a FAMI, Return appropriate PyObject for items, keys, and values. When values are needed they are retrieved from the int_cache. For consistency with NumPy array iteration, arrays use PyArray_ToScalar instead of PyArray_GETITEM.
494470
static PyObject *
495471
fami_iternext(FAMIObject *self)
@@ -543,32 +519,27 @@ fami_iternext(FAMIObject *self)
543519
Py_UNREACHABLE();
544520
}
545521

546-
547522
static PyObject *
548523
fami_length_hint(FAMIObject *self)
549524
{
550525
Py_ssize_t len = Py_MAX(0, self->fam->keys_size - self->index);
551526
return PyLong_FromSsize_t(len);
552527
}
553528

554-
555-
static PyObject *fami_new(FAMObject *, ViewKind, int);
556-
529+
static PyObject *fami_new(FAMObject *, ViewKind, bool);
557530

558531
static PyObject *
559532
fami_reversed(FAMIObject *self)
560533
{
561534
return fami_new(self->fam, self->kind, !self->reversed);
562535
}
563536

564-
565537
static PyMethodDef fami_methods[] = {
566538
{"__length_hint__", (PyCFunction)fami_length_hint, METH_NOARGS, NULL},
567539
{"__reversed__", (PyCFunction)fami_reversed, METH_NOARGS, NULL},
568540
{NULL},
569541
};
570542

571-
572543
static PyTypeObject FAMIType = {
573544
PyVarObject_HEAD_INIT(NULL, 0)
574545
.tp_basicsize = sizeof(FAMIObject),
@@ -581,7 +552,7 @@ static PyTypeObject FAMIType = {
581552

582553

583554
static PyObject *
584-
fami_new(FAMObject *fam, ViewKind kind, int reversed)
555+
fami_new(FAMObject *fam, ViewKind kind, bool reversed)
585556
{
586557
FAMIObject *fami = PyObject_New(FAMIObject, &FAMIType);
587558
if (!fami) {
@@ -683,7 +654,7 @@ famv_dealloc(FAMVObject *self)
683654
static PyObject *
684655
famv_fami_new(FAMVObject *self)
685656
{
686-
return fami_new(self->fam, self->kind, 0);
657+
return fami_new(self->fam, self->kind, false);
687658
}
688659

689660

@@ -697,7 +668,7 @@ famv_length_hint(FAMVObject *self)
697668
static PyObject *
698669
famv_reversed(FAMVObject *self)
699670
{
700-
return fami_new(self->fam, self->kind, 1);
671+
return fami_new(self->fam, self->kind, true);
701672
}
702673

703674

@@ -755,7 +726,7 @@ static PyTypeObject FAMVType = {
755726

756727

757728
static PyObject *
758-
famv_new(FAMObject *fam, int kind)
729+
famv_new(FAMObject *fam, ViewKind kind)
759730
{
760731
FAMVObject *famv = (FAMVObject *)PyObject_New(FAMVObject, &FAMVType);
761732
if (!famv) {
@@ -2297,7 +2268,7 @@ fam_hash(FAMObject *self)
22972268
static PyObject *
22982269
fam_iter(FAMObject *self)
22992270
{
2300-
return fami_new(self, KEYS, 0);
2271+
return fami_new(self, KEYS, false);
23012272
}
23022273

23032274

@@ -2311,7 +2282,7 @@ fam_getnewargs(FAMObject *self)
23112282
static PyObject *
23122283
fam_reversed(FAMObject *self)
23132284
{
2314-
return fami_new(self, KEYS, 1);
2285+
return fami_new(self, KEYS, true);
23152286
}
23162287

23172288

0 commit comments

Comments
 (0)