LibreOffice Module sc (master)  1
csvsplits.cxx
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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <csvsplits.hxx>
21 
22 #include <algorithm>
23 
24 #include <sal/log.hxx>
25 
26 bool ScCsvSplits::Insert( sal_Int32 nPos )
27 {
28  if (nPos < 0)
29  return false;
30 
31  const auto aIter = ::std::lower_bound( maVec.begin(), maVec.end(), nPos );
32 
33  if (aIter != maVec.end() && *aIter == nPos)
34  return false;
35 
36  SAL_WARN_IF(maVec.size()>=static_cast<std::size_t>(SAL_MAX_UINT32-1),
37  "sc.ui", "ScCsvSplits::Insert: too many elements in vector");
38 
39  maVec.insert( aIter, nPos );
40  return true;
41 }
42 
43 bool ScCsvSplits::Remove( sal_Int32 nPos )
44 {
45  sal_uInt32 nIndex = GetIndex( nPos );
46  if (nIndex == CSV_VEC_NOTFOUND)
47  return false;
48 
49  maVec.erase( maVec.begin() + nIndex );
50  return true;
51 }
52 
53 void ScCsvSplits::RemoveRange( sal_Int32 nPosStart, sal_Int32 nPosEnd )
54 {
55  sal_uInt32 nStartIx = LowerBound( nPosStart );
56  sal_uInt32 nEndIx = UpperBound( nPosEnd );
57  if( (nStartIx != CSV_VEC_NOTFOUND) && (nEndIx != CSV_VEC_NOTFOUND) && (nStartIx <= nEndIx) )
58  maVec.erase( maVec.begin() + nStartIx, maVec.begin() + nEndIx + 1 );
59 }
60 
62 {
63  maVec.clear();
64 }
65 
66 bool ScCsvSplits::HasSplit( sal_Int32 nPos ) const
67 {
68  return GetIndex( nPos ) != CSV_VEC_NOTFOUND;
69 }
70 
71 sal_uInt32 ScCsvSplits::GetIndex( sal_Int32 nPos ) const
72 {
73  auto aIter = ::std::lower_bound( maVec.cbegin(), maVec.cend(), nPos );
74  return GetIterIndex( ((aIter != maVec.end()) && (*aIter == nPos)) ? aIter : maVec.end() );
75 }
76 
77 sal_uInt32 ScCsvSplits::LowerBound( sal_Int32 nPos ) const
78 {
79  return GetIterIndex( ::std::lower_bound( maVec.begin(), maVec.end(), nPos ) );
80 }
81 
82 sal_uInt32 ScCsvSplits::UpperBound( sal_Int32 nPos ) const
83 {
84  sal_uInt32 nIndex = LowerBound( nPos );
85  if( nIndex == CSV_VEC_NOTFOUND )
86  return Count() ? (Count() - 1) : CSV_VEC_NOTFOUND;
87  if( GetPos( nIndex ) == nPos )
88  return nIndex;
89  return nIndex ? (nIndex - 1) : CSV_VEC_NOTFOUND;
90 }
91 
92 sal_Int32 ScCsvSplits::GetPos( sal_uInt32 nIndex ) const
93 {
94  return (nIndex < Count()) ? maVec[ nIndex ] : CSV_POS_INVALID;
95 }
96 
97 sal_uInt32 ScCsvSplits::GetIterIndex( const_iterator const & aIter ) const
98 {
99  return (aIter == maVec.end()) ? CSV_VEC_NOTFOUND : (aIter - maVec.begin());
100 }
101 
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt32 UpperBound(sal_Int32 nPos) const
Returns index of the last split less than or equal to nPos.
Definition: csvsplits.cxx:82
sal_Int32 nIndex
const sal_Int32 CSV_POS_INVALID
Constant for an invalid ruler position.
Definition: csvsplits.hxx:29
sal_Int32 GetPos(sal_uInt32 nIndex) const
Returns the position of the specified split.
Definition: csvsplits.cxx:92
sal_uInt32 LowerBound(sal_Int32 nPos) const
Returns index of the first split greater than or equal to nPos.
Definition: csvsplits.cxx:77
#define SAL_MAX_UINT32
bool Remove(sal_Int32 nPos)
Removes a split by position.
Definition: csvsplits.cxx:43
const sal_uInt32 CSV_VEC_NOTFOUND
Constant for an invalid vector index.
Definition: csvsplits.hxx:27
ScSplitVector::const_iterator const_iterator
Definition: csvsplits.hxx:36
sal_uInt32 GetIndex(sal_Int32 nPos) const
Searches for a split at position nPos.
Definition: csvsplits.cxx:71
ScSplitVector maVec
Definition: csvsplits.hxx:38
bool HasSplit(sal_Int32 nPos) const
Returns true if at position nPos is a split.
Definition: csvsplits.cxx:66
sal_uInt32 GetIterIndex(const_iterator const &aIter) const
Returns the vector index of an iterator.
Definition: csvsplits.cxx:97
void Clear()
Removes all elements from the vector.
Definition: csvsplits.cxx:61
#define SAL_WARN_IF(condition, area, stream)
void RemoveRange(sal_Int32 nPosStart, sal_Int32 nPosEnd)
Removes a range of splits in the given position range.
Definition: csvsplits.cxx:53
bool Insert(sal_Int32 nPos)
The split container.
Definition: csvsplits.cxx:26
sal_uInt32 Count() const
Returns the number of splits.
Definition: csvsplits.hxx:68