Beispiel #1
0
/* recursively clone a tree */
static stTree *cloneTree(stTree *srcNode, stTree *destParent, struct malnCompCompMap *srcDestCompMap) {
    stTree *destNode = cloneNode(srcNode, srcDestCompMap);
    stTree_setParent(destNode, destParent);
    for (int i = 0; i < stTree_getChildNumber(srcNode); i++) {
        cloneTree(stTree_getChild(srcNode, i), destNode, srcDestCompMap);
    }
    return destNode;
}
Beispiel #2
0
 UndirectedGraphNode *cloneNode(UndirectedGraphNode *node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>& map) {
     if (map.count(node)) return map[node];
     UndirectedGraphNode* clone = new UndirectedGraphNode(node->label);
     map[node] = clone;
     for (auto n : node->neighbors)
         clone->neighbors.push_back(cloneNode(n, map));
     return clone;
 }
Beispiel #3
0
PassRefPtr<Node> ShadowRoot::cloneNode(bool deep, ExceptionCode& ec)
{
    RefPtr<Node> clone = cloneNode(deep);
    if (!clone) {
        ec = DATA_CLONE_ERR;
        return 0;
    }

    return clone;
}
Beispiel #4
0
/**
 * Clones simple linked list.
 * @param node   Pointer to the start node;
 * @return       Pointer to the start node of the cloned list.
 */
Node* cloneNode(Node* node) {
	Node* newNode = (Node*) malloc(sizeof(Node));
	newNode->vertex = node->vertex;
	if (NULL != node->next) {
		newNode->next = cloneNode(node->next);
	} else {
		newNode->next = NULL;
	}
	return newNode;
}
Beispiel #5
0
    strNode* strNode::clone() const {
      const strNode *nodePos = this;

      strNode *newNodeRoot = cloneNode();
      strNode *newNodePos  = newNodeRoot;

      while(nodePos->right){
        newNodePos->right       = nodePos->right->cloneNode();
        newNodePos->right->left = newNodePos;

        newNodePos = newNodePos->right;
        nodePos    = nodePos->right;
      }

      return newNodeRoot;
    }
Beispiel #6
0
    UndirectedGraphNode* cloneNode(UndirectedGraphNode* node, NodeMap& allNodes) {
        if (!node)
            return 0;
        UndirectedGraphNode* newNode = new UndirectedGraphNode(node->label);
        allNodes[node->label] = newNode;
        for (size_t i = 0; i < node->neighbors.size(); ++i)
        {
            UndirectedGraphNode* neighbor = node->neighbors[i];
            NodeMap::iterator nIter = allNodes.find(neighbor->label);

            if (nIter != allNodes.end())
            {
                newNode->neighbors.push_back(nIter->second);
            }
            else
            {
                newNode->neighbors.push_back(cloneNode(neighbor, allNodes));
            }
        }
        return newNode;
    }
 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
     if (NULL == node) {
         return NULL;
     }
     
     unordered_set<UndirectedGraphNode *> cloned_set;
     queue<UndirectedGraphNode *> q;
     q.push(node);
     
     UndirectedGraphNode *new_node = NULL;
     
     while (q.size()) {
         node = q.front();
         q.pop();
         
         // clone 
         UndirectedGraphNode* clone_node = cloneNode(node);
         cout << long(&node) << " " << long(&clone_node) << endl;
         
         // mark first cloned node for return
         if (cloned_set.empty()) {
             new_node = clone_node;
         }
         
         // add node to visited
         cloned_set.insert(node);
         
         // add not visited neighbors into queue
         vector<UndirectedGraphNode *> neighbors = node->neighbors;
         for (int i = 0; i < neighbors.size(); i++) {
             if (cloned_set.find(neighbors[i]) == cloned_set.end()) {
                 q.push(neighbors[i]);
             }
         }
     }
     
     return new_node;
 }
Beispiel #8
0
/**
 * Clones a graph.
 * @param       graph    The graph to clone.
 * @return               The cloned graph.
 */
Graph cloneGraph(const Graph graph) {
	Graph clonedGraph;
	Node** edges;
	int* edgesCount;
	int i;

	edges = (Node**) malloc(sizeof(Node*) * graph.n);
	edgesCount = (int*) malloc(sizeof(int) * graph.n);
	for (i = 0; i < graph.n; i++) {
		edgesCount[i] = graph.edgesCount[i];

		edges[i] = NULL;
		if (NULL != graph.edges[i]) {
			edges[i] = cloneNode(graph.edges[i]);
		}
	}

	clonedGraph.n = graph.n;
	clonedGraph.m = graph.m;
	clonedGraph.edges = edges;
	clonedGraph.edgesCount = edgesCount;
	return clonedGraph;
}
Beispiel #9
0
 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
     if (!node) return nullptr;
     unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> map;
     return cloneNode(node, map);
 }
Beispiel #10
0
int main(void)
{
	element *html = readDocument();

	// createElement
	element *t1 = createElement("t1", "Text node");

	assert("createElement - empty pointers - parentNode", t1->parentNode == NULL);
	assert("createElement - empty pointers - nextSibling", t1->nextSibling == NULL);
	assert("createElement - empty pointers - previousSibling", t1->previousSibling == NULL);
	assert("createElement - empty pointers - firstChild", t1->firstChild == NULL);
	assert("createElement - empty pointers - lastChild", t1->lastChild == NULL);


	// appendChild
	appendChild(html->lastChild, t1);

	assert("Append child test - parent pointer", t1->parentNode == html->lastChild);

	assert("Append only child test - previousSibling pointer", t1->previousSibling == NULL);
	assert("Append only child test - nextSibling pointer", t1->nextSibling == NULL);

	element *t2 = createElement("t2", NULL);
	appendChild(html->lastChild, t2);

	assert("Append second child test - previousSibling pointer to the first child", t2->previousSibling == html->lastChild->firstChild);
	assert("Append second child test - nextSibling pointer to NULL", t2->nextSibling == NULL);

	element *t3 = createElement("t3", NULL);
	appendChild(html->lastChild, t3);

	assert("Append third child test - second child nextSibling pointer equals to the pointer to the last child", t2->nextSibling == html->lastChild->lastChild);

	// replaceChild
	element *r2 = createElement("r2", NULL);
	replaceChild(html->lastChild, r2, html->lastChild->firstChild->nextSibling);

	assert("Replace child in the middle - parent", r2->parentNode == html->lastChild);
	assert("Replace child in the middle - prevSibling", r2->previousSibling == html->lastChild->firstChild);
	assert("Replace child in the middle - nextSibling", r2->nextSibling == html->lastChild->lastChild);

	element *r3 = createElement("r3", NULL);
	replaceChild(html->lastChild, r3, html->lastChild->lastChild);

	assert("Replace last child - prevSibling", (r3->previousSibling == html->lastChild->firstChild->nextSibling) && (r3->previousSibling == r2));
	assert("Replace last child - nextSibling", r3->nextSibling == NULL);

	element *r1 = createElement("r1", NULL);
	replaceChild(html->lastChild, r1, html->lastChild->firstChild);

	assert("Replace first child - prevSibling", r1->previousSibling == NULL);
	assert("Replace first child - nextSibling", r1->nextSibling == r2);

	// removeChild
	removeChild(html->lastChild, r1);
	assert("Remove child - first child removed", html->lastChild->firstChild == r2);

	removeChild(html->lastChild, r3);
	assert("Remove child - last (not least) child is removed", html->lastChild->lastChild == r2);
	assert("Remove child - last (not least) child is removed - siblings check for the remaining child", r2->previousSibling == NULL && r2->nextSibling == NULL);

	removeChild(r2->parentNode, r2);
	assert("Remove child - body is empty", html->lastChild->firstChild == NULL && html->lastChild->lastChild == NULL);

	appendChild(html->lastChild, r1);
	appendChild(html->lastChild, r2);
	appendChild(html->lastChild, r3);

	removeChild(r2->parentNode, r2);
	assert("Remove the middle child - relation between first and last",
			html->lastChild->firstChild->nextSibling == html->lastChild->lastChild &&
			html->lastChild->firstChild == html->lastChild->lastChild->previousSibling);

	removeChild(html->lastChild, r1);
	removeChild(html->lastChild, r3);

	// insertBefore
	appendChild(html->lastChild, r3);

	insertBefore(html->lastChild, r2, r3);
	assert("insertBefore - new firstChild", html->lastChild->firstChild == r2);
	assert("insertBefore - lastChild kept", html->lastChild->lastChild == r3);
	assert("insertBefore - parentNode set", html->lastChild == r2->parentNode);

	insertBefore(html->lastChild, r1, r3);
	assert("insertBefore - siblings", r1->previousSibling == r2 && r1->nextSibling == r3);
	assert("insertBefore - previousSibling->nextSibling == added element", r2->nextSibling == r1);

	removeChild(html->lastChild, r1);
	removeChild(html->lastChild, r2);
	removeChild(html->lastChild, r3);

	// cloneNode
	appendChild(r2, t1);
	appendChild(t1, t2);
	appendChild(r2, t3);

	element *c0 = cloneNode(t2, false);
	insertBefore(r2, c0, t3);

	element *c1 = cloneNode(r2, true);
	element *c2 = cloneNode(r2, false);
	appendChild(html->lastChild, c1);
	appendChild(html->lastChild, c2);

	element *c3 = cloneNode(r3, true); // r3
	insertBefore(html->lastChild, c3, c1);

	// Now it should be in order: r3, r2 deep, r2

	assert("Clone - first r2 is deep", html->lastChild->firstChild->nextSibling->firstChild->firstChild != NULL // html body r2 t1 t2
			&& html->lastChild->firstChild->nextSibling->lastChild != NULL); // html body r2 t3

	deleteElement(r1);
	deleteElement(r2);
	deleteElement(r3);

	deleteElement(html);

	return 0;
}
Beispiel #11
0
 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     NodeMap allNodes;
     UndirectedGraphNode* result = cloneNode(node, allNodes);
     return result;
 }
Beispiel #12
0
/**
 * Min cuts randomized contraction algorithm. It does not always return the
 * correct value, should be repeated. For n^2 * log n the probaility to fail
 * is < 1 / n
 *
 * @param   graph   The graph to calculate min cuts.
 * @return          Min cuts count.
 */
int randomizedContraction(const Graph graph) {
	Graph g = cloneGraph(graph);

	int randomVertex, randomLinkedVertexIndex, randomLinkedVertex,
			graphNoOfNodes = graph.n, k, returnValue = -1;
	Node *node, *node2, *node3, *node3Prev;
	int* randomlyChoosed = (int*) malloc(sizeof(int) * graph.n);
	int i;
	for (i = 0; i < graph.n; i++) {
		randomlyChoosed[i] = 0;
	}

	while (graphNoOfNodes > 2) {
		/* pickup a random edge */
		do {
			srand(time(NULL));
			randomVertex = rand() % graph.n;
		} while (randomlyChoosed[randomVertex]);
		randomlyChoosed[randomVertex] = 1;
		srand(time(NULL));
		randomLinkedVertexIndex = rand() % g.edgesCount[randomVertex];
		node = g.edges[randomVertex];
		k = 0;
		while (NULL != node && k < randomLinkedVertexIndex) {
			node = node->next;
			k++;
		}
		/* contract randomVertex into randomLinkedVertex */
		if (NULL != node) {
			randomLinkedVertex = node->vertex;
			/* modify globally edges to point to the new combined vertex */
			for (i = 0; i < graph.n; i++) {
				if (!randomlyChoosed[i]) {
					node2 = g.edges[i];
					while (NULL != node2) {
						if (node2->vertex == randomVertex) {
							node2->vertex = randomLinkedVertex;
						}
						node2 = node2->next;
					}
				}
			}
			/* add randomVertex 's edges to the randomLinkedVertex 's edges */
			node2 = g.edges[randomVertex];
			if (NULL != node2) {
				node3 = g.edges[randomLinkedVertex];
				if (NULL != node3) {
					while (NULL != node3->next) {
						node3 = node3->next;
					}
					node3->next = cloneNode(node2);
				} else {
					g.edges[randomLinkedVertex] = cloneNode(node2);
				}
			}
			g.edgesCount[randomLinkedVertex] += g.edgesCount[randomVertex];
			/* eliminate self pointing edges of randomLinkedVertex */
			node3 = g.edges[randomLinkedVertex];
			node3Prev = NULL;
			while (NULL != node3) {
				if (node3->vertex == randomLinkedVertex
						|| node3->vertex == randomVertex) {
					if (NULL != node3Prev) {
						node3Prev->next = node3->next;
					} else {
						g.edges[randomLinkedVertex] = node3->next;
					}
					node3 = node3->next;
					g.edgesCount[randomLinkedVertex]--;
				} else {
					node3Prev = node3;
					node3 = node3->next;
				}
			}
		}
		graphNoOfNodes--;
	}
	/* 2 vertices left, return the number of edges of the first one found */
	for (i = 0; i < graph.n; i++) {
		if (!randomlyChoosed[i]) {
			returnValue = g.edgesCount[i];
			break;
		}
	}
	freeGraph(&g);
	free(randomlyChoosed);
	return returnValue;
}
Beispiel #13
0
PassRefPtr<Node> ShadowRoot::cloneNode(ExceptionState& exceptionState)
{
    return cloneNode(exceptionState);
}
PassRefPtr<Element> Element::cloneElement()
{
    return static_pointer_cast<Element>(cloneNode(false));
}
Beispiel #15
0
/**
 * Makes a copy of y's child x and automatically links it with y.
 */
void cloneSpec (PRBTreeNode * x, PRBTreeNode * y)
{
  if (x == y->left) y->left = cloneNode(x);
  else y->right = cloneNode(x);
}