Example #1
0
/* Listbox item adding function */
static inline void
listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
{
    if (l->list == NULL)
    {
        l->list = g_queue_new ();
        pos = LISTBOX_APPEND_AT_END;
    }

    switch (pos)
    {
    case LISTBOX_APPEND_AT_END:
        g_queue_push_tail (l->list, e);
        break;

    case LISTBOX_APPEND_BEFORE:
        g_queue_insert_before (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
        break;

    case LISTBOX_APPEND_AFTER:
        g_queue_insert_after (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
        break;

    case LISTBOX_APPEND_SORTED:
        g_queue_insert_sorted (l->list, e, (GCompareDataFunc) listbox_entry_cmp, NULL);
        break;

    default:
        break;
    }
}
Example #2
0
TEST_F(GQueueTest, insertAfter)
{
	int testData1 = 42;
	int testData2 = 1337;

	g_queue_insert_after(queue, NULL, &testData1);
	ASSERT_TRUE(queue->head != NULL) << "queue head should not be NULL after inserting an element";
	ASSERT_EQ(queue->head, queue->tail) << "queue tail should be equal to head after inserting an element";
	ASSERT_EQ(&testData1, queue->head->data) << "queue element data should be set correctly";
	ASSERT_TRUE(queue->head->next == NULL) << "queue head should not have a next element after inserting one";
	ASSERT_TRUE(queue->head->prev == NULL) << "queue tail should not have a next element after inserting one";
	ASSERT_EQ(1, queue->length) << "queue length should be one after inserting an element";

	g_queue_insert_after(queue, queue->head, &testData2);
	ASSERT_TRUE(queue->head != NULL) << "queue head should not be NULL after inserting another element";
	ASSERT_NE(queue->head, queue->tail) << "queue tail should not be equal to head after inserting another element";
	ASSERT_EQ(&testData1, queue->head->data) << "second queue element data should be set correctly";
	ASSERT_EQ(queue->tail, queue->head->next) << "second queue head should have tail as next element";
	ASSERT_TRUE(queue->head->prev == NULL) << "queue head should not have a previous element";
	ASSERT_EQ(&testData2, queue->tail->data) << "queue tail data should be set correctly";
	ASSERT_EQ(queue->head, queue->tail->prev) << "queue tail should have head as previous element";
	ASSERT_TRUE(queue->tail->next == NULL) << "queue tail should not have a next element";
	ASSERT_EQ(2, queue->length) << "queue length should be two after inserting another element";
}
Example #3
0
void
_ddm_data_model_add_work_item (DDMDataModel    *model,
                               DDMWorkItem     *item)
{
    GList *l;
    
    _ddm_work_item_ref(item);

    /* Two situations:
     *
     * We have a new item with min_serial = -1; it probably goes right
     * near the beginning of the queue.
     *
     * We have an item that now has a min_serial because we've sent a
     * request to the server; it goes at the end of the queue.
     *
     * Slightly over-optimize here by handling the two cases separately.
     * Note that both cases are different from g_queue_insert_sorted()
     * because we insert after on the tie-break to avoid reordering
     * when we pull things off the front.
     */
    if (_ddm_work_item_get_min_serial(item) == -1) {
        for (l = model->work_items->head; l; l = l->next) {
            if (compare_work_items(l->data, item, NULL) > 0)
                break;
        }

        if (l == NULL)
            g_queue_push_tail(model->work_items, item);
        else
            g_queue_insert_before(model->work_items, l, item);
        
    } else {
        for (l = model->work_items->tail; l; l = l->prev) {
            if (compare_work_items(l->data, item, NULL) <= 0)
                break;
        }
        
        if (l == NULL)
            g_queue_push_head(model->work_items, item);
        else
            g_queue_insert_after(model->work_items, l, item);
    }
    

    g_debug("Scheduling flush due to work item");
    ddm_data_model_schedule_flush(model);
}
gpointer
mex_download_queue_enqueue (MexDownloadQueue               *queue,
                            const char                     *uri,
                            MexDownloadQueueCompletedReply  reply,
                            gpointer                        userdata)
{
  MexDownloadQueuePrivate *priv;
  DQTask *task;

  g_return_val_if_fail (MEX_IS_DOWNLOAD_QUEUE (queue), NULL);
  g_return_val_if_fail (uri, NULL);

  priv = queue->priv;

  task = g_slice_new0 (DQTask);
  task->any.uri = g_strdup (uri);
  task->any.queue = queue;
  task->any.callback = reply;
  task->any.userdata = userdata;

  MEX_NOTE (DOWNLOAD_QUEUE, "queueing download: %s", uri);

  if (g_str_has_prefix (uri, "http://"))
    g_queue_push_tail (priv->queue, task);
  else
    {
      /* Push local requests before web requests */
      if (!priv->last_local)
        {
          g_queue_push_head (priv->queue, task);
          priv->last_local = priv->queue->head;
        }
      else
        {
          g_queue_insert_after (priv->queue, priv->last_local, task);
          priv->last_local = priv->last_local->next;
        }
    }

  process_queue (queue);

  g_object_notify (G_OBJECT (queue), "queue-length");

  return task;
}
static GList *
priority_segment_alloc_node (TrackerPriorityQueue *queue,
                             gint                  priority)
{
	PrioritySegment *segment = NULL;
	gboolean found = FALSE;
	gint l, r, c;
	GList *node;

	/* Perform binary search to find out the segment for
	 * the given priority, create one if it isn't found.
	 */
	l = 0;
	r = queue->segments->len - 1;

	while (queue->segments->len > 0 && !found) {
		c = (r + l) / 2;
		segment = &g_array_index (queue->segments, PrioritySegment, c);

		if (segment->priority == priority) {
			found = TRUE;
			break;
		} else if (segment->priority > priority) {
			r = c - 1;
		} else if (segment->priority < priority) {
			l = c + 1;
		}

		if (l > r) {
			break;
		}
	}

	if (found) {
		/* Element found, append at the end of segment */
		g_assert (segment != NULL);
		g_assert (segment->priority == priority);

		g_queue_insert_after (&queue->queue, segment->last_elem, NULL);
		node = segment->last_elem = segment->last_elem->next;
	} else {
		PrioritySegment new_segment = { 0 };

		new_segment.priority = priority;

		if (segment) {
			g_assert (segment->priority != priority);

			/* Binary search got to one of the closest results,
			 * but we may have come from either of both sides,
			 * so check whether we have to insert after the
			 * segment we got.
			 */
			if (segment->priority > priority) {
				/* We have to insert to the left of this element */
				g_queue_insert_before (&queue->queue, segment->first_elem, NULL);
				node = segment->first_elem->prev;
			} else {
				/* We have to insert to the right of this element */
				g_queue_insert_after (&queue->queue, segment->last_elem, NULL);
				node = segment->last_elem->next;
				c++;
			}

			new_segment.first_elem = new_segment.last_elem = node;
			g_array_insert_val (queue->segments, c, new_segment);
		} else {
			/* Segments list has 0 elements */
			g_assert (queue->segments->len == 0);
			g_assert (g_queue_get_length (&queue->queue) == 0);

			node = g_list_alloc ();
			g_queue_push_head_link (&queue->queue, node);
			new_segment.first_elem = new_segment.last_elem = node;

			g_array_append_val (queue->segments, new_segment);
		}
	}

	return node;
}