Beispiel #1
0
/*
	We are adding values to the tree while insterting the to a std::unordered_set 
	as well. Afterwards we compare the set's and the tree's size and check if 
	every key found in the set can be found in the tree as well. 
*/
TEST(LibAvlBasicSanity, validate_with_set)
{
	auto n = TEST_NUMBER;
	while(n--) {
		auto keys_vec = generate_random_keys(TREE_SIZE, MAX_TEST_UINT);
		std::unordered_set<int> keys{};
		auto avl = avl::AvlTree<int>();
		uint quantity = 0;
	    for(auto value : keys_vec) {
	    	if(avl.add_key(value)) {
	    		keys.insert(value);
	    		quantity++;
	    	}
	    	else {
	    		ASSERT_TRUE(avl.find_key(value));
	    	}
	    	validate_bst(avl.get_root());
			ASSERT_EQ(quantity, get_subtree_count(avl.get_root()));
			ASSERT_EQ(quantity, keys.size());
		}
		for(const auto& value : keys_vec) {
			ASSERT_TRUE(avl.find_key(value));
		}
	}
}
Beispiel #2
0
	void as_xmlsock::close()
	{
		if (get_root())
		{
			get_root()->remove_listener(this);
		}
	}
Beispiel #3
0
template<class X> void splay_forest<X>::join (Pos* d, Pos u, Pos l, Pos r)
{//fast-and-sloppy version, biased to the left

    while (true) {

        POMAGMA_ASSERT5(get_root(l) == get_root(r), "L,R keys disagree");
        POMAGMA_ASSERT5((!u) or get_root(r) == get_root(u), "R,U keys disagree");
        POMAGMA_ASSERT5(rank(l) < rank(r), "L,R in wrong order");

        //look for space below l
        Pos x = R(l);
        if (not x) {                //                l
            *d = l; U(l) = u;       //  l  +  r  ->  w  r
            set_R(l,r);             // w     y z       y z
            return;
        }

        //look for space below r
        Pos y = L(r);
        if (not y) {                //                  r
            *d = r; U(r) = u;       //  l  +  r  ->   l  z
            set_L(r,l);             // w x     z     w x
            return;
        }

        //otherwise push down
        *d = l; U(l) = u;           //                l
        set_R(l,r);                 //  l  +  r  ->  w    r
        d = &(L(r));                // w x   y z       x+y z
        u = r; l = x; r = y;
    }
}
Beispiel #4
0
int
main(int argc, char **argv)
{
	status_t error;
	device_node_cookie root;

	if ((error = init_dm_wrapper()) < 0) {
		printf("Error initializing device manager (%s)\n", strerror(error));
		return error;
	}

	if (argc > 2)
		usage();

	if (argc == 2) {
		if (!strcmp(argv[1], "-d")) {
			gMode = DUMP_MODE;
		} else {
			usage();
		}
	}

	if (gMode == DUMP_MODE) {
		get_root(&root);
		dump_nodes(&root, 0);
	} else {
		get_root(&root);
		display_nodes(&root, 0);
	}

	uninit_dm_wrapper();

	return 0;
}
 bool merge(int a, int b) {
     int r1 = get_root(a), r2 = get_root(b);
     if (r1 == r2)
         return false;
     p[r2] = r1;
     return true;
 }
Beispiel #6
0
void SceneMainLoop::_notification(int p_notification) {



	switch (p_notification) {

		case NOTIFICATION_WM_QUIT_REQUEST: {

			get_root()->propagate_notification(p_notification);

			if (accept_quit) {
				_quit=true;
				break;
			}
		} break;
		case NOTIFICATION_WM_FOCUS_IN:
		case NOTIFICATION_WM_FOCUS_OUT: {

			get_root()->propagate_notification(p_notification);
		} break;
		case NOTIFICATION_WM_UNFOCUS_REQUEST: {

			notify_group(GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input",NOTIFICATION_WM_UNFOCUS_REQUEST);

		} break;

		default:
			break;
	};
};
Beispiel #7
0
        /*!
            This method returns 'true' if the element has
            successfully been inserted into the tree. And
            'false' otherwise
         */
        bool insert(const _Tp& element) {
            std::cout << "\n**********************" << std::endl;
            std::cout << "Inserting : " << element << std::endl;

            if( not get_root() ) {
                assert(mSentinel->add_child(element, Direction::RIGHT)); 
                mSize += 1;
                return true;
            }

            // Get the pointer to the root node, and call it
            // 'currentNode'. Get a pointer to the root node's parent
            // and call it 'currentParent'
            node_ptr_t currentNode = avl_node<_Tp>::get_link(get_root());
            node_ptr_t currentParent = currentNode;
            Direction dir = Direction::NULL_DIRECTION;

            // Walk down the tree
            while( currentNode != nullptr)   {
                std::cout << "Current node : " << currentNode->mNodeValue << std::endl;
                std::cout << "Current node at : " << currentNode << std::endl;
                std::cout << "Current root at : " << get_root() << std::endl;

                // Check to see if 'element' lies to the left or to
                // the right of 'currentNode'
                if( Cmp()(element, currentNode->mNodeValue) )   {
                    // Go left
                    currentParent = currentNode;
                    currentNode = avl_node<_Tp>::get_link(currentNode->left);
                    dir = Direction::LEFT;
                    std::cout << "Going left" << std::endl;
                } else if( Cmp()(currentNode->mNodeValue, element) )    {
                    // Go right
                    currentParent = currentNode;
                    currentNode = avl_node<_Tp>::get_link(currentNode->right);
                    dir = Direction::RIGHT;
                    std::cout << "Going right" << std::endl;
                } else  {
                    // (a < b = false) and (b < a = false) => (a == b)
                    // 'element' already exists in the tree. return 'false'
                    return false;
                }
            }
            mSize += 1;
            // 'currentParent' points to a leaf node. and 'dir' records
            // if we went left or right from that leaf node
            assert(currentParent->add_child(element, dir));
            std::cout << "Number of elements in tree = " << mSize << std::endl;
            std::cout << "Element at root before re-balance = " << avl_node<_Tp>::get_link(get_root())->mNodeValue << std::endl;
            std::cout << "Height of tree before re-balance = " << height() << std::endl;
            std::cout << "location of root before rebalance = " << get_root() << std::endl;
            rebalance(currentParent);
            std::cout << "Element at root after re-balance = " << avl_node<_Tp>::get_link(get_root())->mNodeValue << std::endl;
            std::cout << "Height of tree after re-balance = " << height() << std::endl;
            std::cout << "location of root after rebalance = " << get_root() << std::endl;
            std::cout << "**********************" << std::endl;
            return true;

        }
void get_avg_local(){
   uint16_t total;
   uint16_t count;
   uint16_t avg;
   total=get_total(get_root());
   count=count_nodes(get_root());
   avg=total/count;
   printf("the average is : %d\n",avg);
}
void MultiGrid::redistribute_grids() {
#ifdef HYDRO_GRAV_GRID
    printf("MultiGrid::redistribute_grids should not be called\n");
    abort();
#endif
    get_root()->compute_distribution();
    get_root()->allocate_nodes();
    get_root()->find_local_nodes();
}
Beispiel #10
0
void objects_item::object_destroy(int id) {
    samp_api->destroy_player_object(get_root()->get_id(), id);
    if (manager.is_debug) {
        messages_item msg_item;
        msg_item.get_params()
            ("id", id)
            ("player_name", get_root()->name_get_full());
        msg_item.get_sender()("<log_section objects/debug/player_destroy/>$(player_name) $(id)", msg_debug);
    }
}
Beispiel #11
0
void SceneMainLoop::set_pause(bool p_enabled) {

	if (p_enabled==pause)
		return;
	pause=p_enabled;
	PhysicsServer::get_singleton()->set_active(!p_enabled);
	Physics2DServer::get_singleton()->set_active(!p_enabled);
	if (get_root())
		get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
}
Beispiel #12
0
template<class X> void splay_forest<X>::test_contains (Pos eqn)
{
    for (Iterator iter(get_root(eqn)); iter.ok(); iter.next()) {
        if (*iter == eqn) return;
    }
    Iterator iter;
    for (iter.begin(get_root(eqn)); iter.ok(); iter.next()) {
        if (*iter == eqn) return;
    }
    POMAGMA_ERROR("invalid: eqn not contained in own " << nameof<X>() << " tree");
}
Beispiel #13
0
string
get_root (url u) {
  if (is_concat (u)) return get_root (u[1]);
  if (is_or (u)) {
    string s1= get_root (u[1]);
    string s2= get_root (u[2]);
    if (s1 == s2) return s1; else return "";
  }
  if (is_root (u)) return u[1]->t->label;
  return "";
}
Beispiel #14
0
int objects_item::object_create(int object_key) {
    object_dynamic_t const& obj = manager.streamer.item_get(object_key);
    int id = samp_api->create_player_object(get_root()->get_id(), obj.model_id, obj.x, obj.y, obj.z, obj.rx, obj.ry, obj.rz, obj.visibility);
    if (manager.is_debug) {
        messages_item msg_item;
        msg_item.get_params()
            ("obj", boost::format("%1%") % obj)
            ("id", id)
            ("player_name", get_root()->name_get_full());
        msg_item.get_sender()("<log_section objects/debug/player_create/>$(player_name) $(id) $(obj)", msg_debug);
    }
    return id;
}
Beispiel #15
0
//validation
template<class X> void splay_forest<X>::test_find (Pos eqn)
{
    //test find_pair
    Pos pos = find_pair(get_root(eqn), get_key(eqn), get_val(eqn));
    POMAGMA_ASSERT(pos, "invalid: eqn not found in own " << nameof<X>() << " tree");
    POMAGMA_ASSERT(pos == eqn,
            "invalid: wrong eqn found in own " << nameof<X>() << " tree");

    //test find_key
    pos = find_key(get_root(eqn), get_key(eqn));
    POMAGMA_ASSERT(pos, "invalid: key not found in own " << nameof<X>() << " tree");
    POMAGMA_ASSERT(get_key(pos) == get_key(eqn),
            "invalid: wrong key found in own " << nameof<X>() << " tree");
}
void checkpoints_item::show(int id) {
    if (checkpoints::invalid_checkpoint_id == active_checkpoint_id) {
        active_checkpoint_id = id;
        checkpoint_t const& checkpoint = manager.checkpoint_streamer.item_get(active_checkpoint_id);
        samp::api::instance()->set_player_checkpoint(get_root()->get_id(), checkpoint.x, checkpoint.y, checkpoint.z, checkpoint.size);
    }
}
Beispiel #17
0
/**
 * Handles save of current game to selected file,
 * denoted by the i of the given file_btn
 */
int on_select_save_file(Control* file_btn) {
	if (!save_game(file_btn->i, game)) {
		// TODO error dialog?
	}
	return !show_game_arena(get_root(file_btn), game, on_select_tile,
			game_menu_handles, on_select_difficulty, 4);
}
Beispiel #18
0
/**
 * Check if the translated @host_path belongs to the guest rootfs,
 * that is, isn't from a binding.
 */
bool belongs_to_guestfs(const Tracee *tracee, const char *host_path)
{
	Comparison comparison;

	comparison = compare_paths(get_root(tracee), host_path);
	return (comparison == PATHS_ARE_EQUAL || comparison == PATH1_IS_PREFIX);
}
Beispiel #19
0
AnimalExport int
get_root(ImgPUInt32 *pred, puint32 p)
{
   if (DATA(pred)[p] == p)
      return p;
   return(get_root(pred,DATA(pred)[p]));
}
Beispiel #20
0
/* This implements svn_editor_cb_add_file_t */
static svn_error_t *
add_file_cb(void *baton,
            const char *relpath,
            const svn_checksum_t *checksum,
            svn_stream_t *contents,
            apr_hash_t *props,
            svn_revnum_t replaces_rev,
            apr_pool_t *scratch_pool)
{
    struct edit_baton *eb = baton;
    const char *fspath = FSPATH(relpath, scratch_pool);
    svn_fs_root_t *root;

    SVN_ERR(get_root(&root, eb));

    if (SVN_IS_VALID_REVNUM(replaces_rev))
    {
        SVN_ERR(can_modify(root, fspath, replaces_rev, scratch_pool));
        SVN_ERR(svn_fs_delete(root, fspath, scratch_pool));
    }
    else
    {
        SVN_ERR(can_create(root, fspath, scratch_pool));
    }

    SVN_ERR(svn_fs_make_file(root, fspath, scratch_pool));

    SVN_ERR(set_text(root, fspath, checksum, contents,
                     eb->cancel_func, eb->cancel_baton, scratch_pool));
    SVN_ERR(add_new_props(root, fspath, props, scratch_pool));

    return SVN_NO_ERROR;
}
Beispiel #21
0
/* This implements svn_editor_cb_add_directory_t */
static svn_error_t *
add_directory_cb(void *baton,
                 const char *relpath,
                 const apr_array_header_t *children,
                 apr_hash_t *props,
                 svn_revnum_t replaces_rev,
                 apr_pool_t *scratch_pool)
{
    struct edit_baton *eb = baton;
    const char *fspath = FSPATH(relpath, scratch_pool);
    svn_fs_root_t *root;

    /* Note: we ignore CHILDREN. We have no "incomplete" state to worry about,
       so we don't need to be aware of what children will be created.  */

    SVN_ERR(get_root(&root, eb));

    if (SVN_IS_VALID_REVNUM(replaces_rev))
    {
        SVN_ERR(can_modify(root, fspath, replaces_rev, scratch_pool));
        SVN_ERR(svn_fs_delete(root, fspath, scratch_pool));
    }
    else
    {
        SVN_ERR(can_create(root, fspath, scratch_pool));
    }

    SVN_ERR(svn_fs_make_dir(root, fspath, scratch_pool));
    SVN_ERR(add_new_props(root, fspath, props, scratch_pool));

    return SVN_NO_ERROR;
}
Beispiel #22
0
//Number of Islands II
//A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
//Example:
//Given m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]].
//Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land).
//0 0 0
//0 0 0
//0 0 0
//Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.
//1 0 0
//0 0 0   Number of islands = 1
//0 0 0
//Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.
//1 1 0
//0 0 0   Number of islands = 1
//0 0 0
//Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.
//1 1 0
//0 0 1   Number of islands = 2
//0 0 0
//Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.
//1 1 0
//0 0 1   Number of islands = 3
//0 1 0
//We return the result as an array: [1, 1, 2, 3]
//Challenge:
//Can you do it in time complexity O(k log mn), where k is the length of the positions?
//
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
   int island = 0;
   vector<int> dirs( {0,1,0,-1,0} );
   vector<int> res;
   int cnt = 0;
   vector<int> id(m*n,-1);
   for(auto& p:positions) {
     cnt++;
     int idx= p.first * n + p.second;
     id[idx] = idx;
     for(int i=0;i<4;i++) {
       int dx = p.first + dirs[i];
       int dy = p.second + dirs[i+1];
       if(dx >=0 && dy >=0 && dx < m && dy < n && id[dx*n + dy] != -1) {
             int root = get_root (id, dx * n + dy);
             //Found root ranging from the case, only union, while reducing the count
             if (root != idx) {
                 id[root] = idx;
                 cnt--;
             }
          }
       }
     res.push_back(cnt);
   }
   return res;
}
Beispiel #23
0
	void trie_tree::print_tree(std::ostream& os)const{
		auto root = get_root();
		if (root == NULL)
			os << "the trie_tree is empty!" << std::endl;
		for (auto cit = root->map_childs.cbegin(); cit != root->map_childs.cend(); ++cit)
			_print_tree(os, cit->second, string());
	}
Beispiel #24
0
/* Remove a reserved region. Returns 0 on success, < 0 on error. Note: A region
 * cannot be removed unless it was the last one added. */
int cbmem_entry_remove(const struct cbmem_entry *entry)
{
	unsigned long entry_num;
	struct cbmem_root *root;

	root = get_root();

	if (root == NULL)
		return -1;

	if (root->num_entries == 0)
		return -1;

	/* Nothing can be removed. */
	if (root->locked)
		return -1;

	entry_num = entry - &root->entries[0];

	/* If the entry is the last one in the root it can be removed. */
	if (entry_num == (root->num_entries - 1)) {
		root->num_entries--;
		return 0;
	}

	return -1;
}
Beispiel #25
0
void
World::load_(const std::string& directory)
{
  m_basedir = directory;
  m_worldmap_filename = m_basedir + "/worldmap.stwm";

  std::string filename = m_basedir + "/info";
  register_translation_directory(filename);
  auto doc = ReaderDocument::parse(filename);
  auto root = doc.get_root();

  if(root.get_name() != "supertux-world" &&
     root.get_name() != "supertux-level-subset")
  {
    throw std::runtime_error("File is not a world or levelsubset file");
  }

  m_hide_from_contribs = false;
  m_is_levelset = true;

  auto info = root.get_mapping();

  info.get("title", m_title);
  info.get("description", m_description);
  info.get("levelset", m_is_levelset);
  info.get("hide-from-contribs", m_hide_from_contribs);
}
Beispiel #26
0
/* This implements svn_editor_cb_alter_file_t */
static svn_error_t *
alter_file_cb(void *baton,
              const char *relpath,
              svn_revnum_t revision,
              apr_hash_t *props,
              const svn_checksum_t *checksum,
              svn_stream_t *contents,
              apr_pool_t *scratch_pool)
{
    struct edit_baton *eb = baton;
    const char *fspath = FSPATH(relpath, scratch_pool);
    svn_fs_root_t *root;

    SVN_ERR(get_root(&root, eb));
    SVN_ERR(can_modify(root, fspath, revision, scratch_pool));

    if (contents != NULL)
    {
        SVN_ERR_ASSERT(checksum != NULL);
        SVN_ERR(set_text(root, fspath, checksum, contents,
                         eb->cancel_func, eb->cancel_baton, scratch_pool));
    }

    if (props != NULL)
    {
        SVN_ERR(alter_props(root, fspath, props, scratch_pool));
    }

    return SVN_NO_ERROR;
}
Beispiel #27
0
struct iso *iso_load_fd(int fd, struct iso *iso)
{
    struct stat stat;
    if (fstat(fd, &stat) == -1)
        return NULL;

    size_t size = stat.st_size;
    if (size < 0x8000 + sizeof (struct iso_prim_voldesc))
    {
        errno = EINVAL;
        return NULL;
    }

    void *iso_map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (iso_map == MAP_FAILED)
        return NULL;

    iso->map = iso_map;
    if (!validate_iso(iso_describe(iso)))
    {
        errno = EINVAL;
        return NULL;
    }

    iso->fd = fd;
    iso->size = stat.st_size;
    iso->cwd = get_root(iso);
    return iso;
}
Beispiel #28
0
/* This implements svn_editor_cb_copy_t */
static svn_error_t *
copy_cb(void *baton,
        const char *src_relpath,
        svn_revnum_t src_revision,
        const char *dst_relpath,
        svn_revnum_t replaces_rev,
        apr_pool_t *scratch_pool)
{
    struct edit_baton *eb = baton;
    const char *src_fspath = FSPATH(src_relpath, scratch_pool);
    const char *dst_fspath = FSPATH(dst_relpath, scratch_pool);
    svn_fs_root_t *root;
    svn_fs_root_t *src_root;

    SVN_ERR(get_root(&root, eb));

    /* Check if we can we replace the maybe-specified destination (revision).  */
    if (SVN_IS_VALID_REVNUM(replaces_rev))
    {
        SVN_ERR(can_modify(root, dst_fspath, replaces_rev, scratch_pool));
        SVN_ERR(svn_fs_delete(root, dst_fspath, scratch_pool));
    }
    else
    {
        SVN_ERR(can_create(root, dst_fspath, scratch_pool));
    }

    SVN_ERR(svn_fs_revision_root(&src_root, svn_fs_root_fs(root), src_revision,
                                 scratch_pool));
    SVN_ERR(svn_fs_copy(src_root, src_fspath, root, dst_fspath, scratch_pool));
    svn_fs_close_root(src_root);

    return SVN_NO_ERROR;
}
void SingleStar::write_to_file(int i1, int i2, const char* idname) {
	char* fname;
	Real omega = State::get_omega();
	FILE* fp;
	_3Vec O = get_origin();

//	asprintf(&fname, "mv -f checkpoint.hello.%i.bin checkpoint.goodbye.%i.bin 2> /dev/null\n", MPI_rank(), MPI_rank());
//	system(fname);
//	free(fname);

	asprintf(&fname, "checkpoint.%s.%i.bin", idname, MPI_rank());
	fp = fopen(fname, "wb");
	free(fname);
//	fwrite(&refine_floor, sizeof(Real), 1, fp);
	fwrite(&State::ei_floor, sizeof(Real), 1, fp);
	fwrite(&State::rho_floor, sizeof(Real), 1, fp);
//	fwrite(&com_vel_correction, sizeof(_3Vec), 1, fp);
	fwrite(&O, sizeof(_3Vec), 1, fp);
	fwrite(&omega, sizeof(Real), 1, fp);
	fwrite(&last_dt, sizeof(Real), 1, fp);
	fwrite(&i1, sizeof(int), 1, fp);
	fwrite(&i2, sizeof(int), 1, fp);
	get_root()->write_checkpoint(fp);
	fclose(fp);
}
Beispiel #30
0
void count_nodes_local(){
   uint16_t local_node_count=0;
   node_ptr start;
   start=get_root();
   local_node_count=count_nodes(start);
   printf("number of keys: %d\n",local_node_count);
}