Node Move and Swap

The Node class provides move and swap methods that allow you to efficiently combine or exchange data between Node trees. These provide similar semantics to C++ std::move and std::swap.

  • Node::move(Node &n): Allows you to move the entire contents and description of a passed Node into the calling Node. There are no copies. The calling Node assumes management of all pointers and relevant metadata.

  • Node::swap(Node &n): Allows you to swap the entire contents and description of two Nodes. There are no copies. The Node’s swap management of all pointers and relevant metadata. The schema parent pointers are updated to reflect the new hierarchy.

  • C++ Move Example:

Node n_a, n_b, info;
// setup initial data
n_b["path/to/data"] = 42;

std::cout << "- Before Move -" << std::endl;
std::cout << "n_a contents:" << std::endl;
n_a.print();
std::cout << "n_a memory details:" << std::endl;
n_a.info(info);
info.print();

std::cout << "n_b contents:" << std::endl;
n_b.print();
std::cout << "n_b memory details:" << std::endl;
n_b.info(info);
info.print();

// execute the move
n_a.move(n_b);

std::cout << "- After Move -" << std::endl;
std::cout << "n_a contents:" << std::endl;
n_a.print();
std::cout << "n_a memory details:" << std::endl;
n_a.info(info);
info.print();

std::cout << "n_b contents:" << std::endl;
n_b.print();
std::cout << "n_b memory details:" << std::endl;
n_b.info(info);
info.print();
  • Output:

- Before Move -
n_a contents:

n_a memory details:

total_bytes_allocated: 0
total_bytes_mmaped: 0
total_bytes_compact: 0
total_strided_bytes: 0

n_b contents:

path: 
  to: 
    data: 42

n_b memory details:

mem_spaces: 
  0x7fdbaae04470: 
    path: "path/to/data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4

- After Move -
n_a contents:

path: 
  to: 
    data: 42

n_a memory details:

mem_spaces: 
  0x7fdbaae04470: 
    path: "path/to/data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4

n_b contents:

n_b memory details:

total_bytes_allocated: 0
total_bytes_mmaped: 0
total_bytes_compact: 0
total_strided_bytes: 0

  • C++ Swap Example:

Node n_a, n_b, info;
// setup initial data
n_a["data"] = 10;
n_b["path/to/data"] = 20;

std::cout << "- Before Swap -" << std::endl;
std::cout << "n_a contents:" << std::endl;
n_a.print();
std::cout << "n_a memory details:" << std::endl;
n_a.info(info);
info.print();

std::cout << "n_b contents:" << std::endl;
n_b.print();
std::cout << "n_b memory details:" << std::endl;
n_b.info(info);
info.print();

n_a.swap(n_b);

std::cout << "- After Swap -" << std::endl;
std::cout << "n_a contents:" << std::endl;
n_a.print();
std::cout << "n_a memory details:" << std::endl;
n_a.info(info);
info.print();

std::cout << "n_b contents:" << std::endl;
n_b.print();
std::cout << "n_b memory details:" << std::endl;
n_b.info(info);
info.print();
  • Output:

- Before Swap -
n_a contents:

data: 10

n_a memory details:

mem_spaces: 
  0x7fdbaaf043d0: 
    path: "data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4

n_b contents:

path: 
  to: 
    data: 20

n_b memory details:

mem_spaces: 
  0x7fdbaaf04830: 
    path: "path/to/data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4

- After Swap -
n_a contents:

path: 
  to: 
    data: 20

n_a memory details:

mem_spaces: 
  0x7fdbaaf04830: 
    path: "path/to/data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4

n_b contents:

data: 10

n_b memory details:

mem_spaces: 
  0x7fdbaaf043d0: 
    path: "data"
    type: "allocated"
    bytes: 4
    allocator_id: 0
total_bytes_allocated: 4
total_bytes_mmaped: 0
total_bytes_compact: 4
total_strided_bytes: 4