Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
rope_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
39#include <print_rule.H>
40#include <tpl_rope.H>
41
42#include <iostream>
43#include <string>
44#include <string_view>
45
46using namespace Aleph;
47
48namespace
49{
51{
52 std::cout << "[1] Construction, concat, substr, at, flatten\n";
53 print_rule();
54
55 Rope<char> greeting{std::string_view("Hello, ")};
56 Rope<char> name{std::string_view("world")};
57 Rope<char> punctuation{std::string_view("!")};
58
59 // concat is O(1) plus the occasional rebalance: it wraps existing
60 // trees in a new node, it never copies character data.
62
63 std::cout << "message: \"" << message.to_string() << "\"\n";
64 std::cout << "size: " << message.size() << "\n";
65 std::cout << "char at 7: '" << message.at(7) << "' (expect 'w')\n";
66
67 // substr shares whole subtrees with the source rope where possible;
68 // it is typically O(log size()), but ranges that must be rejoined can
69 // cost closer to the number of leaves in the extracted range.
71 std::cout << "substr(7, 5): \"" << just_name.to_string() << "\" (expect \"world\")\n\n";
72}
73
75{
76 std::cout << "[2] Structural sharing: a copy is O(1) and independent\n";
77 print_rule();
78
79 Rope<char> original{std::string_view("immutable")};
80 Rope<char> copy = original; // O(1): shares the same tree, no data copied.
81
82 // Deriving a new rope from `copy` never touches `original`'s tree.
83 Rope<char> derived = copy.concat(Rope<char>{std::string_view(" and shared")});
84
85 std::cout << "original: \"" << original.to_string() << "\"\n";
86 std::cout << "copy: \"" << copy.to_string() << "\"\n";
87 std::cout << "derived: \"" << derived.to_string() << "\"\n";
88 std::cout << "original == copy (same content): " << std::boolalpha
89 << (original == copy) << "\n\n";
90}
91
93{
94 std::cout << "[3] Text-editing style usage: insert and erase\n";
95 print_rule();
96
97 Rope<char> doc{std::string_view("The fox jumps over the dog.")};
98 std::cout << "original: \"" << doc.to_string() << "\"\n";
99
100 // insert(pos, other) is substr(0,pos) + other + substr(pos, rest),
101 // so it usually touches logarithmically many nodes plus boundary leaves,
102 // instead of shifting the whole suffix like std::string::insert.
103 Rope<char> with_adjective = doc.insert(4, Rope<char>{std::string_view("quick brown ")});
104 std::cout << "after insert: \"" << with_adjective.to_string() << "\"\n";
105
106 // erase(pos, len) removes [pos, pos+len) the same way, via two substr
107 // calls plus one concat.
109 std::cout << "after erase: \"" << without_adjective.to_string()
110 << "\" (back to the original text)\n";
111 std::cout << "doc unchanged by either edit: \"" << doc.to_string() << "\"\n\n";
112}
113} // namespace
114
115int main()
116{
117 std::cout << "\n=== Aleph::Rope: immutable, structurally-shared string ===\n\n";
118
122
123 std::cout << "Done.\n";
124 return 0;
125}
Immutable, structurally-shared rope over a sequence of Char.
Definition tpl_rope.H:176
Rope erase(const size_t pos, const size_t len) const
Return a new rope with [pos, pos+len) removed.
Definition tpl_rope.H:775
Rope insert(const size_t pos, const Rope &other) const
Return a new rope with other inserted at pos.
Definition tpl_rope.H:753
Rope concat(const Rope &other) const
Return a new rope that is *this followed by other.
Definition tpl_rope.H:708
std::basic_string< Char > to_string() const
Return every character of this rope as a std::basic_string.
Definition tpl_rope.H:813
Rope substr(const size_t pos, const size_t len) const
Return a new rope holding [pos, pos+len) of *this.
Definition tpl_rope.H:730
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
Definition ahDefs.C:95
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
static char doc[]
Definition ntreepic.C:1832
int main()
Immutable, structurally-shared rope (Aleph::Rope) for large character sequences.