Exemple #1
0
 ListNode* new_head(ListNode* head, int x)
 {
     if(!head)
         return head;
     ListNode * insert_pos = head->val <x?head:NULL;
     if(head->val >=x)
     {
         ListNode * temp = head;
         while(temp->next && temp ->next->val>=x)
             temp = temp->next;
         if(!temp->next)
             return head;
         else
         {
             ListNode *T = temp->next->next;
             temp->next->next = head;
             head = temp->next;
             temp->next = T;
             head->next = new_head(head->next, x);
             return head;
         }
     }
     else
     {
         head->next = new_head(head->next, x);
         return head;
     }        
 }
RandomListNode *copyRandomList(RandomListNode *head) 
{
	if (head == nullptr)
		return nullptr;

	for (RandomListNode* cur = head; cur != nullptr;)//创建新的节点,把新的节点插入到对应原节点的后面
	{
		RandomListNode* node = new RandomListNode(cur->label);
		node->next = cur->next;
		cur->next = node;
		cur = node->next;
	}

	for (RandomListNode* cur = head; cur != nullptr;)//复制每个节点的random指针
	{
		if (cur->random != nullptr)
			cur->next->random = cur->random->next;//注意是cur->randome->next
		cur = cur->next->next;
	}

	RandomListNode new_head(-1);
	for (RandomListNode* cur = head, *new_node = &new_head; cur != nullptr;)//分割两个链表
	{
		new_node->next = cur->next;
		new_node = new_node->next;
		cur->next = cur->next->next;
		cur = cur->next;
	}

	return new_head.next;
}
Exemple #3
0
size_t ringmaster_getslot(ringmaster_t *rm) {

  advance_tail(rm);

  if (rm->state != ACTIVE) return -1;

  long long h = new_head(rm);
  if (h == -1) return -1;

  return rm->head;
}
Exemple #4
0
 template <class ValueType> bool push(ValueType&& value)
 {
   node_type old_head = head_.load();
   node_type new_head(new node);
   new_head->value  = std::move(value);
   new_head.counter = counter_++;
   do
   {
     new_head->next   = old_head;
   } while(!head_.compare_exchange_weak(old_head, new_head));
   return true;
 }
Exemple #5
0
 ListNode* reverseList(ListNode* head) {
     if(!head || head->next == NULL)
         return head;
     ListNode new_head(0);
     while(head)
     {
         ListNode *tmp = head->next;
         head->next = new_head.next;
         new_head.next = head;
         head = tmp;
     }
     return new_head.next;
 }
Exemple #6
0
 ListNode* reverseBetween(ListNode* head, int m, int n) {
     if(!head)
         return NULL;
     ListNode *last_node = NULL;
     ListNode *tmp, *next;
     ListNode new_head(0), *p;
     new_head.next = head;
     int cnt = n - m + 1;
     p = &new_head;
     for(int i = 0; i < m - 1; i++)
         p = p->next;
     last_node = p->next;
     tmp = p->next;
     while(cnt--)
     {
         next = tmp->next;
         tmp->next = p->next;
         p->next = tmp;
         tmp = next;
     }
     last_node->next = next;
     return new_head.next;
 }
Exemple #7
0
    ListNode* partition(ListNode* head, int x) {

        return new_head(head, x);
    }
Exemple #8
0
void ringmaster_advanceslot(ringmaster_t *rm) {
  rm->head = new_head(rm);
}
Exemple #9
0
// remove the irreducible entry edges on loophead by cloning the body
// of loophead, redirecting the irreducible edges to that cloned body,
// and directing the back edges of the cloned loop back to the entry
// of the real loop. destructively modifies cfg.
void ReduceLoop(BlockCFG *cfg, PPoint loophead,
                const Vector<PEdge*> &irreducible_edges)
{
    PPointListHash remapping;

    Vector<size_t> old_entry_indexes;
    Vector<size_t> old_exit_indexes;
    Vector<size_t> old_back_indexes;

    CloneLoopBody(cfg, loophead, &remapping, cfg,
                  &old_entry_indexes, &old_exit_indexes, &old_back_indexes);

    // replace the irreducible entry edges with edges into the
    // corresponding point in the new loop.
    for (size_t iind = 0; iind < irreducible_edges.Size(); iind++) {
        PEdge *irr_edge = irreducible_edges[iind];
        bool found_index = false;

        for (size_t oind = 0; oind < old_entry_indexes.Size(); oind++) {
            size_t entry_index = old_entry_indexes[oind];
            PEdge *old_edge = cfg->GetEdge(entry_index);

            if (old_edge == irr_edge) {
                Assert(!found_index);
                found_index = true;

                PPoint source = old_edge->GetSource();
                PPoint target = old_edge->GetTarget();
                PPoint new_target = remapping.LookupSingle(target);

                PEdge *new_edge = PEdge::ChangeEdge(old_edge, source, new_target);
                cfg->SetEdge(entry_index, new_edge);
            }
        }

        Assert(found_index);
    }

    // add the old exit edges for the new loop.
    for (size_t oind = 0; oind < old_exit_indexes.Size(); oind++) {
        PEdge *old_edge = cfg->GetEdge(old_exit_indexes[oind]);

        PPoint source = old_edge->GetSource();
        PPoint target = old_edge->GetTarget();
        PPoint new_source = remapping.LookupSingle(source);

        PEdge *new_edge = PEdge::ChangeEdge(old_edge, new_source, target);
        cfg->AddEdge(new_edge);
    }

    // add the old back edges as edges to the entry point of the old loop.
    for (size_t oind = 0; oind < old_back_indexes.Size(); oind++) {
        PEdge *old_edge = cfg->GetEdge(old_back_indexes[oind]);

        PPoint source = old_edge->GetSource();
        PPoint target = old_edge->GetTarget();
        PPoint new_source = remapping.LookupSingle(source);

        Assert(target == loophead);

        PEdge *new_edge = PEdge::ChangeEdge(old_edge, new_source, target);
        cfg->AddEdge(new_edge);
    }

    // add any loopheads that may have been duplicated within the cloned loop.
    // note that the new loophead does not have any incoming edges and will
    // get trimmed by TrimUnreachable().
    for (size_t lind = 0; lind < cfg->GetLoopHeadCount(); lind++) {
        const LoopHead &head = cfg->GetLoopHead(lind);

        Vector<PPoint> *new_point_list = remapping.Lookup(head.point, false);
        if (new_point_list) {
            Assert(new_point_list->Size() == 1);
            LoopHead new_head(new_point_list->At(0), head.end_location);

            if (head.end_location)
                head.end_location->IncRef();

            cfg->AddLoopHead(new_head.point, new_head.end_location);
        }
    }

    // trim unreachable portions of the cfg, but do not collapse skip edges.
    TrimUnreachable(cfg, false);
}
Exemple #10
0
void TrimUnreachable(BlockCFG *cfg, bool flatten_skips)
{
    // can't flatten skips if there might be loops in the CFG.
    Assert(!flatten_skips || cfg->GetLoopHeadCount() == 0);

    // receives the locations of the new points and edges of the CFG. we will
    // fill these in, then replace wholesale the old points/edges on the CFG.
    Vector<Location*> new_points;
    Vector<PEdge*> new_edges;
    Vector<LoopHead> new_loop_heads;

    Vector<PPoint> worklist;

    // get the set of points reachable from CFG entry.
    // worklist items are points in entry_reachable whose outgoing edges
    // have not been examined.
    PPointHash entry_reachable;

    PPoint entry = cfg->GetEntryPoint();
    entry_reachable.Insert(entry);
    worklist.PushBack(entry);

    while (!worklist.Empty()) {
        PPoint back = worklist.Back();
        worklist.PopBack();

        const Vector<PEdge*> &outgoing = cfg->GetOutgoingEdges(back);
        for (size_t oind = 0; oind < outgoing.Size(); oind++) {
            PEdge *edge = outgoing[oind];
            PPoint next = edge->GetTarget();

            if (!entry_reachable.Lookup(next)) {
                entry_reachable.Insert(next);
                worklist.PushBack(next);
            }
        }
    }

    // get the set of points which reach the CFG exit.
    // worklist items are points in exit_reaches whose incoming edges
    // have not been examined.
    PPointHash exit_reaches;

    PPoint exit = cfg->GetExitPoint();
    exit_reaches.Insert(exit);
    worklist.PushBack(exit);

    while (!worklist.Empty()) {
        PPoint back = worklist.Back();
        worklist.PopBack();

        const Vector<PEdge*> &incoming = cfg->GetIncomingEdges(back);
        for (size_t iind = 0; iind < incoming.Size(); iind++) {
            PEdge *edge = incoming[iind];
            PPoint prev = edge->GetSource();

            if (!exit_reaches.Lookup(prev)) {
                exit_reaches.Insert(prev);
                worklist.PushBack(prev);
            }
        }
    }

    // make sure we include the entry regardless of whether the function
    // has a path from entry to exit.
    exit_reaches.Insert(entry);
    if (flatten_skips)
        exit_reaches.Insert(FollowSkipEdges(cfg, entry));

    // map from old points to corresponding new points. only defined for
    // points that are in both entry_reachable and exit_reaches,
    // and that do not have outgoing skip edges (if flatten_skips is set).
    PPointListHash remapping;

    // map from some old p0 to another old p1 where p0 connects to p1 by
    // skip edges and p1 has no outgoing skips. empty if flatten_skips is
    // not set. only defined if remapping is defined for p1.
    PPointListHash skip_remapping;

    for (PPoint point = 1; point <= cfg->GetPointCount(); point++) {
        if (entry_reachable.Lookup(point) && exit_reaches.Lookup(point)) {

            // if this is just the source of some skip edges flatten them out.
            // the target of the skips will be defined by remapping since
            // there can be only one outgoing skip edge from a point and
            // thus all paths from point pass through target_point; if point
            // reaches the exit then so does target_point.
            if (flatten_skips) {
                PPoint target_point = FollowSkipEdges(cfg, point);
                if (target_point != point) {
                    skip_remapping.Insert(point, target_point);

                    // don't add anything to remapping for point
                    continue;
                }
            }

            Location *loc = cfg->GetPointLocation(point);
            loc->IncRef();
            new_points.PushBack(loc);
            PPoint new_point = new_points.Size();

            remapping.Insert(point, new_point);
        }
    }

    for (size_t eind = 0; eind < cfg->GetEdgeCount(); eind++) {
        PEdge *edge = cfg->GetEdge(eind);

        PPoint source = edge->GetSource();
        PPoint target = edge->GetTarget();

        if (skip_remapping.Lookup(source, false))
            continue;

        // flatten any skips after the target point
        Vector<PPoint> *skip_target_list = skip_remapping.Lookup(target, false);
        if (skip_target_list) {
            Assert(skip_target_list->Size() == 1);
            target = skip_target_list->At(0);
        }

        Vector<PPoint> *new_source_list = remapping.Lookup(source, false);
        Vector<PPoint> *new_target_list = remapping.Lookup(target, false);

        if (new_source_list && new_target_list) {
            Assert(new_source_list->Size() == 1);
            Assert(new_target_list->Size() == 1);

            PPoint new_source = new_source_list->At(0);
            PPoint new_target = new_target_list->At(0);

            PEdge *new_edge = PEdge::ChangeEdge(edge, new_source, new_target);
            new_edges.PushBack(new_edge);
        }
    }

    for (size_t lind = 0; lind < cfg->GetLoopHeadCount(); lind++) {
        const LoopHead &head = cfg->GetLoopHead(lind);

        // don't check skip_remapping because we don't allow skip flattening
        // when the CFG still has loops in it

        Vector<PPoint> *new_point_list = remapping.Lookup(head.point, false);
        if (new_point_list) {
            Assert(new_point_list->Size() == 1);
            LoopHead new_head(new_point_list->At(0), head.end_location);

            if (head.end_location)
                head.end_location->IncRef();

            new_loop_heads.PushBack(new_head);
        }
    }

    // clear out the initial CFG.
    cfg->ClearBody();

    // add the new points, edges, loop heads.
    for (size_t pind = 0; pind < new_points.Size(); pind++)
        cfg->AddPoint(new_points[pind]);
    for (size_t eind = 0; eind < new_edges.Size(); eind++)
        cfg->AddEdge(new_edges[eind]);
    for (size_t lind = 0; lind < new_loop_heads.Size(); lind++)
        cfg->AddLoopHead(new_loop_heads[lind].point,
                         new_loop_heads[lind].end_location);

    // set the new entry and exit points of the CFG.

    // the entry may be connected to skip edges
    Vector<PPoint> *skip_entry_list = skip_remapping.Lookup(entry, false);
    if (skip_entry_list) {
        Assert(skip_entry_list->Size() == 1);
        entry = skip_entry_list->At(0);
    }

    PPoint new_entry = remapping.LookupSingle(entry);
    PPoint new_exit = 0;

    Vector<PPoint> *new_exit_list = remapping.Lookup(exit, false);
    if (new_exit_list) {
        Assert(new_exit_list->Size() == 1);
        new_exit = new_exit_list->At(0);
    }

    cfg->SetEntryPoint(new_entry);
    cfg->SetExitPoint(new_exit);
}