Skip to content

Commit 078ae2e

Browse files
committed
General code hygiene
1 parent 51cc943 commit 078ae2e

23 files changed

Lines changed: 104 additions & 122 deletions

src/layout.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ namespace mfl
3333
{
3434
if (std::holds_alternative<glue_spec>(n))
3535
{
36-
const auto& glue = std::get<glue_spec>(n);
37-
if (glue_scale == 0.0) return glue.size;
36+
const auto& [size, stretch, shrink] = std::get<glue_spec>(n);
37+
if (glue_scale == 0.0) return size;
3838

39-
const auto w = (glue_scale < 0.0) ? glue.shrink.value : glue.stretch.value;
39+
const auto w = (glue_scale < 0.0) ? shrink.value : stretch.value;
4040
return static_cast<dist_t>(glue_scale * static_cast<double>(w));
4141
}
4242

src/noad/math_char.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace mfl
1919
return face.glyph_index_from_code_point(char_code, false);
2020

2121
auto result = variants.front().glyph_index;
22-
for (const auto v : variants)
22+
for (const auto [glyph_index, size] : variants)
2323
{
24-
if (v.size > requested_size) return result;
24+
if (size > requested_size) return result;
2525

26-
result = v.glyph_index;
26+
result = glyph_index;
2727
}
2828

2929
return result;

src/noad/noad.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,20 @@ namespace mfl
186186
if (!iterms.empty())
187187
{
188188
const auto& term = iterms.front();
189-
auto tail = iterms.subspan(1);
190-
item_kind kind = item_kind::none;
189+
const auto tail = iterms.subspan(1);
190+
auto kind = item_kind::none;
191191
if (std::holds_alternative<inoad>(term))
192192
{
193-
const auto& n = std::get<inoad>(term);
194-
kind = change_kind(prev_kind, n.kind, tail);
195-
const auto space = make_space(s, math_spacing(prev_kind, n.kind));
196-
result.nodes.insert(result.nodes.end(), space.nodes.begin(), space.nodes.end());
197-
result.nodes.insert(result.nodes.end(), n.translated_noad.nodes.begin(), n.translated_noad.nodes.end());
193+
const auto& [translated_noad, inoad_kind] = std::get<inoad>(term);
194+
kind = change_kind(prev_kind, inoad_kind, tail);
195+
const auto [space_nodes] = make_space(s, math_spacing(prev_kind, inoad_kind));
196+
result.nodes.insert(result.nodes.end(), space_nodes.begin(), space_nodes.end());
197+
result.nodes.insert(result.nodes.end(), translated_noad.nodes.begin(), translated_noad.nodes.end());
198198
}
199199
else if (std::holds_alternative<ispace>(term))
200200
{
201-
const auto space = make_space(s, std::get<ispace>(term));
202-
result.nodes.insert(result.nodes.end(), space.nodes.begin(), space.nodes.end());
201+
const auto [space_nodes] = make_space(s, std::get<ispace>(term));
202+
result.nodes.insert(result.nodes.end(), space_nodes.begin(), space_nodes.end());
203203
}
204204

205205
intermediate_terms_to_hlist(s, has_penalties, kind, tail, result);

src/noad/radical.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace mfl
2222
auto radical_symbol = make_auto_height_glyph(s, font_family::roman, radical_char_code, requested_height).first;
2323
const auto shift =
2424
height(radical_symbol) - (content_box.dims.height + vertical_gap + radical_rule_thickness(s));
25-
constexpr auto percent_divisor = dist_t(100);
25+
constexpr auto percent_divisor = dist_t{100};
2626
degree_box.shift = -degree_box.dims.depth
2727
- (height(radical_symbol) * radical_degree_bottom_raise_percent(s)) / percent_divisor;
2828
auto radical_box =

src/node/box.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace mfl
1717

1818
hlist hlist_with_unit_glue(box&& box)
1919
{
20-
const auto unit_glue_spec = glue_spec{.size = 0,
21-
.stretch = {.value = unit_distance, .order = infinity_order::fil},
22-
.shrink = {.value = unit_distance, .order = infinity_order::fil}};
20+
constexpr auto unit_glue_spec = glue_spec{.size = 0,
21+
.stretch = {.value = unit_distance, .order = infinity_order::fil},
22+
.shrink = {.value = unit_distance, .order = infinity_order::fil}};
2323
return make_hlist(unit_glue_spec, std::move(box), unit_glue_spec);
2424
}
2525
}
@@ -60,7 +60,7 @@ namespace mfl
6060

6161
box make_vbox(const dist_t width, node_variant&& ref_node, vlist&& up_list, vlist&& down_list)
6262
{
63-
auto shift = dist_t(0);
63+
auto shift = dist_t{0};
6464
if (std::holds_alternative<wrapped_box>(ref_node))
6565
{
6666
auto& b = static_cast<box&>(std::get<wrapped_box>(ref_node));
@@ -75,7 +75,7 @@ namespace mfl
7575
nodes.reserve(up_list.nodes.size() + 1 + down_list.nodes.size());
7676
std::move(up_list.nodes.rbegin(), up_list.nodes.rend(), std::back_inserter(nodes));
7777
nodes.emplace_back(std::move(ref_node));
78-
std::move(down_list.nodes.begin(), down_list.nodes.end(), std::back_inserter(nodes));
78+
std::ranges::move(down_list.nodes, std::back_inserter(nodes));
7979

8080
return box{.kind = box_kind::vbox, .dims = dims, .shift = shift, .nodes = std::move(nodes)};
8181
}

src/node/glue.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// if the width_diff is positive
2-
// find highest order for which the sum of all stretch glues in the node list is non-zero
2+
// find the highest order for which the sum of all stretch glues in the node list is non-zero
33
// return the sum and the corresponding order
44
// result is glue_param{.scale = width_diff / sum, .order = order}
55
//
@@ -13,15 +13,15 @@ namespace mfl
1313
{
1414
namespace
1515
{
16-
dist_t sum_of_glue_by_order(const infinity_order order, glue_scale glue_spec::*scale,
16+
dist_t sum_of_glue_by_order(const infinity_order order, glue_scale glue_spec::* scale,
1717
const std::vector<node_variant>& nodes)
1818
{
1919
dist_t glue_sum = 0;
2020
for (const auto& n : nodes)
2121
{
2222
std::visit(overload{[&](const glue_spec& glue) {
23-
const auto& s = glue.*scale;
24-
if (s.order == order) glue_sum += s.value;
23+
if (const auto& [value, scale_order] = glue.*scale; scale_order == order)
24+
glue_sum += value;
2525
},
2626
[](const auto&) {}},
2727
n);
@@ -30,20 +30,20 @@ namespace mfl
3030
return glue_sum;
3131
}
3232

33-
auto highest_order_total_glue(glue_scale glue_spec::*scale, const std::vector<node_variant>& nodes)
33+
auto highest_order_total_glue(glue_scale glue_spec::* scale, const std::vector<node_variant>& nodes)
3434
{
3535
using enum infinity_order;
3636
for (const auto order : {filll, fill, fil, normal})
3737
{
38-
const auto glue_sum = sum_of_glue_by_order(order, scale, nodes);
39-
if (glue_sum != 0) return std::pair(order, glue_sum);
38+
if (const auto glue_sum = sum_of_glue_by_order(order, scale, nodes); glue_sum != 0)
39+
return std::pair(order, glue_sum);
4040
}
4141

42-
return std::pair(infinity_order::normal, dist_t(0));
42+
return std::pair(normal, dist_t{0});
4343
}
4444

4545
glue_param calculate_glue_param(const dist_t width_diff, const std::vector<node_variant>& nodes,
46-
glue_scale glue_spec::*scale_direction, const double sign)
46+
glue_scale glue_spec::* scale_direction, const double sign)
4747
{
4848
const auto [order, sum] = highest_order_total_glue(scale_direction, nodes);
4949
if (sum == 0) return {};

src/node/hlist.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "node/hlist.hpp"
22

33
#include <algorithm>
4-
#include <numeric>
54
#include <ranges>
65

76
namespace mfl
@@ -16,22 +15,28 @@ namespace mfl
1615
hlist concat(hlist&& l0, hlist&& l1)
1716
{
1817
auto result = std::move(l0);
19-
std::move(l1.nodes.begin(), l1.nodes.end(), std::back_inserter(result.nodes));
18+
std::ranges::move(l1.nodes, std::back_inserter(result.nodes));
2019
return result;
2120
}
2221

2322
dist_t hlist_width(const hlist& l)
2423
{
25-
return std::accumulate(l.nodes.begin(), l.nodes.end(), dist_t(0), [&](const dist_t acc, const node_variant& n) { return acc + width(n); });
24+
return std::ranges::fold_left_first(
25+
l.nodes | std::views::transform([](const node_variant& n) { return width(n); }), std::plus{})
26+
.value_or({});
2627
}
2728

2829
dist_t hlist_depth(const hlist& l)
2930
{
30-
return l.nodes.empty() ? 0 : std::ranges::max(l.nodes | std::views::transform([](const node_variant& n) { return depth(n); }));
31+
return l.nodes.empty()
32+
? 0
33+
: std::ranges::max(l.nodes | std::views::transform([](const node_variant& n) { return depth(n); }));
3134
}
3235

3336
dist_t hlist_height(const hlist& l)
3437
{
35-
return l.nodes.empty() ? 0 : std::ranges::max(l.nodes | std::views::transform([](const node_variant& n) { return height(n); }));
38+
return l.nodes.empty()
39+
? 0
40+
: std::ranges::max(l.nodes | std::views::transform([](const node_variant& n) { return height(n); }));
3641
}
3742
}

src/node/node.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,49 @@ namespace mfl
99
dist_t depth(const node_variant& n)
1010
{
1111
return std::visit(overload{[](const box& b) { return b.dims.depth + b.shift; },
12-
[](const glue_spec&) { return dist_t(0); },
13-
[](const glyph& g) { return g.depth; },
14-
[](const kern&) { return dist_t(0); },
15-
[](const rule& r) { return r.depth; }},
12+
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.depth; },
13+
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.depth; }},
1614
n);
1715
}
1816

1917
dist_t height(const node_variant& n)
2018
{
2119
return std::visit(overload{[](const box& b) { return b.dims.height - b.shift; },
22-
[](const glue_spec&) { return dist_t(0); },
23-
[](const glyph& g) { return g.height; },
24-
[](const kern&) { return dist_t(0); },
25-
[](const rule& r) { return r.height; }},
20+
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.height; },
21+
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.height; }},
2622
n);
2723
}
2824

2925
dist_t vsize(const node_variant& n)
3026
{
3127
return std::visit(overload{[](const box& b) { return b.dims.height + b.dims.depth; },
3228
[](const glyph& g) { return g.height + g.depth; },
33-
[](const glue_spec& g) { return g.size; },
34-
[](const kern& k) { return k.size; },
29+
[](const glue_spec& g) { return g.size; }, [](const kern& k) { return k.size; },
3530
[](const rule& r) { return r.height + r.depth; }},
3631
n);
3732
}
3833

3934
dist_t width(const node_variant& n)
4035
{
41-
return std::visit(overload{[](const box& b) { return b.dims.width; },
42-
[](const glue_spec& g) { return g.size; },
43-
[](const glyph& g) { return g.width; },
44-
[](const kern& k) { return k.size; },
36+
return std::visit(overload{[](const box& b) { return b.dims.width; }, [](const glue_spec& g) { return g.size; },
37+
[](const glyph& g) { return g.width; }, [](const kern& k) { return k.size; },
4538
[](const rule& r) { return r.width; }},
4639
n);
4740
}
4841

4942
dist_t vwidth(const node_variant& n)
5043
{
5144
return std::visit(overload{[](const box& b) { return b.shift + b.dims.width; },
52-
[](const glue_spec&) { return dist_t(0); },
53-
[](const glyph& g) { return g.width; },
54-
[](const kern&) { return dist_t(0); },
55-
[](const rule& r) { return r.width; }},
45+
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.width; },
46+
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.width; }},
5647
n);
5748
}
5849

5950
dist_t vheight(const node_variant& n)
6051
{
6152
return std::visit(overload{[](const box& b) { return b.dims.height; },
62-
[](const glue_spec& g) { return g.size; },
63-
[](const glyph& g) { return g.height; },
64-
[](const kern& k) { return k.size; },
65-
[](const rule& r) { return r.height; }},
53+
[](const glue_spec& g) { return g.size; }, [](const glyph& g) { return g.height; },
54+
[](const kern& k) { return k.size; }, [](const rule& r) { return r.height; }},
6655
n);
6756
}
6857
}

src/node/vlist.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#include "node/vlist.hpp"
22

3-
#include <numeric>
3+
#include <algorithm>
4+
#include <ranges>
45

56
namespace mfl
67
{
78
dist_t vlist_size(const vlist& l)
89
{
9-
return std::accumulate(l.nodes.begin(), l.nodes.end(), dist_t(0), [&](const dist_t acc, const node_variant& n) { return acc + vsize(n); });
10+
return std::ranges::fold_left_first(
11+
l.nodes | std::views::transform([](const node_variant& n) { return vsize(n); }), std::plus{})
12+
.value_or({});
1013
}
1114
}

src/parser/big_op.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ namespace mfl::parser
4949

5050
if (sup0 && sup1) state.set_error("multiple superscripts");
5151

52-
optional_noads sub = sub0 ? sub0 : sub1;
53-
optional_noads sup = sup0 ? sup0 : sup1;
52+
const optional_noads sub = sub0 ? sub0 : sub1;
53+
const optional_noads sup = sup0 ? sup0 : sup1;
5454
return {.limits = limits,
5555
.nucleus = {math_char{.kind = item_kind::op, .char_code = unicode_index("\\" + op_name, state)}},
5656
.sub = sub,

0 commit comments

Comments
 (0)