build fix
[LibreOffice.git] / sc / inc / fstalgorithm.hxx
blob73a4c5c3d64a9375054f265e2f23119c62d44922
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 #ifndef INCLUDED_SC_INC_FSTALGORITHM_HXX
11 #define INCLUDED_SC_INC_FSTALGORITHM_HXX
13 #include <mdds/flat_segment_tree.hpp>
14 #include <vector>
16 namespace sc {
18 template<typename Key, typename Span>
19 void buildSpan(
20 std::vector<Span>& rSpans,
21 typename mdds::flat_segment_tree<Key,bool>::const_iterator it,
22 typename mdds::flat_segment_tree<Key,bool>::const_iterator itEnd, const Key* pStart )
24 Key nLastPos = it->first;
25 bool bLastVal = it->second;
26 for (++it; it != itEnd; ++it)
28 Key nThisPos = it->first;
29 bool bThisVal = it->second;
31 if (bLastVal)
33 Key nIndex1 = nLastPos;
34 Key nIndex2 = nThisPos-1;
36 if (!pStart || *pStart < nIndex1)
37 rSpans.push_back(Span(nIndex1, nIndex2));
38 else if (*pStart <= nIndex2)
39 rSpans.push_back(Span(*pStart, nIndex2));
42 nLastPos = nThisPos;
43 bLastVal = bThisVal;
47 template<typename Key, typename Val, typename Span>
48 void buildSpanWithValue(
49 std::vector<Span>& rSpans,
50 typename mdds::flat_segment_tree<Key,Val>::const_iterator it,
51 typename mdds::flat_segment_tree<Key,Val>::const_iterator itEnd, const Key* pStart )
53 Key nLastPos = it->first;
54 Val nLastVal = it->second;
55 for (++it; it != itEnd; ++it)
57 Key nThisPos = it->first;
58 Val nThisVal = it->second;
60 if (nLastVal)
62 Key nIndex1 = nLastPos;
63 Key nIndex2 = nThisPos-1;
65 if (!pStart || *pStart < nIndex1)
66 rSpans.push_back(Span(nIndex1, nIndex2, nLastVal));
67 else if (*pStart <= nIndex2)
68 rSpans.push_back(Span(*pStart, nIndex2, nLastVal));
71 nLastPos = nThisPos;
72 nLastVal = nThisVal;
76 /**
77 * Convert a flat_segment_tree structure whose value type is boolean, into
78 * an array of ranges that corresponds with the segments that have a 'true'
79 * value.
81 template<typename Key, typename Span>
82 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree )
84 typedef mdds::flat_segment_tree<Key,bool> FstType;
86 std::vector<Span> aSpans;
88 typename FstType::const_iterator it = rTree.begin(), itEnd = rTree.end();
89 buildSpan<Key,Span>(aSpans, it, itEnd, nullptr);
90 return aSpans;
93 /**
94 * Convert a flat_segment_tree structure into an array of ranges with
95 * values. Only those ranges whose value is evaluated to be true will be
96 * included. The value type must be something that supports bool operator.
97 * The span type must support a constructor that takes a start key, an end
98 * key and a value in this order.
100 template<typename Key, typename Val, typename Span>
101 std::vector<Span> toSpanArrayWithValue( const mdds::flat_segment_tree<Key,Val>& rTree )
103 typedef mdds::flat_segment_tree<Key,Val> FstType;
105 std::vector<Span> aSpans;
107 typename FstType::const_iterator it = rTree.begin(), itEnd = rTree.end();
108 buildSpanWithValue<Key,Val,Span>(aSpans, it, itEnd, nullptr);
109 return aSpans;
112 template<typename Key, typename Span>
113 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree, Key nStartPos )
115 typedef mdds::flat_segment_tree<Key,bool> FstType;
117 std::vector<Span> aSpans;
118 if (!rTree.is_tree_valid())
119 return aSpans;
121 bool bThisVal = false;
122 std::pair<typename FstType::const_iterator, bool> r =
123 rTree.search_tree(nStartPos, bThisVal);
125 if (!r.second)
126 // Tree search failed.
127 return aSpans;
129 typename FstType::const_iterator it = r.first, itEnd = rTree.end();
130 buildSpan<Key,Span>(aSpans, it, itEnd, &nStartPos);
131 return aSpans;
136 #endif
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */