annotate win64-msvc/include/kj/string-tree.h @ 59:cd4953afea46

This config script works better for libsndfile at least
author Chris Cannam
date Mon, 09 Jan 2017 13:57:37 +0000
parents d93140aac40b
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef KJ_STRING_TREE_H_
Chris@47 23 #define KJ_STRING_TREE_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include "string.h"
Chris@47 30
Chris@47 31 namespace kj {
Chris@47 32
Chris@47 33 class StringTree {
Chris@47 34 // A long string, represented internally as a tree of strings. This data structure is like a
Chris@47 35 // String, but optimized for concatenation and iteration at the expense of seek time. The
Chris@47 36 // structure is intended to be used for building large text blobs from many small pieces, where
Chris@47 37 // repeatedly concatenating smaller strings into larger ones would waste copies. This structure
Chris@47 38 // is NOT intended for use cases requiring random access or computing substrings. For those,
Chris@47 39 // you should use a Rope, which is a much more complicated data structure.
Chris@47 40 //
Chris@47 41 // The proper way to construct a StringTree is via kj::strTree(...), which works just like
Chris@47 42 // kj::str(...) but returns a StringTree rather than a String.
Chris@47 43 //
Chris@47 44 // KJ_STRINGIFY() functions that construct large strings from many smaller strings are encouraged
Chris@47 45 // to return StringTree rather than a flat char container.
Chris@47 46
Chris@47 47 public:
Chris@47 48 inline StringTree(): size_(0) {}
Chris@47 49 inline StringTree(String&& text): size_(text.size()), text(kj::mv(text)) {}
Chris@47 50
Chris@47 51 StringTree(Array<StringTree>&& pieces, StringPtr delim);
Chris@47 52 // Build a StringTree by concatenating the given pieces, delimited by the given delimiter
Chris@47 53 // (e.g. ", ").
Chris@47 54
Chris@47 55 inline size_t size() const { return size_; }
Chris@47 56
Chris@47 57 template <typename Func>
Chris@47 58 void visit(Func&& func) const;
Chris@47 59
Chris@47 60 String flatten() const;
Chris@47 61 // Return the contents as a string.
Chris@47 62
Chris@47 63 // TODO(someday): flatten() when *this is an rvalue and when branches.size() == 0 could simply
Chris@47 64 // return `kj::mv(text)`. Requires reference qualifiers (Clang 3.3 / GCC 4.8).
Chris@47 65
Chris@47 66 void flattenTo(char* __restrict__ target) const;
Chris@47 67 // Copy the contents to the given character array. Does not add a NUL terminator.
Chris@47 68
Chris@47 69 private:
Chris@47 70 size_t size_;
Chris@47 71 String text;
Chris@47 72
Chris@47 73 struct Branch;
Chris@47 74 Array<Branch> branches; // In order.
Chris@47 75
Chris@47 76 inline void fill(char* pos, size_t branchIndex);
Chris@47 77 template <typename First, typename... Rest>
Chris@47 78 void fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest);
Chris@47 79 template <typename... Rest>
Chris@47 80 void fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest);
Chris@47 81 template <typename... Rest>
Chris@47 82 void fill(char* pos, size_t branchIndex, Array<char>&& first, Rest&&... rest);
Chris@47 83 template <typename... Rest>
Chris@47 84 void fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest);
Chris@47 85
Chris@47 86 template <typename... Params>
Chris@47 87 static StringTree concat(Params&&... params);
Chris@47 88 static StringTree&& concat(StringTree&& param) { return kj::mv(param); }
Chris@47 89
Chris@47 90 template <typename T>
Chris@47 91 static inline size_t flatSize(const T& t) { return t.size(); }
Chris@47 92 static inline size_t flatSize(String&& s) { return 0; }
Chris@47 93 static inline size_t flatSize(StringTree&& s) { return 0; }
Chris@47 94
Chris@47 95 template <typename T>
Chris@47 96 static inline size_t branchCount(const T& t) { return 0; }
Chris@47 97 static inline size_t branchCount(String&& s) { return 1; }
Chris@47 98 static inline size_t branchCount(StringTree&& s) { return 1; }
Chris@47 99
Chris@47 100 template <typename... Params>
Chris@47 101 friend StringTree strTree(Params&&... params);
Chris@47 102 };
Chris@47 103
Chris@47 104 inline StringTree&& KJ_STRINGIFY(StringTree&& tree) { return kj::mv(tree); }
Chris@47 105 inline const StringTree& KJ_STRINGIFY(const StringTree& tree) { return tree; }
Chris@47 106
Chris@47 107 inline StringTree KJ_STRINGIFY(Array<StringTree>&& trees) { return StringTree(kj::mv(trees), ""); }
Chris@47 108
Chris@47 109 template <typename... Params>
Chris@47 110 StringTree strTree(Params&&... params);
Chris@47 111 // Build a StringTree by stringifying the given parameters and concatenating the results.
Chris@47 112 // If any of the parameters stringify to StringTree rvalues, they will be incorporated as
Chris@47 113 // branches to avoid a copy.
Chris@47 114
Chris@47 115 // =======================================================================================
Chris@47 116 // Inline implementation details
Chris@47 117
Chris@47 118 namespace _ { // private
Chris@47 119
Chris@47 120 template <typename... Rest>
Chris@47 121 char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest) {
Chris@47 122 // Make str() work with stringifiers that return StringTree by patching fill().
Chris@47 123
Chris@47 124 first.flattenTo(target);
Chris@47 125 return fill(target + first.size(), kj::fwd<Rest>(rest)...);
Chris@47 126 }
Chris@47 127
Chris@47 128 template <typename T> constexpr bool isStringTree() { return false; }
Chris@47 129 template <> constexpr bool isStringTree<StringTree>() { return true; }
Chris@47 130
Chris@47 131 inline StringTree&& toStringTreeOrCharSequence(StringTree&& tree) { return kj::mv(tree); }
Chris@47 132 inline StringTree toStringTreeOrCharSequence(String&& str) { return StringTree(kj::mv(str)); }
Chris@47 133
Chris@47 134 template <typename T>
Chris@47 135 inline auto toStringTreeOrCharSequence(T&& value)
Chris@47 136 -> decltype(toCharSequence(kj::fwd<T>(value))) {
Chris@47 137 static_assert(!isStringTree<Decay<T>>(),
Chris@47 138 "When passing a StringTree into kj::strTree(), either pass it by rvalue "
Chris@47 139 "(use kj::mv(value)) or explicitly call value.flatten() to make a copy.");
Chris@47 140
Chris@47 141 return toCharSequence(kj::fwd<T>(value));
Chris@47 142 }
Chris@47 143
Chris@47 144 } // namespace _ (private)
Chris@47 145
Chris@47 146 struct StringTree::Branch {
Chris@47 147 size_t index;
Chris@47 148 // Index in `text` where this branch should be inserted.
Chris@47 149
Chris@47 150 StringTree content;
Chris@47 151 };
Chris@47 152
Chris@47 153 template <typename Func>
Chris@47 154 void StringTree::visit(Func&& func) const {
Chris@47 155 size_t pos = 0;
Chris@47 156 for (auto& branch: branches) {
Chris@47 157 if (branch.index > pos) {
Chris@47 158 func(text.slice(pos, branch.index));
Chris@47 159 pos = branch.index;
Chris@47 160 }
Chris@47 161 branch.content.visit(func);
Chris@47 162 }
Chris@47 163 if (text.size() > pos) {
Chris@47 164 func(text.slice(pos, text.size()));
Chris@47 165 }
Chris@47 166 }
Chris@47 167
Chris@47 168 inline void StringTree::fill(char* pos, size_t branchIndex) {
Chris@47 169 KJ_IREQUIRE(pos == text.end() && branchIndex == branches.size(),
Chris@47 170 kj::str(text.end() - pos, ' ', branches.size() - branchIndex).cStr());
Chris@47 171 }
Chris@47 172
Chris@47 173 template <typename First, typename... Rest>
Chris@47 174 void StringTree::fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest) {
Chris@47 175 pos = _::fill(pos, kj::fwd<First>(first));
Chris@47 176 fill(pos, branchIndex, kj::fwd<Rest>(rest)...);
Chris@47 177 }
Chris@47 178
Chris@47 179 template <typename... Rest>
Chris@47 180 void StringTree::fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest) {
Chris@47 181 branches[branchIndex].index = pos - text.begin();
Chris@47 182 branches[branchIndex].content = kj::mv(first);
Chris@47 183 fill(pos, branchIndex + 1, kj::fwd<Rest>(rest)...);
Chris@47 184 }
Chris@47 185
Chris@47 186 template <typename... Rest>
Chris@47 187 void StringTree::fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest) {
Chris@47 188 branches[branchIndex].index = pos - text.begin();
Chris@47 189 branches[branchIndex].content = StringTree(kj::mv(first));
Chris@47 190 fill(pos, branchIndex + 1, kj::fwd<Rest>(rest)...);
Chris@47 191 }
Chris@47 192
Chris@47 193 template <typename... Params>
Chris@47 194 StringTree StringTree::concat(Params&&... params) {
Chris@47 195 StringTree result;
Chris@47 196 result.size_ = _::sum({params.size()...});
Chris@47 197 result.text = heapString(
Chris@47 198 _::sum({StringTree::flatSize(kj::fwd<Params>(params))...}));
Chris@47 199 result.branches = heapArray<StringTree::Branch>(
Chris@47 200 _::sum({StringTree::branchCount(kj::fwd<Params>(params))...}));
Chris@47 201 result.fill(result.text.begin(), 0, kj::fwd<Params>(params)...);
Chris@47 202 return result;
Chris@47 203 }
Chris@47 204
Chris@47 205 template <typename... Params>
Chris@47 206 StringTree strTree(Params&&... params) {
Chris@47 207 return StringTree::concat(_::toStringTreeOrCharSequence(kj::fwd<Params>(params))...);
Chris@47 208 }
Chris@47 209
Chris@47 210 } // namespace kj
Chris@47 211
Chris@47 212 #endif // KJ_STRING_TREE_H_