1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sc.hxx" 30 31 32 // ============================================================================ 33 #include "csvsplits.hxx" 34 #include <tools/debug.hxx> 35 36 #include <algorithm> 37 38 39 // ============================================================================ 40 41 bool ScCsvSplits::Insert( sal_Int32 nPos ) 42 { 43 bool bValid = (nPos >= 0); 44 if( bValid ) 45 { 46 iterator aIter = ::std::lower_bound( maVec.begin(), maVec.end(), nPos ); 47 bValid = (aIter == maVec.end()) || (*aIter != nPos); 48 if( bValid ) 49 aIter = maVec.insert( aIter, nPos ); 50 } 51 return bValid; 52 } 53 54 bool ScCsvSplits::Remove( sal_Int32 nPos ) 55 { 56 sal_uInt32 nIndex = GetIndex( nPos ); 57 bool bValid = (nIndex != CSV_VEC_NOTFOUND); 58 if( bValid ) 59 maVec.erase( maVec.begin() + nIndex ); 60 return bValid; 61 } 62 63 void ScCsvSplits::RemoveRange( sal_Int32 nPosStart, sal_Int32 nPosEnd ) 64 { 65 sal_uInt32 nStartIx = LowerBound( nPosStart ); 66 sal_uInt32 nEndIx = UpperBound( nPosEnd ); 67 if( (nStartIx != CSV_VEC_NOTFOUND) && (nEndIx != CSV_VEC_NOTFOUND) && (nStartIx <= nEndIx) ) 68 maVec.erase( maVec.begin() + nStartIx, maVec.begin() + nEndIx + 1 ); 69 } 70 71 void ScCsvSplits::Clear() 72 { 73 maVec.clear(); 74 } 75 76 bool ScCsvSplits::HasSplit( sal_Int32 nPos ) const 77 { 78 return GetIndex( nPos ) != CSV_VEC_NOTFOUND; 79 } 80 81 82 // ---------------------------------------------------------------------------- 83 84 sal_uInt32 ScCsvSplits::GetIndex( sal_Int32 nPos ) const 85 { 86 const_iterator aIter = ::std::lower_bound( maVec.begin(), maVec.end(), nPos ); 87 return GetIterIndex( ((aIter != maVec.end()) && (*aIter == nPos)) ? aIter : maVec.end() ); 88 } 89 90 sal_uInt32 ScCsvSplits::LowerBound( sal_Int32 nPos ) const 91 { 92 return GetIterIndex( ::std::lower_bound( maVec.begin(), maVec.end(), nPos ) ); 93 } 94 95 sal_uInt32 ScCsvSplits::UpperBound( sal_Int32 nPos ) const 96 { 97 sal_uInt32 nIndex = LowerBound( nPos ); 98 if( nIndex == CSV_VEC_NOTFOUND ) 99 return Count() ? (Count() - 1) : CSV_VEC_NOTFOUND; 100 if( GetPos( nIndex ) == nPos ) 101 return nIndex; 102 return nIndex ? (nIndex - 1) : CSV_VEC_NOTFOUND; 103 } 104 105 sal_Int32 ScCsvSplits::GetPos( sal_uInt32 nIndex ) const 106 { 107 return (nIndex < Count()) ? maVec[ nIndex ] : CSV_POS_INVALID; 108 } 109 110 111 // ---------------------------------------------------------------------------- 112 113 sal_uInt32 ScCsvSplits::GetIterIndex( const_iterator aIter ) const 114 { 115 return (aIter == maVec.end()) ? CSV_VEC_NOTFOUND : (aIter - maVec.begin()); 116 } 117 118 119 // ============================================================================ 120 121