diff -r 000000000000 -r 818e61de6cd1 crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Collections/SymbianSortedListByValue.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Collections/SymbianSortedListByValue.cs Thu Feb 11 15:50:58 2010 +0200 @@ -0,0 +1,225 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ +using System; +using System.Collections.Generic; + +namespace SymbianUtils.Collections +{ + public class SymbianSortedListByValue where TValue : class + { + #region Constructors + public SymbianSortedListByValue( IComparer aComparer ) + : this( 10, aComparer ) + { + } + + public SymbianSortedListByValue( int aGranularity, IComparer aComparer ) + { + iList = new List>( aGranularity ); + iComparer = new SymbianSortedListByValueComparer( aComparer ); + } + + public SymbianSortedListByValue( SymbianSortedListByValue aCollection ) + { + int count = aCollection.Count; + // + iList = new List>( count + 1 ); + iComparer = new SymbianSortedListByValue.SymbianSortedListByValueComparer( aCollection.iComparer.Comparer ); + // + for ( int i = 0; i < count; i++ ) + { + KeyValuePair entry = aCollection.iList[ i ]; + Add( entry.Value, entry.Key ); + } + } + #endregion + + #region API + public bool Contains( TKey aKey ) + { + return iDictionary.ContainsKey( aKey ); + } + + public void Add( TKey aKey, TValue aValue ) + { + if ( aKey == null ) + { + throw new ArgumentNullException( "aKey cannot be null" ); + } + + // Add item to dictionary if required + if ( !iDictionary.ContainsKey( aKey ) ) + { + iDictionary.Add( aKey, aValue ); + } + + // Search for item in the list. If it's already present then + // this will return the index. Otherwise, it will return a negative + // number. + // If this index is greater than or equal to the size of the array, + // there are no elements larger than value in the array. + // + // Otherwise, it is the index of the first element that is larger than value. + KeyValuePair entry = new KeyValuePair( aValue, aKey ); + int index = iList.BinarySearch( entry, iComparer ); + if ( index < 0 ) + { + index = ~index; + } + iList.Insert( index, entry ); + } + + public void Remove( Predicate aValue ) + { + Predicate> matchPredicate = delegate( KeyValuePair aItem ) + { + bool remove = aValue.Invoke( aItem.Key ); + return remove; + }; + + RemoveAll( matchPredicate ); + } + + public void Remove( TValue aValue ) + { + Predicate> matchPredicate = delegate( KeyValuePair aItem ) + { + return ( aValue == aItem.Key ); + }; + + RemoveAll( matchPredicate ); + } + + public void Remove( TKey aKey ) + { + TValue v = null; + if ( iDictionary.TryGetValue( aKey, out v ) ) + { + iDictionary.Remove( aKey ); + Remove( v ); + } + } + + public void Clear() + { + iList.Clear(); + iDictionary.Clear(); + } + + public void RemoveRange( int aStartIndex, int aCount ) + { + List> items = iList.GetRange( aStartIndex, aCount ); + RemoveAll( items ); + } + + public void Sort( IComparer aComparer ) + { + SymbianSortedListByValueComparer comparer = new SymbianSortedListByValueComparer( aComparer ); + iList.Sort( comparer ); + iComparer = comparer; + } + #endregion + + #region Properties + public int Count + { + get { return iList.Count; } + } + + public TValue this[ int aIndex ] + { + get + { + KeyValuePair< TValue, TKey> ret = iList[ aIndex ]; + return ret.Key; + } + } + + public TValue this[ TKey aKey ] + { + get + { + TValue ret = null; + bool found = iDictionary.TryGetValue( aKey, out ret ); + return ret; + } + } + #endregion + + #region Internal methods + private void RemoveAll( Predicate> aPredicate ) + { + List> items = iList.FindAll( aPredicate ); + RemoveAll( items ); + } + + private void RemoveAll( List> aItems ) + { + foreach ( KeyValuePair item in aItems ) + { + // Remove from dictionary + TKey key = item.Value; + if ( iDictionary.ContainsKey( key ) ) + { + iDictionary.Remove( key ); + } + + // Remove from list + TValue value = item.Key; + iList.Remove( item ); + } + } + #endregion + + #region Internal classes + private class SymbianSortedListByValueComparer : IComparer< KeyValuePair > + { + #region Constructors + public SymbianSortedListByValueComparer( IComparer aComparer ) + { + iComparer = aComparer; + } + #endregion + + #region Properties + public IComparer Comparer + { + get { return iComparer; } + } + #endregion + + #region From IComparer> + public int Compare( KeyValuePair aLeft, KeyValuePair aRight ) + { + int ret = iComparer.Compare( aLeft.Key, aRight.Key ); + return ret; + } + #endregion + + #region Data members + private readonly IComparer iComparer; + #endregion + } + #endregion + + #region Data members + private List< KeyValuePair > iList = null; + private Dictionary iDictionary = new Dictionary(); + private SymbianSortedListByValueComparer iComparer = null; + #endregion + } +}