sasindexkey.h
Go to the documentation of this file.
/*
* Copyright (c) 2005-2014 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation, Steven Munroe - initial API and implementation
*/
#ifndef __SAS_INDEXKEY_H
#define __SAS_INDEXKEY_H
#include <string.h>
#ifdef __WORDSIZE_64
typedef unsigned long machine_uint_t;
typedef unsigned int machine_uhalf_t;
const unsigned long machine_sign_mask = (0x8000000000000000);
const unsigned long double_exp_mask = (0x7ff0000000000000);
const unsigned long double_mask = (0xffffffffffffffff);
#else
typedef unsigned long machine_uint_t;
typedef unsigned short machine_uhalf_t;
const unsigned long machine_sign_mask = (0x80000000);
const unsigned long double_exp_mask = (0x7ff00000);
const unsigned long double_mask = (0xffffffff);
#endif
typedef struct SASIndexKey_t
{
machine_uhalf_t copy_size;
machine_uhalf_t compare_size;
machine_uint_t data[15];
} SASIndexKey_t;
static inline int
SASIndexKeyCompare (SASIndexKey_t * op_a, SASIndexKey_t * op_b)
{
int rc = 0;
if (op_a->data[0] != op_b->data[0])
{
if (op_a->data[0] < op_b->data[0])
rc = -1;
else
rc = 1;
}
else
{
size_t len = op_a->compare_size;
if (op_a->compare_size != op_b->compare_size)
{
if (op_a->compare_size > op_b->compare_size)
len = op_b->compare_size;
rc = memcmp (&op_a->data[0], &op_b->data[0], len);
if ((rc == 0) && (op_a->compare_size != op_b->compare_size))
{
if (op_a->compare_size < op_b->compare_size)
rc = -1;
else
rc = 1;
}
}
else if (len > sizeof (machine_uint_t))
{
unsigned int i = 1;
for (i = 1; i < (len / sizeof (machine_uint_t)); i++)
{
if (op_a->data[i] != op_b->data[i])
{
if (op_a->data[i] > op_b->data[i])
rc = 1;
else
rc = -1;
break;
}
}
}
}
return rc;
}
static inline void
SASIndexKeyCopy (SASIndexKey_t * dest, SASIndexKey_t * src)
{
memcpy (dest, src, src->copy_size);
}
static inline size_t
SASIndexKeySize (SASIndexKey_t * key)
{
return key->copy_size;
}
static inline void
SASIndexKeyInitRef (SASIndexKey_t * dest, void *value)
{
dest->compare_size = sizeof (void *);
dest->copy_size = 2 * sizeof (void *);
dest->data[0] = (machine_uint_t) value;
}
typedef union {
#ifdef __WORDSIZE_64
machine_uint_t key_element;
#else
struct {
#if __BYTE_ORDER == __BIG_ENDIAN
machine_uint_t data_0;
machine_uint_t data_1;
#else
machine_uint_t data_1;
machine_uint_t data_0;
#endif
} key_element;
#endif
unsigned long long uint64_key;
long long int64_key;
double double_key;
} sasindexkeymap_t;
static inline void
SASIndexKeyInitUInt64 (SASIndexKey_t * dest, unsigned long long value)
{
sasindexkeymap_t key_val;
key_val.uint64_key = value;
#ifdef __WORDSIZE_64
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
dest->data[0] = key_val.key_element;
#else
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
dest->data[0] = key_val.key_element.data_0;
dest->data[1] = key_val.key_element.data_1;
#endif
}
static inline unsigned long long
SASIndexKeyReturn1stUInt64 (SASIndexKey_t * dest)
{
sasindexkeymap_t key_val;
#ifdef __WORDSIZE_64
key_val.key_element = dest->data[0];
#else
key_val.key_element.data_0 = dest->data[0];
key_val.key_element.data_1 = dest->data[1];
#endif
return key_val.uint64_key;
}
static inline void
SASIndexKeyInitInt64 (SASIndexKey_t * dest, signed long long value)
{
sasindexkeymap_t key_val;
key_val.int64_key = value;
#ifdef __WORDSIZE_64
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
dest->data[0] = key_val.key_element ^ machine_sign_mask;
#else
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
dest->data[0] = key_val.key_element.data_0 ^ machine_sign_mask;
dest->data[1] = key_val.key_element.data_1;
#endif
}
static inline long long
SASIndexKeyReturn1stInt64 (SASIndexKey_t * dest)
{
sasindexkeymap_t key_val;
#ifdef __WORDSIZE_64
key_val.key_element = dest->data[0] ^ machine_sign_mask;
#else
key_val.key_element.data_0 = dest->data[0] ^ machine_sign_mask;
key_val.key_element.data_1 = dest->data[1];
#endif
return key_val.int64_key;
}
static inline void
SASIndexKeyInitDouble (SASIndexKey_t * dest, double value)
{
sasindexkeymap_t key_val;
key_val.double_key = value;
#ifdef __WORDSIZE_64
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
if (value < 0.0)
dest->data[0] = key_val.key_element ^ double_mask;
else
dest->data[0] = key_val.key_element ^ machine_sign_mask;
#else
dest->compare_size = sizeof (unsigned long long);
dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
if (value < 0.0)
{
dest->data[0] = key_val.key_element.data_0 ^ double_mask;
dest->data[1] = key_val.key_element.data_1 ^ double_mask;
}
else
{
dest->data[0] = key_val.key_element.data_0 ^ machine_sign_mask;
dest->data[1] = key_val.key_element.data_1;
}
#endif
}
static inline double
SASIndexKeyReturn1stDouble (SASIndexKey_t * dest)
{
sasindexkeymap_t key_val;
#ifdef __WORDSIZE_64
if (dest->data[0] & machine_sign_mask)
key_val.key_element = dest->data[0] ^ machine_sign_mask;
else
key_val.key_element = dest->data[0] ^ double_mask;
#else
if (dest->data[0] & machine_sign_mask)
{
key_val.key_element.data_0 = dest->data[0] ^ machine_sign_mask;
key_val.key_element.data_1 = dest->data[1];
}
else
{
key_val.key_element.data_0 = dest->data[0] ^ double_mask;
key_val.key_element.data_1 = dest->data[1] ^ double_mask;
}
#endif
return key_val.double_key;
}
#endif /* __SAS_INDEXKEY_H */
SASIndexKeyCompare
static int SASIndexKeyCompare(SASIndexKey_t *op_a, SASIndexKey_t *op_b)
binary key compare logic for Index B-Tree keys.
Definition: sasindexkey.h:83
SASIndexKeyInitInt64
static void SASIndexKeyInitInt64(SASIndexKey_t *dest, signed long long value)
Initial a binary key @ destination with a signed 64-bit integer value.
Definition: sasindexkey.h:259
SASIndexKeyReturn1stUInt64
static unsigned long long SASIndexKeyReturn1stUInt64(SASIndexKey_t *dest)
Return the value of the 1st binary key as a unsigned 64-bit integer value.
Definition: sasindexkey.h:235
machine_uhalf_t
unsigned short machine_uhalf_t
data unit used to store copy and compare lengths.
Definition: sasindexkey.h:46
machine_sign_mask
const unsigned long machine_sign_mask
mask use to invert the signbit.
Definition: sasindexkey.h:48
SASIndexKeyInitUInt64
static void SASIndexKeyInitUInt64(SASIndexKey_t *dest, unsigned long long value)
Initial a binary key @ destination with a unsigned 64-bit integer value.
Definition: sasindexkey.h:210
SASIndexKeyReturn1stDouble
static double SASIndexKeyReturn1stDouble(SASIndexKey_t *dest)
Return the value of the 1st binary key as a signed 64-bit integer value.
Definition: sasindexkey.h:345
SASIndexKeyInitRef
static void SASIndexKeyInitRef(SASIndexKey_t *dest, void *value)
Initial a binary key @ destination with a address value.
Definition: sasindexkey.h:169
SASIndexKey_t
struct SASIndexKey_t SASIndexKey_t
Index Key Handle structure for binary index B-trees.
double_exp_mask
const unsigned long double_exp_mask
mask use to invert the exponent.
Definition: sasindexkey.h:50
SASIndexKeySize
static size_t SASIndexKeySize(SASIndexKey_t *key)
Return the binary index key copy_size.
Definition: sasindexkey.h:157
SASIndexKeyInitDouble
static void SASIndexKeyInitDouble(SASIndexKey_t *dest, double value)
Initial a binary key @ destination with a signed 64-bit integer value.
Definition: sasindexkey.h:308
double_mask
const unsigned long double_mask
mask use to invert the sign and exponent.
Definition: sasindexkey.h:52
SASIndexKeyCopy
static void SASIndexKeyCopy(SASIndexKey_t *dest, SASIndexKey_t *src)
Copy a binary key from source to destination.
Definition: sasindexkey.h:142
machine_uint_t
unsigned long machine_uint_t
word size data unit used for binary keys.
Definition: sasindexkey.h:44
SASIndexKeyReturn1stInt64
static long long SASIndexKeyReturn1stInt64(SASIndexKey_t *dest)
Return the value of the 1st binary key as a signed 64-bit integer value.
Definition: sasindexkey.h:284
SASIndexKey_t
Index Key Handle structure for binary index B-trees.
Definition: sasindexkey.h:60
SASIndexKey_t::copy_size
machine_uhalf_t copy_size
Definition: sasindexkey.h:62
SASIndexKey_t::data
machine_uint_t data[15]
Definition: sasindexkey.h:66
SASIndexKey_t::compare_size
machine_uhalf_t compare_size
Definition: sasindexkey.h:64
sasindexkeymap_t
Union of value types and key machine integer types.
Definition: sasindexkey.h:179
sasindexkeymap_t::double_key
double double_key
Definition: sasindexkey.h:199
sasindexkeymap_t::uint64_key
unsigned long long uint64_key
Definition: sasindexkey.h:195
sasindexkeymap_t::key_element
struct sasindexkeymap_t::@0 key_element
sasindexkeymap_t::int64_key
long long int64_key
Definition: sasindexkey.h:197