<commit_before>/*************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "precompiled_toolkit.hxx"

#include "sortablegriddatamodel.hxx"
#include "toolkit/helper/servicenames.hxx"

/** === begin UNO includes === **/
#include <com/sun/star/i18n/XCollator.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/ucb/AlreadyInitializedException.hpp>
/** === end UNO includes === **/

#include <comphelper/anycompare.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <tools/diagnose_ex.h>
#include <tools/debug.hxx>
#include <vcl/svapp.hxx>

#include <set>

//......................................................................................................................
namespace toolkit
{
//......................................................................................................................

    /** === begin UNO using === **/
    using ::com::sun::star::uno::TypeClass;
    using ::com::sun::star::uno::TypeClass_VOID;
    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::uno::XInterface;
    using ::com::sun::star::uno::UNO_QUERY;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::uno::UNO_SET_THROW;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::uno::RuntimeException;
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::uno::makeAny;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::uno::Type;
    using ::com::sun::star::lang::IndexOutOfBoundsException;
    using ::com::sun::star::lang::IllegalArgumentException;
    using ::com::sun::star::awt::grid::XGridDataListener;
    using ::com::sun::star::beans::Pair;
    using ::com::sun::star::util::XCloneable;
    using ::com::sun::star::i18n::XCollator;
    using ::com::sun::star::lang::IllegalArgumentException;
    using ::com::sun::star::lang::XMultiServiceFactory;
    using ::com::sun::star::awt::grid::GridDataEvent;
    using ::com::sun::star::lang::EventObject;
    using ::com::sun::star::ucb::AlreadyInitializedException;
    /** === end UNO using === **/

#ifdef DBG_UTIL
    const char* SortableGridDataModel_checkInvariants( const void* _pInstance )
    {
        return static_cast< const SortableGridDataModel* >( _pInstance )->checkInvariants();
    }

    //------------------------------------------------------------------------------------------------------------------
    const char* SortableGridDataModel::checkInvariants() const
    {
        if ( m_publicToPrivateRowIndex.size() != m_privateToPublicRowIndex.size() )
            return "inconsistent index maps";

        if ( m_delegator.is() )
        {
            if ( m_publicToPrivateRowIndex.size() != size_t( m_delegator->getRowCount() ) )
                return "wrong cached row count";
        }
        else
        {
            if ( !m_publicToPrivateRowIndex.empty() )
                return "disposed or not initialized, but having a non-empty map";
        }

        for ( size_t publicIndex=0; publicIndex<m_publicToPrivateRowIndex.size(); ++publicIndex )
        {
            ::sal_Int32 const privateIndex = m_publicToPrivateRowIndex[ publicIndex ];
            if ( ( privateIndex < 0 ) || ( size_t( privateIndex ) >= m_privateToPublicRowIndex.size() ) )
                return "invalid cached private index";

            if ( m_privateToPublicRowIndex[ privateIndex ] != sal_Int32( publicIndex ) )
                return "index map traversal not commutavive";
        }

        if ( impl_isSorted_nothrow() && m_publicToPrivateRowIndex.empty() )
            return "sorted, but no row index translation tables";

        if ( !impl_isSorted_nothrow() && !m_publicToPrivateRowIndex.empty() )
            return "unsorted, but have index translation tables";

        return NULL;
    }
#endif

#define DBG_CHECK_ME() \
    DBG_CHKTHIS( SortableGridDataModel, SortableGridDataModel_checkInvariants )

    //------------------------------------------------------------------------------------------------------------------
    namespace
    {
        template< class STLCONTAINER >
        static void lcl_clear( STLCONTAINER& i_container )
        {
            STLCONTAINER empty;
            empty.swap( i_container );
        }
    }

    //==================================================================================================================
    //= SortableGridDataModel
    //==================================================================================================================
    DBG_NAME( SortableGridDataModel )
    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::SortableGridDataModel( Reference< XMultiServiceFactory > const & i_factory )
        :SortableGridDataModel_Base( m_aMutex )
        ,SortableGridDataModel_PrivateBase()
        ,m_context( i_factory )
        ,m_isInitialized( false )
        ,m_delegator()
        ,m_collator()
        ,m_currentSortColumn( -1 )
        ,m_sortAscending( true )
        ,m_publicToPrivateRowIndex()
        ,m_privateToPublicRowIndex()
    {
        DBG_CTOR( SortableGridDataModel, SortableGridDataModel_checkInvariants );
    }

    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::SortableGridDataModel( SortableGridDataModel const & i_copySource )
        :cppu::BaseMutex()
        ,SortableGridDataModel_Base( m_aMutex )
        ,SortableGridDataModel_PrivateBase()
        ,m_context( i_copySource.m_context )
        ,m_collator( i_copySource.m_collator )
        ,m_isInitialized( true )
        ,m_delegator()
        ,m_currentSortColumn( i_copySource.m_currentSortColumn )
        ,m_sortAscending( i_copySource.m_sortAscending )
        ,m_publicToPrivateRowIndex( i_copySource.m_publicToPrivateRowIndex )
        ,m_privateToPublicRowIndex( i_copySource.m_privateToPublicRowIndex )
    {
        DBG_CTOR( SortableGridDataModel, SortableGridDataModel_checkInvariants );

        ENSURE_OR_THROW( i_copySource.m_delegator.is(),
            "not expected to be called for a disposed copy source!" );
        m_delegator.set( i_copySource.m_delegator->createClone(), UNO_QUERY_THROW );
    }

    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::~SortableGridDataModel()
    {
        if ( !rBHelper.bDisposed )
        {<commit_msg>Automated merge with ssh://hg@hg.services.openoffice.org/cws/gridsort<commit_after>/*************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "precompiled_toolkit.hxx"

#include "sortablegriddatamodel.hxx"
#include "toolkit/helper/servicenames.hxx"

/** === begin UNO includes === **/
#include <com/sun/star/i18n/XCollator.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/ucb/AlreadyInitializedException.hpp>
/** === end UNO includes === **/

#include <comphelper/anycompare.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <tools/diagnose_ex.h>
#include <tools/debug.hxx>
#include <vcl/svapp.hxx>

#include <set>

//......................................................................................................................
namespace toolkit
{
//......................................................................................................................

    /** === begin UNO using === **/
    using ::com::sun::star::uno::TypeClass;
    using ::com::sun::star::uno::TypeClass_VOID;
    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::uno::XInterface;
    using ::com::sun::star::uno::UNO_QUERY;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::uno::UNO_SET_THROW;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::uno::RuntimeException;
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::uno::makeAny;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::uno::Type;
    using ::com::sun::star::lang::IndexOutOfBoundsException;
    using ::com::sun::star::lang::IllegalArgumentException;
    using ::com::sun::star::awt::grid::XGridDataListener;
    using ::com::sun::star::beans::Pair;
    using ::com::sun::star::util::XCloneable;
    using ::com::sun::star::i18n::XCollator;
    using ::com::sun::star::lang::IllegalArgumentException;
    using ::com::sun::star::lang::XMultiServiceFactory;
    using ::com::sun::star::awt::grid::GridDataEvent;
    using ::com::sun::star::lang::EventObject;
    using ::com::sun::star::ucb::AlreadyInitializedException;
    /** === end UNO using === **/

#ifdef DBG_UTIL
    const char* SortableGridDataModel_checkInvariants( const void* _pInstance )
    {
        return static_cast< const SortableGridDataModel* >( _pInstance )->checkInvariants();
    }

    //------------------------------------------------------------------------------------------------------------------
    const char* SortableGridDataModel::checkInvariants() const
    {
        if ( m_publicToPrivateRowIndex.size() != m_privateToPublicRowIndex.size() )
            return "inconsistent index maps";

        if ( m_delegator.is() )
        {
            if ( m_publicToPrivateRowIndex.size() != size_t( m_delegator->getRowCount() ) )
                return "wrong cached row count";
        }
        else
        {
            if ( !m_publicToPrivateRowIndex.empty() )
                return "disposed or not initialized, but having a non-empty map";
        }

        for ( size_t publicIndex=0; publicIndex<m_publicToPrivateRowIndex.size(); ++publicIndex )
        {
            ::sal_Int32 const privateIndex = m_publicToPrivateRowIndex[ publicIndex ];
            if ( ( privateIndex < 0 ) || ( size_t( privateIndex ) >= m_privateToPublicRowIndex.size() ) )
                return "invalid cached private index";

            if ( m_privateToPublicRowIndex[ privateIndex ] != sal_Int32( publicIndex ) )
                return "index map traversal not commutavive";
        }

        if ( impl_isSorted_nothrow() && m_publicToPrivateRowIndex.empty() )
            return "sorted, but no row index translation tables";

        if ( !impl_isSorted_nothrow() && !m_publicToPrivateRowIndex.empty() )
            return "unsorted, but have index translation tables";

        return NULL;
    }
#endif

#define DBG_CHECK_ME() \
    DBG_CHKTHIS( SortableGridDataModel, SortableGridDataModel_checkInvariants )

    //------------------------------------------------------------------------------------------------------------------
    namespace
    {
        template< class STLCONTAINER >
        static void lcl_clear( STLCONTAINER& i_container )
        {
            STLCONTAINER empty;
            empty.swap( i_container );
        }
    }

    //==================================================================================================================
    //= SortableGridDataModel
    //==================================================================================================================
    DBG_NAME( SortableGridDataModel )
    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::SortableGridDataModel( Reference< XMultiServiceFactory > const & i_factory )
        :SortableGridDataModel_Base( m_aMutex )
        ,SortableGridDataModel_PrivateBase()
        ,m_context( i_factory )
        ,m_isInitialized( false )
        ,m_delegator()
        ,m_collator()
        ,m_currentSortColumn( -1 )
        ,m_sortAscending( true )
        ,m_publicToPrivateRowIndex()
        ,m_privateToPublicRowIndex()
    {
        DBG_CTOR( SortableGridDataModel, SortableGridDataModel_checkInvariants );
    }

    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::SortableGridDataModel( SortableGridDataModel const & i_copySource )
        :cppu::BaseMutex()
        ,SortableGridDataModel_Base( m_aMutex )
        ,SortableGridDataModel_PrivateBase()
        ,m_context( i_copySource.m_context )
        ,m_isInitialized( true )
        ,m_delegator()
        ,m_collator( i_copySource.m_collator )
        ,m_currentSortColumn( i_copySource.m_currentSortColumn )
        ,m_sortAscending( i_copySource.m_sortAscending )
        ,m_publicToPrivateRowIndex( i_copySource.m_publicToPrivateRowIndex )
        ,m_privateToPublicRowIndex( i_copySource.m_privateToPublicRowIndex )
    {
        DBG_CTOR( SortableGridDataModel, SortableGridDataModel_checkInvariants );

        ENSURE_OR_THROW( i_copySource.m_delegator.is(),
            "not expected to be called for a disposed copy source!" );
        m_delegator.set( i_copySource.m_delegator->createClone(), UNO_QUERY_THROW );
    }

    //------------------------------------------------------------------------------------------------------------------
    SortableGridDataModel::~SortableGridDataModel()
    {
        if ( !rBHelper.bDisposed )
        {
{'fraction_non_alphanumeric': 0.25479577189090435, 'fraction_numerical': 0.0030014354691374136, 'mean_word_length': 2.335582154515778, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055490', 'n_tokens_mistral': 4157, 'n_tokens_neox': 4026, 'n_words': 847}