A three-way-merge is a git like merge of two or more branches that have been updated in concurrent flows. The map is cloned in O(1) when the branch point is created. Updating individual elements is O(log n).
Let n be the total number of elements. Let k be the number of updates after the branch point. Merging branches using the example code is O(k*log(n/m)).
Note that this performance is optimal for any sorted index structure as it matches the cost for inserting elements into a sorted ephemeral structure.
#include <iostream>
#include <string>
#include "map.h"
#include "set.h"
return {{"Emma", "(759) 534-1383"}, {"Olivia", "(124) 752-7453"},
{"Ava", "(881) 352-1267"}, {"Sophia", "(213) 687-9617"},
{"Mia", "(653) 724-0068"}, {"Amelia", "(181) 123-9026"},
{"Charlotte", "(889) 254-0786"}, {"Harper", "(491) 307-5074"},
{"Ella", "(608) 692-6507"}, {"Aira", "(860) 871-0985"}};
}
std::string worker_name) {
std::cout << "Apply changes by " << worker_name << std::endl;
std::cout << "------------------------" << std::endl;
Keys keys_modified_by_branch =
Keys keys_modified_by_master =
Keys conflicting_keys = keys_modified_by_branch & keys_modified_by_master;
Keys erase_conflicts = conflicting_keys - branch_inserted.
key_set();
Keys insert_conflicts = conflicting_keys - branch_erased.
key_set();
Keys modify_conflicts = conflicting_keys - erase_conflicts - insert_conflicts;
for (auto key : modify_conflicts)
std::cout << key << "'s record not modified because of conflicts."
<< std::endl;
for (auto key : erase_conflicts)
std::cout << key << "'s record not erased because of conflicts."
<< std::endl;
for (auto key : insert_conflicts)
std::cout << key << "'s record not inserted because of conflicts."
<< std::endl;
branch_erased -= conflicting_keys;
branch_inserted -= conflicting_keys;
new_master -= branch_erased;
new_master |= branch_inserted;
std::cout <<
"erased " << branch_erased.
size() <<
" and inserted " << branch_inserted.
size() <<
" entries" << std::endl
<< std::endl;
return new_master;
}
numbers.
insert(std::make_pair(
"Evelyn",
"(251) 546-9442"));
return numbers;
}
numbers.
insert(std::make_pair(
"Madison",
"(630) 446-8851"));
return numbers;
}
numbers.
insert(std::make_pair(
"Evelyn",
"(949) 569-4371"));
numbers.
insert(std::make_pair(
"Scarlett",
"(402) 139-6590"));
return numbers;
}
std::cout << "Phone numbers" << std::endl;
std::cout << "-------------" << std::endl;
for (auto entry : numbers)
std::cout << entry.first << ": " << entry.second << std::endl;
std::cout << std::endl;
}
int main() {
print(master);
master = apply(tag, master, branch1, "worker1");
master = apply(tag, master, branch2, "worker2");
master = apply(tag, master, branch3, "worker3");
print(master);
}