Ejemplo n.º 1
0
/* Search for an element in a bucket */
static ght_hash_entry_t *search_in_bucket(ght_hash_table_t *p_ht, ght_uint32_t l_bucket,
						 ght_hash_key_t *p_key, unsigned char i_heuristics)
{
  ght_hash_entry_t *p_e;

  for (p_e = p_ht->pp_entries[l_bucket];
       p_e;
       p_e = p_e->p_next)
    {
      if ((p_e->key.i_size == p_key->i_size) &&
	  (memcmp(p_e->key.p_key, p_key->p_key, p_e->key.i_size) == 0))
	{
	  /* Matching entry found - Apply heuristics, if any */
	  switch (i_heuristics)
	    {
	    case GHT_HEURISTICS_MOVE_TO_FRONT:
	      move_to_front(p_ht, l_bucket, p_e);
	      break;
	    case GHT_HEURISTICS_TRANSPOSE:
	      transpose(p_ht, l_bucket, p_e);
	      break;
	    default:
	      break;
	    }
	  return p_e;
	}
    }
  return NULL;
}
Ejemplo n.º 2
0
    int get(int key) {
        if(m.find(key)==m.end())
            return -1;
        node *current=m[key];
        current->prev->next=current->next;
        current->next->prev=current->prev;

        move_to_front(current);
        return m[key]->value;
    }
Ejemplo n.º 3
0
/* Order the list of input files so each next file applies to the previous: */
static bool order_input(struct File **files)
{
    struct File *cur, *succ;

    /* Determine first file */
    cur = find_first(*files);
    if (cur == NULL) return false;
    *files = move_to_front(*files, cur);

    /* Order remaining files */
    while (cur->next)
    {
        succ = find_digest(cur->next, cur->diff.digest2);
        if (succ == NULL) return false;
        cur->next = move_to_front(cur->next, succ);
        cur = cur->next;
    }

    return true;
}
Ejemplo n.º 4
0
int		my_mlx_hook_loop(t_env *e)
{
	if (e->mv->left == 1)
		e = move_to_left(e);
	if (e->mv->right == 1)
		e = move_to_right(e);
	if (e->mv->front == 1)
		e = move_to_front(e);
	if (e->mv->back == 1)
		e = move_to_back(e);
	my_expose_hook(e);
	return (0);
}
Ejemplo n.º 5
0
 void Miniball::mtf_mb (It i)
 {
     support_end = L.begin();
     if ((B.size())==BSIZE) return;
     for (It k=L.begin(); k!=i;) {
         It j=k++;
         if (B.excess(*j) > 0) {
             if (B.push(*j)) {
                 mtf_mb (j);
                 B.pop();
                 move_to_front(j);
             }
         }
     }
 }
Ejemplo n.º 6
0
Archivo: proc.c Proyecto: byan23/OprSys
// Wake up all processes sleeping on chan.
// The ptable lock must be held.
static void
wakeup1(void *chan)
{
  struct proc *p;
  int slot_no = -1;
  for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
    ++slot_no;
    if(p->state == SLEEPING && p->chan == chan) {
      p->state = RUNNABLE;
      //cprintf("pid: %d wakes up.\n", p->pid);
      //dump_queues();
      // move to the front of the queue.
      int pri = proc_stat.priority[slot_no];
      switch(pri) {
	case 0:
	  //cprintf("move to 0 queue front.\n");
	  move_to_front(&q0_head, &q0_tail, p);
	  break; 
	case 1:
	  //cprintf("move to 1 queue front.\n");
	  move_to_front(&q1_head, &q1_tail, p);
	  break; 
	case 2:
	  //cprintf("move to 2 queue front.\n");
	  move_to_front(&q2_head, &q2_tail, p);
	  break; 
	case 3:
	  //cprintf("move to 3 queue front.\n");
	  move_to_front(&q3_head, &q3_tail, p);
	  break;
	default:
	  cprintf("Wrong pri again.\n");
      }
    }
  }
}
Ejemplo n.º 7
0
 void set(int key, int value) {
     if(get(key)!=-1) {
         m[key]->value=value;
         return;
     }
     if(m.size()==capacity) {
         node *temp=tail->prev;
         m.erase(temp->key);
         temp->prev->next=tail;
         tail->prev=temp->prev;
         delete temp;
     }
     node *newnode=new node(key,value);
     m[key]=newnode;
     move_to_front(newnode);
 }
Ejemplo n.º 8
0
 void Miniball::pivot_mb (It i)
 {
     It t = ++L.begin();
     mtf_mb (t);
     double max_e, old_sqr_r;
     do {
         It pivot;
         max_e = max_excess (t, i, pivot);
         if (max_e > 0) {
             t = support_end;
             if (t==pivot) ++t;
             old_sqr_r = B.squared_radius();
             B.push (*pivot);
             mtf_mb (support_end);
             B.pop();
             move_to_front (pivot);
         }
     } while ((max_e > 0) && (B.squared_radius() > old_sqr_r));
 }
Ejemplo n.º 9
0
/* Returns a pointer to a value eq to v in A, and moves it to the front */
void *array_find_MTF(bool (*eq)(void *v, void *d), array_t *A, void *v) {
    if (!A)
        return NULL;

    int i = 0;
    for (i = 0; i < A->size; i++) {
        if (eq(v, A->vals[i]))
            break;
    }

    /* Could not find v in A */
    if (i == A->size) {
        return NULL;
    } else if (i > 0) {
        move_to_front(A, i);
    }

    return A->vals[0];
}
Ejemplo n.º 10
0
fz_glyph *
fz_render_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix *ctm, fz_colorspace *model, const fz_irect *scissor, int alpha)
{
	fz_glyph_cache *cache;
	fz_glyph_key key;
	fz_matrix subpix_ctm;
	fz_irect subpix_scissor;
	float size;
	fz_glyph *val;
	int do_cache, locked, caching;
	fz_glyph_cache_entry *entry;
	unsigned hash;

	fz_var(locked);
	fz_var(caching);
	fz_var(val);

	memset(&key, 0, sizeof key);
	size = fz_subpixel_adjust(ctx, ctm, &subpix_ctm, &key.e, &key.f);
	if (size <= MAX_GLYPH_SIZE)
	{
		scissor = &fz_infinite_irect;
		do_cache = 1;
	}
	else
	{
		if (font->ft_face)
			return NULL;
		subpix_scissor.x0 = scissor->x0 - floorf(ctm->e);
		subpix_scissor.y0 = scissor->y0 - floorf(ctm->f);
		subpix_scissor.x1 = scissor->x1 - floorf(ctm->e);
		subpix_scissor.y1 = scissor->y1 - floorf(ctm->f);
		scissor = &subpix_scissor;
		do_cache = 0;
	}

	cache = ctx->glyph_cache;

	key.font = font;
	key.gid = gid;
	key.a = subpix_ctm.a * 65536;
	key.b = subpix_ctm.b * 65536;
	key.c = subpix_ctm.c * 65536;
	key.d = subpix_ctm.d * 65536;
	key.aa = fz_text_aa_level(ctx);

	hash = do_hash((unsigned char *)&key, sizeof(key)) % GLYPH_HASH_LEN;
	fz_lock(ctx, FZ_LOCK_GLYPHCACHE);
	entry = cache->entry[hash];
	while (entry)
	{
		if (memcmp(&entry->key, &key, sizeof(key)) == 0)
		{
			move_to_front(cache, entry);
			val = fz_keep_glyph(ctx, entry->val);
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
			return val;
		}
		entry = entry->bucket_next;
	}

	locked = 1;
	caching = 0;
	val = NULL;

	fz_try(ctx)
	{
		if (font->ft_face)
		{
			val = fz_render_ft_glyph(ctx, font, gid, &subpix_ctm, key.aa);
		}
		else if (font->t3procs)
		{
			/* We drop the glyphcache here, and execute the t3
			 * glyph code. The danger here is that some other
			 * thread will come along, and want the same glyph
			 * too. If it does, we may both end up rendering
			 * pixmaps. We cope with this later on, by ensuring
			 * that only one gets inserted into the cache. If
			 * we insert ours to find one already there, we
			 * abandon ours, and use the one there already.
			 */
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
			locked = 0;
			val = fz_render_t3_glyph(ctx, font, gid, &subpix_ctm, model, scissor);
			fz_lock(ctx, FZ_LOCK_GLYPHCACHE);
			locked = 1;
		}
		else
		{
			fz_warn(ctx, "assert: uninitialized font structure");
		}
		if (val && do_cache)
		{
			if (val->w < MAX_GLYPH_SIZE && val->h < MAX_GLYPH_SIZE)
			{
				/* If we throw an exception whilst caching,
				 * just ignore the exception and carry on. */
				caching = 1;
				if (!font->ft_face)
				{
					/* We had to unlock. Someone else might
					 * have rendered in the meantime */
					entry = cache->entry[hash];
					while (entry)
					{
						if (memcmp(&entry->key, &key, sizeof(key)) == 0)
						{
							fz_drop_glyph(ctx, val);
							move_to_front(cache, entry);
							val = fz_keep_glyph(ctx, entry->val);
							goto unlock_and_return_val;
						}
						entry = entry->bucket_next;
					}
				}

				entry = fz_malloc_struct(ctx, fz_glyph_cache_entry);
				entry->key = key;
				entry->hash = hash;
				entry->bucket_next = cache->entry[hash];
				if (entry->bucket_next)
					entry->bucket_next->bucket_prev = entry;
				cache->entry[hash] = entry;
				entry->val = fz_keep_glyph(ctx, val);
				fz_keep_font(ctx, key.font);

				entry->lru_next = cache->lru_head;
				if (entry->lru_next)
					entry->lru_next->lru_prev = entry;
				else
					cache->lru_tail = entry;
				cache->lru_head = entry;

				cache->total += fz_glyph_size(ctx, val);
				while (cache->total > MAX_CACHE_SIZE)
				{
#ifndef NDEBUG
					cache->num_evictions++;
					cache->evicted += fz_glyph_size(ctx, cache->lru_tail->val);
#endif
					drop_glyph_cache_entry(ctx, cache->lru_tail);
				}

			}
		}
unlock_and_return_val:
		{
		}
	}
	fz_always(ctx)
	{
		if (locked)
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
	}
	fz_catch(ctx)
	{
		if (caching)
			fz_warn(ctx, "cannot encache glyph; continuing");
		else
			fz_rethrow(ctx);
	}

	return val;
}
Ejemplo n.º 11
0
/* Add v to A at the start of the array */
void array_insert_MTF(array_t **A, void *v) {
    array_insert(A, v);
    move_to_front((*A), (*A)->size - 1);
}
Ejemplo n.º 12
0
error_t cache_get(cache_t* c, cache_id_t id, void** ret_data)
{
  cache_list_entry_t* le;
  void* data;
  error_t err;
  hm_entry_t entry;
  long idx;
  int data_number;

  entry.key = id;
  entry.value = NULL;

  // first, check the mapping.
  if( hashmap_retrieve(&c->mapping, &entry) ) {
    idx = (long) entry.value;
    if( 0 == c->mapping.cmp(id, c->list[idx].id ) ) {
      // it's a valid mapping.
      // move it to the front of the list.
      //printf("Cache before mtf; id=%p\n", c->list[idx].id);
      move_to_front(c, idx, c->list[idx].id);
      data_number = idx;
      data = ((unsigned char*) c->data) + data_number * c->data_size;
      *ret_data = data;
      return ERR_NOERR;
    }
  }

  // if it's not an valid mapping, we'll evict the data
  // item pointed to by the last list element, then
  // load the new item, and finally move it to the front
  // of the list.

  // find a home for the new data item.
  // does the tail item in the list already have a data item?
  le = & c->list[c->tail];
  data_number = c->tail;
  data = ((unsigned char*) c->data) + data_number * c->data_size;
  if( le->id && c->evict ) {
    c->evict(le->id, c->data_size, data, c->context);
  }

  // load the new item.
  //printf("Cache before fault; id=%p\n", id);
  err = c->fault(id, c->data_size, data, c->context);
  if( err ) {
    if( le->id ) {
      // remove the old entry
      entry.key = le->id;
      entry.value = NULL;
      hashmap_delete(&c->mapping, &entry);
      c->free_id(le->id);
      le->id = NULL;
    }
    return err;
  }

  //printf("Cache after fault; id=%p\n", id);

  // move the tail element to the front of the list.
  move_to_front(c, c->tail, id);

  *ret_data = data;

  return ERR_NOERR;
}
Ejemplo n.º 13
0
int wedge(FLOAT halves[][2],
	int m,
	int next[],
	int prev[],
	FLOAT cw_vec[],
	FLOAT ccw_vec[],
	int *degen)
{
	int i;
	FLOAT d_cw, d_ccw;
	int offensive;

	*degen = 0;
	for(i=0;i!=m;i = next[i]) {
		if(!unit2(halves[i],ccw_vec,EPS)) {
/* clock-wise */
			cw_vec[0] = ccw_vec[1];
			cw_vec[1] = -ccw_vec[0];
/* counter-clockwise */
			ccw_vec[0] = -cw_vec[0];
			ccw_vec[1] = -cw_vec[1];
			break;
		}
	}
	if(i==m) return(UNBOUNDED);
	i = 0;
	while(i!=m) {
		offensive = 0;
		d_cw = dot2(cw_vec,halves[i]);
		d_ccw = dot2(ccw_vec,halves[i]);
		if(d_ccw >= 2*EPS) {
			if(d_cw <= -2*EPS) {
				cw_vec[0] = halves[i][1];
				cw_vec[1] = -halves[i][0];
				(void)unit2(cw_vec,cw_vec,EPS);
				offensive = 1;
			}
		} else if(d_cw >= 2*EPS) {
			if(d_ccw <= -2*EPS) {
				ccw_vec[0] = -halves[i][1];
				ccw_vec[1] = halves[i][0];
				(void)unit2(ccw_vec,ccw_vec,EPS);
				offensive = 1;
			}
		} else if(d_ccw <= -2*EPS && d_cw <= -2*EPS) {
			return(INFEASIBLE);
		} else if((d_cw <= -2*EPS) ||
			(d_ccw <= -2*EPS) ||
			(cross2(cw_vec,halves[i]) < 0.0)) {
/* degenerate */
			if(d_cw <= -2*EPS) {
				(void)unit2(ccw_vec,cw_vec,EPS);
			} else if(d_ccw <= -2*EPS) { 
				(void)unit2(cw_vec,ccw_vec,EPS);
			}
			*degen = 1;
			offensive = 1;
		}
/* place this offensive plane in second place */
		if(offensive) i = move_to_front(i,next,prev);
		i = next[i];
		if(*degen) break;
	}
	if(*degen) {
		while(i!=m) {
			d_cw = dot2(cw_vec,halves[i]);
			d_ccw = dot2(ccw_vec,halves[i]);
			if(d_cw < -2*EPS) {
				if(d_ccw < -2*EPS) {
					return(INFEASIBLE);
				} else {
					cw_vec[0] = ccw_vec[0];
					cw_vec[1] = ccw_vec[1];
				}
			} else if(d_ccw < -2*EPS) {
				ccw_vec[0] = cw_vec[0];
				ccw_vec[1] = cw_vec[1];
			}
			i = next[i];
		}
	}
	return(MINIMUM);
}
/*
	Render a glyph and return a bitmap.
	If the glyph is too large to fit the cache we have two choices:
	1) Return NULL so the caller can draw the glyph using an outline.
		Only supported for freetype fonts.
	2) Render a clipped glyph by using the scissor rectangle.
		Only supported for type 3 fonts.
		This must not be inserted into the cache.
 */
fz_pixmap *
fz_render_glyph(fz_context *ctx, fz_font *font, int gid, const fz_matrix *ctm, fz_colorspace *model, fz_irect scissor)
{
	fz_glyph_cache *cache;
	fz_glyph_key key;
	fz_pixmap *val;
	float size = fz_matrix_expansion(ctm);
	int do_cache, locked, caching;
	fz_matrix local_ctm = *ctm;
	fz_glyph_cache_entry *entry;
	unsigned hash;

	fz_var(locked);
	fz_var(caching);

	if (size <= MAX_GLYPH_SIZE)
	{
		scissor = fz_infinite_irect;
		do_cache = 1;
	}
	else
	{
		/* SumatraPDF: don't break clipping by larger glyphs */
		if (font->ft_face && size > 3000)
			return NULL;
		do_cache = 0;
	}

	cache = ctx->glyph_cache;

	memset(&key, 0, sizeof key);
	key.font = font;
	key.gid = gid;
	key.a = local_ctm.a * 65536;
	key.b = local_ctm.b * 65536;
	key.c = local_ctm.c * 65536;
	key.d = local_ctm.d * 65536;
	key.e = (local_ctm.e - floorf(local_ctm.e)) * 256;
	key.f = (local_ctm.f - floorf(local_ctm.f)) * 256;
	key.aa = fz_aa_level(ctx);

	local_ctm.e = floorf(local_ctm.e) + key.e / 256.0f;
	local_ctm.f = floorf(local_ctm.f) + key.f / 256.0f;

	fz_lock(ctx, FZ_LOCK_GLYPHCACHE);
	hash = do_hash((unsigned char *)&key, sizeof(key)) % GLYPH_HASH_LEN;
	entry = cache->entry[hash];
	while (entry)
	{
		if (memcmp(&entry->key, &key, sizeof(key)) == 0)
		{
			move_to_front(cache, entry);
			val = fz_keep_pixmap(ctx, entry->val);
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
			return val;
		}
		entry = entry->bucket_next;
	}

	locked = 1;
	caching = 0;

	fz_try(ctx)
	{
		if (font->ft_face)
		{
			val = fz_render_ft_glyph(ctx, font, gid, &local_ctm, key.aa);
		}
		else if (font->t3procs)
		{
			/* We drop the glyphcache here, and execute the t3
			 * glyph code. The danger here is that some other
			 * thread will come along, and want the same glyph
			 * too. If it does, we may both end up rendering
			 * pixmaps. We cope with this later on, by ensuring
			 * that only one gets inserted into the cache. If
			 * we insert ours to find one already there, we
			 * abandon ours, and use the one there already.
			 */
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
			locked = 0;
			val = fz_render_t3_glyph(ctx, font, gid, &local_ctm, model, scissor);
			fz_lock(ctx, FZ_LOCK_GLYPHCACHE);
			locked = 1;
		}
		else
		{
			fz_warn(ctx, "assert: uninitialized font structure");
			val = NULL;
		}
		if (val && do_cache)
		{
			if (val->w < MAX_GLYPH_SIZE && val->h < MAX_GLYPH_SIZE)
			{
				/* If we throw an exception whilst caching,
				 * just ignore the exception and carry on. */
				caching = 1;
				if (!font->ft_face)
				{
					/* We had to unlock. Someone else might
					 * have rendered in the meantime */
					entry = cache->entry[hash];
					while (entry)
					{
						if (memcmp(&entry->key, &key, sizeof(key)) == 0)
						{
							fz_drop_pixmap(ctx, val);
							move_to_front(cache, entry);
							val = fz_keep_pixmap(ctx, entry->val);
							fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
							return val;
						}
						entry = entry->bucket_next;
					}
				}

				entry = fz_malloc_struct(ctx, fz_glyph_cache_entry);
				entry->key = key;
				entry->hash = hash;
				entry->bucket_next = cache->entry[hash];
				if (entry->bucket_next)
					entry->bucket_next->bucket_prev = entry;
				cache->entry[hash] = entry;
				entry->val = fz_keep_pixmap(ctx, val);
				fz_keep_font(ctx, key.font);

				entry->lru_next = cache->lru_head;
				if (entry->lru_next)
					entry->lru_next->lru_prev = entry;
				else
					cache->lru_tail = entry;
				cache->lru_head = entry;

				cache->total += val->w * val->h;
				while (cache->total > MAX_CACHE_SIZE)
				{
					drop_glyph_cache_entry(ctx, cache->lru_tail);
				}

			}
		}
	}
	fz_always(ctx)
	{
		if (locked)
			fz_unlock(ctx, FZ_LOCK_GLYPHCACHE);
	}
	fz_catch(ctx)
	{
		if (caching)
			fz_warn(ctx, "cannot encache glyph; continuing");
		else
			fz_rethrow(ctx);
	}

	return val;
}