Пример #1
0
static PyObject *
BinaryHeap_put(BinaryHeap* self, PyObject *item)
{
    append(self, item);
    int result = swim(self);
    return Py_BuildValue("i", result);
}
Пример #2
0
Файл: pq.c Проект: alltom/ckv
/* returns 0 if the queue is full and could not be resized */
int
queue_insert(PQ q, double priority, void *data)
{
	int i;
	
	if(q->count == q->capacity) {
		int new_capacity = q->capacity == 0 ? 1 : q->capacity * 2;
		
		PQItem *new_items = (PQItem *)malloc(sizeof(PQItem) * (new_capacity + 1));
		
		if(new_items == NULL)
			return 0;
		
		for(i = 1; i <= q->count; i++)
			new_items[i] = q->items[i];
		
		free(q->items);
		
		q->items = new_items;
		q->capacity = new_capacity;
	}
	
	q->count++;
	q->items[q->count].priority = priority;
	q->items[q->count].data = data;
	q->items[q->count].floats = 0;
	swim(q, q->count);
	
	return 1;
}
Пример #3
0
inline void push(PriorityQueue<T>& pq, T const& item) {
	array::push_back(pq._data, item);
	swim(pq, size(pq));
	#if defined(TOGO_TEST_PRIORITY_QUEUE)
		TOGO_DEBUG_ASSERTE(is_valid(pq));
	#endif
}
Пример #4
0
void node_heap::update(node *n) {
    node *p = parentNode(n);
    node *c = largerChild(n);
    if (p != 0 && (*n) < (*p)) swim(n);
    else if (c != 0 && (*c) < (*n)) sink(n);
    else return;
}
Пример #5
0
void insert(element_t x, heap_t q)
{
    if (!is_full(q)) {
        q->array[++q->size] = x;
        swim(q->size, q);
    }
}
Пример #6
0
void node_heap::push(node *n) {
    if (number == capacity) {
        std::cout << "Heap full" << std::endl;
        return;
    }
    n->set_index(number);
    heap[number++] = n;
    swim(n);
}
Пример #7
0
 void insert(const T& val) {
     if (lastn+1 == tree.size()) {
         vector<T> ntree (tree.size()*2);
         copy(all(tree), begin(ntree));
         tree = ntree;
     }
     tree[lastn] = val;
     lastn++;
     swim(lastn-1);
 }
Пример #8
0
void node_heap::swim(node *n) {
    node *p = parentNode(n);
    if (p == 0) return;

    if ((*n) < (*p)) {
        exch(n, p);
        swim(n);
    } else {
        return;
    }
}
Пример #9
0
int zv_pq_insert(zv_pq_t *zv_pq, void *item) {
    if (zv_pq->nalloc + 1 == zv_pq->size) {
        if (resize(zv_pq, zv_pq->size * 2) < 0) {
            return -1;
        }
    }

    zv_pq->pq[++zv_pq->nalloc] = item;
    swim(zv_pq, zv_pq->nalloc);

    return ZV_OK;
}
Пример #10
0
Файл: pq.c Проект: alltom/ckv
void
remove_queue_items(PQ q, void *data)
{
	int i;
	
	for(i = q->count; i >= 1; i--) {
		if(q->items[i].data == data) {
			q->items[i].floats = 1;
			swim(q, i);
			remove_queue_min(q);
		}
	}
}
Пример #11
0
void
pqueue_insert (PQueue *pqueue,
               Node *data,
               guint priority)
{
  guint index;

  pqueue->elements[++(pqueue->size)].data = data;
  pqueue->elements[pqueue->size].priority = priority;

  index = data->j * pqueue->width + data->i;
  pqueue->map[index] = pqueue->size;

  swim (pqueue, pqueue->size);
}
Пример #12
0
PQ_STATUS insert_item_pq(priority_queue_t* ptr,void *item) {
	uint size = ptr->size;
	if((size + 1) == ptr->capacity) {
		int ret = resize(ptr,ptr->capacity * 2);
		if(ret) {
			LOG_ERROR("insert_item pq resize error!%s","");
			return RESIZE_PQ_FAIL;
		}
	}

	/*优先队列添加元素,重新维护优先队列结构*/
	ptr->priority_queue[++ptr->size] = item;
	swim(ptr,ptr->size);
	
	return INSERT_PQ_OK;
}
Пример #13
0
  void push(int n, bool fix = true) {
    if (len == count || n >= len) {
#ifdef RESIZE
      int newlen = 2 * len;
      while (n >= newlen) newlen *= 2;
      int *newq = new int[newlen], *newloc = new int[newlen];
      rep(i,0,len) newq[i] = q[i], newloc[i] = loc[i];
      memset(newloc + len, 255, (newlen - len) << 2);
      delete[] q, delete[] loc;
      loc = newloc, q = newq, len = newlen;
#else
      assert(false);
#endif
    }
    assert(loc[n] == -1);
    loc[n] = count, q[count++] = n;
    if (fix) swim(count-1); }
Пример #14
0
	void MyPQ<Dtype>::insert(Dtype new_data) {
		this->data[N+1] = new_data;
		swim(N+1);
		N += 1;
	}
Пример #15
0
void smaug() {
    int newWakeup = 1;
    pid_t localid = getpid();
    smaugID = localid;
    printf("DRAGONDRAGONDRAGON      Smaug[%d] is born\n", smaugID);
    setpgid(smaugID, dragonGID);
    printf("DRAGONDRAGONDRAGON      Smaug[%d] goes to sleep\n", smaugID);

    semopChecked(semID, &WaitPDragonJewel, 1);
    *numDragonJewel = INIT_JEWEL;
    semopChecked(semID, &SignalPDragonJewel, 1);

    semopChecked(semID, &WaitSDragonWakeUp, 1);
    newWakeup = 1;
    int time = 0;
    while (1) {  //sleeping/waking loop
        printf("DRAGONDRAGONDRAGON      Smaug[%d] wakes up\n", smaugID);
        while (1) {  //swimming loop
            // the meal, thief, hunter process in this loop
            int onceMeal = 0;
            int onceThief = 0;
            int onceHunter = 0;

            //looking for meals
            semopChecked(semID, &WaitPNumMeal, 1);
            while(*numMeal > 0 && onceMeal < MEAL_ONCE) {
                if(newWakeup==1) newWakeup = 0;
                else semopChecked(semID, &WaitSDragonWakeUp, 1);

                printf("DRAGONDRAGONDRAGON      Smaug[%d] finds the %d-th snack\n", smaugID, onceMeal+1);
                eat();
                onceMeal ++;
            }
            semopChecked(semID, &SignalPNumMeal, 1);

            //check meal this round
            if(onceMeal > 0) {  //if the
                swim();
                continue;
            }
            else printf("DRAGONDRAGONDRAGON      Smaug[%d] finds no snacks, it looks for thieves\n", smaugID);

            //looking for thieves
            semopChecked(semID, &WaitPThiefPath, 1);
            while(*numThiefPath > 0 && onceThief < THIEF_ONCE) {
                if(newWakeup==1) newWakeup = 0;
                else semopChecked(semID, &WaitSDragonWakeUp, 1);

                printf("DRAGONDRAGONDRAGON      Smaug[%d] finds the %d-th thief\n", smaugID, onceThief+1);
                play();
                onceThief ++;
            }
            semopChecked(semID, &SignalPThiefPath, 1);

            //check thief this round
            if(onceThief > 0) {
                printf("DRAGONDRAGONDRAGON      Smaug[%d] is a happy dragon now, it curls up its jewels and goes to sleep\n", smaugID);
                break;
            }
            else printf("DRAGONDRAGONDRAGON      Smaug[%d] finds no thieves, it looks for hunters\n", smaugID);


            //looking for hunters
            semopChecked(semID, &WaitPHunterPath, 1);
            while(*numHunterPath > 0 && onceHunter < HUNTER_ONCE) {
                if(newWakeup==1) newWakeup = 0;
                else semopChecked(semID, &WaitSDragonWakeUp, 1);

                printf("DRAGONDRAGONDRAGON      Smaug[%d] finds the %d-th hunter\n", smaugID, onceHunter + 1);
                fight();
                onceHunter++;
            }
            semopChecked(semID, &SignalPHunterPath, 1);

            //check hunter
            if(onceHunter > 0) {
                printf("DRAGONDRAGONDRAGON      Smaug[%d] fought with the treasure hunter and goes to swim\n", smaugID);
                swim();
                continue;
            }
            else {
                printf("DRAGONDRAGONDRAGON      Smaug[%d] finds no hunters, there is nothing left, smaug goes to sleep\n", smaugID);
                break;
            }
        }

        printf("DRAGONDRAGONDRAGON      Smaug[%d] goes to sleep\n", smaugID);
        semopChecked(semID, &WaitSDragonWakeUp, 1);
        newWakeup = 1;
    }
}
Пример #16
0
 // 插入元素
 void insert(Key value)
 {
     items[++N] = value;
     swim(N);
 }
Пример #17
0
	void insert(int n){
		arr[++N] = n;
		swim(N);
	}
Пример #18
0
void insert(min_pq m, item item) {
				if (m->size - 1 == m->num)
								resize_min_pq(m, 2 * m->size);
				m->items[++(m->num)] = item;
				swim(m, m->num);
}
Пример #19
0
void insert(int a)	 
{
	heap[++hs] = a;
	swim(hs);
}
Пример #20
0
void push(struct MinHeap *heap,int data)
{
  assert(!isFull(heap));
  heap->arr[heap->size++] = data;
  swim(heap,heap->size-1);
}
Пример #21
0
int main(){
    while(~scanf("%d %d", &n, &m)){
        if(n==0 && m==0) break;
        init();
        while(m--){
            int u, v;
            scanf("%d %d", &u, &v);
            add(u, v, head, edge);
            add(v, u, head, edge);
        }
        for(int i = 1; i <= n; i++){
            if(!dfn[i]) tarjan(i, -1);
        }
        cnt = 0;
        for(int u = 1; u <= n; u++){
            for(int i = head[u]; i != -1; i = edge[i].next){
                int v = edge[i].v;
                if(bccno[u] != bccno[v]){
                    add(bccno[u], v, head1, edge1);
                }
            }
        }
        d[1] = 0;
        dfs(1, 1);
        scanf("%d", &m);
        while(m--){
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            int uu = bccno[u], vv = bccno[v], ww = bccno[w];
            int uv = lca(uu, vv), uw = lca(uu, ww), vw = lca(vv, ww);
            if(u==v){
                if(u==w) puts("Yes");
                else puts("No");
            }
            else if(u==w || v==w) puts("Yes");
            else if(uu==vv){
                if(uu==ww) puts("Yes");
                else puts("No");
            }
            else if(uu==ww){
                int ret;
                if(uv==uu){
                    vv = swim(vv, ww);
                    ret = judge(vv, u);
                }
                else ret = judge(p[uu][0], u);
                if(ret!=u) puts("Yes");
                else puts("No");
            }
            else if(vv==ww){
                int ret;
                if(uv==vv){
                    uu = swim(uu, ww);
                    ret = judge(uu, v);
                }
                else ret = judge(p[vv][0],v);
                if(ret!=v) puts("Yes");
                else puts("No");
            }
            else{
                if(uv==uw && vw==ww){
                    int ret1, ret2;
                    vv = swim(vv, ww);
                    ret1 = judge(vv, w);
                    if(uv==ww){
                        uu = swim(uu, ww);
                        ret2 = judge(uu, w);
                    }
                    else ret2 = judge(p[ww][0],w);
                    if(ret1!=w && ret2!=w && ret1==ret2) puts("No");
                    else puts("Yes");
                }
                else if(uv==vw && uw==ww){
                    int ret1, ret2;
                    uu = swim(uu, ww);
                    ret1 = judge(uu, w);
                    if(uv==ww){
                        vv = swim(vv, ww);
                        ret2 = judge(vv, w);
                    }
                    else ret2 = judge(p[ww][0],w);
                    if(ret1!=w && ret2!=w && ret1==ret2) puts("No");
                    else puts("Yes");
                }
                else puts("No");
            }
        }
    }
    return 0;
}
Пример #22
0
 void update_key(int n) {
   assert(loc[n] != -1), swim(loc[n]), sink(loc[n]); }
Пример #23
0
void insert(int* a, int nKey)
{
  a[++last] = nKey;

  swim(a, last);
}
Пример #24
0
 void GoldFish::move()const{
   swim();
 }
Пример #25
0
void Heap::insert(int key)
{
	this->hp.push_back(key);
	swim(++N);
}
Пример #26
0
void insert(int key)
{ 
    pq[++cnt] = key;
    swim(cnt); 
}
Пример #27
0
void insert(int a[], int x)
{
    a[++N] = x;
    swim(a, N);  // move to the right place
}