void heap_sort(int a[], int length) {
  for (int i = length; i > 0; i--) {
    int heap_size = i;
    build_head(a, heap_size);
    int temp = a[heap_size - 1];
    a[heap_size - 1] = a[0];
    a[0] = temp;
  }
}
Exemple #2
0
void build_request(void *msg, entity_t src, entity_t dest, entity_t target, 
        uint64_t req_id, op_t operate, offset_t offset, size_t len, void *data)
{
    assert(msg != NULL);
    build_head(msg, src, dest, target, req_id, operate, 0, offset, len);

    if (len > 0 && data != NULL) {
        memcpy(msg + sizeof(msg_head_t), data, len);
    }
}
Exemple #3
0
void _log ( log_type_t type, const char *format, ... )
{
	char message[BUFSIZ] = { 0 }, head[512] = { 0 };
	va_list args;
	location_t location;

	va_start ( args, format );
	vsnprintf ( message, BUFSIZ, format, args );
	va_end ( args );

	/** Get the location */
	sscanf ( message, "%d, %s |%*s", &location.line, location.filename );

	/** Skip the location */
	if ( strchr ( message, '|' ) ) 
	{
		snprintf ( message, BUFSIZ, "%s", strchr ( message, '|' ) + 1 );
	}

	build_head ( type, head, sizeof head, &location );

	fprintf ( L_ERROR == type ? stderr : stdout, "%s%s\n", head, message );
}
Exemple #4
0
struct nearest_map *nearest_init(const colormap *map)
{
    mempool m = NULL;
    struct nearest_map *centroids = mempool_new(&m, sizeof(*centroids));
    centroids->mempool = m;

    unsigned int skipped=0;
    unsigned int skip_index[map->colors]; for(unsigned int j=0; j < map->colors; j++) skip_index[j]=0;

    colormap *subset_palette = get_subset_palette(map);
    const int selected_heads = subset_palette->colors;
    centroids->heads = mempool_new(&centroids->mempool, sizeof(centroids->heads[0])*(selected_heads+1)); // +1 is fallback head

    unsigned int h=0;
    for(; h < selected_heads; h++)
    {
        unsigned int num_candiadtes = 1+(map->colors - skipped)/((1+selected_heads-h)/2);

        centroids->heads[h] = build_head(subset_palette->palette[h].acolor, map, num_candiadtes, &centroids->mempool, skip_index, &skipped);
        if (centroids->heads[h].num_candidates == 0) {
            break;
        }
    }

    centroids->heads[h].radius = MAX_DIFF;
    centroids->heads[h].center = (f_pixel){0,0,0,0};
    centroids->heads[h].num_candidates = 0;
    centroids->heads[h].candidates = mempool_new(&centroids->mempool, (map->colors - skipped) * sizeof(centroids->heads[h].candidates[0]));
    for(unsigned int i=0; i < map->colors; i++) {
        if (skip_index[i]) continue;

        centroids->heads[h].candidates[centroids->heads[h].num_candidates++] = (struct color_entry) {
            .color = map->palette[i].acolor,
            .index = i,
            .radius = 999,
        };
    }
    centroids->num_heads = ++h;

    // get_subset_palette could have created a copy
    if (subset_palette != map->subset_palette) {
        pam_freecolormap(subset_palette);
    }

    return centroids;
}

unsigned int nearest_search(const struct nearest_map *centroids, const f_pixel px, const float min_opaque_val, float *diff)
{
    const int iebug = px.a > min_opaque_val;

    const struct head *const heads = centroids->heads;
    for(unsigned int i=0; i < centroids->num_heads; i++) {
        float headdist = colordifference(px, heads[i].center);

        if (headdist <= heads[i].radius) {
            assert(heads[i].num_candidates);
            unsigned int ind=heads[i].candidates[0].index;
            float dist = colordifference(px, heads[i].candidates[0].color);

            /* penalty for making holes in IE */
            if (iebug && heads[i].candidates[0].color.a < 1) {
                dist += 1.f/1024.f;
            }

            for(unsigned int j=1; j < heads[i].num_candidates; j++) {
                float newdist = colordifference(px, heads[i].candidates[j].color);

                /* penalty for making holes in IE */
                if (iebug && heads[i].candidates[j].color.a < 1) {
                    newdist += 1.f/1024.f;
                }

                if (newdist < dist) {

                    dist = newdist;
                    ind = heads[i].candidates[j].index;
                }
            }
            if (diff) *diff = dist;
            return ind;
        }
    }
    assert(0);
    return 0;
}

void nearest_free(struct nearest_map *centroids)
{
    mempool_free(centroids->mempool);
}