LibreOffice Module sc (master)  1
fstalgorithm.hxx
Go to the documentation of this file.
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 #ifndef INCLUDED_SC_INC_FSTALGORITHM_HXX
11 #define INCLUDED_SC_INC_FSTALGORITHM_HXX
12 
13 #include <mdds/flat_segment_tree.hpp>
14 #include <vector>
15 
16 namespace sc {
17 
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 )
23 {
24  Key nLastPos = it->first;
25  bool bLastVal = it->second;
26  for (++it; it != itEnd; ++it)
27  {
28  Key nThisPos = it->first;
29  bool bThisVal = it->second;
30 
31  if (bLastVal)
32  {
33  Key nIndex1 = nLastPos;
34  Key nIndex2 = nThisPos-1;
35 
36  if (!pStart || *pStart < nIndex1)
37  rSpans.push_back(Span(nIndex1, nIndex2));
38  else if (*pStart <= nIndex2)
39  rSpans.push_back(Span(*pStart, nIndex2));
40  }
41 
42  nLastPos = nThisPos;
43  bLastVal = bThisVal;
44  }
45 }
46 
47 template<typename Key, typename Val, typename Span>
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 )
52 {
53  Key nLastPos = it->first;
54  Val nLastVal = it->second;
55  for (++it; it != itEnd; ++it)
56  {
57  Key nThisPos = it->first;
58  Val nThisVal = it->second;
59 
60  if (nLastVal)
61  {
62  Key nIndex1 = nLastPos;
63  Key nIndex2 = nThisPos-1;
64  rSpans.push_back(Span(nIndex1, nIndex2, nLastVal));
65  }
66 
67  nLastPos = nThisPos;
68  nLastVal = nThisVal;
69  }
70 }
71 
77 template<typename Key, typename Span>
78 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree )
79 {
80  typedef mdds::flat_segment_tree<Key,bool> FstType;
81 
82  std::vector<Span> aSpans;
83 
84  typename FstType::const_iterator it = rTree.begin(), itEnd = rTree.end();
85  buildSpan<Key,Span>(aSpans, it, itEnd, nullptr);
86  return aSpans;
87 }
88 
96 template<typename Key, typename Val, typename Span>
97 std::vector<Span> toSpanArrayWithValue( const mdds::flat_segment_tree<Key,Val>& rTree )
98 {
99  typedef mdds::flat_segment_tree<Key,Val> FstType;
100 
101  std::vector<Span> aSpans;
102 
103  typename FstType::const_iterator it = rTree.begin(), itEnd = rTree.end();
104  buildSpanWithValue<Key,Val,Span>(aSpans, it, itEnd);
105  return aSpans;
106 }
107 
108 template<typename Key, typename Span>
109 std::vector<Span> toSpanArray( const mdds::flat_segment_tree<Key,bool>& rTree, Key nStartPos )
110 {
111  typedef mdds::flat_segment_tree<Key,bool> FstType;
112 
113  std::vector<Span> aSpans;
114  if (!rTree.is_tree_valid())
115  return aSpans;
116 
117  bool bThisVal = false;
118  std::pair<typename FstType::const_iterator, bool> r =
119  rTree.search_tree(nStartPos, bThisVal);
120 
121  if (!r.second)
122  // Tree search failed.
123  return aSpans;
124 
125  typename FstType::const_iterator it = r.first, itEnd = rTree.end();
126  buildSpan<Key,Span>(aSpans, it, itEnd, &nStartPos);
127  return aSpans;
128 }
129 
130 }
131 
132 #endif
133 
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void buildSpanWithValue(std::vector< Span > &rSpans, typename mdds::flat_segment_tree< Key, Val >::const_iterator it, typename mdds::flat_segment_tree< Key, Val >::const_iterator itEnd)
void buildSpan(std::vector< Span > &rSpans, typename mdds::flat_segment_tree< Key, bool >::const_iterator it, typename mdds::flat_segment_tree< Key, bool >::const_iterator itEnd, const Key *pStart)
std::vector< Span > toSpanArray(const mdds::flat_segment_tree< Key, bool > &rTree)
Convert a flat_segment_tree structure whose value type is boolean, into an array of ranges that corre...
std::vector< Span > toSpanArrayWithValue(const mdds::flat_segment_tree< Key, Val > &rTree)
Convert a flat_segment_tree structure into an array of ranges with values.
struct _ADOKey Key