cid#1607171 Data race condition
[LibreOffice.git] / sc / source / ui / dbgui / csvsplits.cxx
blob575bd53d0ac730a359605e295354c5834995540e
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <csvsplits.hxx>
22 #include <algorithm>
24 #include <sal/log.hxx>
26 bool ScCsvSplits::Insert( sal_Int32 nPos )
28 if (nPos < 0)
29 return false;
31 const auto aIter = ::std::lower_bound( maVec.begin(), maVec.end(), nPos );
33 if (aIter != maVec.end() && *aIter == nPos)
34 return false;
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");
39 maVec.insert( aIter, nPos );
40 return true;
43 bool ScCsvSplits::Remove( sal_Int32 nPos )
45 sal_uInt32 nIndex = GetIndex( nPos );
46 if (nIndex == CSV_VEC_NOTFOUND)
47 return false;
49 maVec.erase( maVec.begin() + nIndex );
50 return true;
53 void ScCsvSplits::RemoveRange( sal_Int32 nPosStart, sal_Int32 nPosEnd )
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 );
61 void ScCsvSplits::Clear()
63 maVec.clear();
66 bool ScCsvSplits::HasSplit( sal_Int32 nPos ) const
68 return GetIndex( nPos ) != CSV_VEC_NOTFOUND;
71 sal_uInt32 ScCsvSplits::GetIndex( sal_Int32 nPos ) const
73 auto aIter = ::std::lower_bound( maVec.cbegin(), maVec.cend(), nPos );
74 return GetIterIndex( ((aIter != maVec.end()) && (*aIter == nPos)) ? aIter : maVec.end() );
77 sal_uInt32 ScCsvSplits::LowerBound( sal_Int32 nPos ) const
79 return GetIterIndex( ::std::lower_bound( maVec.begin(), maVec.end(), nPos ) );
82 sal_uInt32 ScCsvSplits::UpperBound( sal_Int32 nPos ) const
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;
92 sal_Int32 ScCsvSplits::GetPos( sal_uInt32 nIndex ) const
94 return (nIndex < Count()) ? maVec[ nIndex ] : CSV_POS_INVALID;
97 sal_uInt32 ScCsvSplits::GetIterIndex( const_iterator const & aIter ) const
99 return (aIter == maVec.end()) ? CSV_VEC_NOTFOUND : (aIter - maVec.begin());
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */