Ejemplo n.º 1
0
Archivo: backend.c Proyecto: jvns/snake
void add_new_food(Board* board) {
  PointList* new_food;
  do {
    new_food = create_random_cell(board->xmax, board->ymax);
  } while(list_contains(new_food, board->foods) || list_contains(new_food, board->snake));
  new_food->next = board->foods;
  board->foods = new_food;
}
int main() {
    add_to_list(42);
    std::cout << "contains(1) = " << list_contains(1)
              << ", contains(42) = " << list_contains(42) 
              << std::endl;

    return 0;
}
Ejemplo n.º 3
0
void test_list(void) {
    int i, arr[MAX];
    int k, s;
    list *l = EMPTY_LIST;

    generate(arr, MAX);

    for (i = 0; i < MAX; i++) {
        l = list_create(arr[i], 0);

        assert(l != EMPTY_LIST);
        assert(l->key == arr[i]);
        assert(list_count(l) == 1);
        assert(list_contains(l, arr[i]));

        l = list_destroy(l);

        assert(l == EMPTY_LIST);
    }
    for (i = 0; i < MAX; i++) {
        l = list_insert(l, arr[i], arr[i]);
        assert(list_contains(l, arr[i]));
    }
    assert(list_ordered(l));
    for (i = 0; i < MAX; i++) {
        l = list_remove_first(l, &k, &s);
        assert(list_ordered(l));
    }
    l = list_destroy(l);

    l = list_insert(l, 5, 0);
    l = list_insert(l, 4, 1);
    l = list_insert(l, 1, 2);
    l = list_insert(l, 2, 6);
    l = list_insert(l, 7, 4);
    l = list_insert(l, 0, 5);
    list_print(l);

    l = list_insert(l, 20, 10);
    l = list_insert(l, 25, -1);
    l = list_insert(l, 30, 3);
    list_print(l);

    l = list_remove_first(l, &k, &s);
    s = 15;
    l = list_insert(l, k, s);
    list_print(l);

    printf("count: %d\n", list_count(l));
    printf("last: %d\n", list_last(l)->key);

    l = list_destroy(l);
}
Ejemplo n.º 4
0
void enlist_conn(conn *c, conn **list) {
    LIBEVENT_THREAD *thr = c->thread;
    assert(list == &thr->pending_io);
    if ((c->list_state & LIST_STATE_PROCESSING) == 0) {
        assert(!list_contains(thr->pending_io, c));
        assert(c->next == NULL);
        c->next = *list;
        *list = c;
        assert(list_contains(*list, c));
        assert(!has_cycle(*list));
    } else {
        c->list_state |= LIST_STATE_REQ_PENDING_IO;
    }
}
Ejemplo n.º 5
0
/* Tries to add the item (PROD, DOT) to LIST.  If the item is added,
   returns nonzero.  Otherwise, the item was already in LIST, and zero
   is returned. */
static int
list_add (struct list *list, int prod, int dot)
{
  assert (list != NULL);

  if (list_contains (list, prod, dot))
    return 0;

  if (list->n >= list->m)
    {
      if (list->m == 0)
	list->m = 16;
      else
	list->m *= 2;

      list->contents = xrealloc (list->contents,
				 sizeof *list->contents * list->m);
    }
  assert (list->n < list->m);

  list->contents[list->n].prod = prod;
  list->contents[list->n].dot = dot;
  list->n++;

  return 1;
}
Ejemplo n.º 6
0
int list_contains(list *l, int key) {
    if (!l) return 0;
    else {
        if (l->key == key) return 1;
        else return list_contains(l->tail, key);
    }
}
Ejemplo n.º 7
0
void finder(const int value_to_find)
{
    while (!list_contains(value_to_find)) {
        sleep(1);
    }
    std::cout << "value found!" << std::endl;
}
Ejemplo n.º 8
0
size_t
blacklist_glob(Blacklist *blacklist, const char *pattern)
{
	glob_t g;
	size_t count = 0;

	assert(blacklist != NULL);
	assert(pattern != NULL);

	memset(&g, 0, sizeof(glob_t));

	DEBUGF("blacklist", "Adding pattern to blacklist: %s", pattern);

	int rc = glob(pattern, GLOB_TILDE, NULL, &g);

	if(!rc)
	{
		for(size_t i = 0; i < g.gl_pathc; ++i)
		{
			if(!list_contains(blacklist, g.gl_pathv[i]))
			{
				TRACEF("blacklist", "Appending file to blacklist: %s", g.gl_pathv[i]);
				list_append(blacklist, utils_strdup(g.gl_pathv[i]));
			}
		}

		count = g.gl_pathc;
	}

	globfree(&g);

	return count;
}
Ejemplo n.º 9
0
int discovery_search_result(struct Upnp_Discovery *event) {
    if (event->ErrCode != UPNP_E_SUCCESS) {
        fprintf(stderr, "Error in discovering device\n");
        exit(-1);
    }

    IXML_Document *desc = NULL;
    int ret = UpnpDownloadXmlDoc(event->Location, &desc);
    
    if (ret != UPNP_E_SUCCESS) {
        fprintf(stderr, "Error in obtaining device description\n");
        exit(-1);
    }
    
    const char *UUID = get_device_property(desc, "UDN");

    if (!list_contains(&devices, UUID, chromecast_matches_UUID)) {
        struct chromecast_device *device = malloc(sizeof(struct chromecast_device));
        device->addr = ((struct sockaddr_in *)&event->DestAddr)->sin_addr;     
        device->device_name = create_string_copy(get_device_property(desc, "friendlyName"));
        device->device_type = create_string_copy(get_device_property(desc, "deviceType"));
        device->device_UUID = create_string_copy(UUID);
        device->device_OS = create_string_copy(event->Os);
        device->device_manufacturer = create_string_copy(get_device_property(desc, "manufacturer"));
        device->device_model_name = create_string_copy(get_device_property(desc, "modelName"));
        device->service_type = create_string_copy(get_device_property(desc, "serviceType"));
        device->service_version = create_string_copy(event->ServiceVer);
        device->service_id = create_string_copy(get_device_property(desc, "serviceId"));
        list_add_sync(&devices, device);
        print_device(device);
    }
    ixmlDocument_free(desc);
    return 0;
}
Ejemplo n.º 10
0
int test(int M, int N)
{
  Hinit;

  list_t *lists[NO_LISTS];

  build_lists(lists, NO_LISTS, N, M);

  RUN_GC;

  int hit_ratio = 0;
  for (int i = 0; i < N; ++i)
    {
      uint64_t r = RANDOM;
      if (list_contains(lists[r % NO_LISTS], r))
        {
          ++hit_ratio;
        }
    }

  Hexit;
  free_lists(lists, NO_LISTS);

  return hit_ratio;
}
Ejemplo n.º 11
0
bool
blacklist_matches(Blacklist *blacklist, const char *filename)
{
	assert(blacklist != NULL);
	assert(filename != NULL);

	return list_contains(blacklist, (void *)filename);
}
Ejemplo n.º 12
0
lnode_t *list_prev(list_t *list, lnode_t *lnode)
{
    assert (list_contains(list, lnode));

    if (lnode->prev == list_nil(list))
        return NULL;
    return lnode->prev;
}
Ejemplo n.º 13
0
lnode_t *list_next(list_t *list, lnode_t *lnode)
{
    assert (list_contains(list, lnode));

    if (lnode->next == list_nil(list))
        return NULL;
    return lnode->next;
}
Ejemplo n.º 14
0
Archivo: backend.c Proyecto: jvns/snake
enum Status move_snake(Board* board, enum Direction dir) {
  // Create a new beginning. Check boundaries.
  PointList* beginning = next_move(board, dir);
  if (beginning == NULL) {
    return FAILURE;
  }

  // If we've gone backwards, don't do anything
  if (board->snake->next && is_same_place(beginning, board->snake->next)) {
    beginning->next = NULL;
    free(beginning);
    return SUCCESS;
  }

  // Check for collisions
  if (list_contains(beginning, board->snake)) {
    return FAILURE;
  }

  // Check for food
  if (list_contains(beginning, board->foods)) {
    // Attach the beginning to the rest of the snake;
    beginning->next = board->snake;
    board->snake = beginning;
    remove_from_list(beginning, &(board->foods));
    add_new_food(board);

    return SUCCESS;
  }

  // Attach the beginning to the rest of the snake
  beginning->next = board->snake;
  board->snake = beginning;


  // Cut off the end
  PointList* end = board->snake;
  while(end->next->next) {
    end = end->next;
  }
  free(end->next);
  end->next = NULL;

  return SUCCESS;
}
Ejemplo n.º 15
0
void list_ins_before(list_t *list, lnode_t *node, lnode_t *succ)
{
    lnode_t *pred = succ->prev;

    assert (node != NULL);
    assert (!list_contains(list, node));
    assert (!lnode_is_in_a_list(node));
    assert (succ == list_nil(list) || list_contains(list, succ));
    assert (list->nodecount + 1 > list->nodecount);

    node->next = succ;
    node->prev = pred;
    pred->next = node;
    succ->prev = node;
    list->nodecount++;

    assert (list->nodecount <= list->maxcount);
}
Ejemplo n.º 16
0
static void copy_player_name(struct gr_core_player *p, void *args)
{
    struct list *l = (struct list *) args;

    if (list_contains(l, p->nickname))
        return;

    list_add(l, strdup(p->nickname));
}
Ejemplo n.º 17
0
void list_ins_after(list_t *list, lnode_t *node, lnode_t *pred)
{
    lnode_t *succ = pred->next;

    assert (node != NULL);
    assert (!list_contains(list, node));
    assert (!lnode_is_in_a_list(node));
    assert (pred == list_nil(list) || list_contains(list, pred));
    assert (list->nodecount + 1 > list->nodecount);

    node->prev = pred;
    node->next = succ;
    succ->prev = node;
    pred->next = node;
    list->nodecount++;

    assert (list->nodecount <= list->maxcount);
}
Ejemplo n.º 18
0
/**
 * Fonction permettant d'afficher une case en fonction de son type.
 * @param square Le type de case à afficher.
 */
static void print_square(Square * square) {
	if (list_contains(g_player_view, &square)) {
		set_background_color(square);
		print_foreground(square);
	} else {
		ansi_set_bg_color(ANSI_GREY);
		putchar(' ');
	}
}
Ejemplo n.º 19
0
void
list_ins_before (list_t * list, lnode_t * newnode, lnode_t * thisnode)
{
	lnode_t *that = thisnode->prev;

	nassert (newnode != NULL);
	nassert (!list_contains (list, newnode));
	nassert (!lnode_is_in_a_list (newnode));
	nassert (thisnode == list_nil (list) || list_contains (list, thisnode));
	nassert (list->nodecount + 1 > list->nodecount);

	newnode->next = thisnode;
	newnode->prev = that;
	that->next = newnode;
	thisnode->prev = newnode;
	list->nodecount++;

	nassert (list->nodecount <= list->maxcount);
}
Ejemplo n.º 20
0
void
list_extract (list_t * dest, list_t * source, lnode_t * first, lnode_t * last)
{
	listcount_t moved = 1;

	nassert (first == NULL || list_contains (source, first));
	nassert (last == NULL || list_contains (source, last));

	if (first == NULL || last == NULL)
		return;

	/* adjust the destination list so that the slice is spliced out */

	first->prev->next = last->next;
	last->next->prev = first->prev;

	/* graft the splice at the end of the dest list */

	last->next = &dest->nilnode;
	first->prev = dest->nilnode.prev;
	dest->nilnode.prev->next = first;
	dest->nilnode.prev = last;

	while (first != last) {
		first = first->next;
		nassert (first != list_nil (source));	/* oops, last before first! */
		moved++;
	}

	/* nassert no overflows */
	nassert (source->nodecount - moved <= source->nodecount);
	nassert (dest->nodecount + moved >= dest->nodecount);

	/* nassert no weirdness */
	nassert (moved <= source->nodecount);

	source->nodecount -= moved;
	dest->nodecount += moved;

	/* nassert list sanity */
	nassert (list_verify (source));
	nassert (list_verify (dest));
}
Ejemplo n.º 21
0
//i.e. used in semaphore_up
void waitobject_wakeup_and_unlock_single(WaitObject* obj, Thread* wakeup,
                                         uint flags)
{
    assert_spinlock(&obj->lock);
    
    assert(list_contains(&obj->wait_queue, wakeup));
    list_remove(&obj->wait_queue, wakeup);
    thread_wakeup_safe(wakeup, obj);
    
    spinlock_unlock(&obj->lock);
}
Ejemplo n.º 22
0
static bool current_has_lock_on_disk (int d_id)
{
	list_node_t *result;
	osprd_info_t *d = &osprds[d_id];

	spin_lock(&d->mutex);
	result = list_contains(d->lock_holder_l, current->pid);
	spin_unlock(&d->mutex);

	return result ? true : false;
}
Ejemplo n.º 23
0
Archivo: list.c Proyecto: mkopta/sofa
unsigned list_insert_list_uniq(const struct list *source, struct list *dest,
                               int (*compar)(const void *, const void *)) {
    unsigned inserted = 0;
    struct list_node *n = source->head;
    while (n != NULL) {
        if (!list_contains(dest, n->data, compar)) {
            list_push(dest, n->data);
            inserted++;
        }
        n = n->next;
    }
    return inserted;
}
Ejemplo n.º 24
0
FileWatch* add_file_watch_action(World* world, const char* filename, Value* action)
{
    // Fetch the FileWatch entry.
    FileWatch* watch = add_file_watch(world, filename);

    // Check if this exact action already exists, if so do nothing.
    if (list_contains(&watch->onChangeActions, action))
        return watch;

    // Add action
    copy(action, list_append(&watch->onChangeActions));

    return watch;
}
Ejemplo n.º 25
0
void
list_process (list_t * list, void *context, void (*function) (list_t * list, lnode_t * lnode, void *context))
{
	lnode_t *node = list_first_priv (list), *next, *nil = list_nil (list);

	while (node != nil) {
		/* check for callback function deleting */
		/* the next node from under us          */
		nassert (list_contains (list, node));
		next = node->next;
		function (list, node, context);
		node = next;
	}
}
Ejemplo n.º 26
0
/**
 * Return the tentative choice for growing
 */
int greedyBFS_peek(list_graph* graph, partition* p, int node){

	edge* head = graph->vertex[node].edge_list;

	while (head){
		if(graph->vertex[head->id].color < 2){
			if(!list_contains(&(p->constraints), head->id))
				return head->wgt;
		}
		head = head->next;
	}

	return -1;
}
Ejemplo n.º 27
0
lnode_t *list_delete(list_t *list, lnode_t *del)
{
    lnode_t *next = del->next;
    lnode_t *prev = del->prev;

    assert (list_contains(list, del));

    prev->next = next;
    next->prev = prev;
    list->nodecount--;

    del->next = del->prev = NULL;

    return del;
}
Ejemplo n.º 28
0
/**
 * Expand a partition
 */
list* greedyBFS_grow(list_graph* graph, partition* p, int source, int dest){

	//Prepare the queue
	list* q = (list*) malloc ( sizeof(list));
	list_init(q);

	//Get the Adjacency list of the vertex
	if (graph->vertex[dest].color == 2){
		//Someone else already took our node
		return NULL;
	}

	//Check for constraints
	if(list_contains(&(p->constraints), dest)){
		return NULL;
	}

	//Put the node in the partition
	graph->vertex[dest].color = 2;
	partition_add(p, dest, 0);

	//Add the new constraints to the partition
	partition_constraint_add(p, &(graph->vertex[dest].constraints));


	//Check the adjacency list
	edge* head = graph->vertex[dest].edge_list;

	while(head != NULL){

		if(graph->vertex[head->id].color < 2){

			//Color Grey
			graph->vertex[head->id].color = 1;

			//Enqueue
			list_insert(q, head->id);
		}

		head = head->next;
	}
	return q;
}
Ejemplo n.º 29
0
static int find_drive_id_for_waiter (pid_t p)
{
	int i;
	list_node_t *elem = NULL;
	for(i = 0; i < NOSPRD; i++)
	{
		spin_lock(&(osprds[i].mutex));
		elem = list_contains(osprds[i].lock_waiter_l, p);
		spin_unlock(&(osprds[i].mutex));

		if(elem && elem->visited == false)
		{
			elem->visited = true;
			return i;
		}
	}

	// If we get here, we've visited the node before
	return -1;
}
Ejemplo n.º 30
0
/**
 * Initial discovery to populate a list form a seed
 */
void greedyBFS_discovery(list_graph* graph, list* list, partition* p, int seed){

	edge* head = graph->vertex[seed].edge_list;

	//Check the adjacency list
	while(head != NULL){

		if(graph->vertex[head->id].color < 2){
			if(!list_contains(&(p->constraints), head->id)){
				//Color Grey
				graph->vertex[head->id].color = 1;

				//Enqueue
				list_insert(list, head->id);
			}
		}

		head = head->next;
	}
}