ext/nmatrix/nmatrix.h in nmatrix-0.0.3 vs ext/nmatrix/nmatrix.h in nmatrix-0.0.4
- old
+ new
@@ -7,12 +7,12 @@
// NMatrix was originally inspired by and derived from NArray, by
// Masahiro Tanaka: http://narray.rubyforge.org
//
// == Copyright Information
//
-// SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
-// NMatrix is Copyright (c) 2012, Ruby Science Foundation
+// SciRuby is Copyright (c) 2010 - 2013, Ruby Science Foundation
+// NMatrix is Copyright (c) 2013, Ruby Science Foundation
//
// Please see LICENSE.txt for additional copyright notices.
//
// == Contributing
//
@@ -40,12 +40,10 @@
#else
#include <math.h>
#include <string.h>
#endif
-
-
#ifdef BENCHMARK
// SOURCE: http://stackoverflow.com/questions/2349776/how-can-i-benchmark-a-c-program-easily
#ifdef __cplusplus
#include <sys/ctime>
#include <sys/cresource>
@@ -54,19 +52,13 @@
#include <sys/resource.h>
#endif
#endif
/*
- * Project Includes
- */
-
-
-/*
* Macros
*/
-
#define RUBY_ZERO INT2FIX(0)
#ifndef SIZEOF_INT
#error SIZEOF_INT undefined
#else
@@ -110,10 +102,37 @@
*/
#ifdef __cplusplus /* These are the C++ versions of the macros. */
+ /*
+ * If no block is given, return an enumerator. This copied straight out of ruby's include/ruby/intern.h.
+ *
+ * rb_enumeratorize is located in enumerator.c.
+ *
+ * VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) {
+ * return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv);
+ * }
+ */
+ #ifdef RUBY_2
+ #ifndef RETURN_SIZED_ENUMERATOR
+ #undef RETURN_SIZED_ENUMERATOR
+ // Ruby 2.0 and higher has rb_enumeratorize_with_size instead of rb_enumeratorize.
+ // We want to support both in the simplest way possible.
+ #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \
+ if (!rb_block_given_p()) \
+ return rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()), (argc), (argv), (size_fn)); \
+ } while (0)
+ #endif
+ #else
+ #undef RETURN_SIZED_ENUMERATOR
+ #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \
+ if (!rb_block_given_p()) \
+ return rb_enumeratorize((obj), ID2SYM(rb_frame_this_func()), (argc), (argv)); \
+ } while (0)
+ #endif
+
#define NM_DECL_ENUM(enum_type, name) nm::enum_type name
#define NM_DECL_STRUCT(type, name) type name;
#define NM_DEF_STORAGE_ELEMENTS \
NM_DECL_ENUM(dtype_t, dtype); \
@@ -142,11 +161,11 @@
} // end of namespace nm
#else /* These are the C versions of the macros. */
#define NM_DECL_ENUM(enum_type, name) nm_ ## enum_type name
- #define NM_DECL_STRUCT(type, name) NM_ ## type name;
+ #define NM_DECL_STRUCT(type, name) struct NM_ ## type name;
#define NM_DEF_STORAGE_ELEMENTS \
NM_DECL_ENUM(dtype_t, dtype); \
size_t dim; \
size_t* shape; \
@@ -311,30 +330,32 @@
#define NM_IsNVector(obj) \
(rb_obj_is_kind_of(obj, cNVector) == Qtrue)
+#ifdef __cplusplus
typedef VALUE (*METHOD)(...);
-#ifdef __cplusplus
//}; // end of namespace nm
#endif
/*
- * Data
- */
-
-/*
* Functions
*/
+#ifdef __cplusplus
extern "C" {
+#endif
+
void Init_nmatrix();
-
+
// External API
VALUE rb_nmatrix_dense_create(NM_DECL_ENUM(dtype_t, dtype), size_t* shape, size_t dim, void* elements, size_t length);
VALUE rb_nvector_dense_create(NM_DECL_ENUM(dtype_t, dtype), void* elements, size_t length);
NM_DECL_ENUM(dtype_t, nm_dtype_guess(VALUE)); // (This is a function)
+
+#ifdef __cplusplus
}
+#endif
#endif // NMATRIX_H