/src/libreoffice/sc/inc/fstalgorithm.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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 | | */ |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #include <mdds/flat_segment_tree.hpp> |
13 | | #include <vector> |
14 | | |
15 | | namespace sc { |
16 | | |
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 ) |
22 | 3.63M | { |
23 | 3.63M | Key nLastPos = it->first; |
24 | 3.63M | bool bLastVal = it->second; |
25 | 7.79M | for (++it; it != itEnd; ++it) |
26 | 4.15M | { |
27 | 4.15M | Key nThisPos = it->first; |
28 | 4.15M | bool bThisVal = it->second; |
29 | | |
30 | 4.15M | if (bLastVal) |
31 | 313k | { |
32 | 313k | Key nIndex1 = nLastPos; |
33 | 313k | Key nIndex2 = nThisPos-1; |
34 | | |
35 | 313k | if (!pStart || *pStart < nIndex1) |
36 | 313k | rSpans.push_back(Span(nIndex1, nIndex2)); |
37 | 0 | else if (*pStart <= nIndex2) |
38 | 0 | rSpans.push_back(Span(*pStart, nIndex2)); |
39 | 313k | } |
40 | | |
41 | 4.15M | nLastPos = nThisPos; |
42 | 4.15M | bLastVal = bThisVal; |
43 | 4.15M | } |
44 | 3.63M | } void sc::buildSpan<int, sc::RowSpan>(std::__1::vector<sc::RowSpan, std::__1::allocator<sc::RowSpan> >&, mdds::flat_segment_tree<int, bool>::const_iterator, mdds::flat_segment_tree<int, bool>::const_iterator, int const*) Line | Count | Source | 22 | 3.63M | { | 23 | 3.63M | Key nLastPos = it->first; | 24 | 3.63M | bool bLastVal = it->second; | 25 | 7.79M | for (++it; it != itEnd; ++it) | 26 | 4.15M | { | 27 | 4.15M | Key nThisPos = it->first; | 28 | 4.15M | bool bThisVal = it->second; | 29 | | | 30 | 4.15M | if (bLastVal) | 31 | 313k | { | 32 | 313k | Key nIndex1 = nLastPos; | 33 | 313k | Key nIndex2 = nThisPos-1; | 34 | | | 35 | 313k | if (!pStart || *pStart < nIndex1) | 36 | 313k | rSpans.push_back(Span(nIndex1, nIndex2)); | 37 | 0 | else if (*pStart <= nIndex2) | 38 | 0 | rSpans.push_back(Span(*pStart, nIndex2)); | 39 | 313k | } | 40 | | | 41 | 4.15M | nLastPos = nThisPos; | 42 | 4.15M | bLastVal = bThisVal; | 43 | 4.15M | } | 44 | 3.63M | } |
Unexecuted instantiation: void sc::buildSpan<int, sc::ColRowSpan>(std::__1::vector<sc::ColRowSpan, std::__1::allocator<sc::ColRowSpan> >&, mdds::flat_segment_tree<int, bool>::const_iterator, mdds::flat_segment_tree<int, bool>::const_iterator, int const*) |
45 | | |
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 ) |
51 | 0 | { |
52 | 0 | Key nLastPos = it->first; |
53 | 0 | Val nLastVal = it->second; |
54 | 0 | for (++it; it != itEnd; ++it) |
55 | 0 | { |
56 | 0 | Key nThisPos = it->first; |
57 | 0 | Val nThisVal = it->second; |
58 | |
|
59 | 0 | if (nLastVal) |
60 | 0 | { |
61 | 0 | Key nIndex1 = nLastPos; |
62 | 0 | Key nIndex2 = nThisPos-1; |
63 | 0 | rSpans.push_back(Span(nIndex1, nIndex2, nLastVal)); |
64 | 0 | } |
65 | |
|
66 | 0 | nLastPos = nThisPos; |
67 | 0 | nLastVal = std::move(nThisVal); |
68 | 0 | } |
69 | 0 | } |
70 | | |
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. |
75 | | */ |
76 | | template<typename Key, typename Span> |
77 | | std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree ) |
78 | 3.63M | { |
79 | 3.63M | std::vector<Span> aSpans; |
80 | 3.63M | buildSpan<Key,Span>(aSpans, rTree.begin(), rTree.end(), nullptr); |
81 | 3.63M | return aSpans; |
82 | 3.63M | } std::__1::vector<sc::RowSpan, std::__1::allocator<sc::RowSpan> > sc::toSpanArray<int, sc::RowSpan>(mdds::flat_segment_tree<int, bool> const&) Line | Count | Source | 78 | 3.63M | { | 79 | 3.63M | std::vector<Span> aSpans; | 80 | 3.63M | buildSpan<Key,Span>(aSpans, rTree.begin(), rTree.end(), nullptr); | 81 | 3.63M | return aSpans; | 82 | 3.63M | } |
Unexecuted instantiation: std::__1::vector<sc::ColRowSpan, std::__1::allocator<sc::ColRowSpan> > sc::toSpanArray<int, sc::ColRowSpan>(mdds::flat_segment_tree<int, bool> const&) |
83 | | |
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. |
90 | | */ |
91 | | template<typename Key, typename Val, typename Span> |
92 | | std::vector<Span> toSpanArrayWithValue( const mdds::flat_segment_tree<Key,Val>& rTree ) |
93 | 0 | { |
94 | 0 | std::vector<Span> aSpans; |
95 | |
|
96 | 0 | buildSpanWithValue<Key,Val,Span>(aSpans, rTree.begin(), rTree.end()); |
97 | 0 | return aSpans; |
98 | 0 | } |
99 | | |
100 | | template<typename Key, typename Span> |
101 | | std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree, Key nStartPos ) |
102 | 0 | { |
103 | 0 | typedef mdds::flat_segment_tree<Key,bool> FstType; |
104 | |
|
105 | 0 | std::vector<Span> aSpans; |
106 | 0 | if (!rTree.valid_tree()) |
107 | 0 | return aSpans; |
108 | | |
109 | 0 | bool bThisVal = false; |
110 | 0 | std::pair<typename FstType::const_iterator, bool> r = |
111 | 0 | rTree.search_tree(nStartPos, bThisVal); |
112 | |
|
113 | 0 | if (!r.second) |
114 | | // Tree search failed. |
115 | 0 | return aSpans; |
116 | | |
117 | 0 | buildSpan<Key,Span>(aSpans, r.first, rTree.end(), &nStartPos); |
118 | 0 | return aSpans; |
119 | 0 | } |
120 | | |
121 | | } |
122 | | |
123 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |