diff --git a/refcode/1-ds/pbds.cpp b/refcode/1-ds/pbds.cpp new file mode 100644 index 0000000..b9f0b7f --- /dev/null +++ b/refcode/1-ds/pbds.cpp @@ -0,0 +1,27 @@ +#include "../common/common.hpp" +#include +#include +using namespace __gnu_pbds; +#define ordered_set tree, rb_tree_tag, tree_order_statistics_node_update> +int main() { + ordered_set os; // empty + // basic usage + os.insert(1); // 1 + os.insert(1); // 1 + os.insert(4); // 1 4 + os.insert(3); // 1 3 4 + os.insert(2); // 1 2 3 4 + os.insert(4); // 1 2 3 4 + os.erase(2); // 1 3 4 + os.erase(2); // 1 3 4 + os.insert(1); // 1 3 4 + os.erase(1); // 3 4 + // order_of_key() (Assume os = {13, 32, 34, 45, 48, 59, 61, 68, 71, 78, 87, 128}) + cout << os.order_of_key(10) << '\n'; // 0 + cout << os.order_of_key(49) << '\n'; // 5 + cout << os.order_of_key(48) << '\n'; // 4 + // find_by_order() (Assume os = {13, 32, 34, 45, 48, 59, 61, 68, 71, 78, 87, 128}) + cout << *(os.find_by_order(0)) << '\n'; // 13 + cout << *(os.find_by_order(7)) << '\n'; // 68 + cout << *(os.find_by_order(100)) << '\n'; // 0 +} \ No newline at end of file diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 0000000..e1f5d2d --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +root="$(cd "$(dirname "$0")/.." && pwd)" +target="$root/refcode" +mapfile -t files < <(find "$target" -type f \( -name '*.cpp' -o -name '*.hpp' \)) +((${#files[@]})) || { echo "no .cpp/.hpp files in $target"; exit 0; } +declare -A before +for f in "${files[@]}"; do before["$f"]="$(sha1sum "$f" | cut -d' ' -f1)"; done +clang-format -i "${files[@]}" +changed=() +for f in "${files[@]}"; do [[ ${before["$f"]} != "$(sha1sum "$f" | cut -d' ' -f1)" ]] && changed+=("$f"); done +echo "formatted ${#files[@]} file(s) in $target" +((${#changed[@]})) && printf 'changed %d file(s):\n%s\n' "${#changed[@]}" "$(printf '%s\n' "${changed[@]}")" || echo "no files changed"