ext/nmatrix/storage/list/list.cpp in nmatrix-0.1.0.rc3 vs ext/nmatrix/storage/list/list.cpp in nmatrix-0.1.0.rc4

- old
+ new

@@ -181,19 +181,22 @@ if (!val->first) nm::list::del(val, 0); else { nm_list_storage_register_list(val, rec-1); temp_vals.push_front(val); nm::list::insert_helper(x, xcurr, curr->key - offset, val); - } + } curr = curr->next; if (curr && curr->key - offset >= x_shape) curr = NULL; } __nm_list_storage_unregister_temp_list_list(temp_vals, rec-1); } else { std::list<VALUE*> temp_vals; while (curr) { - VALUE val, s_val = rubyobj_from_cval(curr->val, s.dtype()).rval; + VALUE val, s_val; + if (s.dtype() == nm::RUBYOBJ) s_val = (*reinterpret_cast<nm::RubyObject*>(curr->val)).rval; + else s_val = rubyobj_from_cval(curr->val, s.dtype()).rval; + if (rev) val = rb_yield_values(2, t_init, s_val); else val = rb_yield_values(2, s_val, t_init); nm_register_value(val); @@ -260,11 +263,11 @@ std::list<VALUE*> temp_vals; while (lcurr) { size_t key; VALUE val; - val = rb_yield_values(1, rubyobj_from_cval(lcurr->val, left.dtype()).rval); + val = rb_yield_values(1, left.dtype() == nm::RUBYOBJ ? *reinterpret_cast<VALUE*>(lcurr->val) : rubyobj_from_cval(lcurr->val, left.dtype()).rval); key = lcurr->key - left.offset(rec); lcurr = lcurr->next; if (!rb_equal(val, result.init_obj())) { xcurr = nm::list::insert_helper(x, xcurr, key, val); @@ -533,11 +536,11 @@ template <typename D> void set(VALUE left, SLICE* slice, VALUE right) { NM_CONSERVATIVE(nm_register_value(left)); NM_CONSERVATIVE(nm_register_value(right)); LIST_STORAGE* s = NM_STORAGE_LIST(left); - + std::pair<NMATRIX*,bool> nm_and_free = interpret_arg_as_dense_nmatrix(right, NM_DTYPE(left)); // Map the data onto D* v. D* v; @@ -699,15 +702,15 @@ nm_list_storage_unregister_list(*it, recursions); } } void nm_list_storage_register_node(const NODE* curr) { - nm_register_value(*reinterpret_cast<VALUE*>(curr->val)); + nm_register_value(*reinterpret_cast<VALUE*>(curr->val)); } void nm_list_storage_unregister_node(const NODE* curr) { - nm_unregister_value(*reinterpret_cast<VALUE*>(curr->val)); + nm_unregister_value(*reinterpret_cast<VALUE*>(curr->val)); } /** * Gets rid of all instances of a given node in the registration list. * Sometimes a node will get deleted and replaced deep in a recursion, but @@ -773,17 +776,17 @@ /* * Documentation goes here. */ static NODE* list_storage_get_single_node(LIST_STORAGE* s, SLICE* slice) { - size_t r; - LIST* l = s->rows; - NODE* n; + LIST* l = s->rows; + NODE* n; - for (r = 0; r < s->dim; r++) { + for (size_t r = 0; r < s->dim; r++) { n = nm::list::find(l, s->offset[r] + slice->coords[r]); - if (n) l = reinterpret_cast<LIST*>(n->val); + + if (n) l = reinterpret_cast<LIST*>(n->val); else return NULL; } return n; } @@ -796,19 +799,19 @@ static void each_empty_with_indices_r(nm::list_storage::RecurseData& s, size_t rec, VALUE& stack) { VALUE empty = s.dtype() == nm::RUBYOBJ ? *reinterpret_cast<VALUE*>(s.init()) : s.init_obj(); NM_CONSERVATIVE(nm_register_value(stack)); if (rec) { - for (long index = 0; index < s.ref_shape(rec); ++index) { + for (unsigned long index = 0; index < s.ref_shape(rec); ++index) { // Don't do an unshift/shift here -- we'll let that be handled in the lowest-level iteration (recursions == 0) rb_ary_push(stack, LONG2NUM(index)); each_empty_with_indices_r(s, rec-1, stack); rb_ary_pop(stack); } } else { rb_ary_unshift(stack, empty); - for (long index = 0; index < s.ref_shape(rec); ++index) { + for (unsigned long index = 0; index < s.ref_shape(rec); ++index) { rb_ary_push(stack, LONG2NUM(index)); rb_yield_splat(stack); rb_ary_pop(stack); } rb_ary_shift(stack); @@ -831,22 +834,22 @@ while (curr && curr->key < offset) curr = curr->next; if (curr && curr->key - offset >= shape) curr = NULL; if (rec) { - for (long index = 0; index < shape; ++index) { // index in reference + for (unsigned long index = 0; index < shape; ++index) { // index in reference rb_ary_push(stack, LONG2NUM(index)); if (!curr || index < curr->key - offset) { each_empty_with_indices_r(s, rec-1, stack); } else { // index == curr->key - offset each_with_indices_r(s, reinterpret_cast<const LIST*>(curr->val), rec-1, stack); curr = curr->next; } rb_ary_pop(stack); } } else { - for (long index = 0; index < shape; ++index) { + for (unsigned long index = 0; index < shape; ++index) { rb_ary_push(stack, LONG2NUM(index)); if (!curr || index < curr->key - offset) { rb_ary_unshift(stack, s.dtype() == nm::RUBYOBJ ? *reinterpret_cast<VALUE*>(s.init()) : s.init_obj()); @@ -873,11 +876,11 @@ */ static void each_stored_with_indices_r(nm::list_storage::RecurseData& s, const LIST* l, size_t rec, VALUE& stack) { if (s.dtype() == nm::RUBYOBJ) nm_list_storage_register_list(l, rec); NM_CONSERVATIVE(nm_register_value(stack)); - + NODE* curr = l->first; size_t offset = s.offset(rec); size_t shape = s.ref_shape(rec); @@ -917,11 +920,10 @@ if (s.dtype() == nm::RUBYOBJ) nm_list_storage_unregister_list(l, rec); } - /* * Each/each-stored iterator, brings along the indices. */ VALUE nm_list_each_with_indices(VALUE nmatrix, bool stored) { @@ -943,25 +945,23 @@ return nmatrix; } /* - * map merged stored iterator. Always returns a matrix containing RubyObjects which probably needs to be casted. + * map merged stored iterator. Always returns a matrix containing RubyObjects + * which probably needs to be casted. */ VALUE nm_list_map_stored(VALUE left, VALUE init) { NM_CONSERVATIVE(nm_register_value(left)); NM_CONSERVATIVE(nm_register_value(init)); - bool scalar = false; + LIST_STORAGE *s = NM_STORAGE_LIST(left); - LIST_STORAGE *s = NM_STORAGE_LIST(left); - - // For each matrix, if it's a reference, we want to deal directly with the original (with appropriate offsetting) + // For each matrix, if it's a reference, we want to deal directly with the + // original (with appropriate offsetting) nm::list_storage::RecurseData sdata(s); - void* scalar_init = NULL; - //if (!rb_block_given_p()) { // rb_raise(rb_eNotImpError, "RETURN_SIZED_ENUMERATOR probably won't work for a map_merged since no merged object is created"); //} // If we don't have a block, return an enumerator. RETURN_SIZED_ENUMERATOR_PRE @@ -1075,18 +1075,18 @@ */ static LIST* slice_copy(const LIST_STORAGE* src, LIST* src_rows, size_t* coords, size_t* lengths, size_t n) { nm_list_storage_register(src); void *val = NULL; int key; - + LIST* dst_rows = nm::list::create(); NODE* src_node = src_rows->first; std::list<VALUE*> temp_vals; std::list<LIST*> temp_lists; while (src_node) { key = src_node->key - (src->offset[n] + coords[n]); - + if (key >= 0 && (size_t)key < lengths[n]) { if (src->dim - n > 1) { val = slice_copy( src, reinterpret_cast<LIST*>(src_node->val), coords, @@ -1128,26 +1128,27 @@ if (slice->single) { NODE* n = list_storage_get_single_node(s, slice); nm_list_storage_unregister(s); return (n ? n->val : s->default_val); - } else { void *init_val = NM_ALLOC_N(char, DTYPE_SIZES[s->dtype]); memcpy(init_val, s->default_val, DTYPE_SIZES[s->dtype]); if (s->dtype == nm::RUBYOBJ) nm_register_value(*reinterpret_cast<VALUE*>(init_val)); size_t *shape = NM_ALLOC_N(size_t, s->dim); memcpy(shape, slice->lengths, sizeof(size_t) * s->dim); ns = nm_list_storage_create(s->dtype, shape, s->dim, init_val); - + ns->rows = slice_copy(s, s->rows, slice->coords, slice->lengths, 0); - if (s->dtype == nm::RUBYOBJ) + if (s->dtype == nm::RUBYOBJ) { nm_unregister_value(*reinterpret_cast<VALUE*>(init_val)); + } + nm_list_storage_unregister(s); return ns; } } @@ -1164,27 +1165,26 @@ //TODO: It needs a refactoring. if (slice->single) { NODE* n = list_storage_get_single_node(s, slice); nm_list_storage_unregister(s); return (n ? n->val : s->default_val); - } - else { - ns = NM_ALLOC( LIST_STORAGE ); - - ns->dim = s->dim; - ns->dtype = s->dtype; - ns->offset = NM_ALLOC_N(size_t, ns->dim); - ns->shape = NM_ALLOC_N(size_t, ns->dim); + } else { + ns = NM_ALLOC( LIST_STORAGE ); + ns->dim = s->dim; + ns->dtype = s->dtype; + ns->offset = NM_ALLOC_N(size_t, ns->dim); + ns->shape = NM_ALLOC_N(size_t, ns->dim); + for (size_t i = 0; i < ns->dim; ++i) { ns->offset[i] = slice->coords[i] + s->offset[i]; ns->shape[i] = slice->lengths[i]; } ns->rows = s->rows; ns->default_val = s->default_val; - + s->src->count++; ns->src = s->src; nm_list_storage_unregister(s); return ns; } @@ -1202,17 +1202,18 @@ } // drill down into the structure NODE* node = NULL; if (dest->dim - n > 1) { - std::list<LIST*> temp_nodes; + std::list<LIST*> temp_nodes; for (size_t i = 0; i < lengths[n]; ++i) { size_t key = i + dest->offset[n] + coords[n]; if (!node) { - node = nm::list::insert(l, false, key, nm::list::create()); // try to insert list + // try to insert list + node = nm::list::insert(l, false, key, nm::list::create()); } else if (!node->next || (node->next && node->next->key > key)) { node = nm::list::insert_after(node, key, nm::list::create()); } else { node = node->next; // correct rank already exists. } @@ -1362,34 +1363,34 @@ * Recursively count the non-zero elements in a list storage object. */ size_t nm_list_storage_count_elements_r(const LIST* l, size_t recursions) { size_t count = 0; NODE* curr = l->first; - + if (recursions) { while (curr) { count += nm_list_storage_count_elements_r(reinterpret_cast<const LIST*>(curr->val), recursions - 1); curr = curr->next; } - + } else { while (curr) { ++count; curr = curr->next; } } - + return count; } /* * Count non-diagonal non-zero elements. */ size_t nm_list_storage_count_nd_elements(const LIST_STORAGE* s) { NODE *i_curr, *j_curr; size_t count = 0; - + if (s->dim != 2) { rb_raise(rb_eNotImpError, "non-diagonal element counting only defined for dim = 2"); } for (i_curr = s->rows->first; i_curr; i_curr = i_curr->next) { @@ -1401,11 +1402,11 @@ if (j < 0 || j >= (int)s->shape[1]) continue; if (i != j) ++count; } } - + return count; } ///////////////////////// // Copying and Casting // @@ -1417,11 +1418,11 @@ LIST_STORAGE* nm_list_storage_copy(const LIST_STORAGE* rhs) { nm_list_storage_register(rhs); size_t *shape = NM_ALLOC_N(size_t, rhs->dim); memcpy(shape, rhs->shape, sizeof(size_t) * rhs->dim); - + void *init_val = NM_ALLOC_N(char, DTYPE_SIZES[rhs->dtype]); memcpy(init_val, rhs->default_val, DTYPE_SIZES[rhs->dtype]); LIST_STORAGE* lhs = nm_list_storage_create(rhs->dtype, shape, rhs->dim, init_val); nm_list_storage_register(lhs); @@ -1481,10 +1482,10 @@ LIST_STORAGE* lhs = nm_list_storage_create(new_dtype, shape, rhs->dim, default_val); //lhs->rows = nm::list::create(); nm_list_storage_register(lhs); // TODO: Needs optimization. When matrix is reference it is copped twice. - if (rhs->src == rhs) + if (rhs->src == rhs) nm::list::cast_copy_contents<LDType, RDType>(lhs->rows, rhs->rows, rhs->dim - 1); else { LIST_STORAGE *tmp = nm_list_storage_copy(rhs); nm_list_storage_register(tmp); nm::list::cast_copy_contents<LDType, RDType>(lhs->rows, tmp->rows, rhs->dim - 1);