Example #1
0
int main(void) {
    int i;
    printf("%016llx\n",pw2(5,0x1ull));
    printf("%016llx\n",pop(0x11100ull));
    printf("%016llx\n",rev(0x1ull));
    printf("%016llx\n",lg2(0x5ull));
    return 0;
}
Example #2
0
static
int thresold(uint *h, size_t size)
{
    double prob[256];
    size_t min_i = 256;
    double min_var = DBL_MAX;

    for (size_t i = 0; i < 256; ++i)
        prob[i] = (double)h[i] / (double)size;

    for (size_t j = 1; j < 255; ++j) {
        double m_under = 0, c = 0;
        for (size_t k = 0; k < j; ++k) {
            m_under += k * prob[k];
            c += prob[k];
        }

        // next iteration if a class is empty
        if (c == 0 || c == 1)
            continue;
        m_under /= c;

        double m_up = 0;
        for (size_t k = j; k < 256; ++k)
            m_up += k * prob[k];
        m_up /= (1 - c);

        double var = 0;
        for (size_t k = 0; k < j; ++k)
            var += pw2(k - m_under) * prob[k];
        for (size_t k = j; k < 256; ++k)
            var += pw2(k - m_up) * prob[k];

        if (min_var > var) {
            min_i = j;
            min_var = var;
        }
    }

    return min_i;
}
Example #3
0
void flush_pending_line_to_obj(struct g_part *p, int minheight)
{
	int i, pp, pos, w, lbl;
	struct g_object_line *l = p->line;
	struct g_object_area *a;
	if (!l) {
		return;
	}
	for (i = l->n_entries - 1; i >= 0; i--) {
		struct g_object_text *go = (struct g_object_text *)l->entries[i];
		if (go->draw == g_text_draw) {
			int l;
			while ((l = strlen(go->text)) && go->text[l - 1] == ' ') go->text[l - 1] = 0, go->xw -= g_char_width(go->style, ' ');
			if (go->xw < 0) internal("xw(%d) < 0", go->xw);
		}
		if (!go->xw) continue;
		break;
	}
	scan_again:
	pp = 0;
	w = minheight;
	lbl = 0;
	for (i = 0; i < l->n_entries; i++) {
		int yy = l->entries[i]->y;
		if (yy >= G_OBJ_ALIGN_SPECIAL) yy = 0;
		pp += l->entries[i]->xw;
		if (l->entries[i]->xw && l->entries[i]->yw + yy > w) w = l->entries[i]->yw + yy;
		if (yy < lbl) lbl = yy;
	}
	if (lbl < 0) {
		for (i = 0; i < l->n_entries; i++) {
			if (l->entries[i]->y < G_OBJ_ALIGN_SPECIAL) l->entries[i]->y -= lbl;
		}
		goto scan_again;
	}
	if (par_format.align == AL_CENTER) pos = (rm(par_format) + par_format.leftmargin * G_HTML_MARGIN - pp) / 2;
	else if (par_format.align == AL_RIGHT) pos = rm(par_format) - pp;
	else pos = par_format.leftmargin * G_HTML_MARGIN;
	if (pos < par_format.leftmargin * G_HTML_MARGIN) pos = par_format.leftmargin * G_HTML_MARGIN;
	pp = pos;
	for (i = 0; i < l->n_entries; i++) {
		l->entries[i]->x = pp;
		pp += l->entries[i]->xw;
		if (l->entries[i]->y < G_OBJ_ALIGN_SPECIAL) {
			l->entries[i]->y = w - l->entries[i]->yw - l->entries[i]->y;
		} else if (l->entries[i]->y == G_OBJ_ALIGN_TOP) {
			l->entries[i]->y = 0;
		} else if (l->entries[i]->y == G_OBJ_ALIGN_MIDDLE) {
			l->entries[i]->y = (w - l->entries[i]->yw + 1) / 2;
		}
	}
	l->x = 0;
	l->xw = par_format.width;
	l->yw = w;
	l->y = p->cy;
	if (l->xw > p->root->xw) p->root->xw = l->xw;
	p->root->yw = p->cy += w;
	p->root->n_lines++;
	if ((unsigned)p->root->n_lines > (MAXINT - sizeof(struct g_object_area)) / 2 / sizeof(struct g_object_text *) + 1) overalloc();
	a = mem_realloc(p->root, sizeof(struct g_object_area) + sizeof(struct g_object_text *) * pw2(p->root->n_lines + 1));
		/* note: +1 is for extend_area */
	p->root = a;
	p->root->lines[p->root->n_lines - 1] = l;
	p->line = NULL;
	p->w.pos = 0;
	p->w.last_wrap = NULL;
	p->w.last_wrap_obj = NULL;
}
int main()
{

    /* For std::unique_ptr, the type of the deleter is part of the type of the
     * smart pointer.  For std::shared_ptr, it's not.
     */

    std::cout << "Part 1: deleter type being part or not" << std::endl;
    
    std::unique_ptr<                       // deleter type is
      Widget, decltype(loggingDel)         // part of ptr type
      > upw(new Widget, loggingDel);
    
    std::shared_ptr<Widget>                // deleter type is not
      spw(new Widget, loggingDel);         // part of ptr type
    
    
    /* The std::shared_ptr design is more flexible: the type of the custom deleter
     * is not part of the shared_ptr type, so the following things are for example
     * possible:
     * 
     *   -> can be placed in containers of objects of the shared_ptr type
     *   -> they can be assigned to one another
     *   -> they can be passed to a function taking a parameter of
     *      type std::shared_ptr<Widget>.
     *   -> ...
     * 
     * None of these things can be done with std::unique_ptrs that differ in the
     * types of their custom deleters, because the type of the custom deleter would
     * affect the type of the std::unique_ptr.
     */
     
    std::cout << "Part 2: std::shared_ptr design being more flexible" << std::endl;

    auto customDeleter1 = [](Widget *pw) { makeLogentry(pw); delete pw; };  // custom deleters,
    auto customDeleter2 = [](Widget *pw) { makeLogentry(pw); delete pw; };  // each with a
                                                                            // different type
    
    std::shared_ptr<Widget> pw1(new Widget, customDeleter1);
    std::shared_ptr<Widget> pw2(new Widget, customDeleter2);
    
    std::vector<std::shared_ptr<Widget>> vpw { pw1, pw2 };


   /*
    * BAD BAD BAD: constructing more than one std::shared_ptr from a single
    * raw pointer results in undefined behavior.
    */

    std::cout << "Part 3: std::shared_ptr construction error" << std::endl;
   
    //auto pw = new Widget;
    // 
    //std::shared_ptr<Widget> spw1(pw, loggingDel);  // create control
    //                                               // block for *pw
    //
    //std::shared_ptr<Widget> spw2(pw, loggingDel);  // create 2nd
    //                                               // control block
    //                                               // for *pw!
    
    std::shared_ptr<Widget> spw1(new Widget,       // direct use of new
                                 loggingDel);
    
    std::shared_ptr<Widget> spw2(spw1);            // spw2 uses same
                                                   // control block as spw1

    std::cout << "Part 4: right before end of main" << std::endl;
}