Exemple #1
0
/****************************************************************************************
* Function name - heap_remove_node
*
* Description -  Removes hnode from a certain slot position, reheapefies the heap 
* 		 and marks the slot in the ids array as available (-1)
*
* Input -       *h - pointer to an initialized heap
*               slot - index of the heap-array (slot), where to remove hnode
*               reserve_slot - true -means to reserve the slot
* Return Code/Output - On success - a valid hnode, on error - 0
****************************************************************************************/
hnode* heap_remove_node (heap*const h, const size_t slot, int reserve_slot)
{
  hnode* mved_end_node = 0;
  size_t parent_slot = 0;
  hnode* removed_node = h->heap[slot];
  size_t removed_node_id;

  if (!removed_node)
    {
      fprintf(stderr, "%s - error: null removed node.\n", __func__);
      return 0;
    }
	
  removed_node_id = removed_node->node_id;

  /* Decrement the heap size */
  h->curr_heap_size--;

  /* Reheapify only, if we're not deleting the last entry. */
  if (slot < h->curr_heap_size)
    {
      mved_end_node = h->heap[h->curr_heap_size];
      
      /* 
         Move the end node to the location being removed.  Update
         slot in the parallel <ids> array.
      */
      heap_put_node_to_slot (h, slot, mved_end_node);

      /* 	
         If the mved_end_node "node-value" < than the value its 
         parent, we move it up the heap.
      */
      parent_slot = HEAP_PARENT (slot);
		
      if ((*h->fcomp) (mved_end_node, h->heap[parent_slot])) // <
        {
          filter_up (h, slot);
        }
      else
        {
          filter_down (h, slot);
        }
    }
	
  /* Mark the node-id entry as free. */
  if (! reserve_slot)
    {
      release_node_id (h, removed_node_id);
    }

  return removed_node;
}
Exemple #2
0
/****************************************************************************************
* Function name - heap_push
*
* Description - Pushes a new hnode to the heap
*
* Input -       *h - pointer to an initialized heap
*               *nd - pointer to node
*               keep_node_id - flag, whether to respect the <node-id> from the node
*                              (support for periodical timer)
* Return Code/Output - On success - 0 or positive node-id, on error - (-1)
****************************************************************************************/
long heap_push (heap* const h, hnode* const nd, int keep_node_id)
{
  long new_node_id = -1;
	
  if (!h || !nd)
    {
      fprintf(stderr, "%s - error: wrong input\n", __func__);
      return -1;	
    }
	
  if (h->curr_heap_size >= h->max_heap_size)
    {
      if (heap_increase (h) == -1)
        {
          fprintf(stderr, "%s - error: heap_increase() failed\n", __func__);
          return -1;
        }
    }
	
  if (keep_node_id)
    {
      /* Re-scheduled timers */
      new_node_id = nd->node_id;
    }
  else
    {
      /* Get free node-id */
      new_node_id = heap_get_node_id (h);
	
      /* 
         Set node-id to the hnode, it will be further passed from 
         the node to the relevant slot in <ids> array by 
         heap_put_node_to_slot ().
      */
      nd->node_id = new_node_id;
    }
	
  /* Place the node to the end of heap */
  heap_put_node_to_slot (h, h->curr_heap_size, nd);
  
  /* Restore the heap structure */
  filter_up (h, h->curr_heap_size);
  
  /* Increase current number of nodes in heap */ 
  h->curr_heap_size++;
  
  return new_node_id;
}
mng_retcode filter_a_row (mng_datap pData)
{
  mng_retcode iRetcode;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_FILTER_A_ROW, MNG_LC_START)
#endif

  switch (*(pData->pWorkrow + pData->iFilterofs))
  {
    case 1  : {
                iRetcode = filter_sub     (pData);
                break;
              }
    case 2  : {
                iRetcode = filter_up      (pData);
                break;
              }
    case 3  : {
                iRetcode = filter_average (pData);
                break;
              }
    case 4  : {
                iRetcode = filter_paeth   (pData);
                break;
              }

    default : iRetcode = MNG_INVALIDFILTER;
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_FILTER_A_ROW, MNG_LC_END)
#endif

  return iRetcode;
}