void string_truncate( string* self, size_t n ) { assert_invariants( self ); assert( n <= self->capacity ); self->value[self->size = n] = 0; assert_invariants( self ); }
void string_reserve( string* self, size_t capacity ) { assert_invariants( self ); if ( capacity <= self->capacity ) return; string_reserve_internal( self, capacity ); assert_invariants( self ); }
static gpr_avl_node *assert_invariants(gpr_avl_node *n) { if (n == NULL) return NULL; assert_invariants(n->left); assert_invariants(n->right); assert(calculate_height(n) == n->height); assert(labs(node_height(n->left) - node_height(n->right)) <= 1); return n; }
gpr_avl_node *new_node(void *key, void *value, gpr_avl_node *left, gpr_avl_node *right) { gpr_avl_node *node = gpr_malloc(sizeof(*node)); gpr_ref_init(&node->refs, 1); node->key = key; node->value = value; node->left = assert_invariants(left); node->right = assert_invariants(right); node->height = 1 + GPR_MAX(node_height(left), node_height(right)); return node; }
/* Return the first fragment in the chain that E is a member of. */ static inline Lisp_Extent * find_first_frag(Lisp_Extent *e) { while(e->frag_pred != 0) { assert_invariants (e); e = e->frag_pred; } assert_invariants (e); return e; }
/* Return the last fragment in the chain that E is a member of. */ static inline Lisp_Extent * find_last_frag(Lisp_Extent *e) { while(e->frag_next != 0) { assert_invariants (e); e = e->frag_next; } assert_invariants (e); return e; }
void string_append( string * self, char const * rhs ) { size_t rhs_size = strlen( rhs ); size_t new_size = self->size + rhs_size; assert_invariants( self ); maybe_reserve( self, new_size ); memcpy( self->value + self->size, rhs, rhs_size + 1 ); self->size = new_size; assert_invariants( self ); }
void string_append_range( string * self, char const * start, char const * finish ) { size_t rhs_size = finish - start; size_t new_size = self->size + rhs_size; assert_invariants( self ); maybe_reserve( self, new_size ); memcpy( self->value + self->size, start, rhs_size ); self->size = new_size; self->value[ new_size ] = 0; assert_invariants( self ); }
database_entity::database_entity() :key_ (0) ,axis_lengths_ (e_number_of_axes, 1) ,data_values_ (1) { assert_invariants(); }
double& database_entity::operator[](std::vector<int> const& index) { assert_invariants(); LMI_ASSERT(e_number_of_axes == index.size()); int z = 0; for(unsigned int j = 0; j < e_number_of_axes; j++) { if(1 != axis_lengths_[j]) { LMI_ASSERT(index[j] < axis_lengths_[j]); z = z * axis_lengths_[j] + index[j]; } } if(static_cast<int>(data_values_.size()) <= z) { z = 0; fatal_error() << "Trying to index database item '" << GetDBNames()[key_].ShortName << "' past end of data." << LMI_FLUSH ; } return data_values_[z]; }
void string_free( string * s ) { assert_invariants( s ); if ( s->value != s->opt ) BJAM_FREE( s->value ); string_new( s ); }
void string_rtrim( string * self ) { char *p; assert_invariants( self ); p = self->value + self->size - 1; for ( ; p >= self->value && ( *p == '\0' || isspace( *p ) ); *p-- = 0 ); }
gpr_avl gpr_avl_add(gpr_avl avl, void *key, void *value) { gpr_avl_node *old_root = avl.root; avl.root = add(avl.vtable, avl.root, key, value); assert_invariants(avl.root); unref_node(avl.vtable, old_root); return avl; }
/* Create a new extent. All links are null. If CLONEE is non-null, copy the START, END, TX and PLIST fields from it. */ static Lisp_Extent * alloc_extent(Lisp_Extent *clonee) { Lisp_Extent *x = rep_alloc(sizeof(Lisp_Extent)); if(x == 0) { rep_mem_error(); return 0; } x->car = extent_type; x->next = allocated_extents; allocated_extents = x; clean_node(x); if(clonee != 0) { x->start = clonee->start; x->end = clonee->end; x->tx = clonee->tx; x->plist = clonee->plist; x->locals = clonee->locals; } else { x->locals = Qnil; x->plist = Qnil; } assert_invariants (x); return x; }
gpr_avl gpr_avl_remove(gpr_avl avl, void *key) { gpr_avl_node *old_root = avl.root; avl.root = remove(avl.vtable, avl.root, key); assert_invariants(avl.root); unref_node(avl.vtable, old_root); return avl; }
/* Unlink extent E (and all fragments chained off it). */ static void unlink_extent(Lisp_Extent *e) { int n, i; Lisp_Extent *x, **buf; if(e->parent == 0) return; e = find_first_frag(e); /* scan the frags into a buffer (the linkage gets broken) */ n = 0; for (x = e; x != 0; x = x->frag_next) n++; buf = alloca (sizeof (Lisp_Extent *) * n); for (x = e, i = 0; x != 0; x = x->frag_next, i++) buf[i] = x; for (i = 0; i < n; i++) { unlink_extent_fragment(buf[i]); assert_invariants (buf[i]); } }
void database_entity::write(xml::element& e) const { assert_invariants(); xml_serialize::set_element(e, "axis_lengths", axis_lengths_); xml_serialize::set_element(e, "data_values" , data_values_ ); xml_serialize::set_element(e, "gloss" , gloss_ ); }
void database_entity::read(xml::element const& e) { key_ = db_key_from_name(e.get_name()); xml_serialize::get_element(e, "axis_lengths", axis_lengths_); xml_serialize::get_element(e, "data_values" , data_values_ ); xml_serialize::get_element(e, "gloss" , gloss_ ); assert_invariants(); }
static void unlink_extent_recursively(Lisp_Extent *e) { while(e->first_child != 0) unlink_extent_recursively(e->first_child); if(e->parent != 0) unlink_extent_fragment(e); assert_invariants (e); }
void string_append( string* self, char const* rhs ) { char* p = self->value + self->size; char* end = self->value + self->capacity; assert_invariants( self ); while ( *rhs && p != end) *p++ = *rhs++; if ( p != end ) { *p = 0; self->size = p - self->value; } else { extend_full( self, rhs, rhs + strlen(rhs) ); } assert_invariants( self ); }
void string_new( string* s ) { s->value = s->opt; s->size = 0; s->capacity = sizeof(s->opt); s->opt[0] = 0; #ifndef NDEBUG memset(s->magic, JAM_STRING_MAGIC, sizeof(s->magic)); #endif assert_invariants( s ); }
void database_entity::reshape(std::vector<int> const& new_dims) { LMI_ASSERT(e_number_of_axes == new_dims.size()); LMI_ASSERT(1 == new_dims[0] || e_max_dim_gender == new_dims[0]); LMI_ASSERT(1 == new_dims[1] || e_max_dim_uw_class == new_dims[1]); LMI_ASSERT(1 == new_dims[2] || e_max_dim_smoking == new_dims[2]); LMI_ASSERT(1 == new_dims[3] || e_max_dim_issue_age == new_dims[3]); LMI_ASSERT(1 == new_dims[4] || e_max_dim_uw_basis == new_dims[4]); LMI_ASSERT(1 == new_dims[5] || e_max_dim_state == new_dims[5]); LMI_ASSERT(1 <= new_dims[6] && new_dims[6] <= e_max_dim_duration); // Number of times we'll go through the assignment loop. int n_iter = getndata(new_dims); // Create a new instance of this class having the same key but the // desired dimensions, for convenient use of operator[](). std::vector<double> new_data(n_iter); database_entity new_object(key(), new_dims, new_data); std::vector<int> dst_max_idx(e_number_of_axes); assign(dst_max_idx, new_dims - 1); std::vector<int> src_max_idx(e_number_of_axes); assign(src_max_idx, axis_lengths_ - 1); std::vector<int> dst_idx(e_number_of_axes); // indexes new_object std::vector<int> src_idx(e_number_of_axes); // indexes '*this' std::vector<int> working_idx(e_number_of_axes); for(int j = 0; j < n_iter; j++) { int z = j; std::vector<int>::const_iterator i = new_dims.begin(); std::vector<int>::iterator w = working_idx.begin(); while(i != new_dims.end()) { LMI_ASSERT(0 != *i); *w = z % *i; z /= *i; i++; w++; } LMI_ASSERT(0 == z); // limit dst and source indexes to those that actually vary assign(dst_idx, apply_binary(lesser_of<int>(), working_idx, dst_max_idx)); assign(src_idx, apply_binary(lesser_of<int>(), working_idx, src_max_idx)); new_object[dst_idx] = operator[](src_idx); } axis_lengths_ = new_dims; data_values_ = new_object.data_values_; assert_invariants(); }
void string_append_range( string* self, char const* start, char const* finish ) { char* p = self->value + self->size; char* end = self->value + self->capacity; assert_invariants( self ); while ( p != end && start != finish ) *p++ = *start++; if ( p != end ) { *p = 0; self->size = p - self->value; } else { extend_full( self, start, finish ); } assert_invariants( self ); }
database_entity::database_entity (int key ,double datum ,std::string const& gloss ) :key_ (key) ,gloss_ (gloss) { axis_lengths_ .assign(ScalarDims, ScalarDims + e_number_of_axes); data_values_ .push_back(datum); assert_invariants(); }
/* For two adjacent fragments at the same level, LEFT and RIGHT, attempt to join them into a single fragment. Always absorbs RIGHT into LEFT, never LEFT into RIGHT. */ static void try_to_coalesce(Lisp_Extent *left, Lisp_Extent *right) { if(left->frag_next == right && left->parent == right->parent && PPOS_GREATER_EQUAL_P(&left->end, &right->start)) { /* Yep. These two can be united. */ left->right_sibling = right->right_sibling; left->end = right->end; if(left->parent->last_child == right) left->parent->last_child = left; left->frag_next = right->frag_next; clean_node(right); assert_invariants (left); assert_invariants (right); } }
static gpr_avl_node *rebalance(const gpr_avl_vtable *vtable, void *key, void *value, gpr_avl_node *left, gpr_avl_node *right) { switch (node_height(left) - node_height(right)) { case 2: if (node_height(left->left) - node_height(left->right) == -1) { return assert_invariants( rotate_left_right(vtable, key, value, left, right)); } else { return assert_invariants(rotate_right(vtable, key, value, left, right)); } case -2: if (node_height(right->left) - node_height(right->right) == 1) { return assert_invariants( rotate_right_left(vtable, key, value, left, right)); } else { return assert_invariants(rotate_left(vtable, key, value, left, right)); } default: return assert_invariants(new_node(key, value, left, right)); } }
database_entity::database_entity (int key ,std::vector<int> const& dims ,std::vector<double> const& data ,std::string const& gloss ) :key_ (key) ,axis_lengths_ (dims) ,data_values_ (data) ,gloss_ (gloss) { assert_invariants(); }
database_entity::database_entity (int key ,int ndims ,int const* dims ,double const* data ,std::string const& gloss ) :key_ (key) ,gloss_ (gloss) { axis_lengths_ .assign(dims, dims + ndims); data_values_ .assign(data, data + getndata()); assert_invariants(); }
/* Map the function MAP_FUNC(E, DATA) over all innermost extents E containing part of the section of TX specified by START, END. This function can be (and is) safely longjmp'd through. */ void map_section_extents(void (*map_func)(Lisp_Extent *x, void *data), Lisp_Extent *root, Pos *start, Pos *end, void *data) { Lisp_Extent *x = find_extent(root, start); Pos s_copy = *start, e_copy = *end; intptr_t delta = row_delta(x->parent); s_copy.row -= delta; e_copy.row -= delta; while(x != 0 && PPOS_LESS_P(&x->start, &e_copy)) { if(PPOS_GREATER_P(&x->end, &s_copy)) { /* Deleting X in here would screw things up.. Not much that can be done though. And it shouldn't crash (famous last words..) */ map_func(x, data); } /* Try to work downwards and rightwards as much as possible */ if(x->first_child != 0) { /* Map though X's children as well. */ s_copy.row -= x->start.row; e_copy.row -= x->start.row; x = x->first_child; } else if(x->right_sibling != 0) x = x->right_sibling; else if(x->parent != 0) { s_copy.row += x->parent->start.row; e_copy.row += x->parent->start.row; x = x->parent->right_sibling; } else break; assert_invariants (x); } }
Lisp_Extent * find_extent_forwards(Lisp_Extent *root, Pos *pos) { Lisp_Extent *x = root; Pos copy = *pos; copy.row -= row_delta(root); while(x != 0) { if(PPOS_GREATER_EQUAL_P(©, &x->start) && PPOS_LESS_P(©, &x->end)) { /* POS in X somewhere. */ root = x; copy.row -= x->start.row; x = x->first_child; continue; } x = x->right_sibling; assert_invariants (x); } return root; }