persistentstorage/store/UBTREE/UB_KEY.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "UB_STD.H"
       
    17 
       
    18 typedef union
       
    19 	{
       
    20 	const TAny* tany;
       
    21 	const TInt8* tint8;
       
    22 	const TInt16* tint16;
       
    23 	const TInt32* tint32;
       
    24 	const TInt64* tint64;
       
    25 	const TUint8* tuint8;
       
    26 	const TUint16* tuint16;
       
    27 	const TUint32* tuint32;
       
    28 	} UPTR;
       
    29 
       
    30 EXPORT_C const TAny* MBtreeKey::Key(const TAny* anEntry) const
       
    31 /** Gets the key value for an entry.
       
    32 
       
    33 @param anEntry Object for which to get the key value
       
    34 @return Pointer to the key value */
       
    35 	{
       
    36 	return anEntry;
       
    37 	}
       
    38 
       
    39 EXPORT_C TBtreeKey::TBtreeKey(TInt aLength)
       
    40 //
       
    41 // Discriminating key. Does a raw memory comparison on given length
       
    42 //
       
    43 	: iKeyOffset(0),iCmpType(ECmpNormal8),iKeyLength(aLength)
       
    44 	{}
       
    45 
       
    46 EXPORT_C TBtreeKey::TBtreeKey()
       
    47 //
       
    48 // Discriminating key. Does a raw memory comparison on variable length (byte counted)
       
    49 //
       
    50 	: iKeyOffset(0),iCmpType(ECmpCollated16+1+ECmpNormal8)
       
    51 	{}
       
    52 
       
    53 EXPORT_C TBtreeKey::TBtreeKey(TInt anOffset,TKeyCmpText aType)
       
    54 //
       
    55 // Used to specify a variable length text key, the first character is a count of the actual character data that follows
       
    56 //
       
    57 	: iKeyOffset(anOffset),iCmpType(ECmpCollated16+aType+1)
       
    58 	{
       
    59 	switch (aType)
       
    60 		{
       
    61 	case ECmpNormal:
       
    62 	case ECmpFolded:
       
    63 	case ECmpCollated:
       
    64 		Panic(EInvalidKeyComparison);
       
    65 	default:
       
    66 		break;
       
    67 		}
       
    68 	}
       
    69 
       
    70 EXPORT_C TBtreeKey::TBtreeKey(TInt anOffset,TKeyCmpText aType,TInt aLength)
       
    71 //
       
    72 // Used for fixed length charecter data. the length is the character count, not the byte size
       
    73 //
       
    74 	: iKeyOffset(anOffset),iCmpType(aType),iKeyLength(aLength)
       
    75 	{
       
    76 	switch (aType)
       
    77 		{
       
    78 	case ECmpNormal:
       
    79 	case ECmpFolded:
       
    80 	case ECmpCollated:
       
    81 		Panic(EInvalidKeyComparison);
       
    82 	default:
       
    83 		break;
       
    84 		}
       
    85 	}
       
    86 
       
    87 EXPORT_C TBtreeKey::TBtreeKey(TInt anOffset,TKeyCmpNumeric aType)
       
    88 	: iKeyOffset(anOffset),iCmpType(aType)
       
    89 	{
       
    90 	switch (aType)
       
    91 		{
       
    92 	case ECmpTInt:
       
    93 	case ECmpTUint:
       
    94 		Panic(EInvalidKeyComparison);
       
    95 	default:
       
    96 		break;
       
    97 		}
       
    98 	}
       
    99 
       
   100 EXPORT_C const TAny* TBtreeKey::Key(const TAny* anEntry) const
       
   101 	{
       
   102 	return PtrAdd(anEntry,iKeyOffset);
       
   103 	}
       
   104 
       
   105 EXPORT_C TInt TBtreeKey::Compare(const TAny* aLeft,const TAny* aRight) const
       
   106 //
       
   107 // do the right thing
       
   108 //
       
   109 	{
       
   110 	UPTR left;
       
   111 	left.tany=aLeft;
       
   112 	UPTR right;
       
   113 	right.tany=aRight;
       
   114 	switch (iCmpType)
       
   115 		{
       
   116 	case ECmpNormal8:
       
   117 		return Mem::Compare(left.tuint8,iKeyLength,right.tuint8,iKeyLength);
       
   118 	case ECmpFolded8:
       
   119 		return Mem::CompareF(left.tuint8,iKeyLength,right.tuint8,iKeyLength);
       
   120 	case ECmpCollated8:
       
   121 		return Mem::CompareC(left.tuint8,iKeyLength,right.tuint8,iKeyLength);
       
   122 	case ECmpNormal16:
       
   123 		return Mem::Compare(left.tuint16,iKeyLength,right.tuint16,iKeyLength);
       
   124 	case ECmpFolded16:
       
   125 		return Mem::CompareF(left.tuint16,iKeyLength,right.tuint16,iKeyLength);
       
   126 	case ECmpCollated16:
       
   127 		return Mem::CompareC(left.tuint16,iKeyLength,right.tuint16,iKeyLength);
       
   128 	case ECmpCollated16+ECmpNormal8+1:
       
   129 		return Mem::Compare(left.tuint8+1,*left.tuint8,right.tuint8+1,*right.tuint8);
       
   130 	case ECmpCollated16+ECmpFolded8+1:
       
   131 		return Mem::CompareF(left.tuint8+1,*left.tuint8,right.tuint8+1,*right.tuint8);
       
   132 	case ECmpCollated16+ECmpCollated8+1:
       
   133 		return Mem::CompareC(left.tuint8+1,*left.tuint8,right.tuint8+1,*right.tuint8);
       
   134 	case ECmpCollated16+ECmpNormal16+1:
       
   135 		return Mem::Compare(left.tuint16+1,*left.tuint16,right.tuint16+1,*right.tuint16);
       
   136 	case ECmpCollated16+ECmpFolded16+1:
       
   137 		return Mem::CompareF(left.tuint16+1,*left.tuint16,right.tuint16+1,*right.tuint16);
       
   138 	case ECmpCollated16+ECmpCollated16+1:
       
   139 		return Mem::CompareC(left.tuint16+1,*left.tuint16,right.tuint16+1,*right.tuint16);
       
   140 	case ECmpTInt8:
       
   141 		return *left.tint8-*right.tint8;
       
   142 	case ECmpTUint8:
       
   143 		return TInt(*left.tuint8)-TInt(*right.tuint8);
       
   144 	case ECmpTInt16:
       
   145 		return *left.tint16-*right.tint16;
       
   146 	case ECmpTUint16:
       
   147 		return TInt(*left.tuint16)-TInt(*right.tuint16);
       
   148 	case ECmpTInt32:
       
   149 		if (*left.tint32<*right.tint32) 
       
   150 			return -1;
       
   151 		if (*left.tint32>*right.tint32) 
       
   152 			return 1;
       
   153 		break;	   
       
   154 	case ECmpTUint32:
       
   155 		if (*left.tuint32<*right.tuint32)
       
   156 			return -1;
       
   157 		if (*left.tuint32>*right.tuint32)
       
   158 			return 1;
       
   159 		break;
       
   160 	case ECmpTInt64:
       
   161 		if (*left.tint64<*right.tint64) 
       
   162 			return -1;
       
   163 		if (*left.tint64>*right.tint64) 
       
   164 			return 1;
       
   165 		break;	   
       
   166 	default:
       
   167 		break;
       
   168 		}
       
   169 	return 0;
       
   170 	}
       
   171 
       
   172 EXPORT_C void TBtreeKey::Between(const TAny* aLeft,const TAny*  /*aRight*/,TBtreePivot& aPivot) const
       
   173 	{
       
   174 //#pragma message( __FILE__ " : 'TBtreeKey::Between()' whizzy pivot generation not implemented" )
       
   175 	UPTR left;
       
   176 	left.tany=aLeft;
       
   177 //	UPTR right=(UPTR)aRight;
       
   178 	switch (iCmpType)
       
   179 		{
       
   180 	case ECmpNormal8:
       
   181 	case ECmpFolded8:
       
   182 	case ECmpCollated8:
       
   183 		aPivot.Copy(left.tuint8,iKeyLength);
       
   184 		break;
       
   185 	case ECmpNormal16:
       
   186 	case ECmpFolded16:
       
   187 	case ECmpCollated16:
       
   188 		aPivot.Copy(left.tuint8,iKeyLength<<1);
       
   189 		break;
       
   190 	case ECmpCollated16+ECmpNormal8+1:
       
   191 	case ECmpCollated16+ECmpFolded8+1:
       
   192 	case ECmpCollated16+ECmpCollated8+1:
       
   193 		aPivot.Copy(left.tuint8,1+*left.tuint8);	// include length count
       
   194 		break;
       
   195 	case ECmpCollated16+ECmpNormal16+1:
       
   196 	case ECmpCollated16+ECmpFolded16+1:
       
   197 	case ECmpCollated16+ECmpCollated16+1:
       
   198 		aPivot.Copy(left.tuint8,(1+*left.tuint16)<<1);	// include length count
       
   199 		break;
       
   200 	case ECmpTInt8:
       
   201 		aPivot.Copy(left.tuint8,sizeof(TInt8));
       
   202 		break;
       
   203 	case ECmpTUint8:
       
   204 		aPivot.Copy(left.tuint8,sizeof(TUint8));
       
   205 		break;
       
   206 	case ECmpTInt16:
       
   207 		aPivot.Copy(left.tuint8,sizeof(TInt16));
       
   208 		break;
       
   209 	case ECmpTUint16:
       
   210 		aPivot.Copy(left.tuint8,sizeof(TUint16));
       
   211 		break;
       
   212 	case ECmpTInt32:
       
   213 		aPivot.Copy(left.tuint8,sizeof(TInt16));
       
   214 		break;
       
   215 	case ECmpTUint32:
       
   216 		aPivot.Copy(left.tuint8,sizeof(TUint32));
       
   217 		break;
       
   218 	case ECmpTInt64:
       
   219 		aPivot.Copy(left.tuint8,sizeof(TInt64));
       
   220 		break;
       
   221 	default:
       
   222 		break;
       
   223 		}
       
   224 	}
       
   225