tdf#48459 sw inline heading: don't apply inside frames or over 120 chars
[LibreOffice.git] / sc / inc / fstalgorithm.hxx
blob4d46cfaaa28c27427bd8f1f33c6fc9e1642342b8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include <mdds/flat_segment_tree.hpp>
13 #include <vector>
15 namespace sc {
17 template<typename Key, typename Span>
18 void buildSpan(
19 std::vector<Span>& rSpans,
20 typename mdds::flat_segment_tree<Key,bool>::const_iterator it,
21 typename mdds::flat_segment_tree<Key,bool>::const_iterator itEnd, const Key* pStart )
23 Key nLastPos = it->first;
24 bool bLastVal = it->second;
25 for (++it; it != itEnd; ++it)
27 Key nThisPos = it->first;
28 bool bThisVal = it->second;
30 if (bLastVal)
32 Key nIndex1 = nLastPos;
33 Key nIndex2 = nThisPos-1;
35 if (!pStart || *pStart < nIndex1)
36 rSpans.push_back(Span(nIndex1, nIndex2));
37 else if (*pStart <= nIndex2)
38 rSpans.push_back(Span(*pStart, nIndex2));
41 nLastPos = nThisPos;
42 bLastVal = bThisVal;
46 template<typename Key, typename Val, typename Span>
47 void buildSpanWithValue(
48 std::vector<Span>& rSpans,
49 typename mdds::flat_segment_tree<Key,Val>::const_iterator it,
50 typename mdds::flat_segment_tree<Key,Val>::const_iterator itEnd )
52 Key nLastPos = it->first;
53 Val nLastVal = it->second;
54 for (++it; it != itEnd; ++it)
56 Key nThisPos = it->first;
57 Val nThisVal = it->second;
59 if (nLastVal)
61 Key nIndex1 = nLastPos;
62 Key nIndex2 = nThisPos-1;
63 rSpans.push_back(Span(nIndex1, nIndex2, nLastVal));
66 nLastPos = nThisPos;
67 nLastVal = std::move(nThisVal);
71 /**
72 * Convert a flat_segment_tree structure whose value type is boolean, into
73 * an array of ranges that corresponds with the segments that have a 'true'
74 * value.
76 template<typename Key, typename Span>
77 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree )
79 std::vector<Span> aSpans;
80 buildSpan<Key,Span>(aSpans, rTree.begin(), rTree.end(), nullptr);
81 return aSpans;
84 /**
85 * Convert a flat_segment_tree structure into an array of ranges with
86 * values. Only those ranges whose value is evaluated to be true will be
87 * included. The value type must be something that supports bool operator.
88 * The span type must support a constructor that takes a start key, an end
89 * key and a value in this order.
91 template<typename Key, typename Val, typename Span>
92 std::vector<Span> toSpanArrayWithValue( const mdds::flat_segment_tree<Key,Val>& rTree )
94 std::vector<Span> aSpans;
96 buildSpanWithValue<Key,Val,Span>(aSpans, rTree.begin(), rTree.end());
97 return aSpans;
100 template<typename Key, typename Span>
101 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree, Key nStartPos )
103 typedef mdds::flat_segment_tree<Key,bool> FstType;
105 std::vector<Span> aSpans;
106 if (!rTree.is_tree_valid())
107 return aSpans;
109 bool bThisVal = false;
110 std::pair<typename FstType::const_iterator, bool> r =
111 rTree.search_tree(nStartPos, bThisVal);
113 if (!r.second)
114 // Tree search failed.
115 return aSpans;
117 buildSpan<Key,Span>(aSpans, r.first, rTree.end(), &nStartPos);
118 return aSpans;
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */