1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 bool ScCsvSplits::Insert( sal_Int32 nPos
)
26 bool bValid
= (nPos
>= 0);
29 iterator aIter
= ::std::lower_bound( maVec
.begin(), maVec
.end(), nPos
);
30 bValid
= (aIter
== maVec
.end()) || (*aIter
!= nPos
);
32 aIter
= maVec
.insert( aIter
, nPos
);
37 bool ScCsvSplits::Remove( sal_Int32 nPos
)
39 sal_uInt32 nIndex
= GetIndex( nPos
);
40 bool bValid
= (nIndex
!= CSV_VEC_NOTFOUND
);
42 maVec
.erase( maVec
.begin() + nIndex
);
46 void ScCsvSplits::RemoveRange( sal_Int32 nPosStart
, sal_Int32 nPosEnd
)
48 sal_uInt32 nStartIx
= LowerBound( nPosStart
);
49 sal_uInt32 nEndIx
= UpperBound( nPosEnd
);
50 if( (nStartIx
!= CSV_VEC_NOTFOUND
) && (nEndIx
!= CSV_VEC_NOTFOUND
) && (nStartIx
<= nEndIx
) )
51 maVec
.erase( maVec
.begin() + nStartIx
, maVec
.begin() + nEndIx
+ 1 );
54 void ScCsvSplits::Clear()
59 bool ScCsvSplits::HasSplit( sal_Int32 nPos
) const
61 return GetIndex( nPos
) != CSV_VEC_NOTFOUND
;
64 sal_uInt32
ScCsvSplits::GetIndex( sal_Int32 nPos
) const
66 const_iterator aIter
= ::std::lower_bound( maVec
.begin(), maVec
.end(), nPos
);
67 return GetIterIndex( ((aIter
!= maVec
.end()) && (*aIter
== nPos
)) ? aIter
: maVec
.end() );
70 sal_uInt32
ScCsvSplits::LowerBound( sal_Int32 nPos
) const
72 return GetIterIndex( ::std::lower_bound( maVec
.begin(), maVec
.end(), nPos
) );
75 sal_uInt32
ScCsvSplits::UpperBound( sal_Int32 nPos
) const
77 sal_uInt32 nIndex
= LowerBound( nPos
);
78 if( nIndex
== CSV_VEC_NOTFOUND
)
79 return Count() ? (Count() - 1) : CSV_VEC_NOTFOUND
;
80 if( GetPos( nIndex
) == nPos
)
82 return nIndex
? (nIndex
- 1) : CSV_VEC_NOTFOUND
;
85 sal_Int32
ScCsvSplits::GetPos( sal_uInt32 nIndex
) const
87 return (nIndex
< Count()) ? maVec
[ nIndex
] : CSV_POS_INVALID
;
90 sal_uInt32
ScCsvSplits::GetIterIndex( const_iterator aIter
) const
92 return (aIter
== maVec
.end()) ? CSV_VEC_NOTFOUND
: (aIter
- maVec
.begin());
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */