Skip to content

Commit 1398a86

Browse files
authored
loadtxt can deal with multi-line comments (#720)
* loadtxt can deal with multi-line comments * multiline headers/footers are treated correctly * add macro to traverse arrays
1 parent 88ef893 commit 1398a86

File tree

8 files changed

+245
-595
lines changed

8 files changed

+245
-595
lines changed

code/ndarray.c

Lines changed: 50 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -586,43 +586,10 @@ void ndarray_copy_array(ndarray_obj_t *source, ndarray_obj_t *target, uint8_t sh
586586
}
587587
#endif
588588

589-
#if ULAB_MAX_DIMS > 3
590-
size_t i = 0;
591-
do {
592-
#endif
593-
#if ULAB_MAX_DIMS > 2
594-
size_t j = 0;
595-
do {
596-
#endif
597-
#if ULAB_MAX_DIMS > 1
598-
size_t k = 0;
599-
do {
600-
#endif
601-
size_t l = 0;
602-
do {
603-
memcpy(tarray, sarray, target->itemsize);
604-
tarray += target->itemsize;
605-
sarray += source->strides[ULAB_MAX_DIMS - 1];
606-
l++;
607-
} while(l < source->shape[ULAB_MAX_DIMS - 1]);
608-
#if ULAB_MAX_DIMS > 1
609-
sarray -= source->strides[ULAB_MAX_DIMS - 1] * source->shape[ULAB_MAX_DIMS-1];
610-
sarray += source->strides[ULAB_MAX_DIMS - 2];
611-
k++;
612-
} while(k < source->shape[ULAB_MAX_DIMS - 2]);
613-
#endif
614-
#if ULAB_MAX_DIMS > 2
615-
sarray -= source->strides[ULAB_MAX_DIMS - 2] * source->shape[ULAB_MAX_DIMS-2];
616-
sarray += source->strides[ULAB_MAX_DIMS - 3];
617-
j++;
618-
} while(j < source->shape[ULAB_MAX_DIMS - 3]);
619-
#endif
620-
#if ULAB_MAX_DIMS > 3
621-
sarray -= source->strides[ULAB_MAX_DIMS - 3] * source->shape[ULAB_MAX_DIMS-3];
622-
sarray += source->strides[ULAB_MAX_DIMS - 4];
623-
i++;
624-
} while(i < source->shape[ULAB_MAX_DIMS - 4]);
625-
#endif
589+
ITERATOR_HEAD();
590+
memcpy(tarray, sarray, target->itemsize);
591+
tarray += target->itemsize;
592+
ITERATOR_TAIL(source, sarray);
626593
}
627594

628595
ndarray_obj_t *ndarray_new_view(ndarray_obj_t *source, uint8_t ndim, size_t *shape, int32_t *strides, int32_t offset) {
@@ -676,69 +643,36 @@ ndarray_obj_t *ndarray_copy_view_convert_type(ndarray_obj_t *source, uint8_t dty
676643
uint8_t complex_size = 2 * sizeof(mp_float_t);
677644
#endif
678645

679-
#if ULAB_MAX_DIMS > 3
680-
size_t i = 0;
681-
do {
682-
#endif
683-
#if ULAB_MAX_DIMS > 2
684-
size_t j = 0;
685-
do {
646+
ITERATOR_HEAD()
647+
mp_obj_t item;
648+
#if ULAB_SUPPORTS_COMPLEX
649+
if(source->dtype == NDARRAY_COMPLEX) {
650+
if(dtype != NDARRAY_COMPLEX) {
651+
mp_raise_TypeError(MP_ERROR_TEXT("cannot convert complex type"));
652+
} else {
653+
memcpy(array, sarray, complex_size);
654+
}
655+
} else {
686656
#endif
687-
#if ULAB_MAX_DIMS > 1
688-
size_t k = 0;
689-
do {
690-
#endif
691-
size_t l = 0;
692-
do {
693-
mp_obj_t item;
694-
#if ULAB_SUPPORTS_COMPLEX
695-
if(source->dtype == NDARRAY_COMPLEX) {
696-
if(dtype != NDARRAY_COMPLEX) {
697-
mp_raise_TypeError(MP_ERROR_TEXT("cannot convert complex type"));
698-
} else {
699-
memcpy(array, sarray, complex_size);
700-
}
701-
} else {
702-
#endif
703-
if((source->dtype == NDARRAY_FLOAT) && (dtype != NDARRAY_FLOAT)) {
704-
// floats must be treated separately, because they can't directly be converted to integer types
705-
mp_float_t f = ndarray_get_float_value(sarray, source->dtype);
706-
item = mp_obj_new_int((int32_t)MICROPY_FLOAT_C_FUN(round)(f));
707-
} else {
708-
item = mp_binary_get_val_array(source->dtype, sarray, 0);
709-
}
710-
#if ULAB_SUPPORTS_COMPLEX
711-
if(dtype == NDARRAY_COMPLEX) {
712-
ndarray_set_value(NDARRAY_FLOAT, array, 0, item);
713-
} else {
714-
ndarray_set_value(dtype, array, 0, item);
715-
}
716-
}
717-
#else
718-
ndarray_set_value(dtype, array, 0, item);
719-
#endif
720-
array += ndarray->itemsize;
721-
sarray += source->strides[ULAB_MAX_DIMS - 1];
722-
l++;
723-
} while(l < source->shape[ULAB_MAX_DIMS - 1]);
724-
#if ULAB_MAX_DIMS > 1
725-
sarray -= source->strides[ULAB_MAX_DIMS - 1] * source->shape[ULAB_MAX_DIMS-1];
726-
sarray += source->strides[ULAB_MAX_DIMS - 2];
727-
k++;
728-
} while(k < source->shape[ULAB_MAX_DIMS - 2]);
729-
#endif
730-
#if ULAB_MAX_DIMS > 2
731-
sarray -= source->strides[ULAB_MAX_DIMS - 2] * source->shape[ULAB_MAX_DIMS-2];
732-
sarray += source->strides[ULAB_MAX_DIMS - 3];
733-
j++;
734-
} while(j < source->shape[ULAB_MAX_DIMS - 3]);
657+
if((source->dtype == NDARRAY_FLOAT) && (dtype != NDARRAY_FLOAT)) {
658+
// floats must be treated separately, because they can't directly be converted to integer types
659+
mp_float_t f = ndarray_get_float_value(sarray, source->dtype);
660+
item = mp_obj_new_int((int32_t)MICROPY_FLOAT_C_FUN(round)(f));
661+
} else {
662+
item = mp_binary_get_val_array(source->dtype, sarray, 0);
663+
}
664+
#if ULAB_SUPPORTS_COMPLEX
665+
if(dtype == NDARRAY_COMPLEX) {
666+
ndarray_set_value(NDARRAY_FLOAT, array, 0, item);
667+
} else {
668+
ndarray_set_value(dtype, array, 0, item);
669+
}
670+
}
671+
#else
672+
ndarray_set_value(dtype, array, 0, item);
735673
#endif
736-
#if ULAB_MAX_DIMS > 3
737-
sarray -= source->strides[ULAB_MAX_DIMS - 3] * source->shape[ULAB_MAX_DIMS-3];
738-
sarray += source->strides[ULAB_MAX_DIMS - 4];
739-
i++;
740-
} while(i < source->shape[ULAB_MAX_DIMS - 4]);
741-
#endif
674+
array += ndarray->itemsize;
675+
ITERATOR_TAIL(source, sarray);
742676
return ndarray;
743677
}
744678

@@ -765,54 +699,21 @@ mp_obj_t ndarray_byteswap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
765699
return MP_OBJ_FROM_PTR(ndarray);
766700
} else {
767701
uint8_t *array = (uint8_t *)ndarray->array;
768-
#if ULAB_MAX_DIMS > 3
769-
size_t i = 0;
770-
do {
771-
#endif
772-
#if ULAB_MAX_DIMS > 2
773-
size_t j = 0;
774-
do {
775-
#endif
776-
#if ULAB_MAX_DIMS > 1
777-
size_t k = 0;
778-
do {
779-
#endif
780-
size_t l = 0;
781-
do {
782-
if(self->dtype == NDARRAY_FLOAT) {
783-
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
784-
SWAP(uint8_t, array[0], array[3]);
785-
SWAP(uint8_t, array[1], array[2]);
786-
#else
787-
SWAP(uint8_t, array[0], array[7]);
788-
SWAP(uint8_t, array[1], array[6]);
789-
SWAP(uint8_t, array[2], array[5]);
790-
SWAP(uint8_t, array[3], array[4]);
791-
#endif
792-
} else {
793-
SWAP(uint8_t, array[0], array[1]);
794-
}
795-
array += ndarray->strides[ULAB_MAX_DIMS - 1];
796-
l++;
797-
} while(l < ndarray->shape[ULAB_MAX_DIMS - 1]);
798-
#if ULAB_MAX_DIMS > 1
799-
array -= ndarray->strides[ULAB_MAX_DIMS - 1] * ndarray->shape[ULAB_MAX_DIMS-1];
800-
array += ndarray->strides[ULAB_MAX_DIMS - 2];
801-
k++;
802-
} while(k < ndarray->shape[ULAB_MAX_DIMS - 2]);
702+
ITERATOR_HEAD();
703+
if(self->dtype == NDARRAY_FLOAT) {
704+
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
705+
SWAP(uint8_t, array[0], array[3]);
706+
SWAP(uint8_t, array[1], array[2]);
707+
#else
708+
SWAP(uint8_t, array[0], array[7]);
709+
SWAP(uint8_t, array[1], array[6]);
710+
SWAP(uint8_t, array[2], array[5]);
711+
SWAP(uint8_t, array[3], array[4]);
803712
#endif
804-
#if ULAB_MAX_DIMS > 2
805-
array -= ndarray->strides[ULAB_MAX_DIMS - 2] * ndarray->shape[ULAB_MAX_DIMS-2];
806-
array += ndarray->strides[ULAB_MAX_DIMS - 3];
807-
j++;
808-
} while(j < ndarray->shape[ULAB_MAX_DIMS - 3]);
809-
#endif
810-
#if ULAB_MAX_DIMS > 3
811-
array -= ndarray->strides[ULAB_MAX_DIMS - 3] * ndarray->shape[ULAB_MAX_DIMS-3];
812-
array += ndarray->strides[ULAB_MAX_DIMS - 4];
813-
i++;
814-
} while(i < ndarray->shape[ULAB_MAX_DIMS - 4]);
815-
#endif
713+
} else {
714+
SWAP(uint8_t, array[0], array[1]);
715+
}
716+
ITERATOR_TAIL(ndarray, array);
816717
}
817718
return MP_OBJ_FROM_PTR(ndarray);
818719
}
@@ -1441,43 +1342,10 @@ mp_obj_t ndarray_flatten(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
14411342
uint8_t *array = (uint8_t *)ndarray->array;
14421343

14431344
if(memcmp(order, "C", 1) == 0) { // C-type ordering
1444-
#if ULAB_MAX_DIMS > 3
1445-
size_t i = 0;
1446-
do {
1447-
#endif
1448-
#if ULAB_MAX_DIMS > 2
1449-
size_t j = 0;
1450-
do {
1451-
#endif
1452-
#if ULAB_MAX_DIMS > 1
1453-
size_t k = 0;
1454-
do {
1455-
#endif
1456-
size_t l = 0;
1457-
do {
1458-
memcpy(array, sarray, self->itemsize);
1459-
array += ndarray->strides[ULAB_MAX_DIMS - 1];
1460-
sarray += self->strides[ULAB_MAX_DIMS - 1];
1461-
l++;
1462-
} while(l < self->shape[ULAB_MAX_DIMS - 1]);
1463-
#if ULAB_MAX_DIMS > 1
1464-
sarray -= self->strides[ULAB_MAX_DIMS - 1] * self->shape[ULAB_MAX_DIMS-1];
1465-
sarray += self->strides[ULAB_MAX_DIMS - 2];
1466-
k++;
1467-
} while(k < self->shape[ULAB_MAX_DIMS - 2]);
1468-
#endif
1469-
#if ULAB_MAX_DIMS > 2
1470-
sarray -= self->strides[ULAB_MAX_DIMS - 2] * self->shape[ULAB_MAX_DIMS-2];
1471-
sarray += self->strides[ULAB_MAX_DIMS - 3];
1472-
j++;
1473-
} while(j < self->shape[ULAB_MAX_DIMS - 3]);
1474-
#endif
1475-
#if ULAB_MAX_DIMS > 3
1476-
sarray -= self->strides[ULAB_MAX_DIMS - 3] * self->shape[ULAB_MAX_DIMS-3];
1477-
sarray += self->strides[ULAB_MAX_DIMS - 4];
1478-
i++;
1479-
} while(i < self->shape[ULAB_MAX_DIMS - 4]);
1480-
#endif
1345+
ITERATOR_HEAD();
1346+
memcpy(array, sarray, self->itemsize);
1347+
array += ndarray->strides[ULAB_MAX_DIMS - 1];
1348+
ITERATOR_TAIL(self, sarray);
14811349
} else { // 'F', Fortran-type ordering
14821350
#if ULAB_MAX_DIMS > 3
14831351
size_t i = 0;

code/ndarray.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,89 @@ ndarray_obj_t *ndarray_from_mp_obj(mp_obj_t , uint8_t );
709709
#endif /* ULAB_MAX_DIMS == 4 */
710710
#endif /* ULAB_HAS_FUNCTION_ITERATOR */
711711

712+
713+
// iterator macro for traversing arrays over all dimensions
714+
#if ULAB_MAX_DIMS == 1
715+
#define ITERATOR_HEAD()\
716+
size_t _l_ = 0;\
717+
do {
718+
719+
#define ITERATOR_TAIL(_source_, _source_array_)\
720+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 1];\
721+
_l_++;\
722+
} while(_l_ < (_source_)->shape[ULAB_MAX_DIMS - 1]);
723+
724+
#endif /* ULAB_MAX_DIMS == 1 */
725+
726+
#if ULAB_MAX_DIMS == 2
727+
#define ITERATOR_HEAD()\
728+
size_t _k_ = 0;\
729+
do {\
730+
size_t _l_ = 0;\
731+
do {
732+
733+
#define ITERATOR_TAIL(_source_, _source_array_)\
734+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 1];\
735+
_l_++;\
736+
} while(_l_ < (_source_)->shape[ULAB_MAX_DIMS - 1]);\
737+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 1] * (_source_)->shape[ULAB_MAX_DIMS - 1];\
738+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 2];\
739+
_k_++;\
740+
} while(_k_ < (_source_)->shape[ULAB_MAX_DIMS - 2]);
741+
#endif /* ULAB_MAX_DIMS == 2 */
742+
743+
#if ULAB_MAX_DIMS == 3
744+
#define ITERATOR_HEAD()\
745+
size_t _j_ = 0;\
746+
do {\
747+
size_t _k_ = 0;\
748+
do {\
749+
size_t _l_ = 0;\
750+
do {
751+
752+
#define ITERATOR_TAIL(_source_, _source_array_)\
753+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 1];\
754+
_l_++;\
755+
} while(_l_ < (_source_)->shape[ULAB_MAX_DIMS - 1]);\
756+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 1] * (_source_)->shape[ULAB_MAX_DIMS - 1];\
757+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 2];\
758+
_k_++;\
759+
} while(_k_ < (_source_)->shape[ULAB_MAX_DIMS - 2]);\
760+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 2] * (_source_)->shape[ULAB_MAX_DIMS - 2];\
761+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 3];\
762+
j++;\
763+
} while(j < (_source_)->shape[ULAB_MAX_DIMS - 3]);
764+
765+
#endif /* ULAB_MAX_DIMS == 3 */
766+
767+
#if ULAB_MAX_DIMS == 4
768+
#define ITERATOR_HEAD()\
769+
size_t _i_ = 0;\
770+
do {\
771+
size_t _j_ = 0;\
772+
do {\
773+
size_t _k_ = 0;\
774+
do {\
775+
size_t _l_ = 0;\
776+
do {
777+
778+
#define ITERATOR_TAIL(_source_, _source_array_)\
779+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 1];\
780+
_l_++;\
781+
} while(_l_ < (_source_)->shape[ULAB_MAX_DIMS - 1]);\
782+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 1] * (_source_)->shape[ULAB_MAX_DIMS - 1];\
783+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 2];\
784+
_k_++;\
785+
} while(_k_ < (_source_)->shape[ULAB_MAX_DIMS - 2]);\
786+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 2] * (_source_)->shape[ULAB_MAX_DIMS - 2];\
787+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 3];\
788+
_j_++;\
789+
} while(_j_ < (_source_)->shape[ULAB_MAX_DIMS - 3]);\
790+
(_source_array_) -= (_source_)->strides[ULAB_MAX_DIMS - 3] * (_source_)->shape[ULAB_MAX_DIMS - 3];\
791+
(_source_array_) += (_source_)->strides[ULAB_MAX_DIMS - 4];\
792+
_i_++;\
793+
} while(_i_ < (_source_)->shape[ULAB_MAX_DIMS - 4]);
794+
#endif /* ULAB_MAX_DIMS == 4 */
795+
796+
712797
#endif

0 commit comments

Comments
 (0)