/** * Calculate distance between two iterators. */ int _slist_iterator_distance(slist_iterator_t it_first, slist_iterator_t it_second) { int n_distance = 0; _slistnode_t* pt_node = NULL; assert(_iterator_same_type(it_first, it_second)); assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second)); if (_slist_iterator_before(it_first, it_second)) { for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_first); pt_node != (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second); pt_node = pt_node->_pt_next) { n_distance++; } return n_distance; } else if (_slist_iterator_before(it_second, it_first)) { for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second); pt_node != (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_first); pt_node = pt_node->_pt_next) { n_distance++; } return -n_distance; } else { return 0; } }
/** * Get data value pointer referenced by iterator, but ignore char*. */ const void* _slist_iterator_get_pointer_ignore_cstr(slist_iterator_t it_iter) { assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter)); assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter)))); return ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data; }
/** * Transfer the range [it_begin, it_end) to position it_pos. */ void _slist_transfer(slist_iterator_t it_pos, slist_iterator_t it_begin, slist_iterator_t it_end) { assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_pos), it_pos)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_begin), it_begin)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_end), it_end)); assert(_SLIST_ITERATOR_CONTAINER(it_begin) == _SLIST_ITERATOR_CONTAINER(it_end)); assert(iterator_equal(it_begin, it_end) || _slist_iterator_before(it_begin, it_end)); assert(_slist_same_slist_iterator_type(_SLIST_ITERATOR_CONTAINER(it_pos), it_begin)); /* empty range */ if(iterator_equal(it_begin, it_end)) { return; } /* same slist container */ if(_SLIST_ITERATOR_CONTAINER(it_pos) == _SLIST_ITERATOR_CONTAINER(it_begin)) { assert(iterator_equal(it_pos, it_begin) || iterator_equal(it_pos, it_end) || _slist_iterator_before(it_pos, it_begin) || _slist_iterator_before(it_end, it_pos)); if(iterator_equal(it_pos, it_begin) || iterator_equal(it_pos, it_end)) { return; } } slist_insert_range(_SLIST_ITERATOR_CONTAINER(it_pos), it_pos, it_begin, it_end); slist_erase_range(_SLIST_ITERATOR_CONTAINER(it_begin), it_begin, it_end); }
/** * Return iterator reference next element. */ slist_iterator_t _slist_iterator_next(slist_iterator_t it_iter) { assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter)); assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter)))); _SLIST_ITERATOR_COREPOS(it_iter) = (_byte_t*)(((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pt_next); return it_iter; }
/** * Test the two slist iterator are equal. */ bool_t _slist_iterator_equal(slist_iterator_t it_first, slist_iterator_t it_second) { assert(_iterator_same_type(it_first, it_second)); assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second)); return _SLIST_ITERATOR_COREPOS(it_first) == _SLIST_ITERATOR_COREPOS(it_second) ? true : false; }
/** * Test the type that saved in the slist container and referenced by it_iter are same. */ bool_t _slist_same_slist_iterator_type(const slist_t* cpslist_slist, slist_iterator_t it_iter) { assert(cpslist_slist != NULL); assert(_SLIST_ITERATOR_CONTAINER(it_iter) != NULL); assert(_SLIST_ITERATOR_CONTAINER_TYPE(it_iter) == _SLIST_CONTAINER); assert(_SLIST_ITERATOR_ITERATOR_TYPE(it_iter) == _FORWARD_ITERATOR); return _slist_same_type(cpslist_slist, _SLIST_ITERATOR_CONTAINER(it_iter)); }
/** * Get data value pointer referenced by iterator. */ const void* _slist_iterator_get_pointer(slist_iterator_t it_iter) { assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter)); assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter)))); /* char* */ if (strncmp(_GET_SLIST_TYPE_BASENAME(_SLIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) { return string_c_str((string_t*)((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data); } else { return ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data; } }
/** * Set data value referenced by iterator. */ void _slist_iterator_set_value(slist_iterator_t it_iter, const void* cpv_value) { assert(cpv_value != NULL); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter)); assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter)))); /* char* */ if (strncmp(_GET_SLIST_TYPE_BASENAME(_SLIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) { string_assign_cstr((string_t*)((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data, (char*)cpv_value); } else { bool_t b_result = _GET_SLIST_TYPE_SIZE(_SLIST_ITERATOR_CONTAINER(it_iter)); _GET_SLIST_TYPE_COPY_FUNCTION(_SLIST_ITERATOR_CONTAINER(it_iter))( ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data, cpv_value, &b_result); assert(b_result); } }
/** * Test iterator referenced data is within the slist. */ bool_t _slist_iterator_belong_to_slist(const slist_t* cpslist_slist, slist_iterator_t it_iter) { _slistnode_t* pt_node = NULL; assert(cpslist_slist != NULL); assert(_slist_is_inited(cpslist_slist)); assert(_SLIST_ITERATOR_CONTAINER(it_iter) == cpslist_slist); assert(_SLIST_ITERATOR_CONTAINER_TYPE(it_iter) == _SLIST_CONTAINER); assert(_SLIST_ITERATOR_ITERATOR_TYPE(it_iter) == _FORWARD_ITERATOR); /* the end iterator of slist corepos is NULL */ if(_SLIST_ITERATOR_COREPOS(it_iter) == NULL) { return true; } for(pt_node = cpslist_slist->_t_head._pt_next; pt_node != NULL; pt_node = pt_node->_pt_next) { if(pt_node == (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter)) { return true; } } return false; }
/** * Test the first iterator is before the second. */ bool_t _slist_iterator_before(slist_iterator_t it_first, slist_iterator_t it_second) { _slistnode_t* pt_node = NULL; assert(_iterator_same_type(it_first, it_second)); assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first)); assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second)); if (_SLIST_ITERATOR_COREPOS(it_first) == _SLIST_ITERATOR_COREPOS(it_second)) { return false; } for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_first); pt_node != NULL; pt_node = pt_node->_pt_next) { if (pt_node == (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second)) { return true; } } return _SLIST_ITERATOR_COREPOS(it_second) == NULL ? true : false; }