From 9b066f432793b3a39fdcfaf3265258d9095528a9 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Fri, 6 Nov 2015 17:55:15 +0100 Subject: [PATCH] Major update: obiarrays with columns containing indices referring to character strings. --- python/obitools3/obidms/_obidms.pxd | 22 +- python/obitools3/obidms/_obidms.pyx | 26 +- .../obitools3/obidms/_obidmscolumn_bool.pxd | 20 +- .../obitools3/obidms/_obidmscolumn_bool.pyx | 20 +- .../obitools3/obidms/_obidmscolumn_char.pxd | 20 +- .../obitools3/obidms/_obidmscolumn_char.pyx | 26 +- .../obitools3/obidms/_obidmscolumn_float.pxd | 20 +- .../obitools3/obidms/_obidmscolumn_float.pyx | 20 +- python/obitools3/obidms/_obidmscolumn_int.pxd | 20 +- python/obitools3/obidms/_obidmscolumn_int.pyx | 20 +- python/obitools3/obidms/_obidmscolumn_str.pxd | 19 +- python/obitools3/obidms/_obidmscolumn_str.pyx | 44 +-- python/obitools3/obidms/capi/obidmscolumn.pxd | 71 ++-- python/obitools3/obidms/capi/obitypes.pxd | 6 +- python/obitools3/unit_tests.py | 64 +++- python/obitools3/version.py | 2 +- src/obiarray.c | 313 +++++++++--------- src/obiarray.h | 152 +++++++-- src/obidms.c | 4 - src/obidms.h | 2 + src/obidmscolumn.c | 44 +-- src/obidmscolumn.h | 20 +- src/obidmscolumn_bool.c | 16 +- src/obidmscolumn_bool.h | 10 +- src/obidmscolumn_char.c | 16 +- src/obidmscolumn_char.h | 10 +- src/obidmscolumn_float.c | 16 +- src/obidmscolumn_float.h | 10 +- src/obidmscolumn_int.c | 16 +- src/obidmscolumn_int.h | 10 +- src/obidmscolumn_str.c | 43 +-- src/obidmscolumn_str.h | 48 +-- src/obierrno.h | 2 +- src/obitypes.c | 4 +- src/obitypes.h | 8 +- 35 files changed, 632 insertions(+), 532 deletions(-) diff --git a/python/obitools3/obidms/_obidms.pxd b/python/obitools3/obidms/_obidms.pxd index 4699fe5..a25ad03 100644 --- a/python/obitools3/obidms/_obidms.pxd +++ b/python/obitools3/obidms/_obidms.pxd @@ -2,7 +2,7 @@ from .capi.obidms cimport OBIDMS_p from .capi.obidmscolumn cimport OBIDMS_column_p -from .capi.obitypes cimport obiversion_t, OBIType_t +from .capi.obitypes cimport obiversion_t, OBIType_t, index_t cdef class OBIDMS_column @@ -21,8 +21,8 @@ cdef class OBIDMS: bint clone=*, bint clone_data=*, obiversion_t version_number=*, OBIType_t data_type=*, - size_t nb_lines=*, - size_t nb_elements_per_line=*, + index_t nb_lines=*, + index_t nb_elements_per_line=*, list elements_names=*, str array_name=*) @@ -34,14 +34,14 @@ cdef class OBIDMS_column: cdef str data_type # TODO keep as OBIType_t? both? cdef str dms_name cdef str column_name - cdef size_t nb_elements_per_line + cdef index_t nb_elements_per_line cdef list elements_names -# cpdef object get_item(self, size_t line_nb, str element_name) TODO can't declare because not the same in all subclasses -# cpdef set_item(self, size_t line_nb, str element_name, object value) TODO can't declare because object value - cpdef list get_elements_names(self) - cpdef str get_data_type(self) - cpdef size_t get_nb_lines_used(self) - cpdef str get_creation_date(self) - cpdef close(self) +# cpdef object get_item(self, index_t line_nb, str element_name) TODO can't declare because not the same in all subclasses +# cpdef set_item(self, index_t line_nb, str element_name, object value) TODO can't declare because object value + cpdef list get_elements_names(self) + cpdef str get_data_type(self) + cpdef index_t get_nb_lines_used(self) + cpdef str get_creation_date(self) + cpdef close(self) diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index 36ef4f9..46a39a9 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -80,7 +80,7 @@ cdef class OBIDMS : cdef str data_type cdef str creation_date cdef obiversion_t latest_version - cdef size_t line_count + cdef index_t line_count cdef OBIDMS_column_header_p header p = Path(self.dms_name+'.obidms') @@ -117,8 +117,8 @@ cdef class OBIDMS : bint clone=False, bint clone_data=True, obiversion_t version_number=-1, OBIType_t data_type= 0, - size_t nb_lines=0, - size_t nb_elements_per_line=0, + index_t nb_lines=0, + index_t nb_elements_per_line=0, list elements_names=None, str array_name="default_obiarray"): @@ -222,7 +222,7 @@ cdef class OBIDMS : version_number, data_type, nb_lines, nb_elements_per_line, elements_names, array_name) - + return column @@ -237,8 +237,8 @@ cdef class OBIDMS_column : bint clone, bint clone_data, obiversion_t version_number, OBIType_t type, - size_t nb_lines, - size_t nb_elements_per_line, + index_t nb_lines, + index_t nb_elements_per_line, list elements_names, str array_name): @@ -279,8 +279,8 @@ cdef class OBIDMS_column : def __iter__(self): # Declarations - cdef size_t lines_used - cdef size_t line_nb + cdef index_t lines_used + cdef index_t line_nb # Yield each line lines_used = self.pointer.header.lines_used @@ -288,19 +288,19 @@ cdef class OBIDMS_column : yield self.get_line(line_nb) - def __setitem__(self, size_t line_nb, object value): + def __setitem__(self, index_t line_nb, object value): self.set_line(line_nb, value) - def __getitem__(self, size_t line_nb): + def __getitem__(self, index_t line_nb): return self.get_line(line_nb) -# cpdef object get_item(self, size_t line_nb, str element_name): TODO +# cpdef object get_item(self, index_t line_nb, str element_name): TODO # raise NotImplementedError -# cpdef set_item(self, size_t line_nb, str element_name, object value): TODO +# cpdef set_item(self, index_t line_nb, str element_name, object value): TODO # raise NotImplementedError @@ -312,7 +312,7 @@ cdef class OBIDMS_column : return self.data_type - cpdef size_t get_nb_lines_used(self): + cpdef index_t get_nb_lines_used(self): return self.pointer.header.lines_used diff --git a/python/obitools3/obidms/_obidmscolumn_bool.pxd b/python/obitools3/obidms/_obidmscolumn_bool.pxd index 6a9f6ae..f07ae93 100644 --- a/python/obitools3/obidms/_obidmscolumn_bool.pxd +++ b/python/obitools3/obidms/_obidmscolumn_bool.pxd @@ -1,25 +1,25 @@ #cython: language_level=3 -from .capi.obitypes cimport obibool_t +from .capi.obitypes cimport obibool_t, index_t from ._obidms cimport OBIDMS_column cdef class OBIDMS_column_bool(OBIDMS_column): - cpdef object get_line(self, size_t line_nb) - cpdef set_line(self, size_t line_nb, object value) + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_bool_writable(OBIDMS_column_bool): - cpdef set_line(self, size_t line_nb, object value) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_bool_multi_elts(OBIDMS_column_bool): - cpdef object get_item(self, size_t line_nb, str element_name) - cpdef object get_line(self, size_t line_nb) - cpdef set_item(self, size_t line_nb, str element_name, obibool_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef object get_item(self, index_t line_nb, str element_name) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, str element_name, obibool_t value) + cpdef set_line(self, index_t line_nb, object values) cdef class OBIDMS_column_bool_multi_elts_writable(OBIDMS_column_bool_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obibool_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef set_item(self, index_t line_nb, str element_name, obibool_t value) + cpdef set_line(self, index_t line_nb, object values) cpdef close(self) diff --git a/python/obitools3/obidms/_obidmscolumn_bool.pyx b/python/obitools3/obidms/_obidmscolumn_bool.pyx index 1e5c6ca..8bc04b8 100644 --- a/python/obitools3/obidms/_obidmscolumn_bool.pyx +++ b/python/obitools3/obidms/_obidmscolumn_bool.pyx @@ -16,7 +16,7 @@ from cpython.bool cimport PyBool_FromLong cdef class OBIDMS_column_bool(OBIDMS_column): - cpdef object get_line(self, size_t line_nb): + cpdef object get_line(self, index_t line_nb): cdef obibool_t value cdef object result value = obi_column_get_obibool_with_elt_idx(self.pointer, line_nb, 0) @@ -28,7 +28,7 @@ cdef class OBIDMS_column_bool(OBIDMS_column): result = PyBool_FromLong(value) return result - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): raise Exception("Column is read-only") cpdef close(self): @@ -38,7 +38,7 @@ cdef class OBIDMS_column_bool(OBIDMS_column): cdef class OBIDMS_column_bool_writable(OBIDMS_column_bool): - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): if obi_column_set_obibool_with_elt_idx(self.pointer, line_nb, 0, value) < 0: raise Exception("Problem setting a value in a column") @@ -49,7 +49,7 @@ cdef class OBIDMS_column_bool_writable(OBIDMS_column_bool): cdef class OBIDMS_column_bool_multi_elts(OBIDMS_column_bool): - cpdef object get_item(self, size_t line_nb, str element_name): + cpdef object get_item(self, index_t line_nb, str element_name): cdef obibool_t value cdef object result value = obi_column_get_obibool_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) @@ -61,10 +61,10 @@ cdef class OBIDMS_column_bool_multi_elts(OBIDMS_column_bool): result = PyBool_FromLong(value) return result - cpdef object get_line(self, size_t line_nb) : + cpdef object get_line(self, index_t line_nb) : cdef obibool_t value cdef object result - cdef size_t i + cdef index_t i cdef bint all_NA result = {} all_NA = True @@ -79,20 +79,20 @@ cdef class OBIDMS_column_bool_multi_elts(OBIDMS_column_bool): result = None return result - cpdef set_item(self, size_t line_nb, str element_name, obibool_t value): + cpdef set_item(self, index_t line_nb, str element_name, obibool_t value): raise Exception("Column is read-only") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): raise Exception("Column is read-only") cdef class OBIDMS_column_bool_multi_elts_writable(OBIDMS_column_bool_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obibool_t value): + cpdef set_item(self, index_t line_nb, str element_name, obibool_t value): if obi_column_set_obibool_with_elt_name(self.pointer, line_nb, str2bytes(element_name), value) < 0: raise Exception("Problem setting a value in a column") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): cdef obibool_t value for element_name in values : value = values[element_name] diff --git a/python/obitools3/obidms/_obidmscolumn_char.pxd b/python/obitools3/obidms/_obidmscolumn_char.pxd index 6c88ee4..d85d4ac 100644 --- a/python/obitools3/obidms/_obidmscolumn_char.pxd +++ b/python/obitools3/obidms/_obidmscolumn_char.pxd @@ -1,25 +1,25 @@ #cython: language_level=3 -from .capi.obitypes cimport obichar_t +from .capi.obitypes cimport obichar_t, index_t from ._obidms cimport OBIDMS_column cdef class OBIDMS_column_char(OBIDMS_column): - cpdef object get_line(self, size_t line_nb) - cpdef set_line(self, size_t line_nb, object value) + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_char_writable(OBIDMS_column_char): - cpdef set_line(self, size_t line_nb, object value) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_char_multi_elts(OBIDMS_column_char): - cpdef object get_item(self, size_t line_nb, str element_name) - cpdef object get_line(self, size_t line_nb) - cpdef set_item(self, size_t line_nb, str element_name, bytes value) - cpdef set_line(self, size_t line_nb, object values) + cpdef object get_item(self, index_t line_nb, str element_name) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, str element_name, bytes value) + cpdef set_line(self, index_t line_nb, object values) cdef class OBIDMS_column_char_multi_elts_writable(OBIDMS_column_char_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, bytes value) - cpdef set_line(self, size_t line_nb, object values) + cpdef set_item(self, index_t line_nb, str element_name, bytes value) + cpdef set_line(self, index_t line_nb, object values) cpdef close(self) diff --git a/python/obitools3/obidms/_obidmscolumn_char.pyx b/python/obitools3/obidms/_obidmscolumn_char.pyx index 98bc220..161968b 100644 --- a/python/obitools3/obidms/_obidmscolumn_char.pyx +++ b/python/obitools3/obidms/_obidmscolumn_char.pyx @@ -14,7 +14,7 @@ from obitools3.utils cimport str2bytes cdef class OBIDMS_column_char(OBIDMS_column): - cpdef object get_line(self, size_t line_nb): + cpdef object get_line(self, index_t line_nb): cdef obichar_t value cdef object result value = obi_column_get_obichar_with_elt_idx(self.pointer, line_nb, 0) @@ -26,7 +26,7 @@ cdef class OBIDMS_column_char(OBIDMS_column): result = value return result - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): raise Exception("Column is read-only") cpdef close(self): @@ -36,7 +36,7 @@ cdef class OBIDMS_column_char(OBIDMS_column): cdef class OBIDMS_column_char_writable(OBIDMS_column_char): - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): if obi_column_set_obichar_with_elt_idx(self.pointer, line_nb, 0, value[0]) < 0: raise Exception("Problem setting a value in a column") @@ -47,7 +47,7 @@ cdef class OBIDMS_column_char_writable(OBIDMS_column_char): cdef class OBIDMS_column_char_multi_elts(OBIDMS_column_char): - cpdef object get_item(self, size_t line_nb, str element_name): + cpdef object get_item(self, index_t line_nb, str element_name): cdef obichar_t value cdef object result value = obi_column_get_obichar_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) @@ -59,10 +59,10 @@ cdef class OBIDMS_column_char_multi_elts(OBIDMS_column_char): result = value return result - cpdef object get_line(self, size_t line_nb) : + cpdef object get_line(self, index_t line_nb) : cdef obichar_t value cdef object result - cdef size_t i + cdef index_t i cdef bint all_NA result = {} all_NA = True @@ -77,20 +77,20 @@ cdef class OBIDMS_column_char_multi_elts(OBIDMS_column_char): result = None return result - cpdef set_item(self, size_t line_nb, str element_name, bytes value): + cpdef set_item(self, index_t line_nb, str element_name, bytes value): raise Exception("Column is read-only") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): raise Exception("Column is read-only") cdef class OBIDMS_column_char_multi_elts_writable(OBIDMS_column_char_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, bytes value): + cpdef set_item(self, index_t line_nb, str element_name, bytes value): if obi_column_set_obichar_with_elt_name(self.pointer, line_nb, str2bytes(element_name), value[0]) < 0: raise Exception("Problem setting a value in a column") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): cdef bytes value for element_name in values : value = values[element_name] @@ -106,7 +106,7 @@ cdef class OBIDMS_column_char_multi_elts_writable(OBIDMS_column_char_multi_elts) # cdef class OBIDMS_column_char(OBIDMS_column) : # -# cpdef object get_item(self, size_t line_nb, str element_name): +# cpdef object get_item(self, index_t line_nb, str element_name): # cdef char value # cdef object result # value = obi_column_get_obichar_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) @@ -118,7 +118,7 @@ cdef class OBIDMS_column_char_multi_elts_writable(OBIDMS_column_char_multi_elts) # result = value # return result # -# cpdef set_item(self, size_t line_nb, str element_name, bytes value): +# cpdef set_item(self, index_t line_nb, str element_name, bytes value): # raise Exception("Column is read-only") # # cpdef close(self): @@ -128,7 +128,7 @@ cdef class OBIDMS_column_char_multi_elts_writable(OBIDMS_column_char_multi_elts) # # cdef class OBIDMS_column_char_writable(OBIDMS_column_char) : # -# cpdef set_item(self, size_t line_nb, str element_name, bytes value): +# cpdef set_item(self, index_t line_nb, str element_name, bytes value): # if obi_column_set_obichar_with_elt_name(self.pointer, line_nb, str2bytes(element_name), value[0]) < 0: # raise Exception("Problem setting a value in a column") # diff --git a/python/obitools3/obidms/_obidmscolumn_float.pxd b/python/obitools3/obidms/_obidmscolumn_float.pxd index dba433d..e14d9c1 100644 --- a/python/obitools3/obidms/_obidmscolumn_float.pxd +++ b/python/obitools3/obidms/_obidmscolumn_float.pxd @@ -1,25 +1,25 @@ #cython: language_level=3 -from .capi.obitypes cimport obifloat_t +from .capi.obitypes cimport obifloat_t, index_t from ._obidms cimport OBIDMS_column cdef class OBIDMS_column_float(OBIDMS_column): - cpdef object get_line(self, size_t line_nb) - cpdef set_line(self, size_t line_nb, object value) + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_float_writable(OBIDMS_column_float): - cpdef set_line(self, size_t line_nb, object value) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_float_multi_elts(OBIDMS_column_float): - cpdef object get_item(self, size_t line_nb, str element_name) - cpdef object get_line(self, size_t line_nb) - cpdef set_item(self, size_t line_nb, str element_name, obifloat_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef object get_item(self, index_t line_nb, str element_name) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, str element_name, obifloat_t value) + cpdef set_line(self, index_t line_nb, object values) cdef class OBIDMS_column_float_multi_elts_writable(OBIDMS_column_float_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obifloat_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef set_item(self, index_t line_nb, str element_name, obifloat_t value) + cpdef set_line(self, index_t line_nb, object values) cpdef close(self) diff --git a/python/obitools3/obidms/_obidmscolumn_float.pyx b/python/obitools3/obidms/_obidmscolumn_float.pyx index 77c2ff3..e7516ac 100644 --- a/python/obitools3/obidms/_obidmscolumn_float.pyx +++ b/python/obitools3/obidms/_obidmscolumn_float.pyx @@ -14,7 +14,7 @@ from obitools3.utils cimport str2bytes cdef class OBIDMS_column_float(OBIDMS_column): - cpdef object get_line(self, size_t line_nb): + cpdef object get_line(self, index_t line_nb): cdef obifloat_t value cdef object result value = obi_column_get_obifloat_with_elt_idx(self.pointer, line_nb, 0) @@ -26,7 +26,7 @@ cdef class OBIDMS_column_float(OBIDMS_column): result = value return result - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): raise Exception("Column is read-only") cpdef close(self): @@ -36,7 +36,7 @@ cdef class OBIDMS_column_float(OBIDMS_column): cdef class OBIDMS_column_float_writable(OBIDMS_column_float): - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): if obi_column_set_obifloat_with_elt_idx(self.pointer, line_nb, 0, value) < 0: raise Exception("Problem setting a value in a column") @@ -47,7 +47,7 @@ cdef class OBIDMS_column_float_writable(OBIDMS_column_float): cdef class OBIDMS_column_float_multi_elts(OBIDMS_column_float): - cpdef object get_item(self, size_t line_nb, str element_name): + cpdef object get_item(self, index_t line_nb, str element_name): cdef obifloat_t value cdef object result value = obi_column_get_obifloat_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) @@ -59,10 +59,10 @@ cdef class OBIDMS_column_float_multi_elts(OBIDMS_column_float): result = value return result - cpdef object get_line(self, size_t line_nb) : + cpdef object get_line(self, index_t line_nb) : cdef obifloat_t value cdef object result - cdef size_t i + cdef index_t i cdef bint all_NA result = {} all_NA = True @@ -77,20 +77,20 @@ cdef class OBIDMS_column_float_multi_elts(OBIDMS_column_float): result = None return result - cpdef set_item(self, size_t line_nb, str element_name, obifloat_t value): + cpdef set_item(self, index_t line_nb, str element_name, obifloat_t value): raise Exception("Column is read-only") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): raise Exception("Column is read-only") cdef class OBIDMS_column_float_multi_elts_writable(OBIDMS_column_float_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obifloat_t value): + cpdef set_item(self, index_t line_nb, str element_name, obifloat_t value): if obi_column_set_obifloat_with_elt_name(self.pointer, line_nb, str2bytes(element_name), value) < 0: raise Exception("Problem setting a value in a column") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): cdef obifloat_t value for element_name in values : value = values[element_name] diff --git a/python/obitools3/obidms/_obidmscolumn_int.pxd b/python/obitools3/obidms/_obidmscolumn_int.pxd index d851c42..ebe9bcd 100644 --- a/python/obitools3/obidms/_obidmscolumn_int.pxd +++ b/python/obitools3/obidms/_obidmscolumn_int.pxd @@ -1,25 +1,25 @@ #cython: language_level=3 -from .capi.obitypes cimport obiint_t +from .capi.obitypes cimport obiint_t, index_t from ._obidms cimport OBIDMS_column cdef class OBIDMS_column_int(OBIDMS_column): - cpdef object get_line(self, size_t line_nb) - cpdef set_line(self, size_t line_nb, object value) + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_int_writable(OBIDMS_column_int): - cpdef set_line(self, size_t line_nb, object value) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_int_multi_elts(OBIDMS_column_int): - cpdef object get_item(self, size_t line_nb, str element_name) - cpdef object get_line(self, size_t line_nb) - cpdef set_item(self, size_t line_nb, str element_name, obiint_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef object get_item(self, index_t line_nb, str element_name) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, str element_name, obiint_t value) + cpdef set_line(self, index_t line_nb, object values) cdef class OBIDMS_column_int_multi_elts_writable(OBIDMS_column_int_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obiint_t value) - cpdef set_line(self, size_t line_nb, object values) + cpdef set_item(self, index_t line_nb, str element_name, obiint_t value) + cpdef set_line(self, index_t line_nb, object values) cpdef close(self) diff --git a/python/obitools3/obidms/_obidmscolumn_int.pyx b/python/obitools3/obidms/_obidmscolumn_int.pyx index 5f50f82..ff8d013 100644 --- a/python/obitools3/obidms/_obidmscolumn_int.pyx +++ b/python/obitools3/obidms/_obidmscolumn_int.pyx @@ -16,7 +16,7 @@ from cpython.int cimport PyInt_FromLong cdef class OBIDMS_column_int(OBIDMS_column): - cpdef object get_line(self, size_t line_nb): + cpdef object get_line(self, index_t line_nb): cdef obiint_t value cdef object result value = obi_column_get_obiint_with_elt_idx(self.pointer, line_nb, 0) @@ -28,7 +28,7 @@ cdef class OBIDMS_column_int(OBIDMS_column): result = PyInt_FromLong(value) return result - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): raise Exception("Column is read-only") cpdef close(self): @@ -38,7 +38,7 @@ cdef class OBIDMS_column_int(OBIDMS_column): cdef class OBIDMS_column_int_writable(OBIDMS_column_int): - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): if obi_column_set_obiint_with_elt_idx(self.pointer, line_nb, 0, value) < 0: raise Exception("Problem setting a value in a column") @@ -49,7 +49,7 @@ cdef class OBIDMS_column_int_writable(OBIDMS_column_int): cdef class OBIDMS_column_int_multi_elts(OBIDMS_column_int): - cpdef object get_item(self, size_t line_nb, str element_name): + cpdef object get_item(self, index_t line_nb, str element_name): cdef obiint_t value cdef object result value = obi_column_get_obiint_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) @@ -61,10 +61,10 @@ cdef class OBIDMS_column_int_multi_elts(OBIDMS_column_int): result = PyInt_FromLong(value) return result - cpdef object get_line(self, size_t line_nb) : + cpdef object get_line(self, index_t line_nb) : cdef obiint_t value cdef object result - cdef size_t i + cdef index_t i cdef bint all_NA result = {} all_NA = True @@ -79,20 +79,20 @@ cdef class OBIDMS_column_int_multi_elts(OBIDMS_column_int): result = None return result - cpdef set_item(self, size_t line_nb, str element_name, obiint_t value): + cpdef set_item(self, index_t line_nb, str element_name, obiint_t value): raise Exception("Column is read-only") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): raise Exception("Column is read-only") cdef class OBIDMS_column_int_multi_elts_writable(OBIDMS_column_int_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, obiint_t value): + cpdef set_item(self, index_t line_nb, str element_name, obiint_t value): if obi_column_set_obiint_with_elt_name(self.pointer, line_nb, str2bytes(element_name), value) < 0: raise Exception("Problem setting a value in a column") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): cdef obiint_t value for element_name in values : value = values[element_name] diff --git a/python/obitools3/obidms/_obidmscolumn_str.pxd b/python/obitools3/obidms/_obidmscolumn_str.pxd index b3c2488..62fc0e3 100644 --- a/python/obitools3/obidms/_obidmscolumn_str.pxd +++ b/python/obitools3/obidms/_obidmscolumn_str.pxd @@ -1,24 +1,25 @@ #cython: language_level=3 +from .capi.obitypes cimport index_t from ._obidms cimport OBIDMS_column cdef class OBIDMS_column_str(OBIDMS_column): - cpdef object get_line(self, size_t line_nb) - cpdef set_line(self, size_t line_nb, object value) + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_str_writable(OBIDMS_column_str): - cpdef set_line(self, size_t line_nb, object value) + cpdef set_line(self, index_t line_nb, object value) cpdef close(self) cdef class OBIDMS_column_str_multi_elts(OBIDMS_column_str): - cpdef object get_item(self, size_t line_nb, str element_name) - cpdef object get_line(self, size_t line_nb) - cpdef set_item(self, size_t line_nb, str element_name, str value) - cpdef set_line(self, size_t line_nb, object values) + cpdef object get_item(self, index_t line_nb, str element_name) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, str element_name, str value) + cpdef set_line(self, index_t line_nb, object values) cdef class OBIDMS_column_str_multi_elts_writable(OBIDMS_column_str_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, str value) - cpdef set_line(self, size_t line_nb, object values) + cpdef set_item(self, index_t line_nb, str element_name, str value) + cpdef set_line(self, index_t line_nb, object values) cpdef close(self) diff --git a/python/obitools3/obidms/_obidmscolumn_str.pyx b/python/obitools3/obidms/_obidmscolumn_str.pyx index 1a114f2..433b5d5 100644 --- a/python/obitools3/obidms/_obidmscolumn_str.pyx +++ b/python/obitools3/obidms/_obidmscolumn_str.pyx @@ -9,42 +9,24 @@ from .capi.obidmscolumn cimport obi_close_column,\ from .capi.obierrno cimport obi_errno from .capi.obitypes cimport OBIIdx_NA, const_char_p - -from libc.string cimport strlen - from obitools3.utils cimport str2bytes, bytes2str -from cpython.int cimport PyInt_FromSsize_t - cdef class OBIDMS_column_str(OBIDMS_column): - cpdef object get_line(self, size_t line_nb): - cdef const_char_p cvalue + cpdef object get_line(self, index_t line_nb): cdef bytes value cdef object result - - cvalue = obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, 0) - - print('test 1') - - print(hex( cvalue)) - - - print(strlen(cvalue)) - print("test") - value = cvalue - print(value) + value = obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, 0) if obi_errno > 0 : raise IndexError(line_nb) if value == OBIIdx_NA : result = None else : - print(value) result = bytes2str(value) return result - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): raise Exception("Column is read-only") cpdef close(self): @@ -54,7 +36,7 @@ cdef class OBIDMS_column_str(OBIDMS_column): cdef class OBIDMS_column_str_writable(OBIDMS_column_str): - cpdef set_line(self, size_t line_nb, object value): + cpdef set_line(self, index_t line_nb, object value): if obi_column_set_obistr_with_elt_idx(self.pointer, line_nb, 0, str2bytes(value)) < 0: raise Exception("Problem setting a value in a column") @@ -65,10 +47,10 @@ cdef class OBIDMS_column_str_writable(OBIDMS_column_str): cdef class OBIDMS_column_str_multi_elts(OBIDMS_column_str): - cpdef object get_item(self, size_t line_nb, str element_name): + cpdef object get_item(self, index_t line_nb, str element_name): cdef bytes value cdef object result - value = obi_column_get_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) + value = obi_column_get_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name)) if obi_errno > 0 : raise IndexError(line_nb, element_name) if value == OBIIdx_NA : @@ -77,15 +59,15 @@ cdef class OBIDMS_column_str_multi_elts(OBIDMS_column_str): result = bytes2str(value) return result - cpdef object get_line(self, size_t line_nb) : + cpdef object get_line(self, index_t line_nb) : cdef bytes value cdef object result - cdef size_t i + cdef index_t i cdef bint all_NA result = {} all_NA = True for i in range(self.nb_elements_per_line) : - value = obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, i) + value = obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, i) if obi_errno > 0 : raise IndexError(line_nb) result[self.elements_names[i]] = bytes2str(value) @@ -95,20 +77,20 @@ cdef class OBIDMS_column_str_multi_elts(OBIDMS_column_str): result = None return result - cpdef set_item(self, size_t line_nb, str element_name, str value): + cpdef set_item(self, index_t line_nb, str element_name, str value): raise Exception("Column is read-only") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): raise Exception("Column is read-only") cdef class OBIDMS_column_str_multi_elts_writable(OBIDMS_column_str_multi_elts): - cpdef set_item(self, size_t line_nb, str element_name, str value): + cpdef set_item(self, index_t line_nb, str element_name, str value): if obi_column_set_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name), str2bytes(value)) < 0: raise Exception("Problem setting a value in a column") - cpdef set_line(self, size_t line_nb, object values): + cpdef set_line(self, index_t line_nb, object values): cdef str value for element_name in values : value = values[element_name] diff --git a/python/obitools3/obidms/capi/obidmscolumn.pxd b/python/obitools3/obidms/capi/obidmscolumn.pxd index b153b28..71bbab0 100644 --- a/python/obitools3/obidms/capi/obidmscolumn.pxd +++ b/python/obitools3/obidms/capi/obidmscolumn.pxd @@ -8,6 +8,7 @@ from ..capi.obitypes cimport const_char_p, \ obibool_t, \ obichar_t, \ obifloat_t, \ + index_t, \ time_t @@ -16,9 +17,9 @@ cdef extern from "obidmscolumn.h" nogil: struct OBIDMS_column_header_t: bint little_endian int header_size - size_t line_count - size_t lines_used - size_t nb_elements_per_line + index_t line_count + index_t lines_used + index_t nb_elements_per_line const_char_p elements_names OBIType_t data_type time_t creation_date @@ -38,8 +39,8 @@ cdef extern from "obidmscolumn.h" nogil: OBIDMS_column_p obi_create_column(OBIDMS_p dms, const_char_p column_name, OBIType_t type, - size_t nb_lines, - size_t nb_elements_per_line, + index_t nb_lines, + index_t nb_elements_per_line, const_char_p elements_names, const_char_p array_name) @@ -69,101 +70,101 @@ cdef extern from "obidmscolumn.h" nogil: cdef extern from "obidmscolumn_int.h" nogil: int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name, obiint_t value) int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx, + index_t line_nb, + index_t element_idx, obiint_t value) obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name) obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx) + index_t line_nb, + index_t element_idx) cdef extern from "obidmscolumn_bool.h" nogil: int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name, obibool_t value) int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx, + index_t line_nb, + index_t element_idx, obibool_t value) obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name) obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx) + index_t line_nb, + index_t element_idx) cdef extern from "obidmscolumn_char.h" nogil: int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name, obichar_t value) int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx, + index_t line_nb, + index_t element_idx, obichar_t value) obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name) obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx) + index_t line_nb, + index_t element_idx) cdef extern from "obidmscolumn_float.h" nogil: int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name, obifloat_t value) int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx, + index_t line_nb, + index_t element_idx, obifloat_t value) obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name) obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx) + index_t line_nb, + index_t element_idx) cdef extern from "obidmscolumn_str.h" nogil: int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name, char* value) int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx, + index_t line_nb, + index_t element_idx, char* value) const_char_p obi_column_get_obistr_with_elt_name(OBIDMS_column_p column, - size_t line_nb, + index_t line_nb, const_char_p element_name) const_char_p obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, - size_t line_nb, - size_t element_idx) + index_t line_nb, + index_t element_idx) diff --git a/python/obitools3/obidms/capi/obitypes.pxd b/python/obitools3/obidms/capi/obitypes.pxd index 4150eca..a485eb4 100644 --- a/python/obitools3/obidms/capi/obitypes.pxd +++ b/python/obitools3/obidms/capi/obitypes.pxd @@ -1,7 +1,7 @@ #cython: language_level=3 -from libc.stdint cimport int32_t +from libc.stdint cimport int32_t, int64_t from posix.types cimport time_t @@ -33,10 +33,10 @@ cdef extern from "obitypes.h" nogil: ctypedef int32_t obiint_t ctypedef double obifloat_t ctypedef char obichar_t - ctypedef size_t obiidx_t + ctypedef int64_t index_t extern obiint_t OBIInt_NA - extern obiidx_t OBIIdx_NA + extern index_t OBIIdx_NA extern obifloat_t OBIFloat_NA extern obichar_t OBIChar_NA extern obibool_t OBIBool_NA diff --git a/python/obitools3/unit_tests.py b/python/obitools3/unit_tests.py index 6829683..cf93dd2 100644 --- a/python/obitools3/unit_tests.py +++ b/python/obitools3/unit_tests.py @@ -2,16 +2,17 @@ import os import sys import shutil import unittest -from random import randint, uniform +from random import randint, uniform, choice +import string from obitools3.obidms._obidms import OBIDMS -LINE_COUNT_FOR_TEST_COLUMN = 100000 # TODO randomize? -SMALLER_LINE_COUNT_FOR_TEST_COLUMN = 10000 # TODO randomize? -NB_ELEMENTS_PER_LINE = 50 # TODO randomize? +LINE_COUNT_FOR_TEST_COLUMN = 10000 # TODO randomize? +SMALLER_LINE_COUNT_FOR_TEST_COLUMN = 1000 # TODO randomize? +NB_ELEMENTS_PER_LINE = 20 # TODO randomize? DMS_NAME = "unit_test_dms" -DATA_TYPES = ['OBI_INT', 'OBI_FLOAT', 'OBI_BOOL', 'OBI_CHAR'] +DATA_TYPES = ['OBI_INT', 'OBI_FLOAT', 'OBI_BOOL', 'OBI_CHAR', 'OBI_IDX'] def create_test_obidms(): @@ -26,7 +27,7 @@ def create_test_column(dms, data_type_code, multiple_elements_per_line=False): data_type_code = data_type_code data_type_str = data_types[data_type_code-1] col_name = "unit_test_"+data_type_str - + if multiple_elements_per_line : elts_names = elements_names() col = dms.open_column(col_name, @@ -41,7 +42,7 @@ def create_test_column(dms, data_type_code, multiple_elements_per_line=False): create=True, data_type=data_type_code) return (col, col_name, data_type_str) - + def elements_names(): names = [str(i) for i in range(NB_ELEMENTS_PER_LINE)] @@ -60,8 +61,9 @@ def random_obivalue(data_type): nucs = 'atgc' return bytes(nucs[randint(0,3)], 'utf-8') elif data_type == "OBI_IDX" : - return randint(0,r) - + length = randint(1,500) + randoms = ''.join(choice(string.ascii_lowercase) for i in range(length)) + return randoms class OBIDMS_Column_TestCase(unittest.TestCase): def tearDown(self): @@ -228,14 +230,42 @@ class OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase(OBIDMS_Column_multiple_e multiple_elements_per_line=True) +class OBIDMS_Column_OBI_STR_TestCase(OBIDMS_Column_TestCase): + def setUp(self): + self.data_type_code = 5 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code) + + +class OBIDMS_Column_OBI_STR_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase): + def setUp(self): + self.data_type_code = 5 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.elts_names, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code, + multiple_elements_per_line=True) + + if __name__ == '__main__': - unittest.main(verbosity=2, defaultTest=["OBIDMS_Column_OBI_INT_TestCase", - "OBIDMS_Column_OBI_INT_multiple_elements_TestCase", - "OBIDMS_Column_OBI_FLOAT_TestCase", - "OBIDMS_Column_OBI_FLOAT_multiple_elements_TestCase", - "OBIDMS_Column_OBI_BOOL_TestCase", - "OBIDMS_Column_OBI_BOOL_multiple_elements_TestCase", - "OBIDMS_Column_OBI_CHAR_TestCase", - "OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase"]) + unittest.main(verbosity=2, defaultTest=["OBIDMS_Column_OBI_INT_TestCase", + "OBIDMS_Column_OBI_INT_multiple_elements_TestCase", + "OBIDMS_Column_OBI_FLOAT_TestCase", + "OBIDMS_Column_OBI_FLOAT_multiple_elements_TestCase", + "OBIDMS_Column_OBI_BOOL_TestCase", + "OBIDMS_Column_OBI_BOOL_multiple_elements_TestCase", + "OBIDMS_Column_OBI_CHAR_TestCase", + "OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase", + "OBIDMS_Column_OBI_STR_TestCase", + "OBIDMS_Column_OBI_STR_multiple_elements_TestCase"]) diff --git a/python/obitools3/version.py b/python/obitools3/version.py index fa724f7..d2e77c8 100644 --- a/python/obitools3/version.py +++ b/python/obitools3/version.py @@ -2,4 +2,4 @@ major = 1 minor = 1 serial= '16' -version ="%2d.%02d.%s" % (major,minor,serial) +version ="%d.%02d.%s" % (major,minor,serial) diff --git a/src/obiarray.c b/src/obiarray.c index f5d5ce4..7828762 100644 --- a/src/obiarray.c +++ b/src/obiarray.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +21,9 @@ #include "obiarray.h" #include "obierrno.h" +#include "obitypes.h" #include "obidebug.h" +#include "private_at_functions.h" #define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?) @@ -37,7 +40,7 @@ * * @warning The returned pointer has to be freed by the caller. * - * @param name The name of the array. + * @param array_name The name of the array. * * @returns A pointer to the array file name. * @retval NULL if an error occurred. @@ -53,7 +56,7 @@ static char* build_array_file_name(const char* array_name); * * @warning The returned pointer has to be freed by the caller. * - * @param name The name of the array. + * @param array_name The name of the array. * * @returns A pointer to the array data file name. * @retval NULL if an error occurred. @@ -65,62 +68,121 @@ static char* build_array_data_file_name(const char* array_name); /** - * PRIVATE - */ -static char* build_array_file_path(OBIDMS_p dms, const char* array_name); - - -/** - * PRIVATE + * @brief Internal function returning the size of an array header on this platform. + * + * @returns The size of an array header in bytes. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ size_t get_array_header_size(); /** - * PRIVATE + * @brief Internal function returning the initial size of an array on this platform. + * + * @returns The initial size of an array in bytes. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ size_t get_initial_array_size(); /** - * PRIVATE + * @brief Internal function returning the size of a data array header on this platform. + * + * @returns The size of a data array header in bytes. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ size_t get_array_data_header_size(); /** - * PRIVATE + * @brief Internal function returning the initial size of a data array on this platform. + * + * @returns The initial size of a data array in bytes. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ size_t get_initial_array_data_size(); /** - * PRIVATE + * @brief Internal function closing a data array structure. + * + * @param array_data A pointer to the data array. + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int close_array_data(OBIDMS_array_data_p array_data); /** - * PRIVATE + * @brief Internal function enlarging an array. + * + * @param array A pointer to the array structure. + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int grow_array(OBIDMS_array_p array); /** - * PRIVATE + * @brief Internal function enlarging a data array. + * + * @param array A pointer to the array structure. + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int grow_array_data(OBIDMS_array_p array); /** - * PRIVATE + * @brief Internal function comparing two byte arrays. + * + * The encoding is compared first, then the length of the + * values, then the values themselves. + * + * @param value_1 A pointer to the first byte array. + * @param value_2 A pointer to the second byte array. + * + * @returns A value < 0 if value_1 < value_2, + * a value > 0 if value_1 > value_2, + * and 0 if value_1 == value_2. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int array_compare(byte_t* value_1, byte_t* value_2); + /** - * PRIVATE + * @brief Internal function calculating the size in bytes of a byte array. + * + * @param value A pointer to the byte array. + * + * @returns The size of the byte array in bytes. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int32_t array_sizeof(byte_t* value); // TODO not sure about type +size_t array_sizeof(byte_t* value); // TODO not sure about type /************************************************************************ @@ -170,33 +232,6 @@ static char* build_array_data_file_name(const char* array_name) } -static char* build_array_file_path(OBIDMS_p dms, const char* array_name) // TODO store DIR in dms structure -{ - char* array_file_path; - char* array_file_name; - - // Build the array file name - array_file_name = build_array_file_name(array_name); - if (array_file_name == NULL) - return NULL; - - // TODO use get_full_path(int directory_file_descriptor, const char* path_name) - array_file_path = (char*) malloc((strlen(dms->directory_name) + strlen("/arrays/") + strlen(array_file_name) + 1) * sizeof(char)); - if (array_file_path == NULL) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError building an array file path"); - return NULL; - } - - strcpy(array_file_path, dms->directory_name); - strcat(array_file_path, "/arrays/"); - strcat(array_file_path, array_file_name); - - return array_file_path; -} - - size_t get_array_header_size() { return getpagesize() * 1; @@ -272,7 +307,7 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed return -1; } - // Open the array data file + // Open the array file array_file_descriptor = openat(array_dir_file_descriptor, array_file_name, O_RDWR); if (array_file_descriptor < 0) { @@ -285,7 +320,7 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed free(array_file_name); // Calculate the new file size - old_data_size = ((array->header)->nb_items_max) * sizeof(index_t); + old_data_size = (array->header)->array_size; new_data_size = old_data_size * ARRAY_GROWTH_FACTOR; header_size = (array->header)->header_size; file_size = header_size + new_data_size; @@ -331,8 +366,11 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed // Set new maximum number of items (array->header)->nb_items_max = ceil(((double) new_data_size) / ((double) sizeof(index_t))); + // Set the new array size + (array->header)->array_size = new_data_size; + // Initialize new data to 0 - memset((array->first)+old_data_size, 0, new_data_size - old_data_size); + memset(((uint8_t*)(array->first))+old_data_size, 0, (new_data_size-old_data_size)); close(array_file_descriptor); @@ -343,8 +381,8 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed { size_t file_size; - size_t old_data_size; - size_t new_data_size; + index_t old_data_size; + index_t new_data_size; size_t header_size; int array_dir_file_descriptor; int array_data_file_descriptor; @@ -444,7 +482,7 @@ int array_compare(byte_t* value_1, byte_t* value_2) int32_t len_2; int32_t b; - obidebug(1, "\nCOMPARING 1=%d,%.*s; 2=%d,%.*s", (int32_t) *(value_1+1), (int32_t) *(value_1+1), value_1+5, (int32_t) *(value_2+1), (int32_t) *(value_2+1), value_2+5); + //obidebug(1, "\nCOMPARING 1=%d,%.*s; 2=%d,%.*s", *((int32_t*)(value_1+1)), *((int32_t*)(value_1+1)), value_1+BYTE_ARRAY_HEADER_SIZE, *((int32_t*)(value_2+1)), *((int32_t*)(value_2+1)), value_2+BYTE_ARRAY_HEADER_SIZE); size_1 = (uint8_t) *(value_1); size_2 = (uint8_t) *(value_2); @@ -452,31 +490,26 @@ int array_compare(byte_t* value_1, byte_t* value_2) if (size_1 != size_2) return (size_1 - size_2); - len_1 = (int32_t) *(value_1+1); - len_2 = (int32_t) *(value_2+1); - - obidebug(1, "\ncomp length = %d - %d = %d", len_1, len_2, (len_1 - len_2)); + len_1 = *((int32_t*)(value_1+1)); + len_2 = *((int32_t*)(value_2+1)); if (len_1 != len_2) return (len_1 - len_2); - b = 0; + b = BYTE_ARRAY_HEADER_SIZE; comp = 0; - while (!comp && (b < len_1)) + while (!comp && (b < len_1+BYTE_ARRAY_HEADER_SIZE)) { comp = *(value_1+b) - *(value_2+b); b++; } - - obidebug(1, "\ncomp = %d", comp); - return comp; } -int32_t array_sizeof(byte_t* value) +size_t array_sizeof(byte_t* value) { - return (6 + (int32_t) *(value+1)); + return (BYTE_ARRAY_HEADER_SIZE + *((int32_t*)(value+1)) + 1); } @@ -490,14 +523,39 @@ int obi_array_exists(OBIDMS_p dms, const char* array_name) { struct stat buffer; char* array_file_path; + char* array_file_name; int check_dir; + int array_dir_file_descriptor; + + // Get the file descriptor of the array directory + array_dir_file_descriptor = dirfd(dms->array_directory); + if (array_dir_file_descriptor < 0) + { + obi_set_errno(OBI_ARRAY_ERROR); + obidebug(1, "\nError getting the file descriptor for an array directory"); + return -1; + } + + // Build file name + array_file_name = build_array_file_name(array_name); + if (array_file_name == NULL) + { + close(array_dir_file_descriptor); + return -1; + } // Build the array file path - array_file_path = build_array_file_path(dms, array_name); + array_file_path = get_full_path(array_dir_file_descriptor, array_file_name); + if (array_file_path == NULL) + { + obidebug(1, "\nError getting the file path for an array file"); + return -1; + } check_dir = stat(array_file_path, &buffer); free(array_file_path); + free(array_file_name); if (check_dir == 0) return 1; @@ -549,8 +607,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) return NULL; } - obidebug(1, "\nboom"); - // Build file name array_data_file_name = build_array_data_file_name(array_name); if (array_data_file_name == NULL) @@ -559,8 +615,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) return NULL; } - obidebug(1, "\nboom"); - // Create file array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR | O_CREAT | O_EXCL, 0777); if (array_data_file_descriptor < 0) @@ -719,7 +773,7 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) return NULL; } - array->first = mmap(NULL, + array->first = mmap(NULL, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, @@ -742,6 +796,7 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) array->directory = dms->array_directory; (array->header)->header_size = header_size; + (array->header)->array_size = data_size; (array->header)->nb_items = 0; (array->header)->nb_items_max = (index_t) ceil(((double) get_initial_array_size()) / ((double) sizeof(index_t))); (array->header)->creation_date = time(NULL); @@ -751,7 +806,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) memset(array->first, 0, data_size); close(array_file_descriptor); - //close(array_dir_file_descriptor); TODO return array; } @@ -929,7 +983,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) array->directory = dms->array_directory; close(array_file_descriptor); - //close(array_dir_file_descriptor); TODO return array; } @@ -961,29 +1014,17 @@ int obi_close_array(OBIDMS_array_p array) } -//int obi_delete_array(const char* array_name) -//{ -// -//} - - index_t obi_array_add(OBIDMS_array_p array, byte_t* value) { - index_t idx; - index_t nb_items; - index_t nb_items_max; - index_t* a; - byte_t* data; - int64_t data_size_used; - int64_t* data_size_max_p; - int32_t value_size; - - obidebug(1, "\n"); + index_t idx; + index_t nb_items; + index_t nb_items_max; + index_t data_size_used; + index_t* data_size_max_p; + size_t value_size; nb_items = (array->header)->nb_items; nb_items_max = (array->header)->nb_items_max; - a = array->first; - data = (array->data)->data; data_size_used = ((array->data)->header)->data_size_used; data_size_max_p = &(((array->data)->header)->data_size_max); @@ -999,8 +1040,6 @@ index_t obi_array_add(OBIDMS_array_p array, byte_t* value) else // First item idx = 0; - obidebug(1, "\n"); - // Grow the array if needed if (nb_items == nb_items_max) { @@ -1008,37 +1047,22 @@ index_t obi_array_add(OBIDMS_array_p array, byte_t* value) return -1; } - obidebug(1, "\n"); - // Grow the data if needed value_size = array_sizeof(value); - while (*data_size_max_p < (data_size_used + value_size)) + while (*data_size_max_p < (data_size_used + value_size)) // TODO problem with types { if (grow_array_data(array) < 0) return -1; } - obidebug(1, "\nidx = %d", idx); + // Store the index of the value in the array - // Add the index of the value in the array - memmove((a+idx+1), (a+idx), ((nb_items - idx) * sizeof(index_t))); - a[idx] = data_size_used; - - int i; - for (i=0; i 0) + memmove(((array->first)+idx+1), ((array->first)+idx), ((nb_items - idx) * sizeof(index_t))); + (array->first)[idx] = data_size_used; // Store the value itself at the end of the data - memcpy((data+data_size_used), value, value_size); - - //fprintf(stderr, "\nAFTER COPY DATA+8=%s\n", data+8); - - //fprintf(stderr, "\nAFTER COPY DATA+5=%.4s, DATA+14=%.5s\n", data+5, data+14); + memcpy((((array->data)->data)+data_size_used), value, value_size); // Update the data size ((array->data)->header)->data_size_used = data_size_used + value_size; @@ -1047,18 +1071,18 @@ index_t obi_array_add(OBIDMS_array_p array, byte_t* value) ((array->header)->nb_items)++; (((array->data)->header)->nb_items)++; - //obidebug(1, "\nSHOULD ++ : %d", (array->header)->nb_items); - - for (i=0; i<(array->header)->nb_items; i++) - fprintf(stderr, "\narray[%d] = %d\n", i, a[i]); - - fprintf(stderr, "\nreturning %lld\n", data_size_used); + // PRINT ARRAY +// fprintf(stderr, "\nARRAY:"); +// int i; +// for (i=0; i<=nb_items; i++) +// fprintf(stderr, "\narray[%d] = %d -> %s", i, a[i], data+a[i]+BYTE_ARRAY_HEADER_SIZE); + // Return the data index of the value return data_size_used; } -byte_t* obi_array_get(OBIDMS_array_p array, index_t idx) // TODO copy? +byte_t* obi_array_get(OBIDMS_array_p array, index_t idx) { return (((array->data)->data)+idx); } @@ -1080,48 +1104,37 @@ index_t obi_array_search(OBIDMS_array_p array, byte_t* value) data = (array->data)->data; a = array->first; - obidebug(1, "\nnb items = %d", nb_items); - if (nb_items == 0) - return 0; + return -1; - idx_max = nb_items; + idx_max = nb_items-1; idx_min = 0; - obidebug(1, "\n"); - - while (idx_min < idx_max) + while (idx_min <= idx_max) { - idx_mid = nb_items/2; - - obidebug(1, "\ncomparing with idx = %d, data idx %d", idx_mid, a[idx_mid]); - //fprintf(stderr, "\ndata+14=%.5s\n", data+14); - + idx_mid = (idx_min+idx_max)/2; to_compare = obi_array_get(array, a[idx_mid]); - - //fprintf(stderr, "\nto_compare=%.5s\n", to_compare+5); - comp = array_compare(to_compare, value); - - obidebug(1, "\nCOMP = %d", comp); - if (!comp) + { + // Found return a[idx_mid]; + } else if (comp < 0) { - idx = idx_mid + 1; // Search in upper half + idx = idx_mid + 1; idx_min = idx_mid + 1; } else - idx = idx_mid; + { // Search in lower half + idx = idx_mid; idx_max = idx_mid - 1; + } } - obidebug(1, "\nNOT FOUND, idx=%d", idx); - - // Not found: return where the value should be + // Not found: return where the value should be in the array return -(idx+1); } @@ -1134,7 +1147,7 @@ byte_t* obi_str_to_obibytes(char* value) size = 8; length = strlen(value); - value_b = (byte_t*) malloc(length + 6); + value_b = (byte_t*) malloc(length + BYTE_ARRAY_HEADER_SIZE + 1); if (value_b == NULL) { obi_set_errno(OBI_ARRAY_ERROR); @@ -1143,11 +1156,10 @@ byte_t* obi_str_to_obibytes(char* value) } *(value_b) = size; - *(value_b+1) = length; - strcpy(value_b+5, value); + *((int32_t*)(value_b+1)) = length; - // TODO 5 as macro + strcpy(value_b+BYTE_ARRAY_HEADER_SIZE, value); return value_b; } @@ -1157,17 +1169,8 @@ const char* obi_obibytes_to_str(byte_t* value_b) { const char* value; - value = value_b+5; - - obidebug(1, "\nvalue = %s", value); - - // TODO 5 as macro + value = value_b+BYTE_ARRAY_HEADER_SIZE; return value; } - - - - - diff --git a/src/obiarray.h b/src/obiarray.h index 4b8154f..26c2226 100644 --- a/src/obiarray.h +++ b/src/obiarray.h @@ -3,15 +3,15 @@ ****************************************************************************/ /** - * @file array.h + * @file obiarray.h * @author Celine Mercier * @date October 19th 2015 - * @brief Header file for handling arrays for storing and retrieving data arrays (i.e. character strings). + * @brief Header file for handling arrays for storing and retrieving byte arrays (i.e. coding for character strings). */ -#ifndef ARRAY_H_ -#define ARRAY_H_ +#ifndef OBIARRAY_H_ +#define OBIARRAY_H_ #include @@ -29,9 +29,10 @@ */ #define ARRAY_GROWTH_FACTOR (2) /**< The growth factor when an array is enlarged. */ +#define BYTE_ARRAY_HEADER_SIZE (5) /**< The size of the header of a byte array. + */ + -typedef int32_t index_t; /**< Type of the array indices. - */ typedef char byte_t; /**< Defining byte type since data is stored in bits * and char (stored on one byte) is the smallest addressable unit. */ @@ -43,9 +44,9 @@ typedef char byte_t; /**< Defining byte type since data is stored in bits typedef struct OBIDMS_array_data_header { int header_size; /**< Size of the header in bytes. */ - int64_t data_size_used; /**< Size of the data used in bytes. // TODO not sure about type + index_t data_size_used; /**< Size of the data used in bytes. */ - int64_t data_size_max; /**< Max size of the data in bytes. // TODO not sure about type + index_t data_size_max; /**< Max size of the data in bytes. */ index_t nb_items; /**< Number of items. */ @@ -73,6 +74,8 @@ typedef struct OBIDMS_array_data { typedef struct OBIDMS_array_header { int header_size; /**< Size of the header in bytes. */ + size_t array_size; /**< Size of the array in bytes. + */ index_t nb_items; /**< Number of items in the array. */ index_t nb_items_max; /**< Maximum number of items in the array before it has to be enlarged. @@ -102,76 +105,181 @@ typedef struct OBIDMS_array { /** + * @brief Checks if an obiarray already exists or not. * + * @param dms The OBIDMS to which the obiarray belongs. + * @param array_name The name of the obiarray. + * + * @returns A value indicating whether the obiarray exists or not. + * @retval 1 if the obiarray exists. + * @retval 0 if the obiarray does not exist. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int obi_array_exists(OBIDMS_p dms, const char* array_name); /** + * @brief Opens an obiarray and creates it if it does not already exist. * + * Note: An obiarray is made of two files (referred to by two structures). + * One file contains the indices referring to the data, and the other + * file contains the data itself. The obiarray as a whole is referred + * to via the OBIDMS_array structure. + * + * @param dms The OBIDMS to which the obiarray belongs. + * @param array_name The name of the obiarray. + * + * @returns A pointer to the obiarray structure. + * @retval NULL if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ OBIDMS_array_p obi_array(OBIDMS_p dms, const char* array_name); /** + * @brief Creates an obiarray. Fails if it already exists. * + * Note: An obiarray is made of two files (referred to by two structures). + * One file contains the indices referring to the data, and the other + * file contains the data itself. The obiarray as a whole is referred + * to via the OBIDMS_array structure. + * + * @param dms The OBIDMS to which the obiarray belongs. + * @param array_name The name of the obiarray. + * + * @returns A pointer to the newly created obiarray structure. + * @retval NULL if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name); /** + * @brief Opens an obiarray. Fails if it does not already exist. * + * Note: An obiarray is made of two files (referred to by two structures). + * One file contains the indices referring to the data, and the other + * file contains the data itself. The obiarray as a whole is referred + * to via the OBIDMS_array structure. + * + * @param dms The OBIDMS to which the obiarray belongs. + * @param array_name The name of the obiarray. + * + * @returns A pointer to the obiarray structure. + * @retval NULL if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name); /** + * @brief Closes an obiarray. * + * Note: An obiarray is made of two files (referred to by two structures). + * One file contains the indices referring to the data, and the other + * file contains the data itself. The obiarray as a whole is referred + * to via the OBIDMS_array structure. + * + * @param array A pointer to the obiarray structure to close and free. + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ int obi_close_array(OBIDMS_array_p array); /** + * @brief Adds a value (byte array) in an obiarray, checking first if it is already in it. * - */ -//int obi_delete_array(OBIDMS_p dms, const char* array_name); - - -/** - * The header is already in the byte array + * @warning The byte array to add must already be encoded and contain its header. + * + * @param array A pointer to the obiarray. + * @param value The byte array to add in the obiarray. + * + * @returns The index of the value, whether it was added or already in the obiarray. + * @retval -1 if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ index_t obi_array_add(OBIDMS_array_p array, byte_t* value); /** - * Returns the entire array including the header + * @brief Recovers a value (byte array) in an obiarray. + * + * @warning The byte array recovered is encoded and contains its header. + * + * @param array A pointer to the obiarray. + * @param index The index of the value in the data array. + * + * @returns A pointer to the byte array recovered. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ byte_t* obi_array_get(OBIDMS_array_p array, index_t index); /** + * @brief Searches a value (byte array) in an obiarray performing a binary search. * + * @warning The byte array to search must already be encoded and contain its header. + * + * @param array A pointer to the obiarray. + * @param value The byte array to add in the obiarray. + * + * @returns If the value is found, its data index is returned. + * If the value is not found, the array index indicating where the value's data index + * should be in the array is returned in the form (- (index + 1)), as data indices in an + * obiarray are sorted according to the ascending order of the values (byte arrays) themselves. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ index_t obi_array_search(OBIDMS_array_p array, byte_t* value); /** + * @brief Converts a character string to a byte array with a header. * + * @warning The byte array must be freed by the caller. + * + * @param value The character string to convert. + * + * @returns A pointer to the byte array created. + * @retval NULL if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ byte_t* obi_str_to_obibytes(char* value); /** + * @brief Converts a byte array to a character string. * + * @param value_b The byte array to convert. + * + * @returns A pointer to the character string contained in the byte array. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) */ const char* obi_obibytes_to_str(byte_t* value_b); -/** - * TODO LATER - */ -// OBIDMS_array_p obi_column_arrayify(FILE, encoding); - - -#endif /* ARRAY_H_ */ +#endif /* OBIARRAY_H_ */ diff --git a/src/obidms.c b/src/obidms.c index c0a1ced..2004a7a 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -163,8 +163,6 @@ OBIDMS_p obi_create_dms(const char* dms_name) return NULL; } - // TODO close file descriptor? - free(directory_name); return obi_open_dms(dms_name); @@ -247,8 +245,6 @@ OBIDMS_p obi_open_dms(const char* dms_name) free(directory_name); - // TODO test if close file descriptor - return dms; } diff --git a/src/obidms.h b/src/obidms.h index cded60b..f26cf7f 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -72,6 +72,8 @@ int obi_dms_exists(const char* dms_name); * if a directory with this name does not already exist * before creating the new database. * + * A directory to store obiarrays is also created. + * * @param dms_name A pointer to a C string containing the name of the database. * The actual directory name used to store the DMS will be * `.obidms`. diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 889a3dc..1fe86e8 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -143,7 +143,7 @@ static int obi_column_set_elements_names(OBIDMS_column_p column, const char* ele * @since September 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -static size_t get_line_count_per_page(OBIType_t data_type, size_t nb_elements_per_line); +static index_t get_line_count_per_page(OBIType_t data_type, index_t nb_elements_per_line); /************************************************************************ @@ -463,7 +463,7 @@ int obi_column_set_elements_names(OBIDMS_column_p column, const char* elements_n return 0; } -size_t get_line_count_per_page(OBIType_t data_type, size_t nb_elements_per_line) +index_t get_line_count_per_page(OBIType_t data_type, index_t nb_elements_per_line) { return getpagesize() / (obi_sizeof(data_type) * nb_elements_per_line); } @@ -595,15 +595,15 @@ obiversion_t obi_column_get_latest_version_from_name(OBIDMS_p dms, const char* c size_t obi_get_platform_header_size() { - return getpagesize() * 1; + return getpagesize() * 2; } OBIDMS_column_p obi_create_column(OBIDMS_p dms, const char* column_name, OBIType_t data_type, - size_t nb_lines, - size_t nb_elements_per_line, + index_t nb_lines, + index_t nb_elements_per_line, const char* elements_names, const char* array_name) { @@ -618,7 +618,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, int column_dir_file_descriptor; size_t header_size; size_t data_size; - size_t minimum_line_count; + index_t minimum_line_count; new_column = NULL; @@ -664,7 +664,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, else if ((elements_names != NULL) && (nb_elements_per_line > 1)) { char* token; - size_t n = 0; + index_t n = 0; token = strdup(elements_names); token = strtok(token, ";"); while (token != NULL) @@ -989,8 +989,8 @@ OBIDMS_column_p obi_clone_column(OBIDMS_p dms, { OBIDMS_column_p column_to_clone; OBIDMS_column_p new_column; - size_t nb_lines; - size_t nb_elements_per_line; + index_t nb_lines; + index_t nb_elements_per_line; size_t data_size; OBIType_t data_type; @@ -1083,7 +1083,7 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece { size_t file_size; size_t data_size; - size_t new_line_count; + index_t new_line_count; double multiple; int column_dir_file_descriptor; int column_file_descriptor; @@ -1180,8 +1180,8 @@ int obi_enlarge_column(OBIDMS_column_p column) size_t old_data_size; size_t new_data_size; size_t header_size; - size_t old_line_count; - size_t new_line_count; + index_t old_line_count; + index_t new_line_count; int column_dir_file_descriptor; int column_file_descriptor; char* column_file_name; @@ -1316,10 +1316,10 @@ int obi_truncate_and_close_column(OBIDMS_column_p column) void obi_ini_to_NA_values(OBIDMS_column_p column, - size_t first_line_nb, - size_t nb_lines) + index_t first_line_nb, + index_t nb_lines) { - size_t i, start, end, nb_elements; + index_t i, start, end, nb_elements; nb_elements = nb_lines*((column->header)->nb_elements_per_line); start = first_line_nb*((column->header)->nb_elements_per_line); @@ -1355,14 +1355,14 @@ void obi_ini_to_NA_values(OBIDMS_column_p column, case OBI_IDX: for (i=start;idata)) + i) = OBIIdx_NA; + *(((index_t*) (column->data)) + i) = OBIIdx_NA; } break; } } -OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* column_name) // ADD VERSION ARGUMENT +OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* column_name) // TODO ADD VERSION ARGUMENT { OBIDMS_column_header_p header; OBIDMS_column_directory_p column_directory; @@ -1391,7 +1391,7 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* } // Calculate the header size - header_size = obi_get_platform_header_size(); + header_size = obi_get_platform_header_size(); // TODO read in header itself // Get the latest version number version_number = obi_get_latest_version_number(column_directory); @@ -1458,17 +1458,17 @@ int obi_unmap_header(OBIDMS_column_header_p header) // TODO to be rewritten in an optimized and safe way -size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name) +index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name) { char* elements_names; char* name; - size_t element_index; + index_t element_index; elements_names = strdup((column->header)->elements_names); if (elements_names == NULL) { obidebug(1, "\nError strdup-ing the elements names"); - return SIZE_MAX; //TODO + return OBIIdx_NA; } element_index = 0; @@ -1494,7 +1494,7 @@ size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char obidebug(1, "\nCan't find an element name"); free(elements_names); - return SIZE_MAX; //TODO + return OBIIdx_NA; } diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 8e7ea84..4b32bce 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -53,13 +53,13 @@ typedef struct OBIDMS_column_header { * - `false` on big endian platforms * @see obi_is_little_endian() */ - int header_size; /**< Size of the header in bytes. + size_t header_size; /**< Size of the header in bytes. */ - size_t line_count; /**< Number of lines of data allocated. + index_t line_count; /**< Number of lines of data allocated. */ - size_t lines_used; /**< Number of lines of data used. + index_t lines_used; /**< Number of lines of data used. */ - size_t nb_elements_per_line; /**< Number of elements per line (default: 1). + index_t nb_elements_per_line; /**< Number of elements per line (default: 1). */ char elements_names[ELEMENTS_NAMES_MAX+1]; /**< Names of the line elements with ';' as separator * (should be the column name if one element per line). @@ -149,7 +149,7 @@ obiversion_t obi_column_get_latest_version_from_name(OBIDMS_p dms, const char* c * The header size is defined as a multiple of the memory page size. * As of now the header size is defined as one time the page size. * - * @returns a `size_t` value corresponding to the header size in bytes. + * @returns The header size in bytes. * * @since May 2015 * @author Eric Coissac (eric.coissac@metabarcoding.org) @@ -161,6 +161,7 @@ size_t obi_get_platform_header_size(); * @brief Creates a column. * * The minimum data size allocated is one memory page, and the data is initialized to the NA value of the OBIType. + * If there is an array associated with the column, it is opened or created if it does not already exist. * * @warning If there is one element per line, elements_names should be equal to column_name. // TODO change this condition? * @@ -170,6 +171,7 @@ size_t obi_get_platform_header_size(); * @param nb_lines The number of lines to be stored. * @param nb_elements_per_line The number of elements per line. * @param elements_names The names of the elements with ';' as separator. + * @param array_name The name of the array if there is one associated with the column. * * @returns A pointer on the newly created column structure. * @retval NULL if an error occurred. @@ -180,8 +182,8 @@ size_t obi_get_platform_header_size(); OBIDMS_column_p obi_create_column(OBIDMS_p dms, const char* column_name, OBIType_t data_type, - size_t nb_lines, - size_t nb_elements_per_line, + index_t nb_lines, + index_t nb_elements_per_line, const char* elements_names, const char* array_name); @@ -287,7 +289,7 @@ int obi_truncate_and_close_column(OBIDMS_column_p column); * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -void obi_ini_to_NA_values(OBIDMS_column_p column, size_t first_line_nb, size_t nb_lines); // TO make private? +void obi_ini_to_NA_values(OBIDMS_column_p column, index_t first_line_nb, index_t nb_lines); // TO make private? /** @@ -333,7 +335,7 @@ int obi_unmap_header(OBIDMS_column_header_p header); * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name); +index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name); /** diff --git a/src/obidmscolumn_bool.c b/src/obidmscolumn_bool.c index 10f605a..4f1bca7 100644 --- a/src/obidmscolumn_bool.c +++ b/src/obidmscolumn_bool.c @@ -28,7 +28,7 @@ * **********************************************************************/ -int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obibool_t value) +int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obibool_t value) { // Check that the line number is not greater than the maximum allowed if (line_nb >= MAXIMUM_LINE_COUNT) @@ -57,7 +57,7 @@ int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, } -obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) +obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { if ((line_nb+1) > (column->header)->lines_used) { @@ -69,23 +69,23 @@ obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t lin } -int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obibool_t value) +int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obibool_t value) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return -1; obi_column_set_obibool_with_elt_idx(column, line_nb, element_idx, value); return 0; } -obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name) +obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return OBIBool_NA; return obi_column_get_obibool_with_elt_idx(column, line_nb, element_idx); } diff --git a/src/obidmscolumn_bool.h b/src/obidmscolumn_bool.h index 791579d..deeaffc 100644 --- a/src/obidmscolumn_bool.h +++ b/src/obidmscolumn_bool.h @@ -38,7 +38,7 @@ * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obibool_t value); +int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, index_t line_nb, size_t element_idx, obibool_t value); /** @@ -54,7 +54,7 @@ int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx); +obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, index_t line_nb, size_t element_idx); /** @@ -66,7 +66,6 @@ obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t lin * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be set. * @param element_name The name of the element that should be set in the line. - * If empty, it is checked that there is only one element per line. * @param value The value that should be set. * * @returns An integer value indicating the success of the operation. @@ -76,7 +75,7 @@ obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t lin * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obibool_t value); +int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obibool_t value); /** @@ -86,7 +85,6 @@ int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_name The name of the element that should be recovered in the line. - * If empty, it is checked that there is only one element per line. * * @returns The recovered value. * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. @@ -94,7 +92,7 @@ int obi_column_set_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name); +obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name); #endif /* OBIDMSCOLUMN_BOOL_H_ */ diff --git a/src/obidmscolumn_char.c b/src/obidmscolumn_char.c index 51f5922..b086682 100644 --- a/src/obidmscolumn_char.c +++ b/src/obidmscolumn_char.c @@ -28,7 +28,7 @@ * **********************************************************************/ -int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obichar_t value) +int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obichar_t value) { // Check that the line number is not greater than the maximum allowed if (line_nb >= MAXIMUM_LINE_COUNT) @@ -57,7 +57,7 @@ int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, } -obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) +obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { if ((line_nb+1) > (column->header)->lines_used) { @@ -69,23 +69,23 @@ obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t lin } -int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obichar_t value) +int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obichar_t value) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return -1; obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value); return 0; } -obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name) +obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return OBIChar_NA; return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx); } diff --git a/src/obidmscolumn_char.h b/src/obidmscolumn_char.h index 9688c24..ed2f7fc 100644 --- a/src/obidmscolumn_char.h +++ b/src/obidmscolumn_char.h @@ -38,7 +38,7 @@ * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obichar_t value); +int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obichar_t value); /** @@ -54,7 +54,7 @@ int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx); +obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx); /** @@ -66,7 +66,6 @@ obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t lin * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be set. * @param element_name The name of the element that should be set in the line. - * If empty, it is checked that there is only one element per line. * @param value The value that should be set. * * @returns An integer value indicating the success of the operation. @@ -76,7 +75,7 @@ obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t lin * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obichar_t value); +int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obichar_t value); /** @@ -86,7 +85,6 @@ int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_name The name of the element that should be recovered in the line. - * If empty, it is checked that there is only one element per line. * * @returns The recovered value. * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. @@ -94,7 +92,7 @@ int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name); +obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name); #endif /* OBIDMSCOLUMN_CHAR_H_ */ diff --git a/src/obidmscolumn_float.c b/src/obidmscolumn_float.c index 497629b..db36f8e 100644 --- a/src/obidmscolumn_float.c +++ b/src/obidmscolumn_float.c @@ -28,7 +28,7 @@ * **********************************************************************/ -int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obifloat_t value) +int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obifloat_t value) { // Check that the line number is not greater than the maximum allowed if (line_nb >= MAXIMUM_LINE_COUNT) @@ -57,7 +57,7 @@ int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, } -obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) +obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { if ((line_nb+1) > (column->header)->lines_used) { @@ -69,23 +69,23 @@ obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t l } -int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obifloat_t value) +int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obifloat_t value) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return -1; obi_column_set_obifloat_with_elt_idx(column, line_nb, element_idx, value); return 0; } -obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name) +obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return OBIFloat_NA; return obi_column_get_obifloat_with_elt_idx(column, line_nb, element_idx); } diff --git a/src/obidmscolumn_float.h b/src/obidmscolumn_float.h index e918494..407c34f 100644 --- a/src/obidmscolumn_float.h +++ b/src/obidmscolumn_float.h @@ -38,7 +38,7 @@ * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obifloat_t value); +int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obifloat_t value); /** @@ -54,7 +54,7 @@ int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx); +obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx); /** @@ -66,7 +66,6 @@ obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t l * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be set. * @param element_name The name of the element that should be set in the line. - * If empty, it is checked that there is only one element per line. * @param value The value that should be set. * * @returns An integer value indicating the success of the operation. @@ -76,7 +75,7 @@ obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t l * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obifloat_t value); +int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obifloat_t value); /** @@ -86,7 +85,6 @@ int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_name The name of the element that should be recovered in the line. - * If empty, it is checked that there is only one element per line. * * @returns The recovered value. * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. @@ -94,7 +92,7 @@ int obi_column_set_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name); +obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name); #endif /* OBIDMSCOLUMN_FLOAT_H_ */ diff --git a/src/obidmscolumn_int.c b/src/obidmscolumn_int.c index 6a04649..2c5e91c 100644 --- a/src/obidmscolumn_int.c +++ b/src/obidmscolumn_int.c @@ -28,7 +28,7 @@ * **********************************************************************/ -int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obiint_t value) +int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obiint_t value) { // Check that the line number is not greater than the maximum allowed if (line_nb >= MAXIMUM_LINE_COUNT) @@ -57,7 +57,7 @@ int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, s } -obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) +obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { if ((line_nb+1) > (column->header)->lines_used) { @@ -69,23 +69,23 @@ obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_ } -int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obiint_t value) +int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obiint_t value) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return -1; obi_column_set_obiint_with_elt_idx(column, line_nb, element_idx, value); return 0; } -obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name) +obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return OBIInt_NA; return obi_column_get_obiint_with_elt_idx(column, line_nb, element_idx); } diff --git a/src/obidmscolumn_int.h b/src/obidmscolumn_int.h index c4154b5..3e6f600 100644 --- a/src/obidmscolumn_int.h +++ b/src/obidmscolumn_int.h @@ -38,7 +38,7 @@ * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obiint_t value); +int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obiint_t value); /** @@ -54,7 +54,7 @@ int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, s * @since July 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx); +obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx); /** @@ -66,7 +66,6 @@ obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_ * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be set. * @param element_name The name of the element that should be set in the line. - * If empty, it is checked that there is only one element per line. * @param value The value that should be set. * * @returns An integer value indicating the success of the operation. @@ -76,7 +75,7 @@ obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_ * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obiint_t value); +int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obiint_t value); /** @@ -86,7 +85,6 @@ int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_name The name of the element that should be recovered in the line. - * If empty, it is checked that there is only one element per line. * * @returns The recovered value. * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. @@ -94,7 +92,7 @@ int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, * @since August 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name); +obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name); #endif /* OBIDMSCOLUMN_INT_H_ */ diff --git a/src/obidmscolumn_str.c b/src/obidmscolumn_str.c index 1fa4c5a..598767c 100644 --- a/src/obidmscolumn_str.c +++ b/src/obidmscolumn_str.c @@ -6,7 +6,7 @@ * @file obidsmcolumn_str.c * @author Celine Mercier * @date October 28th 2015 - * @brief Functions handling OBIColumns containing data with the OBIType OBI_IDX. + * @brief Functions handling OBIColumns containing data in the form of indices referring to character strings. */ @@ -29,7 +29,7 @@ * **********************************************************************/ -int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, char* value) +int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, char* value) { byte_t* value_b; index_t idx; @@ -59,31 +59,24 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, size_t line_nb, s if (value_b == NULL) return -1; - obidebug(1, "\nvalue=%s", value); - //obidebug(1, "\nbytes=%s", value_b+5); - // Add in the obiarray idx = obi_array_add(column->array, value_b); if (idx == -1) return -1; - obidebug(1, "\nidx=%d", idx); - //obidebug(1, "\nbytes 2=%s", obi_array_get(column->array, idx)+5); - // Add the value's index in the column - *(((obiidx_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx; + *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx; - // TODO free value_b probably + free(value_b); return 0; } -const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) +const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { index_t idx; byte_t* value_b; - const char* value; if ((line_nb+1) > (column->header)->lines_used) { @@ -92,32 +85,22 @@ const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, size_t li return "\0"; // TODO } - idx = *(((obiidx_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); + idx = *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); // Check NA if (idx == OBIIdx_NA) return "\0"; // TODO - obidebug(1, "\nwhy, idx = %d", idx); - value_b = obi_array_get(column->array, idx); - - obidebug(1, "\nwhyyyy"); - - value = obi_obibytes_to_str(value_b); - - obidebug(1, "\nwhyyyyyyyyyyyy, value=%s %p", value, value); - obidebug(1, "\nwhyyyyyyyyyyyy, len value=%d", strlen(value)); - - return value; + return obi_obibytes_to_str(value_b); } -int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, char* value) +int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, char* value) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return -1; if (obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value) < 0) return -1; @@ -125,12 +108,12 @@ int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, size_t line_nb, } -const char* obi_column_get_obistr_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name) +const char* obi_column_get_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name) { - size_t element_idx; + index_t element_idx; element_idx = obi_column_get_element_index_from_name(column, element_name); - if (element_idx == SIZE_MAX) //TODO + if (element_idx == OBIIdx_NA) return "\0"; return obi_column_get_obistr_with_elt_idx(column, line_nb, element_idx); } diff --git a/src/obidmscolumn_str.h b/src/obidmscolumn_str.h index 984a54a..8a2e195 100644 --- a/src/obidmscolumn_str.h +++ b/src/obidmscolumn_str.h @@ -1,17 +1,17 @@ /**************************************************************************** - * OBIDMS_column_idx header file * + * OBIDMS_column_str header file * ****************************************************************************/ /** - * @file obidsmcolumn_idx.h + * @file obidsmcolumn_str.h * @author Celine Mercier - * @date August 10th 2015 - * @brief Header file for the functions handling OBIColumns containing data with the OBIType OBI_IDX. + * @date October 28th 2015 + * @brief Header file for the functions handling OBIColumns containing data in the form of indices referring to character strings. */ -#ifndef OBIDMSCOLUMN_IDX_H_ -#define OBIDMSCOLUMN_IDX_H_ +#ifndef OBIDMSCOLUMN_STR_H_ +#define OBIDMSCOLUMN_STR_H_ #include @@ -22,7 +22,8 @@ /** - * @brief Sets a value in an OBIDMS column containing data with the type OBI_IDX, using the index of the element in the line. + * @brief Sets a value in an OBIDMS column containing data in the form of indices referring + * to character strings in an obiarray, using the index of the element in the line. * * @warning Pointers returned by obi_open_column() don't allow writing. * @@ -35,66 +36,65 @@ * @retval 0 on success. * @retval -1 if an error occurred. * - * @since July 2015 + * @since October 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obiidx_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, void* value); +int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, char* value); /** - * @brief Recovers a value in an OBIDMS column containing data with the type OBI_IDX. + * @brief Recovers a value in an OBIDMS column containing data in the form of indices referring + * to character strings in an obiarray, using the index of the element in the line. * * @param column A pointer as returned by obi_create_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_idx The index of the element that should be recovered in the line. * * @returns The recovered value. - * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. + * @retval '\0' the NA value of the type if an error occurred and obi_errno is set. * - * @since July 2015 + * @since October 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -const char* obi_column_get_obiidx_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx); +const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx); /** - * @brief Sets a value in an OBIDMS column containing data with the type OBI_IDX, - * using the name of the element in the line. + * @brief Sets a value in an OBIDMS column containing data in the form of indices referring + * to character strings in an obiarray, using the name of the element in the line. * * @warning Pointers returned by obi_open_column() don't allow writing. * * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be set. * @param element_name The name of the element that should be set in the line. - * If empty, it is checked that there is only one element per line. * @param value The value that should be set. * * @returns An integer value indicating the success of the operation. * @retval 0 on success. * @retval -1 if an error occurred. * - * @since August 2015 + * @since October 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -int obi_column_set_obiidx_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, void* value); +int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, char* value); /** - * @brief Recovers a value in an OBIDMS column containing data with the type OBI_IDX, - * using the name of the element in the line. + * @brief Recovers a value in an OBIDMS column containing data in the form of indices referring + * to character strings in an obiarray, using the name of the element in the line. * * @param column A pointer as returned by obi_create_column() or obi_clone_column(). * @param line_nb The number of the line where the value should be recovered. * @param element_name The name of the element that should be recovered in the line. - * If empty, it is checked that there is only one element per line. * * @returns The recovered value. - * @retval OBIBool_NA the NA value of the type if an error occurred and obi_errno is set. + * @retval '\0' the NA value of the type if an error occurred and obi_errno is set. * - * @since August 2015 + * @since October 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -const char* obi_column_get_obiidx_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name); +const char* obi_column_get_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name); #endif /* OBIDMSCOLUMN_IDX_H_ */ diff --git a/src/obierrno.h b/src/obierrno.h index 1abcd12..568c75f 100644 --- a/src/obierrno.h +++ b/src/obierrno.h @@ -98,7 +98,7 @@ extern int obi_errno; */ #define OBICOL_ACCESS_ERROR (19) /**< Permission error trying to access an OBIDSM column directory */ -#define OBI_ARRAY_ERROR (20) /** Error while handling a heap +#define OBI_ARRAY_ERROR (20) /** Error while handling an array */ /**@}*/ diff --git a/src/obitypes.c b/src/obitypes.c index c37e3a8..b5bf0b0 100644 --- a/src/obitypes.c +++ b/src/obitypes.c @@ -40,7 +40,7 @@ size_t obi_sizeof(OBIType_t type) case OBI_CHAR: size = sizeof(obichar_t); break; - case OBI_IDX: size = sizeof(obiidx_t); + case OBI_IDX: size = sizeof(index_t); break; default: size = 0; @@ -50,7 +50,7 @@ size_t obi_sizeof(OBIType_t type) } -size_t obi_array_sizeof(OBIType_t type, size_t nb_lines, size_t nb_elements_per_line) +size_t obi_array_sizeof(OBIType_t type, index_t nb_lines, index_t nb_elements_per_line) { size_t size; size_t rsize; diff --git a/src/obitypes.h b/src/obitypes.h index 5fd9655..9f99ec6 100644 --- a/src/obitypes.h +++ b/src/obitypes.h @@ -19,7 +19,7 @@ #define OBIInt_NA (INT32_MIN) /**< NA value for the type OBI_INT */ -#define OBIIdx_NA (SIZE_MAX) /**< NA value for the type OBI_IDX */ +#define OBIIdx_NA (INT64_MIN) /**< NA value for indices */ #define OBIFloat_NA (float_NA()) /**< NA value for the type OBI_FLOAT */ #define OBIChar_NA (0) /**< NA value for the type OBI_CHAR */ // TODO not sure about this one as it can be impossible to distinguish from uninitialized values @@ -44,14 +44,14 @@ typedef enum OBIType { OBI_FLOAT, /**< a floating value (C type : double) */ OBI_BOOL, /**< a boolean true/false value, see obibool_t enum */ OBI_CHAR, /**< a character (C type : char) */ - OBI_IDX /**< an index in a data structure (C type : size_t) */ + OBI_IDX /**< an index in a data structure (C type : int64_t) */ } OBIType_t, *OBIType_p; +typedef int64_t index_t; typedef int32_t obiint_t; typedef double obifloat_t; typedef char obichar_t; -typedef size_t obiidx_t; /** @@ -107,7 +107,7 @@ size_t obi_sizeof(OBIType_t type); * @since May 2015 * @author Eric Coissac (eric.coissac@metabarcoding.org) */ -size_t obi_array_sizeof(OBIType_t data_type, size_t nb_lines, size_t nb_elements_per_line); +size_t obi_array_sizeof(OBIType_t data_type, index_t nb_lines, index_t nb_elements_per_line); /**