diff --git a/.gitignore b/.gitignore index 2784f5945b..d223ddf7db 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ pyne/*/*.cpp # Ignore specific files examples/BASE_TAPE9.INP pyne/tests/test.file +pyne/tests/test.txt diff --git a/pyne/CMakeLists.txt b/pyne/CMakeLists.txt index 5c3b24e0b1..7cb0a92890 100644 --- a/pyne/CMakeLists.txt +++ b/pyne/CMakeLists.txt @@ -5,13 +5,17 @@ include_directories(${PYNE_INCLUDE_DIRS}) get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES) message("-- C_INCLUDE_PATH for ${CMAKE_CURRENT_SOURCE_DIR}: ${inc_dirs}") +# extra_types +set_source_files_properties(${PROJECT_SOURCE_DIR}/pyne/extra_types.pyx + PROPERTIES CYTHON_IS_CXX TRUE) +cython_add_module(extra_types extra_types.pyx) -# STL converters +# STL containers # If the pyx file is a C++ file, we should specify that here. # then, add the module -set_source_files_properties(${PROJECT_SOURCE_DIR}/pyne/stlconverters.pyx +set_source_files_properties(${PROJECT_SOURCE_DIR}/pyne/stlcontainers.pyx PROPERTIES CYTHON_IS_CXX TRUE) -cython_add_module(stlconverters stlconverters.pyx) +cython_add_module(stlcontainers stlcontainers.pyx) # jsoncpp set_source_files_properties(${PROJECT_SOURCE_DIR}/pyne/jsoncpp.pyx diff --git a/pyne/extra_types.pxd b/pyne/extra_types.pxd index b24208c753..1f378353c0 100644 --- a/pyne/extra_types.pxd +++ b/pyne/extra_types.pxd @@ -7,3 +7,5 @@ cdef extern from "extra_types.h" namespace "extra_types": ctypedef struct complex_t: double re double im + +cdef complex_t py2c_complex(object pyv) diff --git a/pyne/extra_types.pyx b/pyne/extra_types.pyx new file mode 100644 index 0000000000..706f7550a2 --- /dev/null +++ b/pyne/extra_types.pyx @@ -0,0 +1,9 @@ +cimport extra_types + +cdef extra_types.complex_t py2c_complex(object pyv): + cdef extra_types.complex_t cv = extra_types.complex_t() + pyv = complex(pyv) + cv.re = pyv.real + cv.im = pyv.imag + return cv + diff --git a/pyne/stlconverters.pxd b/pyne/stlcontainers.pxd similarity index 55% rename from pyne/stlconverters.pxd rename to pyne/stlcontainers.pxd index 6f54b6110c..9ca3eef23e 100644 --- a/pyne/stlconverters.pxd +++ b/pyne/stlcontainers.pxd @@ -8,77 +8,37 @@ from libcpp.set cimport set as cpp_set from libcpp.vector cimport vector as cpp_vector from cython.operator cimport dereference as deref from cython.operator cimport preincrement as inc +from libcpp.string cimport string as std_string +from libcpp.utility cimport pair +from libcpp.map cimport map as cpp_map +from libcpp.vector cimport vector as cpp_vector # Python Imports cimport numpy as np # Local imports -include "include/cython_version.pxi" -IF CYTHON_VERSION_MAJOR == 0 and CYTHON_VERSION_MINOR >= 17: - from libcpp.string cimport string as std_string - from libcpp.utility cimport pair - from libcpp.map cimport map as cpp_map - from libcpp.vector cimport vector as cpp_vector -ELSE: - from pyne._includes.libcpp.string cimport string as std_string - from pyne._includes.libcpp.utility cimport pair - from pyne._includes.libcpp.map cimport map as cpp_map - from pyne._includes.libcpp.vector cimport vector as cpp_vector cimport extra_types cimport numpy as np -cdef extra_types.complex_t py2c_complex(object) - -cdef np.ndarray c2py_vector_dbl(cpp_vector[double] *) - -cdef cpp_vector[double] py2c_vector_dbl(object) - -# integer sets -cdef cpp_set[int] py_to_cpp_set_int(set) -cdef set cpp_to_py_set_int(cpp_set[int]) - - -# string sets -cdef cpp_set[std_string] py_to_cpp_set_str(set) -cdef set cpp_to_py_set_str(cpp_set[std_string]) - - -# conversions -cdef cpp_map[int, int] dict_to_map_int_int(dict) -cdef dict map_to_dict_int_int(cpp_map[int, int]) - - - -# conversions -cdef cpp_map[int, double] dict_to_map_int_dbl(dict) -cdef dict map_to_dict_int_dbl(cpp_map[int, double]) - - - -# conversions -cdef cpp_map[std_string, int] dict_to_map_str_int(dict) -cdef dict map_to_dict_str_int(cpp_map[std_string, int]) - - - -# conversions -cdef cpp_map[int, std_string] dict_to_map_int_str(dict) -cdef dict map_to_dict_int_str(cpp_map[int, std_string]) - +# SetStr +cdef class _SetIterStr(object): + cdef cpp_set[std_string].iterator * iter_now + cdef cpp_set[std_string].iterator * iter_end + cdef void init(_SetIterStr, cpp_set[std_string] *) +cdef class _SetStr: + cdef cpp_set[std_string] * set_ptr + cdef public bint _free_set -# conversions -cdef cpp_map[std_string, double] dict_to_map_str_dbl(dict) -cdef dict map_to_dict_str_dbl(cpp_map[std_string, double]) # SetInt -cdef class SetIterInt(object): +cdef class _SetIterInt(object): cdef cpp_set[int].iterator * iter_now cdef cpp_set[int].iterator * iter_end - cdef void init(SetIterInt, cpp_set[int] *) + cdef void init(_SetIterInt, cpp_set[int] *) cdef class _SetInt: cdef cpp_set[int] * set_ptr @@ -87,24 +47,11 @@ cdef class _SetInt: -# SetStr -cdef class SetIterStr(object): - cdef cpp_set[std_string].iterator * iter_now - cdef cpp_set[std_string].iterator * iter_end - cdef void init(SetIterStr, cpp_set[std_string] *) - -cdef class _SetStr: - cdef cpp_set[std_string] * set_ptr - cdef public bint _free_set - - - - # MapStrStr -cdef class MapIterStrStr(object): +cdef class _MapIterStrStr(object): cdef cpp_map[std_string, std_string].iterator * iter_now cdef cpp_map[std_string, std_string].iterator * iter_end - cdef void init(MapIterStrStr, cpp_map[std_string, std_string] *) + cdef void init(_MapIterStrStr, cpp_map[std_string, std_string] *) cdef class _MapStrStr: cdef cpp_map[std_string, std_string] * map_ptr @@ -114,10 +61,10 @@ cdef class _MapStrStr: # MapStrInt -cdef class MapIterStrInt(object): +cdef class _MapIterStrInt(object): cdef cpp_map[std_string, int].iterator * iter_now cdef cpp_map[std_string, int].iterator * iter_end - cdef void init(MapIterStrInt, cpp_map[std_string, int] *) + cdef void init(_MapIterStrInt, cpp_map[std_string, int] *) cdef class _MapStrInt: cdef cpp_map[std_string, int] * map_ptr @@ -127,10 +74,10 @@ cdef class _MapStrInt: # MapIntStr -cdef class MapIterIntStr(object): +cdef class _MapIterIntStr(object): cdef cpp_map[int, std_string].iterator * iter_now cdef cpp_map[int, std_string].iterator * iter_end - cdef void init(MapIterIntStr, cpp_map[int, std_string] *) + cdef void init(_MapIterIntStr, cpp_map[int, std_string] *) cdef class _MapIntStr: cdef cpp_map[int, std_string] * map_ptr @@ -140,10 +87,10 @@ cdef class _MapIntStr: # MapStrUInt -cdef class MapIterStrUInt(object): +cdef class _MapIterStrUInt(object): cdef cpp_map[std_string, extra_types.uint].iterator * iter_now cdef cpp_map[std_string, extra_types.uint].iterator * iter_end - cdef void init(MapIterStrUInt, cpp_map[std_string, extra_types.uint] *) + cdef void init(_MapIterStrUInt, cpp_map[std_string, extra_types.uint] *) cdef class _MapStrUInt: cdef cpp_map[std_string, extra_types.uint] * map_ptr @@ -153,10 +100,10 @@ cdef class _MapStrUInt: # MapUIntStr -cdef class MapIterUIntStr(object): +cdef class _MapIterUIntStr(object): cdef cpp_map[extra_types.uint, std_string].iterator * iter_now cdef cpp_map[extra_types.uint, std_string].iterator * iter_end - cdef void init(MapIterUIntStr, cpp_map[extra_types.uint, std_string] *) + cdef void init(_MapIterUIntStr, cpp_map[extra_types.uint, std_string] *) cdef class _MapUIntStr: cdef cpp_map[extra_types.uint, std_string] * map_ptr @@ -165,37 +112,37 @@ cdef class _MapUIntStr: -# MapUIntUInt -cdef class MapIterUIntUInt(object): - cdef cpp_map[extra_types.uint, extra_types.uint].iterator * iter_now - cdef cpp_map[extra_types.uint, extra_types.uint].iterator * iter_end - cdef void init(MapIterUIntUInt, cpp_map[extra_types.uint, extra_types.uint] *) +# MapStrDouble +cdef class _MapIterStrDouble(object): + cdef cpp_map[std_string, double].iterator * iter_now + cdef cpp_map[std_string, double].iterator * iter_end + cdef void init(_MapIterStrDouble, cpp_map[std_string, double] *) -cdef class _MapUIntUInt: - cdef cpp_map[extra_types.uint, extra_types.uint] * map_ptr +cdef class _MapStrDouble: + cdef cpp_map[std_string, double] * map_ptr cdef public bint _free_map -# MapStrDouble -cdef class MapIterStrDouble(object): - cdef cpp_map[std_string, double].iterator * iter_now - cdef cpp_map[std_string, double].iterator * iter_end - cdef void init(MapIterStrDouble, cpp_map[std_string, double] *) +# MapUIntUInt +cdef class _MapIterUIntUInt(object): + cdef cpp_map[extra_types.uint, extra_types.uint].iterator * iter_now + cdef cpp_map[extra_types.uint, extra_types.uint].iterator * iter_end + cdef void init(_MapIterUIntUInt, cpp_map[extra_types.uint, extra_types.uint] *) -cdef class _MapStrDouble: - cdef cpp_map[std_string, double] * map_ptr +cdef class _MapUIntUInt: + cdef cpp_map[extra_types.uint, extra_types.uint] * map_ptr cdef public bint _free_map # MapIntInt -cdef class MapIterIntInt(object): +cdef class _MapIterIntInt(object): cdef cpp_map[int, int].iterator * iter_now cdef cpp_map[int, int].iterator * iter_end - cdef void init(MapIterIntInt, cpp_map[int, int] *) + cdef void init(_MapIterIntInt, cpp_map[int, int] *) cdef class _MapIntInt: cdef cpp_map[int, int] * map_ptr @@ -205,10 +152,10 @@ cdef class _MapIntInt: # MapIntDouble -cdef class MapIterIntDouble(object): +cdef class _MapIterIntDouble(object): cdef cpp_map[int, double].iterator * iter_now cdef cpp_map[int, double].iterator * iter_end - cdef void init(MapIterIntDouble, cpp_map[int, double] *) + cdef void init(_MapIterIntDouble, cpp_map[int, double] *) cdef class _MapIntDouble: cdef cpp_map[int, double] * map_ptr @@ -217,24 +164,11 @@ cdef class _MapIntDouble: -# MapUIntDouble -cdef class MapIterUIntDouble(object): - cdef cpp_map[extra_types.uint, double].iterator * iter_now - cdef cpp_map[extra_types.uint, double].iterator * iter_end - cdef void init(MapIterUIntDouble, cpp_map[extra_types.uint, double] *) - -cdef class _MapUIntDouble: - cdef cpp_map[extra_types.uint, double] * map_ptr - cdef public bint _free_map - - - - # MapIntComplex -cdef class MapIterIntComplex(object): +cdef class _MapIterIntComplex(object): cdef cpp_map[int, extra_types.complex_t].iterator * iter_now cdef cpp_map[int, extra_types.complex_t].iterator * iter_end - cdef void init(MapIterIntComplex, cpp_map[int, extra_types.complex_t] *) + cdef void init(_MapIterIntComplex, cpp_map[int, extra_types.complex_t] *) cdef class _MapIntComplex: cdef cpp_map[int, extra_types.complex_t] * map_ptr @@ -243,24 +177,24 @@ cdef class _MapIntComplex: -# MapIntVectorDouble -cdef class MapIterIntVectorDouble(object): - cdef cpp_map[int, cpp_vector[double]].iterator * iter_now - cdef cpp_map[int, cpp_vector[double]].iterator * iter_end - cdef void init(MapIterIntVectorDouble, cpp_map[int, cpp_vector[double]] *) +# MapUIntDouble +cdef class _MapIterUIntDouble(object): + cdef cpp_map[extra_types.uint, double].iterator * iter_now + cdef cpp_map[extra_types.uint, double].iterator * iter_end + cdef void init(_MapIterUIntDouble, cpp_map[extra_types.uint, double] *) -cdef class _MapIntVectorDouble: - cdef cpp_map[int, cpp_vector[double]] * map_ptr +cdef class _MapUIntDouble: + cdef cpp_map[extra_types.uint, double] * map_ptr cdef public bint _free_map # MapStrVectorDouble -cdef class MapIterStrVectorDouble(object): +cdef class _MapIterStrVectorDouble(object): cdef cpp_map[std_string, cpp_vector[double]].iterator * iter_now cdef cpp_map[std_string, cpp_vector[double]].iterator * iter_end - cdef void init(MapIterStrVectorDouble, cpp_map[std_string, cpp_vector[double]] *) + cdef void init(_MapIterStrVectorDouble, cpp_map[std_string, cpp_vector[double]] *) cdef class _MapStrVectorDouble: cdef cpp_map[std_string, cpp_vector[double]] * map_ptr @@ -269,3 +203,16 @@ cdef class _MapStrVectorDouble: +# MapIntVectorDouble +cdef class _MapIterIntVectorDouble(object): + cdef cpp_map[int, cpp_vector[double]].iterator * iter_now + cdef cpp_map[int, cpp_vector[double]].iterator * iter_end + cdef void init(_MapIterIntVectorDouble, cpp_map[int, cpp_vector[double]] *) + +cdef class _MapIntVectorDouble: + cdef cpp_map[int, cpp_vector[double]] * map_ptr + cdef public bint _free_map + + + + diff --git a/pyne/stlconverters.pyx b/pyne/stlcontainers.pyx similarity index 84% rename from pyne/stlconverters.pyx rename to pyne/stlcontainers.pyx index 5c8cc0bf38..1bdd555276 100644 --- a/pyne/stlconverters.pyx +++ b/pyne/stlcontainers.pyx @@ -9,6 +9,10 @@ from libcpp.vector cimport vector as cpp_vector from cython.operator cimport dereference as deref from cython.operator cimport preincrement as inc from libc.stdlib cimport malloc, free +from libcpp.string cimport string as std_string +from libcpp.utility cimport pair +from libcpp.map cimport map as cpp_map +from libcpp.vector cimport vector as cpp_vector # Python Imports import collections @@ -18,177 +22,16 @@ import numpy as np np.import_array() -# Local imports -include "include/cython_version.pxi" -IF CYTHON_VERSION_MAJOR == 0 and CYTHON_VERSION_MINOR >= 17: - from libcpp.string cimport string as std_string - from libcpp.utility cimport pair - from libcpp.map cimport map as cpp_map - from libcpp.vector cimport vector as cpp_vector -ELSE: - from pyne._includes.libcpp.string cimport string as std_string - from pyne._includes.libcpp.utility cimport pair - from pyne._includes.libcpp.map cimport map as cpp_map - from pyne._includes.libcpp.vector cimport vector as cpp_vector cimport extra_types -cdef extra_types.complex_t py2c_complex(object pyv): - cdef extra_types.complex_t cv - pyv = complex(pyv) - cv = extra_types.complex_t() - cv.re = pyv.real - cv.im = pyv.imag - return cv - -cdef np.ndarray c2py_vector_dbl(cpp_vector[double] * v): - cdef np.ndarray vview - cdef np.ndarray pyv - cdef np.npy_intp v_shape[1] - v_shape[0] = v.size() - vview = np.PyArray_SimpleNewFromData(1, v_shape, np.NPY_FLOAT64, &v[0][0]) - pyv = np.PyArray_Copy(vview) - return pyv - -cdef cpp_vector[double] py2c_vector_dbl(object v): - cdef int i - cdef int v_size = len(v) - cdef double * v_data - cdef cpp_vector[double] vec - if isinstance(v, np.ndarray) and ( v).descr.type_num == np.NPY_FLOAT64: - v_data = np.PyArray_DATA( v) - vec = cpp_vector[double]( v_size) - for i in range(v_size): - vec[i] = v_data[i] - else: - vec = cpp_vector[double]( v_size) - for i in range(v_size): - vec[i] = v[i] - return vec - -# integer sets -cdef cpp_set[int] py_to_cpp_set_int(set pyset): - cdef int v - cdef cpp_set[int] cppset = cpp_set[int]() - for value in pyset: - v = value - cppset.insert(v) - return cppset - -cdef set cpp_to_py_set_int(cpp_set[int] cppset): - pyset = set() - cdef cpp_set[int].iterator setiter = cppset.begin() - while setiter != cppset.end(): - pyset.add(int(deref(setiter))) - inc(setiter) - return pyset - - -# string sets -cdef cpp_set[std_string] py_to_cpp_set_str(set pyset): - cdef std_string v - cdef cpp_set[std_string] cppset = cpp_set[std_string]() - for value in pyset: - v = std_string( value) - cppset.insert(v) - return cppset - -cdef set cpp_to_py_set_str(cpp_set[std_string] cppset): - pyset = set() - cdef cpp_set[std_string].iterator setiter = cppset.begin() - while setiter != cppset.end(): - pyset.add(str( deref(setiter).c_str())) - inc(setiter) - return pyset - - -# conversions -cdef cpp_map[int, int] dict_to_map_int_int(dict pydict): - cdef cpp_map[int, int] cppmap = cpp_map[int, int]() - for key, value in pydict.items(): - cppmap[key] = value - return cppmap - -cdef dict map_to_dict_int_int(cpp_map[int, int] cppmap): - pydict = {} - cdef cpp_map[int, int].iterator mapiter = cppmap.begin() - while mapiter != cppmap.end(): - pydict[int(deref(mapiter).first)] = int(deref(mapiter).second) - inc(mapiter) - return pydict - - -# conversions -cdef cpp_map[int, double] dict_to_map_int_dbl(dict pydict): - cdef cpp_map[int, double] cppmap = cpp_map[int, double]() - for key, value in pydict.items(): - cppmap[key] = value - return cppmap - -cdef dict map_to_dict_int_dbl(cpp_map[int, double] cppmap): - pydict = {} - cdef cpp_map[int, double].iterator mapiter = cppmap.begin() - while mapiter != cppmap.end(): - pydict[int(deref(mapiter).first)] = float(deref(mapiter).second) - inc(mapiter) - return pydict - - -# conversions -cdef cpp_map[std_string, int] dict_to_map_str_int(dict pydict): - cdef cpp_map[std_string, int] cppmap = cpp_map[std_string, int]() - for key, value in pydict.items(): - cppmap[std_string( key)] = value - return cppmap - -cdef dict map_to_dict_str_int(cpp_map[std_string, int] cppmap): - pydict = {} - cdef cpp_map[std_string, int].iterator mapiter = cppmap.begin() - while mapiter != cppmap.end(): - pydict[str( deref(mapiter).first.c_str())] = int(deref(mapiter).second) - inc(mapiter) - return pydict - - -# conversions -cdef cpp_map[int, std_string] dict_to_map_int_str(dict pydict): - cdef cpp_map[int, std_string] cppmap = cpp_map[int, std_string]() - for key, value in pydict.items(): - cppmap[key] = std_string( value) - return cppmap - -cdef dict map_to_dict_int_str(cpp_map[int, std_string] cppmap): - pydict = {} - cdef cpp_map[int, std_string].iterator mapiter = cppmap.begin() - while mapiter != cppmap.end(): - pydict[int(deref(mapiter).first)] = str( deref(mapiter).second.c_str()) - inc(mapiter) - return pydict - - -# conversions -cdef cpp_map[std_string, double] dict_to_map_str_dbl(dict pydict): - cdef cpp_map[std_string, double] cppmap = cpp_map[std_string, double]() - for key, value in pydict.items(): - cppmap[std_string( key)] = value - return cppmap - -cdef dict map_to_dict_str_dbl(cpp_map[std_string, double] cppmap): - pydict = {} - cdef cpp_map[std_string, double].iterator mapiter = cppmap.begin() - while mapiter != cppmap.end(): - pydict[str( deref(mapiter).first.c_str())] = float(deref(mapiter).second) - inc(mapiter) - return pydict - - -# SetInt -cdef class SetIterInt(object): - cdef void init(self, cpp_set[int] * set_ptr): - cdef cpp_set[int].iterator * itn = malloc(sizeof(set_ptr.begin())) +# SetStr +cdef class _SetIterStr(object): + cdef void init(self, cpp_set[std_string] * set_ptr): + cdef cpp_set[std_string].iterator * itn = malloc(sizeof(set_ptr.begin())) itn[0] = set_ptr.begin() self.iter_now = itn - cdef cpp_set[int].iterator * ite = malloc(sizeof(set_ptr.end())) + cdef cpp_set[std_string].iterator * ite = malloc(sizeof(set_ptr.end())) ite[0] = set_ptr.end() self.iter_end = ite @@ -200,11 +43,12 @@ cdef class SetIterInt(object): return self def __next__(self): - cdef cpp_set[int].iterator inow = deref(self.iter_now) - cdef cpp_set[int].iterator iend = deref(self.iter_end) + cdef cpp_set[std_string].iterator inow = deref(self.iter_now) + cdef cpp_set[std_string].iterator iend = deref(self.iter_end) if inow != iend: - pyval = int(deref(inow)) + + pyval = str( deref(inow).c_str()) else: raise StopIteration @@ -212,22 +56,24 @@ cdef class SetIterInt(object): return pyval -cdef class _SetInt: +cdef class _SetStr: def __cinit__(self, new_set=True, bint free_set=True): - cdef int s + cdef std_string s + # Decide how to init set, if at all - if isinstance(new_set, _SetInt): - self.set_ptr = (<_SetInt> new_set).set_ptr + if isinstance(new_set, _SetStr): + self.set_ptr = (<_SetStr> new_set).set_ptr elif hasattr(new_set, '__iter__') or \ (hasattr(new_set, '__len__') and hasattr(new_set, '__getitem__')): - self.set_ptr = new cpp_set[int]() + self.set_ptr = new cpp_set[std_string]() for value in new_set: - s = value + + s = std_string( value) self.set_ptr.insert(s) elif bool(new_set): - self.set_ptr = new cpp_set[int]() + self.set_ptr = new cpp_set[std_string]() # Store free_set self._free_set = free_set @@ -237,9 +83,11 @@ cdef class _SetInt: del self.set_ptr def __contains__(self, value): - cdef int s - if isinstance(value, int): - s = value + cdef std_string s + + if isinstance(value, basestring): + + s = std_string( value) else: return False @@ -252,26 +100,30 @@ cdef class _SetInt: return self.set_ptr.size() def __iter__(self): - cdef SetIterInt si = SetIterInt() + cdef _SetIterStr si = _SetIterStr() si.init(self.set_ptr) return si - def add(self, int value): - cdef int v - v = value + def add(self, char * value): + cdef std_string v + + + v = std_string( value) self.set_ptr.insert(v) return def discard(self, value): - cdef int v + cdef std_string v + if value in self: - v = value + + v = std_string( value) self.set_ptr.erase(v) return -class SetInt(_SetInt, collections.Set): - """Wrapper class for C++ standard library sets of type . +class SetStr(_SetStr, collections.Set): + """Wrapper class for C++ standard library sets of type . Provides set like interface on the Python level. Parameters @@ -292,14 +144,14 @@ class SetInt(_SetInt, collections.Set): -# SetStr -cdef class SetIterStr(object): - cdef void init(self, cpp_set[std_string] * set_ptr): - cdef cpp_set[std_string].iterator * itn = malloc(sizeof(set_ptr.begin())) +# SetInt +cdef class _SetIterInt(object): + cdef void init(self, cpp_set[int] * set_ptr): + cdef cpp_set[int].iterator * itn = malloc(sizeof(set_ptr.begin())) itn[0] = set_ptr.begin() self.iter_now = itn - cdef cpp_set[std_string].iterator * ite = malloc(sizeof(set_ptr.end())) + cdef cpp_set[int].iterator * ite = malloc(sizeof(set_ptr.end())) ite[0] = set_ptr.end() self.iter_end = ite @@ -311,11 +163,12 @@ cdef class SetIterStr(object): return self def __next__(self): - cdef cpp_set[std_string].iterator inow = deref(self.iter_now) - cdef cpp_set[std_string].iterator iend = deref(self.iter_end) + cdef cpp_set[int].iterator inow = deref(self.iter_now) + cdef cpp_set[int].iterator iend = deref(self.iter_end) if inow != iend: - pyval = str( deref(inow).c_str()) + + pyval = int(deref(inow)) else: raise StopIteration @@ -323,22 +176,24 @@ cdef class SetIterStr(object): return pyval -cdef class _SetStr: +cdef class _SetInt: def __cinit__(self, new_set=True, bint free_set=True): - cdef std_string s + cdef int s + # Decide how to init set, if at all - if isinstance(new_set, _SetStr): - self.set_ptr = (<_SetStr> new_set).set_ptr + if isinstance(new_set, _SetInt): + self.set_ptr = (<_SetInt> new_set).set_ptr elif hasattr(new_set, '__iter__') or \ (hasattr(new_set, '__len__') and hasattr(new_set, '__getitem__')): - self.set_ptr = new cpp_set[std_string]() + self.set_ptr = new cpp_set[int]() for value in new_set: - s = std_string( value) + + s = value self.set_ptr.insert(s) elif bool(new_set): - self.set_ptr = new cpp_set[std_string]() + self.set_ptr = new cpp_set[int]() # Store free_set self._free_set = free_set @@ -348,9 +203,11 @@ cdef class _SetStr: del self.set_ptr def __contains__(self, value): - cdef std_string s - if isinstance(value, basestring): - s = std_string( value) + cdef int s + + if isinstance(value, int) or isinstance(value, long): + + s = value else: return False @@ -363,26 +220,30 @@ cdef class _SetStr: return self.set_ptr.size() def __iter__(self): - cdef SetIterStr si = SetIterStr() + cdef _SetIterInt si = _SetIterInt() si.init(self.set_ptr) return si - def add(self, char * value): - cdef std_string v - v = std_string( value) + def add(self, int value): + cdef int v + + + v = value self.set_ptr.insert(v) return def discard(self, value): - cdef std_string v + cdef int v + if value in self: - v = std_string( value) + + v = value self.set_ptr.erase(v) return -class SetStr(_SetStr, collections.Set): - """Wrapper class for C++ standard library sets of type . +class SetInt(_SetInt, collections.Set): + """Wrapper class for C++ standard library sets of type . Provides set like interface on the Python level. Parameters @@ -404,7 +265,7 @@ class SetStr(_SetStr, collections.Set): # Map(Str, Str) -cdef class MapIterStrStr(object): +cdef class _MapIterStrStr(object): cdef void init(self, cpp_map[std_string, std_string] * map_ptr): cdef cpp_map[std_string, std_string].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -426,6 +287,7 @@ cdef class MapIterStrStr(object): cdef cpp_map[std_string, std_string].iterator iend = deref(self.iter_end) if inow != iend: + pyval = str( deref(inow).first.c_str()) else: raise StopIteration @@ -437,17 +299,23 @@ cdef class _MapStrStr: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[std_string, std_string] item + + # Decide how to init map, if at all if isinstance(new_map, _MapStrStr): self.map_ptr = (<_MapStrStr> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[std_string, std_string]() for key, value in new_map.items(): + + item = pair[std_string, std_string](std_string( key), std_string( value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[std_string, std_string]() for key, value in new_map: + + item = pair[std_string, std_string](std_string( key), std_string( value)) self.map_ptr.insert(item) elif bool(new_map): @@ -462,8 +330,10 @@ cdef class _MapStrStr: def __contains__(self, key): cdef std_string k + if not isinstance(key, basestring): return False + k = std_string( key) if 0 < self.map_ptr.count(k): @@ -475,7 +345,7 @@ cdef class _MapStrStr: return self.map_ptr.size() def __iter__(self): - cdef MapIterStrStr mi = MapIterStrStr() + cdef _MapIterStrStr mi = _MapIterStrStr() mi.init(self.map_ptr) return mi @@ -483,23 +353,33 @@ cdef class _MapStrStr: cdef std_string k cdef std_string v + if not isinstance(key, basestring): raise TypeError("Only string keys are valid.") + k = std_string( key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return str( v.c_str()) + + return str( deref(self.map_ptr)[k].c_str()) else: raise KeyError def __setitem__(self, key, value): - cdef pair[std_string, std_string] item = pair[std_string, std_string](std_string( key), std_string( value)) + + + cdef pair[std_string, std_string] item + + + item = pair[std_string, std_string](std_string( key), std_string( value)) self.map_ptr.insert(item) def __delitem__(self, key): cdef std_string k + if key in self: + k = std_string( key) self.map_ptr.erase(k) @@ -527,7 +407,7 @@ class MapStrStr(_MapStrStr, collections.MutableMapping): # Map(Str, Int) -cdef class MapIterStrInt(object): +cdef class _MapIterStrInt(object): cdef void init(self, cpp_map[std_string, int] * map_ptr): cdef cpp_map[std_string, int].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -549,6 +429,7 @@ cdef class MapIterStrInt(object): cdef cpp_map[std_string, int].iterator iend = deref(self.iter_end) if inow != iend: + pyval = str( deref(inow).first.c_str()) else: raise StopIteration @@ -560,17 +441,23 @@ cdef class _MapStrInt: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[std_string, int] item + + # Decide how to init map, if at all if isinstance(new_map, _MapStrInt): self.map_ptr = (<_MapStrInt> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[std_string, int]() for key, value in new_map.items(): + + item = pair[std_string, int](std_string( key), value) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[std_string, int]() for key, value in new_map: + + item = pair[std_string, int](std_string( key), value) self.map_ptr.insert(item) elif bool(new_map): @@ -585,8 +472,10 @@ cdef class _MapStrInt: def __contains__(self, key): cdef std_string k + if not isinstance(key, basestring): return False + k = std_string( key) if 0 < self.map_ptr.count(k): @@ -598,7 +487,7 @@ cdef class _MapStrInt: return self.map_ptr.size() def __iter__(self): - cdef MapIterStrInt mi = MapIterStrInt() + cdef _MapIterStrInt mi = _MapIterStrInt() mi.init(self.map_ptr) return mi @@ -606,23 +495,33 @@ cdef class _MapStrInt: cdef std_string k cdef int v + if not isinstance(key, basestring): raise TypeError("Only string keys are valid.") + k = std_string( key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return int(v) + + return int(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[std_string, int] item = pair[std_string, int](std_string( key), value) + + + cdef pair[std_string, int] item + + + item = pair[std_string, int](std_string( key), value) self.map_ptr.insert(item) def __delitem__(self, key): cdef std_string k + if key in self: + k = std_string( key) self.map_ptr.erase(k) @@ -650,7 +549,7 @@ class MapStrInt(_MapStrInt, collections.MutableMapping): # Map(Int, Str) -cdef class MapIterIntStr(object): +cdef class _MapIterIntStr(object): cdef void init(self, cpp_map[int, std_string] * map_ptr): cdef cpp_map[int, std_string].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -672,6 +571,7 @@ cdef class MapIterIntStr(object): cdef cpp_map[int, std_string].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -683,17 +583,23 @@ cdef class _MapIntStr: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[int, std_string] item + + # Decide how to init map, if at all if isinstance(new_map, _MapIntStr): self.map_ptr = (<_MapIntStr> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[int, std_string]() for key, value in new_map.items(): + + item = pair[int, std_string](key, std_string( value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[int, std_string]() for key, value in new_map: + + item = pair[int, std_string](key, std_string( value)) self.map_ptr.insert(item) elif bool(new_map): @@ -708,8 +614,10 @@ cdef class _MapIntStr: def __contains__(self, key): cdef int k - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): return False + k = key if 0 < self.map_ptr.count(k): @@ -721,7 +629,7 @@ cdef class _MapIntStr: return self.map_ptr.size() def __iter__(self): - cdef MapIterIntStr mi = MapIterIntStr() + cdef _MapIterIntStr mi = _MapIterIntStr() mi.init(self.map_ptr) return mi @@ -729,23 +637,33 @@ cdef class _MapIntStr: cdef int k cdef std_string v - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): raise TypeError("Only integer keys are valid.") + k = key if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return str( v.c_str()) + + return str( deref(self.map_ptr)[k].c_str()) else: raise KeyError def __setitem__(self, key, value): - cdef pair[int, std_string] item = pair[int, std_string](key, std_string( value)) + + + cdef pair[int, std_string] item + + + item = pair[int, std_string](key, std_string( value)) self.map_ptr.insert(item) def __delitem__(self, key): cdef int k + if key in self: + k = key self.map_ptr.erase(k) @@ -773,7 +691,7 @@ class MapIntStr(_MapIntStr, collections.MutableMapping): # Map(Str, UInt) -cdef class MapIterStrUInt(object): +cdef class _MapIterStrUInt(object): cdef void init(self, cpp_map[std_string, extra_types.uint] * map_ptr): cdef cpp_map[std_string, extra_types.uint].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -795,6 +713,7 @@ cdef class MapIterStrUInt(object): cdef cpp_map[std_string, extra_types.uint].iterator iend = deref(self.iter_end) if inow != iend: + pyval = str( deref(inow).first.c_str()) else: raise StopIteration @@ -806,17 +725,23 @@ cdef class _MapStrUInt: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[std_string, extra_types.uint] item + + # Decide how to init map, if at all if isinstance(new_map, _MapStrUInt): self.map_ptr = (<_MapStrUInt> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[std_string, extra_types.uint]() for key, value in new_map.items(): + + item = pair[std_string, extra_types.uint](std_string( key), long(value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[std_string, extra_types.uint]() for key, value in new_map: + + item = pair[std_string, extra_types.uint](std_string( key), long(value)) self.map_ptr.insert(item) elif bool(new_map): @@ -831,8 +756,10 @@ cdef class _MapStrUInt: def __contains__(self, key): cdef std_string k + if not isinstance(key, basestring): return False + k = std_string( key) if 0 < self.map_ptr.count(k): @@ -844,7 +771,7 @@ cdef class _MapStrUInt: return self.map_ptr.size() def __iter__(self): - cdef MapIterStrUInt mi = MapIterStrUInt() + cdef _MapIterStrUInt mi = _MapIterStrUInt() mi.init(self.map_ptr) return mi @@ -852,23 +779,33 @@ cdef class _MapStrUInt: cdef std_string k cdef extra_types.uint v + if not isinstance(key, basestring): raise TypeError("Only string keys are valid.") + k = std_string( key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return int(v) + + return int(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[std_string, extra_types.uint] item = pair[std_string, extra_types.uint](std_string( key), long(value)) + + + cdef pair[std_string, extra_types.uint] item + + + item = pair[std_string, extra_types.uint](std_string( key), long(value)) self.map_ptr.insert(item) def __delitem__(self, key): cdef std_string k + if key in self: + k = std_string( key) self.map_ptr.erase(k) @@ -896,7 +833,7 @@ class MapStrUInt(_MapStrUInt, collections.MutableMapping): # Map(UInt, Str) -cdef class MapIterUIntStr(object): +cdef class _MapIterUIntStr(object): cdef void init(self, cpp_map[extra_types.uint, std_string] * map_ptr): cdef cpp_map[extra_types.uint, std_string].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -918,6 +855,7 @@ cdef class MapIterUIntStr(object): cdef cpp_map[extra_types.uint, std_string].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -929,17 +867,23 @@ cdef class _MapUIntStr: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[extra_types.uint, std_string] item + + # Decide how to init map, if at all if isinstance(new_map, _MapUIntStr): self.map_ptr = (<_MapUIntStr> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[extra_types.uint, std_string]() for key, value in new_map.items(): + + item = pair[extra_types.uint, std_string]( long(key), std_string( value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[extra_types.uint, std_string]() for key, value in new_map: + + item = pair[extra_types.uint, std_string]( long(key), std_string( value)) self.map_ptr.insert(item) elif bool(new_map): @@ -954,8 +898,10 @@ cdef class _MapUIntStr: def __contains__(self, key): cdef extra_types.uint k + if not isinstance(key, int) and not isinstance(key, long): return False + k = long(key) if 0 < self.map_ptr.count(k): @@ -967,7 +913,7 @@ cdef class _MapUIntStr: return self.map_ptr.size() def __iter__(self): - cdef MapIterUIntStr mi = MapIterUIntStr() + cdef _MapIterUIntStr mi = _MapIterUIntStr() mi.init(self.map_ptr) return mi @@ -975,23 +921,33 @@ cdef class _MapUIntStr: cdef extra_types.uint k cdef std_string v + if not isinstance(key, int) and not isinstance(key, long): raise TypeError("Only unsigned integer keys are valid.") + k = long(key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return str( v.c_str()) + + return str( deref(self.map_ptr)[k].c_str()) else: raise KeyError def __setitem__(self, key, value): - cdef pair[extra_types.uint, std_string] item = pair[extra_types.uint, std_string]( long(key), std_string( value)) + + + cdef pair[extra_types.uint, std_string] item + + + item = pair[extra_types.uint, std_string]( long(key), std_string( value)) self.map_ptr.insert(item) def __delitem__(self, key): cdef extra_types.uint k + if key in self: + k = long(key) self.map_ptr.erase(k) @@ -1018,14 +974,14 @@ class MapUIntStr(_MapUIntStr, collections.MutableMapping): -# Map(UInt, UInt) -cdef class MapIterUIntUInt(object): - cdef void init(self, cpp_map[extra_types.uint, extra_types.uint] * map_ptr): - cdef cpp_map[extra_types.uint, extra_types.uint].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(Str, Double) +cdef class _MapIterStrDouble(object): + cdef void init(self, cpp_map[std_string, double] * map_ptr): + cdef cpp_map[std_string, double].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[extra_types.uint, extra_types.uint].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[std_string, double].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1037,36 +993,43 @@ cdef class MapIterUIntUInt(object): return self def __next__(self): - cdef cpp_map[extra_types.uint, extra_types.uint].iterator inow = deref(self.iter_now) - cdef cpp_map[extra_types.uint, extra_types.uint].iterator iend = deref(self.iter_end) + cdef cpp_map[std_string, double].iterator inow = deref(self.iter_now) + cdef cpp_map[std_string, double].iterator iend = deref(self.iter_end) if inow != iend: - pyval = int(deref(inow).first) + + pyval = str( deref(inow).first.c_str()) else: raise StopIteration inc(deref(self.iter_now)) return pyval -cdef class _MapUIntUInt: +cdef class _MapStrDouble: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[extra_types.uint, extra_types.uint] item + cdef pair[std_string, double] item + + # Decide how to init map, if at all - if isinstance(new_map, _MapUIntUInt): - self.map_ptr = (<_MapUIntUInt> new_map).map_ptr + if isinstance(new_map, _MapStrDouble): + self.map_ptr = (<_MapStrDouble> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() + self.map_ptr = new cpp_map[std_string, double]() for key, value in new_map.items(): - item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) + + + item = pair[std_string, double](std_string( key), value) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() + self.map_ptr = new cpp_map[std_string, double]() for key, value in new_map: - item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) + + + item = pair[std_string, double](std_string( key), value) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() + self.map_ptr = new cpp_map[std_string, double]() # Store free_map self._free_map = free_map @@ -1076,10 +1039,12 @@ cdef class _MapUIntUInt: del self.map_ptr def __contains__(self, key): - cdef extra_types.uint k - if not isinstance(key, int) and not isinstance(key, long): - return False - k = long(key) + cdef std_string k + + if not isinstance(key, basestring): + return False + + k = std_string( key) if 0 < self.map_ptr.count(k): return True @@ -1090,37 +1055,47 @@ cdef class _MapUIntUInt: return self.map_ptr.size() def __iter__(self): - cdef MapIterUIntUInt mi = MapIterUIntUInt() + cdef _MapIterStrDouble mi = _MapIterStrDouble() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef extra_types.uint k - cdef extra_types.uint v + cdef std_string k + cdef double v - if not isinstance(key, int) and not isinstance(key, long): - raise TypeError("Only unsigned integer keys are valid.") - k = long(key) + + if not isinstance(key, basestring): + raise TypeError("Only string keys are valid.") + + k = std_string( key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return int(v) + + return float(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[extra_types.uint, extra_types.uint] item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) + + + cdef pair[std_string, double] item + + + item = pair[std_string, double](std_string( key), value) self.map_ptr.insert(item) def __delitem__(self, key): - cdef extra_types.uint k + cdef std_string k + if key in self: - k = long(key) + + k = std_string( key) self.map_ptr.erase(k) -class MapUIntUInt(_MapUIntUInt, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapStrDouble(_MapStrDouble, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters @@ -1141,14 +1116,14 @@ class MapUIntUInt(_MapUIntUInt, collections.MutableMapping): -# Map(Str, Double) -cdef class MapIterStrDouble(object): - cdef void init(self, cpp_map[std_string, double] * map_ptr): - cdef cpp_map[std_string, double].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(UInt, UInt) +cdef class _MapIterUIntUInt(object): + cdef void init(self, cpp_map[extra_types.uint, extra_types.uint] * map_ptr): + cdef cpp_map[extra_types.uint, extra_types.uint].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[std_string, double].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[extra_types.uint, extra_types.uint].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1160,36 +1135,43 @@ cdef class MapIterStrDouble(object): return self def __next__(self): - cdef cpp_map[std_string, double].iterator inow = deref(self.iter_now) - cdef cpp_map[std_string, double].iterator iend = deref(self.iter_end) + cdef cpp_map[extra_types.uint, extra_types.uint].iterator inow = deref(self.iter_now) + cdef cpp_map[extra_types.uint, extra_types.uint].iterator iend = deref(self.iter_end) if inow != iend: - pyval = str( deref(inow).first.c_str()) + + pyval = int(deref(inow).first) else: raise StopIteration inc(deref(self.iter_now)) return pyval -cdef class _MapStrDouble: +cdef class _MapUIntUInt: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[std_string, double] item + cdef pair[extra_types.uint, extra_types.uint] item + + # Decide how to init map, if at all - if isinstance(new_map, _MapStrDouble): - self.map_ptr = (<_MapStrDouble> new_map).map_ptr + if isinstance(new_map, _MapUIntUInt): + self.map_ptr = (<_MapUIntUInt> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[std_string, double]() + self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() for key, value in new_map.items(): - item = pair[std_string, double](std_string( key), value) + + + item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[std_string, double]() + self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() for key, value in new_map: - item = pair[std_string, double](std_string( key), value) + + + item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[std_string, double]() + self.map_ptr = new cpp_map[extra_types.uint, extra_types.uint]() # Store free_map self._free_map = free_map @@ -1199,10 +1181,12 @@ cdef class _MapStrDouble: del self.map_ptr def __contains__(self, key): - cdef std_string k - if not isinstance(key, basestring): + cdef extra_types.uint k + + if not isinstance(key, int) and not isinstance(key, long): return False - k = std_string( key) + + k = long(key) if 0 < self.map_ptr.count(k): return True @@ -1213,37 +1197,47 @@ cdef class _MapStrDouble: return self.map_ptr.size() def __iter__(self): - cdef MapIterStrDouble mi = MapIterStrDouble() + cdef _MapIterUIntUInt mi = _MapIterUIntUInt() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef std_string k - cdef double v + cdef extra_types.uint k + cdef extra_types.uint v - if not isinstance(key, basestring): - raise TypeError("Only string keys are valid.") - k = std_string( key) + + if not isinstance(key, int) and not isinstance(key, long): + raise TypeError("Only unsigned integer keys are valid.") + + k = long(key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return float(v) + + return int(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[std_string, double] item = pair[std_string, double](std_string( key), value) + + + cdef pair[extra_types.uint, extra_types.uint] item + + + item = pair[extra_types.uint, extra_types.uint]( long(key), long(value)) self.map_ptr.insert(item) def __delitem__(self, key): - cdef std_string k + cdef extra_types.uint k + if key in self: - k = std_string( key) + + k = long(key) self.map_ptr.erase(k) -class MapStrDouble(_MapStrDouble, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapUIntUInt(_MapUIntUInt, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters @@ -1265,7 +1259,7 @@ class MapStrDouble(_MapStrDouble, collections.MutableMapping): # Map(Int, Int) -cdef class MapIterIntInt(object): +cdef class _MapIterIntInt(object): cdef void init(self, cpp_map[int, int] * map_ptr): cdef cpp_map[int, int].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -1287,6 +1281,7 @@ cdef class MapIterIntInt(object): cdef cpp_map[int, int].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -1298,17 +1293,23 @@ cdef class _MapIntInt: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[int, int] item + + # Decide how to init map, if at all if isinstance(new_map, _MapIntInt): self.map_ptr = (<_MapIntInt> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[int, int]() for key, value in new_map.items(): + + item = pair[int, int](key, value) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[int, int]() for key, value in new_map: + + item = pair[int, int](key, value) self.map_ptr.insert(item) elif bool(new_map): @@ -1323,8 +1324,10 @@ cdef class _MapIntInt: def __contains__(self, key): cdef int k - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): return False + k = key if 0 < self.map_ptr.count(k): @@ -1336,7 +1339,7 @@ cdef class _MapIntInt: return self.map_ptr.size() def __iter__(self): - cdef MapIterIntInt mi = MapIterIntInt() + cdef _MapIterIntInt mi = _MapIterIntInt() mi.init(self.map_ptr) return mi @@ -1344,23 +1347,33 @@ cdef class _MapIntInt: cdef int k cdef int v - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): raise TypeError("Only integer keys are valid.") + k = key if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return int(v) + + return int(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[int, int] item = pair[int, int](key, value) + + + cdef pair[int, int] item + + + item = pair[int, int](key, value) self.map_ptr.insert(item) def __delitem__(self, key): cdef int k + if key in self: + k = key self.map_ptr.erase(k) @@ -1388,7 +1401,7 @@ class MapIntInt(_MapIntInt, collections.MutableMapping): # Map(Int, Double) -cdef class MapIterIntDouble(object): +cdef class _MapIterIntDouble(object): cdef void init(self, cpp_map[int, double] * map_ptr): cdef cpp_map[int, double].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() @@ -1410,6 +1423,7 @@ cdef class MapIterIntDouble(object): cdef cpp_map[int, double].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -1421,17 +1435,23 @@ cdef class _MapIntDouble: def __cinit__(self, new_map=True, bint free_map=True): cdef pair[int, double] item + + # Decide how to init map, if at all if isinstance(new_map, _MapIntDouble): self.map_ptr = (<_MapIntDouble> new_map).map_ptr elif hasattr(new_map, 'items'): self.map_ptr = new cpp_map[int, double]() for key, value in new_map.items(): + + item = pair[int, double](key, value) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): self.map_ptr = new cpp_map[int, double]() for key, value in new_map: + + item = pair[int, double](key, value) self.map_ptr.insert(item) elif bool(new_map): @@ -1446,8 +1466,10 @@ cdef class _MapIntDouble: def __contains__(self, key): cdef int k - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): return False + k = key if 0 < self.map_ptr.count(k): @@ -1459,7 +1481,7 @@ cdef class _MapIntDouble: return self.map_ptr.size() def __iter__(self): - cdef MapIterIntDouble mi = MapIterIntDouble() + cdef _MapIterIntDouble mi = _MapIterIntDouble() mi.init(self.map_ptr) return mi @@ -1467,23 +1489,33 @@ cdef class _MapIntDouble: cdef int k cdef double v - if not isinstance(key, int): + + if not isinstance(key, int) and not isinstance(key, long): raise TypeError("Only integer keys are valid.") + k = key if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return float(v) + + return float(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[int, double] item = pair[int, double](key, value) + + + cdef pair[int, double] item + + + item = pair[int, double](key, value) self.map_ptr.insert(item) def __delitem__(self, key): cdef int k + if key in self: + k = key self.map_ptr.erase(k) @@ -1510,14 +1542,14 @@ class MapIntDouble(_MapIntDouble, collections.MutableMapping): -# Map(UInt, Double) -cdef class MapIterUIntDouble(object): - cdef void init(self, cpp_map[extra_types.uint, double] * map_ptr): - cdef cpp_map[extra_types.uint, double].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(Int, Complex) +cdef class _MapIterIntComplex(object): + cdef void init(self, cpp_map[int, extra_types.complex_t] * map_ptr): + cdef cpp_map[int, extra_types.complex_t].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[extra_types.uint, double].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[int, extra_types.complex_t].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1529,10 +1561,11 @@ cdef class MapIterUIntDouble(object): return self def __next__(self): - cdef cpp_map[extra_types.uint, double].iterator inow = deref(self.iter_now) - cdef cpp_map[extra_types.uint, double].iterator iend = deref(self.iter_end) + cdef cpp_map[int, extra_types.complex_t].iterator inow = deref(self.iter_now) + cdef cpp_map[int, extra_types.complex_t].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -1540,25 +1573,31 @@ cdef class MapIterUIntDouble(object): inc(deref(self.iter_now)) return pyval -cdef class _MapUIntDouble: +cdef class _MapIntComplex: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[extra_types.uint, double] item + cdef pair[int, extra_types.complex_t] item + + # Decide how to init map, if at all - if isinstance(new_map, _MapUIntDouble): - self.map_ptr = (<_MapUIntDouble> new_map).map_ptr + if isinstance(new_map, _MapIntComplex): + self.map_ptr = (<_MapIntComplex> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[extra_types.uint, double]() + self.map_ptr = new cpp_map[int, extra_types.complex_t]() for key, value in new_map.items(): - item = pair[extra_types.uint, double]( long(key), value) + + + item = pair[int, extra_types.complex_t](key, extra_types.py2c_complex(value)) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[extra_types.uint, double]() + self.map_ptr = new cpp_map[int, extra_types.complex_t]() for key, value in new_map: - item = pair[extra_types.uint, double]( long(key), value) + + + item = pair[int, extra_types.complex_t](key, extra_types.py2c_complex(value)) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[extra_types.uint, double]() + self.map_ptr = new cpp_map[int, extra_types.complex_t]() # Store free_map self._free_map = free_map @@ -1568,10 +1607,12 @@ cdef class _MapUIntDouble: del self.map_ptr def __contains__(self, key): - cdef extra_types.uint k + cdef int k + if not isinstance(key, int) and not isinstance(key, long): return False - k = long(key) + + k = key if 0 < self.map_ptr.count(k): return True @@ -1582,37 +1623,47 @@ cdef class _MapUIntDouble: return self.map_ptr.size() def __iter__(self): - cdef MapIterUIntDouble mi = MapIterUIntDouble() + cdef _MapIterIntComplex mi = _MapIterIntComplex() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef extra_types.uint k - cdef double v + cdef int k + cdef extra_types.complex_t v + if not isinstance(key, int) and not isinstance(key, long): - raise TypeError("Only unsigned integer keys are valid.") - k = long(key) + raise TypeError("Only integer keys are valid.") + + k = key if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return float(v) + + return complex(float(deref(self.map_ptr)[k].re), float(deref(self.map_ptr)[k].im)) else: raise KeyError def __setitem__(self, key, value): - cdef pair[extra_types.uint, double] item = pair[extra_types.uint, double]( long(key), value) + + + cdef pair[int, extra_types.complex_t] item + + + item = pair[int, extra_types.complex_t](key, extra_types.py2c_complex(value)) self.map_ptr.insert(item) def __delitem__(self, key): - cdef extra_types.uint k + cdef int k + if key in self: - k = long(key) + + k = key self.map_ptr.erase(k) -class MapUIntDouble(_MapUIntDouble, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapIntComplex(_MapIntComplex, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters @@ -1633,14 +1684,14 @@ class MapUIntDouble(_MapUIntDouble, collections.MutableMapping): -# Map(Int, Complex) -cdef class MapIterIntComplex(object): - cdef void init(self, cpp_map[int, extra_types.complex_t] * map_ptr): - cdef cpp_map[int, extra_types.complex_t].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(UInt, Double) +cdef class _MapIterUIntDouble(object): + cdef void init(self, cpp_map[extra_types.uint, double] * map_ptr): + cdef cpp_map[extra_types.uint, double].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[int, extra_types.complex_t].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[extra_types.uint, double].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1652,10 +1703,11 @@ cdef class MapIterIntComplex(object): return self def __next__(self): - cdef cpp_map[int, extra_types.complex_t].iterator inow = deref(self.iter_now) - cdef cpp_map[int, extra_types.complex_t].iterator iend = deref(self.iter_end) + cdef cpp_map[extra_types.uint, double].iterator inow = deref(self.iter_now) + cdef cpp_map[extra_types.uint, double].iterator iend = deref(self.iter_end) if inow != iend: + pyval = int(deref(inow).first) else: raise StopIteration @@ -1663,25 +1715,31 @@ cdef class MapIterIntComplex(object): inc(deref(self.iter_now)) return pyval -cdef class _MapIntComplex: +cdef class _MapUIntDouble: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[int, extra_types.complex_t] item + cdef pair[extra_types.uint, double] item + + # Decide how to init map, if at all - if isinstance(new_map, _MapIntComplex): - self.map_ptr = (<_MapIntComplex> new_map).map_ptr + if isinstance(new_map, _MapUIntDouble): + self.map_ptr = (<_MapUIntDouble> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[int, extra_types.complex_t]() + self.map_ptr = new cpp_map[extra_types.uint, double]() for key, value in new_map.items(): - item = pair[int, extra_types.complex_t](key, py2c_complex(value)) + + + item = pair[extra_types.uint, double]( long(key), value) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[int, extra_types.complex_t]() + self.map_ptr = new cpp_map[extra_types.uint, double]() for key, value in new_map: - item = pair[int, extra_types.complex_t](key, py2c_complex(value)) + + + item = pair[extra_types.uint, double]( long(key), value) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[int, extra_types.complex_t]() + self.map_ptr = new cpp_map[extra_types.uint, double]() # Store free_map self._free_map = free_map @@ -1691,10 +1749,12 @@ cdef class _MapIntComplex: del self.map_ptr def __contains__(self, key): - cdef int k - if not isinstance(key, int): + cdef extra_types.uint k + + if not isinstance(key, int) and not isinstance(key, long): return False - k = key + + k = long(key) if 0 < self.map_ptr.count(k): return True @@ -1705,37 +1765,47 @@ cdef class _MapIntComplex: return self.map_ptr.size() def __iter__(self): - cdef MapIterIntComplex mi = MapIterIntComplex() + cdef _MapIterUIntDouble mi = _MapIterUIntDouble() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef int k - cdef extra_types.complex_t v + cdef extra_types.uint k + cdef double v - if not isinstance(key, int): - raise TypeError("Only integer keys are valid.") - k = key + + if not isinstance(key, int) and not isinstance(key, long): + raise TypeError("Only unsigned integer keys are valid.") + + k = long(key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return complex(float(v.re), float(v.im)) + + return float(deref(self.map_ptr)[k]) else: raise KeyError def __setitem__(self, key, value): - cdef pair[int, extra_types.complex_t] item = pair[int, extra_types.complex_t](key, py2c_complex(value)) + + + cdef pair[extra_types.uint, double] item + + + item = pair[extra_types.uint, double]( long(key), value) self.map_ptr.insert(item) def __delitem__(self, key): - cdef int k + cdef extra_types.uint k + if key in self: - k = key + + k = long(key) self.map_ptr.erase(k) -class MapIntComplex(_MapIntComplex, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapUIntDouble(_MapUIntDouble, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters @@ -1756,14 +1826,14 @@ class MapIntComplex(_MapIntComplex, collections.MutableMapping): -# Map(Int, VectorDouble) -cdef class MapIterIntVectorDouble(object): - cdef void init(self, cpp_map[int, cpp_vector[double]] * map_ptr): - cdef cpp_map[int, cpp_vector[double]].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(Str, VectorDouble) +cdef class _MapIterStrVectorDouble(object): + cdef void init(self, cpp_map[std_string, cpp_vector[double]] * map_ptr): + cdef cpp_map[std_string, cpp_vector[double]].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[int, cpp_vector[double]].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[std_string, cpp_vector[double]].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1775,36 +1845,64 @@ cdef class MapIterIntVectorDouble(object): return self def __next__(self): - cdef cpp_map[int, cpp_vector[double]].iterator inow = deref(self.iter_now) - cdef cpp_map[int, cpp_vector[double]].iterator iend = deref(self.iter_end) + cdef cpp_map[std_string, cpp_vector[double]].iterator inow = deref(self.iter_now) + cdef cpp_map[std_string, cpp_vector[double]].iterator iend = deref(self.iter_end) if inow != iend: - pyval = int(deref(inow).first) + + pyval = str( deref(inow).first.c_str()) else: raise StopIteration inc(deref(self.iter_now)) return pyval -cdef class _MapIntVectorDouble: +cdef class _MapStrVectorDouble: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[int, cpp_vector[double]] item + cdef pair[std_string, cpp_vector[double]] item + + cdef cpp_vector[double] value_proxy + cdef int i + cdef int value_size + cdef double * value_data # Decide how to init map, if at all - if isinstance(new_map, _MapIntVectorDouble): - self.map_ptr = (<_MapIntVectorDouble> new_map).map_ptr + if isinstance(new_map, _MapStrVectorDouble): + self.map_ptr = (<_MapStrVectorDouble> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[int, cpp_vector[double]]() + self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() for key, value in new_map.items(): - item = pair[int, cpp_vector[double]](key, py2c_vector_dbl(value)) + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[std_string, cpp_vector[double]](std_string( key), value_proxy) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[int, cpp_vector[double]]() + self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() for key, value in new_map: - item = pair[int, cpp_vector[double]](key, py2c_vector_dbl(value)) + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[std_string, cpp_vector[double]](std_string( key), value_proxy) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[int, cpp_vector[double]]() + self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() # Store free_map self._free_map = free_map @@ -1814,10 +1912,12 @@ cdef class _MapIntVectorDouble: del self.map_ptr def __contains__(self, key): - cdef int k - if not isinstance(key, int): + cdef std_string k + + if not isinstance(key, basestring): return False - k = key + + k = std_string( key) if 0 < self.map_ptr.count(k): return True @@ -1828,37 +1928,61 @@ cdef class _MapIntVectorDouble: return self.map_ptr.size() def __iter__(self): - cdef MapIterIntVectorDouble mi = MapIterIntVectorDouble() + cdef _MapIterStrVectorDouble mi = _MapIterStrVectorDouble() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef int k + cdef std_string k cdef cpp_vector[double] v - if not isinstance(key, int): - raise TypeError("Only integer keys are valid.") - k = key + cdef np.ndarray v_proxy + cdef np.npy_intp v_proxy_shape[1] + if not isinstance(key, basestring): + raise TypeError("Only string keys are valid.") + + k = std_string( key) if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return c2py_vector_dbl(&v) + v_proxy_shape[0] = deref(self.map_ptr)[k].size() + v_proxy = np.PyArray_SimpleNewFromData(1, v_proxy_shape, np.NPY_FLOAT64, &deref(self.map_ptr)[k][0]) + return v_proxy else: raise KeyError def __setitem__(self, key, value): - cdef pair[int, cpp_vector[double]] item = pair[int, cpp_vector[double]](key, py2c_vector_dbl(value)) + + cdef cpp_vector[double] value_proxy + cdef int i + cdef int value_size + cdef double * value_data + cdef pair[std_string, cpp_vector[double]] item + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[std_string, cpp_vector[double]](std_string( key), value_proxy) self.map_ptr.insert(item) def __delitem__(self, key): - cdef int k + cdef std_string k + if key in self: - k = key + + k = std_string( key) self.map_ptr.erase(k) -class MapIntVectorDouble(_MapIntVectorDouble, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapStrVectorDouble(_MapStrVectorDouble, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters @@ -1879,14 +2003,14 @@ class MapIntVectorDouble(_MapIntVectorDouble, collections.MutableMapping): -# Map(Str, VectorDouble) -cdef class MapIterStrVectorDouble(object): - cdef void init(self, cpp_map[std_string, cpp_vector[double]] * map_ptr): - cdef cpp_map[std_string, cpp_vector[double]].iterator * itn = malloc(sizeof(map_ptr.begin())) +# Map(Int, VectorDouble) +cdef class _MapIterIntVectorDouble(object): + cdef void init(self, cpp_map[int, cpp_vector[double]] * map_ptr): + cdef cpp_map[int, cpp_vector[double]].iterator * itn = malloc(sizeof(map_ptr.begin())) itn[0] = map_ptr.begin() self.iter_now = itn - cdef cpp_map[std_string, cpp_vector[double]].iterator * ite = malloc(sizeof(map_ptr.end())) + cdef cpp_map[int, cpp_vector[double]].iterator * ite = malloc(sizeof(map_ptr.end())) ite[0] = map_ptr.end() self.iter_end = ite @@ -1898,36 +2022,64 @@ cdef class MapIterStrVectorDouble(object): return self def __next__(self): - cdef cpp_map[std_string, cpp_vector[double]].iterator inow = deref(self.iter_now) - cdef cpp_map[std_string, cpp_vector[double]].iterator iend = deref(self.iter_end) + cdef cpp_map[int, cpp_vector[double]].iterator inow = deref(self.iter_now) + cdef cpp_map[int, cpp_vector[double]].iterator iend = deref(self.iter_end) if inow != iend: - pyval = str( deref(inow).first.c_str()) + + pyval = int(deref(inow).first) else: raise StopIteration inc(deref(self.iter_now)) return pyval -cdef class _MapStrVectorDouble: +cdef class _MapIntVectorDouble: def __cinit__(self, new_map=True, bint free_map=True): - cdef pair[std_string, cpp_vector[double]] item + cdef pair[int, cpp_vector[double]] item + + cdef cpp_vector[double] value_proxy + cdef int i + cdef int value_size + cdef double * value_data # Decide how to init map, if at all - if isinstance(new_map, _MapStrVectorDouble): - self.map_ptr = (<_MapStrVectorDouble> new_map).map_ptr + if isinstance(new_map, _MapIntVectorDouble): + self.map_ptr = (<_MapIntVectorDouble> new_map).map_ptr elif hasattr(new_map, 'items'): - self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() + self.map_ptr = new cpp_map[int, cpp_vector[double]]() for key, value in new_map.items(): - item = pair[std_string, cpp_vector[double]](std_string( key), py2c_vector_dbl(value)) + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[int, cpp_vector[double]](key, value_proxy) self.map_ptr.insert(item) elif hasattr(new_map, '__len__'): - self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() + self.map_ptr = new cpp_map[int, cpp_vector[double]]() for key, value in new_map: - item = pair[std_string, cpp_vector[double]](std_string( key), py2c_vector_dbl(value)) + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[int, cpp_vector[double]](key, value_proxy) self.map_ptr.insert(item) elif bool(new_map): - self.map_ptr = new cpp_map[std_string, cpp_vector[double]]() + self.map_ptr = new cpp_map[int, cpp_vector[double]]() # Store free_map self._free_map = free_map @@ -1937,10 +2089,12 @@ cdef class _MapStrVectorDouble: del self.map_ptr def __contains__(self, key): - cdef std_string k - if not isinstance(key, basestring): + cdef int k + + if not isinstance(key, int) and not isinstance(key, long): return False - k = std_string( key) + + k = key if 0 < self.map_ptr.count(k): return True @@ -1951,37 +2105,61 @@ cdef class _MapStrVectorDouble: return self.map_ptr.size() def __iter__(self): - cdef MapIterStrVectorDouble mi = MapIterStrVectorDouble() + cdef _MapIterIntVectorDouble mi = _MapIterIntVectorDouble() mi.init(self.map_ptr) return mi def __getitem__(self, key): - cdef std_string k + cdef int k cdef cpp_vector[double] v - if not isinstance(key, basestring): - raise TypeError("Only string keys are valid.") - k = std_string( key) + cdef np.ndarray v_proxy + cdef np.npy_intp v_proxy_shape[1] + if not isinstance(key, int) and not isinstance(key, long): + raise TypeError("Only integer keys are valid.") + + k = key if 0 < self.map_ptr.count(k): v = deref(self.map_ptr)[k] - return c2py_vector_dbl(&v) + v_proxy_shape[0] = deref(self.map_ptr)[k].size() + v_proxy = np.PyArray_SimpleNewFromData(1, v_proxy_shape, np.NPY_FLOAT64, &deref(self.map_ptr)[k][0]) + return v_proxy else: raise KeyError def __setitem__(self, key, value): - cdef pair[std_string, cpp_vector[double]] item = pair[std_string, cpp_vector[double]](std_string( key), py2c_vector_dbl(value)) + + cdef cpp_vector[double] value_proxy + cdef int i + cdef int value_size + cdef double * value_data + cdef pair[int, cpp_vector[double]] item + + value_size = len(value) + if isinstance(value, np.ndarray) and ( value).descr.type_num == np.NPY_FLOAT64: + value_data = np.PyArray_DATA( value) + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value_data[i] + else: + value_proxy = cpp_vector[double]( value_size) + for i in range(value_size): + value_proxy[i] = value[i] + item = pair[int, cpp_vector[double]](key, value_proxy) self.map_ptr.insert(item) def __delitem__(self, key): - cdef std_string k + cdef int k + if key in self: - k = std_string( key) + + k = key self.map_ptr.erase(k) -class MapStrVectorDouble(_MapStrVectorDouble, collections.MutableMapping): - """Wrapper class for C++ standard library maps of type . +class MapIntVectorDouble(_MapIntVectorDouble, collections.MutableMapping): + """Wrapper class for C++ standard library maps of type . Provides dictionary like interface on the Python level. Parameters diff --git a/pyne/tests/test_stlcontainers.py b/pyne/tests/test_stlcontainers.py new file mode 100644 index 0000000000..bd7e7e2fae --- /dev/null +++ b/pyne/tests/test_stlcontainers.py @@ -0,0 +1,762 @@ +"""Tests the part of stlconverters that is accessible from Python.""" +################### +### WARNING!!! ### +################### +# This file has been autogenerated + +from unittest import TestCase +import nose + +from nose.tools import assert_equal, assert_not_equal, assert_raises, raises, \ + assert_almost_equal, assert_true, assert_false, assert_in + +from numpy.testing import assert_array_equal, assert_array_almost_equal + +import os +import numpy as np +from collections import Container, Mapping + +from pyne import stlcontainers + + +# SetStr +def test_set_str(): + s = stlcontainers.SetStr() + s.add('Aha') + assert_true('Aha' in s) + assert_true('Me' not in s) + + s = stlcontainers.SetStr(['Aha', 'Take', 'Me']) + assert_true('Take' in s) + assert_true('On' not in s) + + + +# SetInt +def test_set_int(): + s = stlcontainers.SetInt() + s.add(1) + assert_true(1 in s) + assert_true(-65 not in s) + + s = stlcontainers.SetInt([1, 42, -65]) + assert_true(42 in s) + assert_true(18 not in s) + + + +# MapStrStr +def test_map_str_str(): + m = stlcontainers.MapStrStr() + uismap = isinstance('Me', Mapping) + m['Aha'] = 'On' + m['Take'] = 'Me' + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Take'].items(): + print key, value, 'Me'[key] + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m['Take'], 'Me') + + m = stlcontainers.MapStrStr({'Me': 'Take', 'On': 'Aha'}) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + print key, value, 'Take'[key] + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m['Me'], 'Take') + + n = stlcontainers.MapStrStr(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m['Me'], 'Take') + + # points to the same underlying map + n['Take'] = 'Me' + if uismap: + for key, value in m['Take'].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m['Take'], 'Me') + + + +# MapStrInt +def test_map_str_int(): + m = stlcontainers.MapStrInt() + uismap = isinstance(-65, Mapping) + m['Aha'] = 18 + m['Take'] = -65 + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Take'].items(): + print key, value, -65[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65[key]) + else: + assert_equal(value, -65[key]) + else: + assert_almost_equal(m['Take'], -65) + + m = stlcontainers.MapStrInt({'Me': 42, 'On': 1}) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + print key, value, 42[key] + assert_almost_equal(value, 42[key]) + else: + assert_equal(value, 42[key]) + else: + assert_almost_equal(m['Me'], 42) + + n = stlcontainers.MapStrInt(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 42[key]) + else: + assert_equal(value, 42[key]) + else: + assert_almost_equal(m['Me'], 42) + + # points to the same underlying map + n['Take'] = -65 + if uismap: + for key, value in m['Take'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65[key]) + else: + assert_equal(value, -65[key]) + else: + assert_almost_equal(m['Take'], -65) + + + +# MapIntStr +def test_map_int_str(): + m = stlcontainers.MapIntStr() + uismap = isinstance('Me', Mapping) + m[1] = 'On' + m[42] = 'Me' + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[42].items(): + print key, value, 'Me'[key] + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m[42], 'Me') + + m = stlcontainers.MapIntStr({-65: 'Take', 18: 'Aha'}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + print key, value, 'Take'[key] + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m[-65], 'Take') + + n = stlcontainers.MapIntStr(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m[-65], 'Take') + + # points to the same underlying map + n[42] = 'Me' + if uismap: + for key, value in m[42].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m[42], 'Me') + + + +# MapStrUInt +def test_map_str_uint(): + m = stlcontainers.MapStrUInt() + uismap = isinstance(4043370667L, Mapping) + m['Aha'] = 42L + m['Take'] = 4043370667L + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Take'].items(): + print key, value, 4043370667L[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, 4043370667L[key]) + else: + assert_equal(value, 4043370667L[key]) + else: + assert_almost_equal(m['Take'], 4043370667L) + + m = stlcontainers.MapStrUInt({'Me': 65, 'On': 1}) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + print key, value, 65[key] + assert_almost_equal(value, 65[key]) + else: + assert_equal(value, 65[key]) + else: + assert_almost_equal(m['Me'], 65) + + n = stlcontainers.MapStrUInt(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 65[key]) + else: + assert_equal(value, 65[key]) + else: + assert_almost_equal(m['Me'], 65) + + # points to the same underlying map + n['Take'] = 4043370667L + if uismap: + for key, value in m['Take'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 4043370667L[key]) + else: + assert_equal(value, 4043370667L[key]) + else: + assert_almost_equal(m['Take'], 4043370667L) + + + +# MapUIntStr +def test_map_uint_str(): + m = stlcontainers.MapUIntStr() + uismap = isinstance('Me', Mapping) + m[1] = 'On' + m[65] = 'Me' + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[65].items(): + print key, value, 'Me'[key] + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m[65], 'Me') + + m = stlcontainers.MapUIntStr({4043370667L: 'Take', 42L: 'Aha'}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + print key, value, 'Take'[key] + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m[4043370667L], 'Take') + + n = stlcontainers.MapUIntStr(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Take'[key]) + else: + assert_equal(value, 'Take'[key]) + else: + assert_equal(m[4043370667L], 'Take') + + # points to the same underlying map + n[65] = 'Me' + if uismap: + for key, value in m[65].items(): + if isinstance(value, np.ndarray): + assert_equal(value, 'Me'[key]) + else: + assert_equal(value, 'Me'[key]) + else: + assert_equal(m[65], 'Me') + + + +# MapStrDouble +def test_map_str_double(): + m = stlcontainers.MapStrDouble() + uismap = isinstance(-65.5555, Mapping) + m['Aha'] = 18 + m['Take'] = -65.5555 + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Take'].items(): + print key, value, -65.5555[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m['Take'], -65.5555) + + m = stlcontainers.MapStrDouble({'Me': 42.42, 'On': 1.0}) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + print key, value, 42.42[key] + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m['Me'], 42.42) + + n = stlcontainers.MapStrDouble(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m['Me'], 42.42) + + # points to the same underlying map + n['Take'] = -65.5555 + if uismap: + for key, value in m['Take'].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m['Take'], -65.5555) + + + +# MapUIntUInt +def test_map_uint_uint(): + m = stlcontainers.MapUIntUInt() + uismap = isinstance(4043370667L, Mapping) + m[1] = 42L + m[65] = 4043370667L + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[65].items(): + print key, value, 4043370667L[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, 4043370667L[key]) + else: + assert_equal(value, 4043370667L[key]) + else: + assert_almost_equal(m[65], 4043370667L) + + m = stlcontainers.MapUIntUInt({4043370667L: 65, 42L: 1}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + print key, value, 65[key] + assert_almost_equal(value, 65[key]) + else: + assert_equal(value, 65[key]) + else: + assert_almost_equal(m[4043370667L], 65) + + n = stlcontainers.MapUIntUInt(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 65[key]) + else: + assert_equal(value, 65[key]) + else: + assert_almost_equal(m[4043370667L], 65) + + # points to the same underlying map + n[65] = 4043370667L + if uismap: + for key, value in m[65].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 4043370667L[key]) + else: + assert_equal(value, 4043370667L[key]) + else: + assert_almost_equal(m[65], 4043370667L) + + + +# MapIntInt +def test_map_int_int(): + m = stlcontainers.MapIntInt() + uismap = isinstance(-65, Mapping) + m[1] = 18 + m[42] = -65 + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[42].items(): + print key, value, -65[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65[key]) + else: + assert_equal(value, -65[key]) + else: + assert_almost_equal(m[42], -65) + + m = stlcontainers.MapIntInt({-65: 42, 18: 1}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + print key, value, 42[key] + assert_almost_equal(value, 42[key]) + else: + assert_equal(value, 42[key]) + else: + assert_almost_equal(m[-65], 42) + + n = stlcontainers.MapIntInt(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 42[key]) + else: + assert_equal(value, 42[key]) + else: + assert_almost_equal(m[-65], 42) + + # points to the same underlying map + n[42] = -65 + if uismap: + for key, value in m[42].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65[key]) + else: + assert_equal(value, -65[key]) + else: + assert_almost_equal(m[42], -65) + + + +# MapIntDouble +def test_map_int_double(): + m = stlcontainers.MapIntDouble() + uismap = isinstance(-65.5555, Mapping) + m[1] = 18 + m[42] = -65.5555 + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[42].items(): + print key, value, -65.5555[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m[42], -65.5555) + + m = stlcontainers.MapIntDouble({-65: 42.42, 18: 1.0}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + print key, value, 42.42[key] + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m[-65], 42.42) + + n = stlcontainers.MapIntDouble(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m[-65], 42.42) + + # points to the same underlying map + n[42] = -65.5555 + if uismap: + for key, value in m[42].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m[42], -65.5555) + + + +# MapIntComplex +def test_map_int_complex(): + m = stlcontainers.MapIntComplex() + uismap = isinstance((-65.55-1j), Mapping) + m[1] = 0.18j + m[42] = (-65.55-1j) + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[42].items(): + print key, value, (-65.55-1j)[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, (-65.55-1j)[key]) + else: + assert_equal(value, (-65.55-1j)[key]) + else: + assert_almost_equal(m[42], (-65.55-1j)) + + m = stlcontainers.MapIntComplex({-65: (42+42j), 18: 1.0}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + print key, value, (42+42j)[key] + assert_almost_equal(value, (42+42j)[key]) + else: + assert_equal(value, (42+42j)[key]) + else: + assert_almost_equal(m[-65], (42+42j)) + + n = stlcontainers.MapIntComplex(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, (42+42j)[key]) + else: + assert_equal(value, (42+42j)[key]) + else: + assert_almost_equal(m[-65], (42+42j)) + + # points to the same underlying map + n[42] = (-65.55-1j) + if uismap: + for key, value in m[42].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, (-65.55-1j)[key]) + else: + assert_equal(value, (-65.55-1j)[key]) + else: + assert_almost_equal(m[42], (-65.55-1j)) + + + +# MapUIntDouble +def test_map_uint_double(): + m = stlcontainers.MapUIntDouble() + uismap = isinstance(-65.5555, Mapping) + m[1] = 18 + m[65] = -65.5555 + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[65].items(): + print key, value, -65.5555[key] + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m[65], -65.5555) + + m = stlcontainers.MapUIntDouble({4043370667L: 42.42, 42L: 1.0}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + print key, value, 42.42[key] + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m[4043370667L], 42.42) + + n = stlcontainers.MapUIntDouble(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[4043370667L].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, 42.42[key]) + else: + assert_equal(value, 42.42[key]) + else: + assert_almost_equal(m[4043370667L], 42.42) + + # points to the same underlying map + n[65] = -65.5555 + if uismap: + for key, value in m[65].items(): + if isinstance(value, np.ndarray): + assert_almost_equal(value, -65.5555[key]) + else: + assert_equal(value, -65.5555[key]) + else: + assert_almost_equal(m[65], -65.5555) + + + +# MapStrVectorDouble +def test_map_str_vector_double(): + m = stlcontainers.MapStrVectorDouble() + uismap = isinstance([1.0, -65.5555, 1.0, -65.5555], Mapping) + m['Aha'] = [42.42, 18, 42.42, 18] + m['Take'] = [1.0, -65.5555, 1.0, -65.5555] + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Take'].items(): + print key, value, [1.0, -65.5555, 1.0, -65.5555][key] + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_array_almost_equal(m['Take'], [1.0, -65.5555, 1.0, -65.5555]) + + m = stlcontainers.MapStrVectorDouble({'Me': [18, -65.5555, 42.42, 1.0], 'On': [1.0, 42.42, -65.5555, 18]}) + assert_equal(len(m), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + print key, value, [18, -65.5555, 42.42, 1.0][key] + assert_array_almost_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_array_almost_equal(m['Me'], [18, -65.5555, 42.42, 1.0]) + + n = stlcontainers.MapStrVectorDouble(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m['Me'].items(): + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_array_almost_equal(m['Me'], [18, -65.5555, 42.42, 1.0]) + + # points to the same underlying map + n['Take'] = [1.0, -65.5555, 1.0, -65.5555] + if uismap: + for key, value in m['Take'].items(): + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_array_almost_equal(m['Take'], [1.0, -65.5555, 1.0, -65.5555]) + + + +# MapIntVectorDouble +def test_map_int_vector_double(): + m = stlcontainers.MapIntVectorDouble() + uismap = isinstance([1.0, -65.5555, 1.0, -65.5555], Mapping) + m[1] = [42.42, 18, 42.42, 18] + m[42] = [1.0, -65.5555, 1.0, -65.5555] + import pprint + pprint.pprint(m) + assert_equal(len(m), 2) + if uismap: + for key, value in m[42].items(): + print key, value, [1.0, -65.5555, 1.0, -65.5555][key] + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_array_almost_equal(m[42], [1.0, -65.5555, 1.0, -65.5555]) + + m = stlcontainers.MapIntVectorDouble({-65: [18, -65.5555, 42.42, 1.0], 18: [1.0, 42.42, -65.5555, 18]}) + assert_equal(len(m), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + print key, value, [18, -65.5555, 42.42, 1.0][key] + assert_array_almost_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_array_almost_equal(m[-65], [18, -65.5555, 42.42, 1.0]) + + n = stlcontainers.MapIntVectorDouble(m, False) + assert_equal(len(n), 2) + if uismap: + for key, value in m[-65].items(): + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_equal(value, [18, -65.5555, 42.42, 1.0][key]) + else: + assert_array_almost_equal(m[-65], [18, -65.5555, 42.42, 1.0]) + + # points to the same underlying map + n[42] = [1.0, -65.5555, 1.0, -65.5555] + if uismap: + for key, value in m[42].items(): + if isinstance(value, np.ndarray): + assert_array_almost_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_equal(value, [1.0, -65.5555, 1.0, -65.5555][key]) + else: + assert_array_almost_equal(m[42], [1.0, -65.5555, 1.0, -65.5555]) + + + diff --git a/pyne/tests/test_stlconverters.py b/pyne/tests/test_stlconverters.py deleted file mode 100644 index c7329b6164..0000000000 --- a/pyne/tests/test_stlconverters.py +++ /dev/null @@ -1,347 +0,0 @@ -"""Tests the part of stlconverters that is accessible from Python.""" -################### -### WARNING!!! ### -################### -# This file has been autogenerated - -from unittest import TestCase -import nose - -from nose.tools import assert_equal, assert_not_equal, assert_raises, raises, \ - assert_almost_equal, assert_true, assert_false, assert_in - -from numpy.testing import assert_array_equal, assert_array_almost_equal - -import os -import numpy as np -import tables as tb - -import pyne.stlconverters as conv - - - - - - - - - - - - - - - - -# SetInt -def test_set_int(): - s = conv.SetInt() - s.add(1) - assert_true(1 in s) - assert_true(-65 not in s) - - s = conv.SetInt([1, 42, -65]) - assert_true(42 in s) - assert_true(18 not in s) - - - -# SetStr -def test_set_str(): - s = conv.SetStr() - s.add('Aha') - assert_true('Aha' in s) - assert_true('Me' not in s) - - s = conv.SetStr(['Aha', 'Take', 'Me']) - assert_true('Take' in s) - assert_true('On' not in s) - - - -# MapStrStr -def test_map_str_str(): - m = conv.MapStrStr() - m['Aha'] = 'On' - m['Take'] = 'Me' - assert_equal(len(m), 2) - assert_equal(m['Take'], 'Me') - - m = conv.MapStrStr({'Me': 'Take', 'On': 'Aha'}) - assert_equal(len(m), 2) - assert_equal(m['Me'], 'Take') - - n = conv.MapStrStr(m, False) - assert_equal(len(n), 2) - assert_equal(n['Me'], 'Take') - - # points to the same underlying map - n['Take'] = 'Me' - assert_equal(m['Take'], 'Me') - - - -# MapStrInt -def test_map_str_int(): - m = conv.MapStrInt() - m['Aha'] = 18 - m['Take'] = -65 - assert_equal(len(m), 2) - assert_equal(m['Take'], -65) - - m = conv.MapStrInt({'Me': 42, 'On': 1}) - assert_equal(len(m), 2) - assert_equal(m['Me'], 42) - - n = conv.MapStrInt(m, False) - assert_equal(len(n), 2) - assert_equal(n['Me'], 42) - - # points to the same underlying map - n['Take'] = -65 - assert_equal(m['Take'], -65) - - - -# MapIntStr -def test_map_int_str(): - m = conv.MapIntStr() - m[1] = 'On' - m[42] = 'Me' - assert_equal(len(m), 2) - assert_equal(m[42], 'Me') - - m = conv.MapIntStr({-65: 'Take', 18: 'Aha'}) - assert_equal(len(m), 2) - assert_equal(m[-65], 'Take') - - n = conv.MapIntStr(m, False) - assert_equal(len(n), 2) - assert_equal(n[-65], 'Take') - - # points to the same underlying map - n[42] = 'Me' - assert_equal(m[42], 'Me') - - - -# MapStrUInt -def test_map_str_uint(): - m = conv.MapStrUInt() - m['Aha'] = 42L - m['Take'] = 4043370667L - assert_equal(len(m), 2) - assert_equal(m['Take'], 4043370667L) - - m = conv.MapStrUInt({'Me': 65, 'On': 1}) - assert_equal(len(m), 2) - assert_equal(m['Me'], 65) - - n = conv.MapStrUInt(m, False) - assert_equal(len(n), 2) - assert_equal(n['Me'], 65) - - # points to the same underlying map - n['Take'] = 4043370667L - assert_equal(m['Take'], 4043370667L) - - - -# MapUIntStr -def test_map_uint_str(): - m = conv.MapUIntStr() - m[1] = 'On' - m[65] = 'Me' - assert_equal(len(m), 2) - assert_equal(m[65], 'Me') - - m = conv.MapUIntStr({4043370667L: 'Take', 42L: 'Aha'}) - assert_equal(len(m), 2) - assert_equal(m[4043370667L], 'Take') - - n = conv.MapUIntStr(m, False) - assert_equal(len(n), 2) - assert_equal(n[4043370667L], 'Take') - - # points to the same underlying map - n[65] = 'Me' - assert_equal(m[65], 'Me') - - - -# MapUIntUInt -def test_map_uint_uint(): - m = conv.MapUIntUInt() - m[1] = 42L - m[65] = 4043370667L - assert_equal(len(m), 2) - assert_equal(m[65], 4043370667L) - - m = conv.MapUIntUInt({4043370667L: 65, 42L: 1}) - assert_equal(len(m), 2) - assert_equal(m[4043370667L], 65) - - n = conv.MapUIntUInt(m, False) - assert_equal(len(n), 2) - assert_equal(n[4043370667L], 65) - - # points to the same underlying map - n[65] = 4043370667L - assert_equal(m[65], 4043370667L) - - - -# MapStrDouble -def test_map_str_dbl(): - m = conv.MapStrDouble() - m['Aha'] = 18 - m['Take'] = -65.5555 - assert_equal(len(m), 2) - assert_equal(m['Take'], -65.5555) - - m = conv.MapStrDouble({'Me': 42.42, 'On': 1.0}) - assert_equal(len(m), 2) - assert_equal(m['Me'], 42.42) - - n = conv.MapStrDouble(m, False) - assert_equal(len(n), 2) - assert_equal(n['Me'], 42.42) - - # points to the same underlying map - n['Take'] = -65.5555 - assert_equal(m['Take'], -65.5555) - - - -# MapIntInt -def test_map_int_int(): - m = conv.MapIntInt() - m[1] = 18 - m[42] = -65 - assert_equal(len(m), 2) - assert_equal(m[42], -65) - - m = conv.MapIntInt({-65: 42, 18: 1}) - assert_equal(len(m), 2) - assert_equal(m[-65], 42) - - n = conv.MapIntInt(m, False) - assert_equal(len(n), 2) - assert_equal(n[-65], 42) - - # points to the same underlying map - n[42] = -65 - assert_equal(m[42], -65) - - - -# MapIntDouble -def test_map_int_dbl(): - m = conv.MapIntDouble() - m[1] = 18 - m[42] = -65.5555 - assert_equal(len(m), 2) - assert_equal(m[42], -65.5555) - - m = conv.MapIntDouble({-65: 42.42, 18: 1.0}) - assert_equal(len(m), 2) - assert_equal(m[-65], 42.42) - - n = conv.MapIntDouble(m, False) - assert_equal(len(n), 2) - assert_equal(n[-65], 42.42) - - # points to the same underlying map - n[42] = -65.5555 - assert_equal(m[42], -65.5555) - - - -# MapUIntDouble -def test_map_uint_dbl(): - m = conv.MapUIntDouble() - m[1] = 18 - m[65] = -65.5555 - assert_equal(len(m), 2) - assert_equal(m[65], -65.5555) - - m = conv.MapUIntDouble({4043370667L: 42.42, 42L: 1.0}) - assert_equal(len(m), 2) - assert_equal(m[4043370667L], 42.42) - - n = conv.MapUIntDouble(m, False) - assert_equal(len(n), 2) - assert_equal(n[4043370667L], 42.42) - - # points to the same underlying map - n[65] = -65.5555 - assert_equal(m[65], -65.5555) - - - -# MapIntComplex -def test_map_int_complex(): - m = conv.MapIntComplex() - m[1] = 0.18j - m[42] = (-65.55-1j) - assert_equal(len(m), 2) - assert_equal(m[42], (-65.55-1j)) - - m = conv.MapIntComplex({-65: (42+42j), 18: 1.0}) - assert_equal(len(m), 2) - assert_equal(m[-65], (42+42j)) - - n = conv.MapIntComplex(m, False) - assert_equal(len(n), 2) - assert_equal(n[-65], (42+42j)) - - # points to the same underlying map - n[42] = (-65.55-1j) - assert_equal(m[42], (-65.55-1j)) - - - -# MapIntVectorDouble -def test_map_int_vector_dbl(): - m = conv.MapIntVectorDouble() - m[1] = [0, 1, 2, 3, 4, 5] - m[42] = [1, 2] - assert_array_almost_equal(len(m), 2) - assert_array_almost_equal(m[42], [1, 2]) - - m = conv.MapIntVectorDouble({-65: (1,), 18: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}) - assert_array_almost_equal(len(m), 2) - assert_array_almost_equal(m[-65], (1,)) - - n = conv.MapIntVectorDouble(m, False) - assert_array_almost_equal(len(n), 2) - assert_array_almost_equal(n[-65], (1,)) - - # points to the same underlying map - n[42] = [1, 2] - assert_array_almost_equal(m[42], [1, 2]) - - - -# MapStrVectorDouble -def test_map_str_vector_dbl(): - m = conv.MapStrVectorDouble() - m['Aha'] = [0, 1, 2, 3, 4, 5] - m['Take'] = [1, 2] - assert_array_almost_equal(len(m), 2) - assert_array_almost_equal(m['Take'], [1, 2]) - - m = conv.MapStrVectorDouble({'Me': (1,), 'On': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}) - assert_array_almost_equal(len(m), 2) - assert_array_almost_equal(m['Me'], (1,)) - - n = conv.MapStrVectorDouble(m, False) - assert_array_almost_equal(len(n), 2) - assert_array_almost_equal(n['Me'], (1,)) - - # points to the same underlying map - n['Take'] = [1, 2] - assert_array_almost_equal(m['Take'], [1, 2]) - - - diff --git a/xdressrc.py b/xdressrc.py new file mode 100644 index 0000000000..113aa007fd --- /dev/null +++ b/xdressrc.py @@ -0,0 +1,29 @@ +package = 'pyne' +packagedir = 'pyne' +sourcedir = 'cpp' + +extra_types = 'extra_types' # non-default value + +stlcontainers = [ + ('set', 'str'), + ('set', 'int32'), + ('map', 'str', 'str'), + ('map', 'str', 'int32'), + ('map', 'int32', 'str'), + ('map', 'str', 'uint32'), + ('map', 'uint32', 'str'), + ('map', 'str', 'float64'), + ('map', 'uint32', 'uint32'), + ('map', 'int32', 'int32'), + ('map', 'int32', 'float64'), + ('map', 'int32', 'complex'), + ('map', 'uint32', 'float64'), + ('map', 'str', ('vector', 'float64')), + ('map', 'int32', ('vector', 'float64')), + ] + +#stlcontainers_module = 'stlcontainers' + +classes = [] + +functions = []