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