void test_detectCycle()
{
    {
        int a[] = { 1, 2 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1;
        ListNode *e = p1->next;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
    {
        int a[] = { 1, 2, 3, 4, 5 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1->next->next;
        ListNode *e = p1->next->next->next->next;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
    {
        int a[] = { 1, 2 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1->next;
        ListNode *e = s;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }

    if (1)
    {
        int a[] = { 2, 2, 3, 4 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
}
Ejemplo n.º 2
0
static int detectCycle(BNode *node, BArc *src_arc)
{
	int value = 0;
	
	if (node->flag == 0) {
		int i;

		/* mark node as visited */
		node->flag = 1;

		for (i = 0; i < node->degree && value == 0; i++) {
			BArc *arc = node->arcs[i];
			
			/* don't go back on the source arc */
			if (arc != src_arc) {
				value = detectCycle(BLI_otherNode(arc, node), arc);
			}
		}
	}
	else {
		value = 1;
	}
	
	return value;
}
Ejemplo n.º 3
0
int main()
{
    struct timespec start, end;
    double cpu_time;
    LinkNode *head = CreateNode();
    assert((head->next != NULL && head->next->next !=NULL) && "no cycle");
    clock_gettime(CLOCK_REALTIME, &start);
    LinkNode *res = detectCycle(head);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    //use print to check the list 
    /*printf("%d->%d->%d->%d->%d->%d->%d\n",head->data,
				    head->next->data,
				    head->next->next->data,
				    head->next->next->next->data,
				    head->next->next->next->next->data,
				    head->next->next->next->next->next->data,
				    head->next->next->next->next->next->next->data);*/
    if (res != NULL)
	//printf("result: n%d\n",res->data);
	printf("execution time: %.10lf sec\n", cpu_time);
    else
	printf("NULL\n");
    return 0;
}
void c_AsyncFunctionWaitHandle::prepareChild(c_WaitableWaitHandle* child) {
  assert(!child->isFinished());

  // import child into the current context, throw on cross-context cycles
  asio::enter_context(child, getContextIdx());

  // detect cycles
  detectCycle(child);
}
Ejemplo n.º 5
0
int main() {
	struct ListNode *nil = NULL;
	assert(NULL == detectCycle(nil));

	struct ListNode n = { 0, &n };
	assert(&n == detectCycle(&n));

	struct ListNode n3 = { 0, NULL };
	struct ListNode n2 = { 0, &n3 };
	struct ListNode n1 = { 0, &n2 };
	n3.next = &n2;
	assert(&n2 == detectCycle(&n1));

	n.next = NULL;
	assert(NULL == detectCycle(&n));

	return 0;
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
	struct ListNode *head;
	int a[] = {1,2,3,4,5};
	mk_list(&head, a, 5);
	//head->next->next = head;
	printf("%d\n", hasCycle(head));
	printf("%d\n", detectCycle(head)->val);
	return 0;
}
Ejemplo n.º 7
0
ListNode *detectCycle(ListNode *head)
{
    if (head == NULL)
        return NULL;
    if (head->pNext == (ListNode *)detectCycle)
        return head;

    ListNode *temp = head->pNext;
    head->pNext = (ListNode *)detectCycle;

    return detectCycle(temp);
}
Ejemplo n.º 8
0
int main(int argc, char const *argv[])
{
    struct ListNode* head = BuildByLength(2);
    struct ListNode* tail = head;
    while(tail->next != NULL) {
        tail = tail->next;
    }
    tail->next = head->next;

    if(detectCycle(head) == NULL)
        printf("no cycle found!\n");
    else
        printf("%d\n", (detectCycle(head))->val);

    head = BuildByLength(2);
    if(detectCycle(head) == NULL)
        printf("no cycle found!\n");
    else
        printf("%d\n", (detectCycle(head))->val);

    return 0;
}
Ejemplo n.º 9
0
int main()
{
    ListNode *root = (ListNode *)malloc(sizeof(ListNode));
    root->val = 1;
    creat_tree(root);
    ListNode *cycle_begin = (ListNode *)detectCycle(root);
    if ( cycle_begin  ) {
        printf( "Yes the cycle begins at No.%d node!\n", cycle_begin->val );
    } else {
        printf( "There is no cycle!\n" );
    }
    return 0;
}
Ejemplo n.º 10
0
ListNode *detectCycle(ListNode *node)
{
    if ( !node || !node->next) {
        return NULL;
    }
    if ( find(node)  ) {
        return node;
    } else {
        return detectCycle(node->next);
    }

    return NULL;
}
Ejemplo n.º 11
0
void c_GenMapWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (;
       m_deps->iter_valid(m_iterPos);
       m_iterPos = m_deps->iter_next(m_iterPos)) {

    auto* current = tvAssertCell(m_deps->iter_value(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      auto k = m_deps->iter_key(m_iterPos);
      m_deps->set(k.asCell(), &child->getResult());
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  auto const parentChain = getFirstParent();
  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  UnblockChain(parentChain);
  decRefObj(this);
}
Ejemplo n.º 12
0
void c_AwaitAllWaitHandle::blockOnCurrent() {
  auto child = m_children[m_cur];
  assert(!child->isFinished());

  if (checkCycle) {
    try {
      detectCycle(child);
    } catch (const Object& cycle_exception) {
      markAsFailed(cycle_exception);
      return;
    }
  }

  child->getParentChain()
    .addParent(m_blockable, AsioBlockable::Kind::AwaitAllWaitHandle);
}
Ejemplo n.º 13
0
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    ListNode* node1 = new ListNode(1);
    ListNode* node2 = new ListNode(2);
    ListNode* node3 = new ListNode(3);
    ListNode* node4 = new ListNode(4);
    node1->next = NULL;
    node2->next = node3;
    node3->next = node4;
    node4->next = node2;
    ListNode *p = detectCycle(node1);
    if (p) std::cout << p->val << std::endl;
    else std::cout << "ops" << std::endl;
    return 0;
}
Ejemplo n.º 14
0
void c_AwaitAllWaitHandle::blockOnCurrent() {
  auto child = m_children[m_cur];
  assert(!child->isFinished());

  try {
    if (isInContext()) {
      child->enterContext(getContextIdx());
    }
    if (checkCycle) {
      detectCycle(child);
    }
  } catch (const Object& cycle_exception) {
    markAsFailed(cycle_exception);
    return;
  }

  blockOn(child);
}
Ejemplo n.º 15
0
void c_GenVectorWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (; m_iterPos < m_deps->size(); ++m_iterPos) {

    Cell* current = tvAssertCell(m_deps->at(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      auto result = child->getResult();
      m_deps->set(m_iterPos, &result);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitableWaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        detectCycle(child_wh);
        child_wh->getParentChain()
          .addParent(m_blockable, AsioBlockable::Kind::GenVectorWaitHandle);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  auto parentChain = getParentChain();
  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  parentChain.unblock();
  decRefObj(this);
}
Ejemplo n.º 16
0
int main()
{
    LinkNode *head = CreateNode();
    _head = head;
    LinkNode *res = detectCycle(head);
    //use print to check the list
    /*printf("%d->%d->%d->%d->%d->%d->%d\n",head->data,
    			    head->next->data,
    			    head->next->next->data,
    			    head->next->next->next->data,
    			    head->next->next->next->next->data,
    			    head->next->next->next->next->next->data,
    			    head->next->next->next->next->next->next->data);*/
    if (res != NULL)
        printf("result: n%d\n",res->data);
    else
        printf("NULL\n");
    return 0;
}
Ejemplo n.º 17
0
LinkNode *detectCycle(LinkNode *head) {
    assert((head->next != NULL && head->next->next !=NULL) && "no cycle");
    LinkNode *now = head;
    LinkNode *temp = now->next;
    if(temp->next != _head && temp->next != NULL) {
        now->next = _head;
        now = temp;
        temp = temp->next;
        return detectCycle(now);
    }
    else if(temp->next == NULL) {
        printf("no cycle\n");
        return NULL;
    }
    else {
        printf("cycle detected!\n");
        return temp;
    }
}
Ejemplo n.º 18
0
void c_GenVectorWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (; m_iterPos < m_deps->size(); ++m_iterPos) {

    Cell* current = tvAssertCell(m_deps->at(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      cellSet(child->getResult(), *current);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitableWaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  done();
}
Ejemplo n.º 19
0
int main()
{
    ListNode *input = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input2 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input3 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input4 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input5 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input6 = (ListNode *)malloc(sizeof(ListNode));

    input->val=1;
    input2->val=2;
    input3->val=3;
    input4->val=4;
    input5->val=5;
    input6->val=6;

    input->next=input2;
    input2->next=input3;
    input3->next=input4;
    input4->next=input5;
    input5->next=input6;
    input6->next=input2;

    printf("input: ");
    printf("%d",input->val);//1
    printf("->");
    printf("%d",input->next->val);//2
    printf("->");
    printf("%d",input->next->next->val);//3
    printf("->");
    printf("%d",input->next->next->next->val);//4
    printf("->");
    printf("%d",input->next->next->next->next->val);//5
    printf("->");
    printf("%d",input->next->next->next->next->next->val);//6
    printf("->");
    printf("%d\n",input->next->val);//2

    printf("result: ");
    printf("%d\n",detectCycle(input,input,input));
    return 0;
}
Ejemplo n.º 20
0
int	BLI_isGraphCyclic(BGraph *graph)
{
	BNode *node;
	int value = 0;
	
	/* NEED TO CHECK IF ADJACENCY LIST EXIST */
	
	/* Mark all nodes as not visited */
	BLI_flagNodes(graph, 0);

	/* detectCycles in subgraphs */	
	for (node = graph->nodes.first; node && value == 0; node = node->next) {
		/* only for nodes in subgraphs that haven't been visited yet */
		if (node->flag == 0) {
			value = value || detectCycle(node, NULL);
		}		
	}
	
	return value;
}
Ejemplo n.º 21
0
int main(){
	
	node *head = NULL;
	insert( &head , 12 );
	insert( &head , 2 );
	insert( &head , 26 );
	insert( &head , 32 );
	insert( &head , 50 );
	printlist( head );
	
	head -> next -> next -> next -> next -> next = head -> next;
	
	if( detectCycle( head ) )
		std::cout<<"There's a loop in the list";
	
	else
		std::cout<<"No LOOP";
	
	//printlist( head );
}
Ejemplo n.º 22
0
void c_GenMapWaitHandle::onUnblocked() {
  for (;
       m_deps->iter_valid(m_iterPos);
       m_iterPos = m_deps->iter_next(m_iterPos)) {

    Cell* current = tvAssertCell(m_deps->iter_value(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      cellSet(child->getResult(), *current);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  if (m_exception.isNull()) {
    setResult(make_tv<KindOfObject>(m_deps.get()));
    m_deps = nullptr;
  } else {
    setException(m_exception.get());
    m_exception = nullptr;
    m_deps = nullptr;
  }
}
Ejemplo n.º 23
0
int detectCycle(ListNode *head,ListNode *p,ListNode *q) {
    if(!head)
        return 0;

    if(p->next!=NULL)
        p=p->next;
    else
        return 0;
    if(q->next!=NULL && q->next->next!=NULL)
        q=q->next->next;
    else
        return 0;
    if (p==q) {
        q=head;
        while (p!=q) {
            p=p->next;
            q=q->next;
        }
        return p->val;
    }
    detectCycle(head,p,q);
}
Ejemplo n.º 24
0
void c_AwaitAllWaitHandle::onUnblocked(uint32_t idx) {
  assert(idx <= m_unfinished);
  assert(getState() == STATE_BLOCKED);

  if (idx == m_unfinished) {
    for (uint32_t next = idx - 1; next < idx; --next) {
      auto const child = m_children[next].m_child;
      if (!child->isFinished()) {
        // Found the next unfinished child.
        m_unfinished = next;

        // Make sure there's no cyclic dependencies.
        try {
          detectCycle(child);
        } catch (const Object& cycle_exception) {
          markAsFailed(cycle_exception);
        }
        return;
      }
    }
    // All children finished.
    markAsFinished();
  }
}