summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2012-08-07 14:36:26 +0200
committerTor Lillqvist <tlillqvist@suse.com>2012-08-08 10:47:31 +0300
commit7a597eb6248ef48ebeb23daa40d2c75e5a24d9ee (patch)
treeb3cbd0db774b6dc5c06466b85ed2c4b42868afac /tools
parent976deb6b99df6a34f627cef726db595e924a8d87 (diff)
STL'ify UniqueIndex
Convert the UniqueIndex code from a macro to a C++ template. Also use std::map as the underlying container instead of tools/contnr.hxx. Change-Id: I0b7b37dd7160ae019aaecdacd1e973ac6d8498e2
Diffstat (limited to 'tools')
-rw-r--r--tools/inc/tools/pstm.hxx2
-rw-r--r--tools/inc/tools/unqidx.hxx124
-rw-r--r--tools/source/memtools/unqidx.cxx244
-rw-r--r--tools/source/ref/pstm.cxx8
4 files changed, 97 insertions, 281 deletions
diff --git a/tools/inc/tools/pstm.hxx b/tools/inc/tools/pstm.hxx
index c313b6b2dbf7..64a3b64d9175 100644
--- a/tools/inc/tools/pstm.hxx
+++ b/tools/inc/tools/pstm.hxx
@@ -134,7 +134,7 @@ public:\
SV_DECL_PERSIST_LIST(ClassName,EntryName)\
SV_IMPL_PERSIST_LIST(ClassName,EntryName)
-DECLARE_UNIQUEINDEX( SvPersistUIdx,SvPersistBase *)
+typedef UniqueIndex<SvPersistBase> SvPersistUIdx;
typedef std::map<SvPersistBase*, sal_uIntPtr> PersistBaseMap;
diff --git a/tools/inc/tools/unqidx.hxx b/tools/inc/tools/unqidx.hxx
index aeaa7e79c0f9..9938961e1a02 100644
--- a/tools/inc/tools/unqidx.hxx
+++ b/tools/inc/tools/unqidx.hxx
@@ -22,6 +22,7 @@
#include "tools/toolsdllapi.h"
#include <tools/solar.h>
#include <tools/contnr.hxx>
+#include <map>
// ---------------
// - UniqueIndex -
@@ -29,109 +30,46 @@
#define UNIQUEINDEX_ENTRY_NOTFOUND CONTAINER_ENTRY_NOTFOUND
-class TOOLS_DLLPUBLIC UniqueIndex : private Container
+class TOOLS_DLLPUBLIC UniqueIndexImpl : public std::map<sal_uInt32, void*>
{
private:
- sal_uIntPtr nReSize;
sal_uIntPtr nStartIndex;
sal_uIntPtr nUniqIndex;
sal_uIntPtr nCount;
- void* Seek( sal_uIntPtr nIndex ); //not implemented
-
public:
- using Container::GetCurObject;
-
- UniqueIndex( sal_uIntPtr nStartIndex = 0,
- sal_uIntPtr nInitSize = 16,
- sal_uIntPtr nReSize = 16 );
- UniqueIndex( const UniqueIndex& rIdx );
-
- sal_uIntPtr Insert( sal_uIntPtr nIndex, void* p );
- sal_uIntPtr Insert( void* p );
- void* Remove( sal_uIntPtr nIndex );
- void* Get( sal_uIntPtr nIndex ) const;
-
- void Clear();
- sal_uIntPtr Count() const { return nCount; }
-
- sal_uIntPtr GetCurIndex() const;
- sal_uIntPtr GetIndex( const void* p ) const;
-
- void* Seek( void* p );
- void* First();
- void* Last();
- void* Next();
- void* Prev();
-
- sal_uIntPtr GetStartIndex() const { return nStartIndex; }
- sal_uIntPtr GetCurMaxIndex() const
- { return (nStartIndex + Container::GetSize()); }
-
- UniqueIndex& operator =( const UniqueIndex& rIdx );
-
- sal_Bool operator ==( const UniqueIndex& rIdx ) const;
- sal_Bool operator !=( const UniqueIndex& rIdx ) const
- { return !(UniqueIndex::operator==( rIdx )); }
+ UniqueIndexImpl( sal_uIntPtr _nStartIndex = 0 )
+ : std::map<sal_uInt32, void*>(),
+ nStartIndex(_nStartIndex), nUniqIndex(_nStartIndex), nCount(0) {}
+
+ sal_uIntPtr Insert( void* p );
+ // insert value with key, replacing existing entry if necessary
+ void Insert( sal_uIntPtr aIndex, void* p );
+ void* Remove( sal_uIntPtr aIndex );
+ void* Get( sal_uIntPtr aIndex ) const;
+
+ sal_uIntPtr GetIndexOf( void* p ) const;
+ sal_uIntPtr FirstIndex() const;
+ sal_uIntPtr LastIndex() const;
+ sal_uIntPtr NextIndex( sal_uIntPtr aCurrIndex ) const;
};
-inline void UniqueIndex::Clear()
+template<typename T>
+class UniqueIndex : private UniqueIndexImpl
{
- Container::Clear();
- nCount = 0;
- nUniqIndex = 0;
-}
-
-// -----------------------
-// - DECLARE_UNIQUEINDEX -
-// -----------------------
-
-#define DECLARE_UNIQUEINDEX( ClassName, Type ) \
-class ClassName : private UniqueIndex \
-{ \
- Type Seek( sal_uIntPtr nKey ); \
-public: \
- using UniqueIndex::Clear; \
- using UniqueIndex::Count; \
- using UniqueIndex::GetCurIndex; \
- using UniqueIndex::GetStartIndex; \
- using UniqueIndex::GetCurMaxIndex; \
- \
- ClassName( sal_uIntPtr _nStartIndex = 0, \
- sal_uIntPtr _nInitSize = 16, sal_uIntPtr _nReSize = 16 ):\
- UniqueIndex( _nStartIndex, _nInitSize, _nReSize ) {}\
- ClassName( const ClassName& rClassName ) : \
- UniqueIndex( rClassName ) {} \
- \
- sal_uIntPtr Insert( sal_uIntPtr nIndex, Type p ) \
- { return UniqueIndex::Insert( nIndex, (void*)p ); } \
- sal_uIntPtr Insert( Type p ) \
- { return UniqueIndex::Insert( (void*)p ); } \
- Type Remove( sal_uIntPtr nIndex ) \
- { return (Type)UniqueIndex::Remove( nIndex ); } \
- Type Get( sal_uIntPtr nIndex ) const \
- { return (Type)UniqueIndex::Get( nIndex ); } \
- \
- Type GetCurObject() const \
- { return (Type)UniqueIndex::GetCurObject(); } \
- sal_uIntPtr GetIndex( const Type p ) const \
- { return UniqueIndex::GetIndex( (const void*)p ); } \
- \
- Type Seek( Type p ) \
- { return (Type)UniqueIndex::Seek( (void*)p ); } \
- Type First() { return (Type)UniqueIndex::First(); } \
- Type Last() { return (Type)UniqueIndex::Last(); } \
- Type Next() { return (Type)UniqueIndex::Next(); } \
- Type Prev() { return (Type)UniqueIndex::Prev(); } \
- \
- ClassName& operator =( const ClassName& rClassName ) \
- { UniqueIndex::operator =( rClassName ); \
- return *this; } \
- \
- sal_Bool operator ==( const ClassName& rIdx ) const \
- { return UniqueIndex::operator ==( rIdx ); } \
- sal_Bool operator !=( const ClassName& rIdx ) const \
- { return UniqueIndex::operator !=( rIdx ); } \
+public:
+ UniqueIndex<T>( sal_uIntPtr _nStartIndex = 0 ) : UniqueIndexImpl(_nStartIndex) {}
+
+ sal_uIntPtr Insert(T* p) { return UniqueIndexImpl::Insert(p); }
+ void Insert(sal_uIntPtr aIdx, T* p) { return UniqueIndexImpl::Insert(aIdx, p); }
+ T* Get(sal_uIntPtr idx) const { return static_cast<T*>( UniqueIndexImpl::Get(idx) ); }
+ T* Remove(sal_uIntPtr idx) { return static_cast<T*>( UniqueIndexImpl::Remove(idx) ); }
+ sal_uIntPtr Count() const { return UniqueIndexImpl::size(); }
+ sal_uIntPtr GetIndexOf(T* p) const { return UniqueIndexImpl::GetIndexOf(p); }
+
+ using UniqueIndexImpl::FirstIndex;
+ using UniqueIndexImpl::LastIndex;
+ using UniqueIndexImpl::NextIndex;
};
#endif // _UNQIDX_HXX
diff --git a/tools/source/memtools/unqidx.cxx b/tools/source/memtools/unqidx.cxx
index a66a48171ec2..63539d09f21b 100644
--- a/tools/source/memtools/unqidx.cxx
+++ b/tools/source/memtools/unqidx.cxx
@@ -20,36 +20,6 @@
#include <impcont.hxx>
#include <tools/unqidx.hxx>
-/*************************************************************************
-|*
-|* UniqueIndex::UniqueIndex()
-|*
-*************************************************************************/
-
-UniqueIndex::UniqueIndex( sal_uIntPtr _nStartIndex,
- sal_uIntPtr _nInitSize, sal_uIntPtr _nReSize ) :
- Container( _nInitSize )
-{
- nReSize = _nReSize;
- nStartIndex = _nStartIndex;
- nUniqIndex = 0;
- nCount = 0;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::UniqueIndex()
-|*
-*************************************************************************/
-
-UniqueIndex::UniqueIndex( const UniqueIndex& rIdx ) :
- Container( rIdx )
-{
- nReSize = rIdx.nReSize;
- nStartIndex = rIdx.nStartIndex;
- nUniqIndex = rIdx.nUniqIndex;
- nCount = rIdx.nCount;
-}
/*************************************************************************
|*
@@ -57,25 +27,26 @@ UniqueIndex::UniqueIndex( const UniqueIndex& rIdx ) :
|*
*************************************************************************/
-sal_uIntPtr UniqueIndex::Insert( void* p )
+sal_uIntPtr UniqueIndexImpl::Insert( void* p )
{
// NULL-Pointer ist nicht erlaubt
if ( !p )
return UNIQUEINDEX_ENTRY_NOTFOUND;
- // Ist Array voll, dann expandieren
- if ( nCount == Container::GetSize() )
- SetSize( nCount + nReSize );
+ // Ist Array voll, dann expandieren
+ sal_uIntPtr nTmp = size();
+ if( nTmp == nCount )
+ nTmp++;
// Damit UniqIndex nicht ueberlaeuft, wenn Items geloescht wurden
- nUniqIndex = nUniqIndex % Container::GetSize();
+ nUniqIndex = nUniqIndex % nTmp;
// Leeren Eintrag suchen
- while ( Container::ImpGetObject( nUniqIndex ) != NULL )
- nUniqIndex = (nUniqIndex+1) % Container::GetSize();
+ while ( find( nUniqIndex ) != end() )
+ nUniqIndex = (nUniqIndex+1) % nTmp;
// Object im Array speichern
- Container::Replace( p, nUniqIndex );
+ (*this)[ nUniqIndex ] = p;
// Anzahl der Eintraege erhoehen und Index zurueckgeben
nCount++;
@@ -85,222 +56,129 @@ sal_uIntPtr UniqueIndex::Insert( void* p )
/*************************************************************************
|*
-|* UniqueIndex::Insert()
+|* UniqueIndexImpl::Insert()
|*
*************************************************************************/
-sal_uIntPtr UniqueIndex::Insert( sal_uIntPtr nIndex, void* p )
+void UniqueIndexImpl::Insert( sal_uIntPtr nIndex, void* p )
{
// NULL-Pointer ist nicht erlaubt
if ( !p )
- return UNIQUEINDEX_ENTRY_NOTFOUND;
+ return;
sal_uIntPtr nContIndex = nIndex - nStartIndex;
- // Ist Array voll, dann expandieren
- if ( nContIndex >= Container::GetSize() )
- SetSize( nContIndex + nReSize );
+
+ bool bFound = find( nContIndex ) != end();
// Object im Array speichern
- Container::Replace( p, nContIndex );
+ (*this)[ nContIndex ] = p;
- // Anzahl der Eintraege erhoehen und Index zurueckgeben
- nCount++;
- return nIndex;
+ if( !bFound )
+ nCount++;
}
/*************************************************************************
|*
-|* UniqueIndex::Remove()
+|* UniqueIndexImpl::Remove()
|*
*************************************************************************/
-void* UniqueIndex::Remove( sal_uIntPtr nIndex )
+void* UniqueIndexImpl::Remove( sal_uIntPtr nIndex )
{
// Ist Index zulaessig
if ( (nIndex >= nStartIndex) &&
- (nIndex < (Container::GetSize()+nStartIndex)) )
+ (nIndex < (size() + nStartIndex)) )
{
// Index-Eintrag als leeren Eintrag setzen und Anzahl der
// gespeicherten Indexe erniedriegen, wenn Eintrag belegt war
- void* p = Container::Replace( NULL, nIndex-nStartIndex );
- if ( p )
+ iterator it = find( nIndex - nStartIndex );
+ if( it != end() )
+ {
+ void* p = it->second;
+ erase( it );
nCount--;
- return p;
+ return p;
+ }
}
- else
- return NULL;
+ return NULL;
}
/*************************************************************************
|*
-|* UniqueIndex::Get()
+|* UniqueIndexImpl::Get()
|*
*************************************************************************/
-void* UniqueIndex::Get( sal_uIntPtr nIndex ) const
+void* UniqueIndexImpl::Get( sal_uIntPtr nIndex ) const
{
// Ist Index zulaessig
if ( (nIndex >= nStartIndex) &&
- (nIndex < (Container::GetSize()+nStartIndex)) )
- return Container::ImpGetObject( nIndex-nStartIndex );
- else
- return NULL;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::GetCurIndex()
-|*
-*************************************************************************/
-
-sal_uIntPtr UniqueIndex::GetCurIndex() const
-{
- sal_uIntPtr nPos = Container::GetCurPos();
-
- // Ist der Current-Index nicht belegt, dann gibt es keinen Current-Index
- if ( !Container::ImpGetObject( nPos ) )
- return UNIQUEINDEX_ENTRY_NOTFOUND;
- else
- return nPos+nStartIndex;
+ (nIndex < (size() + nStartIndex)) )
+ {
+ const_iterator it = find( nIndex - nStartIndex );
+ if( it != end() )
+ return it->second;
+ }
+ return NULL;
}
/*************************************************************************
|*
-|* UniqueIndex::GetIndex()
+|* UniqueIndexImpl::FirstIndex()
|*
*************************************************************************/
-sal_uIntPtr UniqueIndex::GetIndex( const void* p ) const
+sal_uIntPtr UniqueIndexImpl::FirstIndex() const
{
- // Wird ein NULL-Pointer uebergeben, dann wurde Pointer nicht gefunden
- if ( !p )
- return UNIQUEINDEX_ENTRY_NOTFOUND;
-
- sal_uIntPtr nIndex = Container::GetPos( p );
-
- if ( nIndex != CONTAINER_ENTRY_NOTFOUND )
- return nIndex+nStartIndex;
- else
+ if ( empty() )
return UNIQUEINDEX_ENTRY_NOTFOUND;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::Seek()
-|*
-*************************************************************************/
-
-void* UniqueIndex::Seek( void* p )
-{
- // Wird ein NULL-Pointer uebergeben, dann wurde Pointer nicht gefunden
- if ( !p )
- return NULL;
-
- sal_uIntPtr nIndex = GetIndex( p );
-
- // Ist Index vorhanden, dann als aktuellen Eintrag setzen
- if ( nIndex != UNIQUEINDEX_ENTRY_NOTFOUND )
- return Container::Seek( nIndex-nStartIndex );
- else
- return NULL;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::First()
-|*
-*************************************************************************/
-
-void* UniqueIndex::First()
-{
- void* p = Container::First();
-
- while ( !p && (Container::GetCurPos() < (Container::GetSize()-1)) )
- p = Container::Next();
-
- return p;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::Last()
-|*
-*************************************************************************/
-
-void* UniqueIndex::Last()
-{
- void* p = Container::Last();
-
- while ( !p && Container::GetCurPos() )
- p = Container::Prev();
-
- return p;
-}
-
-/*************************************************************************
-|*
-|* UniqueIndex::Next()
-|*
-*************************************************************************/
-
-void* UniqueIndex::Next()
-{
- void* p = NULL;
- while ( !p && (Container::GetCurPos() < (Container::GetSize()-1)) )
- p = Container::Next();
-
- return p;
+ return begin()->first;
}
/*************************************************************************
|*
-|* UniqueIndex::Prev()
+|* UniqueIndexImpl::LastIndex()
|*
*************************************************************************/
-void* UniqueIndex::Prev()
+sal_uIntPtr UniqueIndexImpl::LastIndex() const
{
- void* p = NULL;
-
- while ( !p && Container::GetCurPos() )
- p = Container::Prev();
+ if ( empty() )
+ return UNIQUEINDEX_ENTRY_NOTFOUND;
- return p;
+ return rbegin()->first;
}
/*************************************************************************
|*
-|* UniqueIndex::operator =()
+|* UniqueIndexImpl::NextIndex()
|*
*************************************************************************/
-UniqueIndex& UniqueIndex::operator =( const UniqueIndex& rIdx )
+sal_uIntPtr UniqueIndexImpl::NextIndex(sal_uIntPtr aIndex) const
{
- // Neue Werte zuweisen
- Container::operator =( rIdx );
- nReSize = rIdx.nReSize;
- nStartIndex = rIdx.nStartIndex;
- nUniqIndex = rIdx.nUniqIndex;
- nCount = rIdx.nCount;
- return *this;
+ const_iterator it = find( aIndex );
+ if ( it == end() )
+ return UNIQUEINDEX_ENTRY_NOTFOUND;
+ it++;
+ if ( it == end() )
+ return UNIQUEINDEX_ENTRY_NOTFOUND;
+ return it->first;
}
/*************************************************************************
|*
-|* UniqueIndex::operator ==()
+|* UniqueIndexImpl::GetIndexOf()
|*
*************************************************************************/
-sal_Bool UniqueIndex::operator ==( const UniqueIndex& rIdx ) const
+sal_uIntPtr UniqueIndexImpl::GetIndexOf(void* p) const
{
- // Neue Werte zuweisen
- if ( (nStartIndex == rIdx.nStartIndex) &&
- (nCount == rIdx.nCount) &&
- (Container::operator ==( rIdx )) )
- return sal_True;
- else
- return sal_False;
+ for( const_iterator it = begin(); it != end(); ++it )
+ if( it->second == p )
+ return it->first;
+ return UNIQUEINDEX_ENTRY_NOTFOUND;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/ref/pstm.cxx b/tools/source/ref/pstm.cxx
index 442d2663e3df..064a09bd8b35 100644
--- a/tools/source/ref/pstm.cxx
+++ b/tools/source/ref/pstm.cxx
@@ -792,14 +792,14 @@ SvStream& operator <<
rThis << bTmp; // Version
sal_uInt32 nCount = (sal_uInt32)rThis.aPUIdx.Count();
rThis << nCount;
- SvPersistBase * pEle = rThis.aPUIdx.First();
+ sal_uIntPtr aIndex = rThis.aPUIdx.FirstIndex();
for( sal_uInt32 i = 0; i < nCount; i++ )
{
+ SvPersistBase * pEle = rThis.aPUIdx.Get(aIndex);
sal_uInt8 nP = P_OBJ | P_ID | P_STD;
- WriteId( rThis, nP, rThis.aPUIdx.GetCurIndex(),
- pEle->GetClassId() );
+ WriteId( rThis, nP, aIndex, pEle->GetClassId() );
rThis.WriteObj( nP, pEle );
- pEle = rThis.aPUIdx.Next();
+ aIndex = rThis.aPUIdx.NextIndex( aIndex );
}
rThis.SetStream( pOldStm );
return rStm;