/** * Pack a vector of unsigned 64 bits integer into this byte array. See * _byte_array_pack64() for performance and resizing issues. */ void _byte_array_pack16v(struct byte_array *ba, uint16_t *v, size_t n) { ensure_size(ba, n * sizeof(v[0])); size_t i; for (i = 0; i < n; i++) _byte_array_pack16(ba, v[i]); }
byte_buffer& operator<<(const std::string& str) { ensure_size(str.size()); for (auto it : str) { data_[size_++] = it; } return *this; }
static void term_resize(int w, int h) { minsize.width = w; minsize.height = h; // try to set the terminal size if the terminal will let us: term_set_size(h, w); // (this works in gnome-terminal, but causes trouble for curses on maximized windows.) // now make sure it worked, and ask the user to resize the terminal if it didn't ensure_size(); // make a new cell buffer if (cell_buffer) free(cell_buffer); cell_buffer = malloc(sizeof(pairmode_cell) * w * h); // add error checking int i; for (i = 0; i < w * h; i++) { // I guess we could just zero it all, hmm cell_buffer[i].ch = 0; cell_buffer[i].pair = 0; cell_buffer[i].fore.idx = 0; cell_buffer[i].back.idx = 0; } }
static int term_getkey( ) { Term.mouse.justPressed = 0; Term.mouse.justReleased = 0; Term.mouse.justMoved = 0; while (1) { int got = getch(); if (got == KEY_RESIZE) { ensure_size( ); } else if (got == KEY_MOUSE) { MEVENT mevent; getmouse (&mevent); Term.mouse.x = mevent.x; Term.mouse.y = mevent.y; Term.mouse.shift = (mevent.bstate & BUTTON_SHIFT) != 0; Term.mouse.control = (mevent.bstate & BUTTON_CTRL) != 0; if (mevent.bstate & BUTTON1_PRESSED) { Term.mouse.justPressed = 1; Term.mouse.isPressed = 1; } else if (mevent.bstate & BUTTON1_RELEASED) { if (Term.mouse.isPressed) { Term.mouse.justReleased = 1; Term.mouse.isPressed = 0; } } else { Term.mouse.justMoved = 1; } return TERM_MOUSE; } else { if (got == KEY_ENTER) got = 13; // KEY_ENTER -> ^M for systems with odd values for KEY_ENTER if (got == ERR) return TERM_NONE; else return got; } } }
inline void APSizedArrayBase<_ElementType>::ensure_length_null(uint32_t length_min) { if (this->m_count < length_min) { ensure_size(length_min); ::memset(this->m_array_p + this->m_count, 0, (length_min - this->m_count) * sizeof(_ElementType *)); this->m_count = length_min; } }
/** * Pack a unsigned 16 bits integer into this byte array. See * _byte_array_pack64() for performance and resizing issues. */ void _byte_array_pack16(struct byte_array *ba, uint16_t v) { ensure_size(ba, sizeof(v)); union { uint16_t i; char c[2]; } nv = { htons(v) }; memcpy(ba->ptr + ba->len, nv.c, sizeof(v)); ba->len += sizeof(v); }
byte_buffer& operator<<(char c) { ensure_size(1); data_[size_++] = c; return *this; }
byte_buffer& operator<<(const ContinuousByteSequence& sequence) { ensure_size(sequence.size()); std::memcpy(data_ + size_, sequence.data(), sequence.size()); size_ += sequence.size(); return *this; }
byte_buffer& operator<<(const wrapper& w) { ensure_size(w.size); std::memcpy(data_ + size_, w.data, w.size); size_ += w.size; return *this; }
byte_buffer& operator<<(const byte_buffer& buffer) { ensure_size(buffer.size()); std::memcpy(data_ + size_, buffer.data(), buffer.size()); size_ += buffer.size(); return *this; }
// Partition a set of items into two distinct sets. // Return end if the set is not to be partitioned. size_t partition( vector<UniqueID>& items, vector<GAABB3>& bboxes, const size_t begin, const size_t end, const GAABB3& bbox) { const size_t count = end - begin; assert(count > 1); // Ensure that sufficient memory is allocated for the working arrays. ensure_size(m_indices, count); ensure_size(m_left_bboxes, count); ensure_size(m_temp_items, count); ensure_size(m_temp_bboxes, count); // Create the set of indices. for (size_t i = 0; i < count; ++i) m_indices[i] = i; GScalar best_split_cost = numeric_limits<GScalar>::max(); size_t best_split_dim = 0; size_t best_split_pivot = 0; GAABB3 group_bbox; for (size_t dim = 0; dim < 3; ++dim) { // Sort the items according to their bounding boxes. BboxSortPredicate predicate(bboxes, begin, dim); sort(&m_indices[0], &m_indices[0] + count, predicate); // Left-to-right sweep to accumulate bounding boxes. group_bbox.invalidate(); for (size_t i = 0; i < count; ++i) { group_bbox.insert(bboxes[begin + m_indices[i]]); m_left_bboxes[i] = group_bbox; } // Right-to-left sweep to accumulate bounding boxes and evaluate SAH. group_bbox.invalidate(); for (size_t i = count - 1; i > 0; --i) { // Get left and right bounding boxes. const GAABB3& left_bbox = m_left_bboxes[i - 1]; group_bbox.insert(bboxes[begin + m_indices[i]]); // Compute the cost of this partition. const GScalar left_cost = left_bbox.half_surface_area() * i; const GScalar right_cost = group_bbox.half_surface_area() * (count - i); const GScalar split_cost = left_cost + right_cost; // Keep track of the partition with the lowest cost. if (best_split_cost > split_cost) { best_split_cost = split_cost; best_split_dim = dim; best_split_pivot = i; } } } // Just split in half if the cost of the best partition is too high. const GScalar leaf_cost = bbox.half_surface_area() * count; if (best_split_cost >= leaf_cost) return (begin + end) / 2; // Sort the indices according to the item bounding boxes. BboxSortPredicate predicate(bboxes, begin, best_split_dim); sort(&m_indices[0], &m_indices[0] + count, predicate); // Reorder the items. small_item_reorder(&items[begin], &m_temp_items[0], &m_indices[0], count); small_item_reorder(&bboxes[begin], &m_temp_bboxes[0], &m_indices[0], count); assert(begin + best_split_pivot < end); return begin + best_split_pivot; }
/** * Pack a vector of unsigned 64 bits integer into this byte array. See * _byte_array_pack64() for performance and resizing issues. */ void _byte_array_pack8v(struct byte_array *ba, uint8_t *v, size_t n) { ensure_size(ba, n); memcpy(ba->ptr + ba->len, v, n); ba->len += n; }
/** * Pack a unsigned 8 bits integer into this byte array. See * _byte_array_pack64() for performance and resizing issues. */ void _byte_array_pack8(struct byte_array *ba, uint8_t v) { ensure_size(ba, 1); *(ba->ptr + ba->len) = v; ba->len++; }