Commit 5b18cea8 authored by Brice Videau's avatar Brice Videau
Browse files

Added a hash function for configurations.

parent 84613a98
......@@ -10,9 +10,11 @@ extern "C" {
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
typedef double ccs_float_t;
typedef int64_t ccs_int_t;
typedef int32_t ccs_bool_t;
typedef double ccs_float_t;
typedef int64_t ccs_int_t;
typedef int32_t ccs_bool_t;
typedef uint32_t ccs_hash_t;
typedef struct {
uint16_t major;
uint16_t minor;
......
......@@ -46,7 +46,11 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
ccs_datum_t *value_ret);
extern ccs_error_t
ccs_configuration_check(ccs_configuration_t configuration);
ccs_configuration_check(ccs_configuration_t configuration);
extern ccs_error_t
ccs_configuration_hash(ccs_configuration_t configuration,
ccs_hash_t *hash_ret);
#ifdef __cplusplus
}
......
#include "cconfigspace_internal.h"
#include "configuration_internal.h"
#include "datum_hash.h"
#include <string.h>
static inline _ccs_configuration_ops_t *
......@@ -154,9 +155,29 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
}
ccs_error_t
ccs_configuration_check(ccs_configuration_t configuration) {
ccs_configuration_check(ccs_configuration_t configuration) {
if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT;
return ccs_configuration_space_check_configuration(
configuration->data->configuration_space, configuration);
}
ccs_error_t
ccs_configuration_hash(ccs_configuration_t configuration,
ccs_hash_t *hash_ret) {
if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT;
if (!hash_ret)
return -CCS_INVALID_VALUE;
_ccs_configuration_data_t *data = configuration->data;
ccs_hash_t h, ht;
HASH_JEN(&(data->configuration_space), sizeof(data->configuration_space), h);
HASH_JEN(&(data->num_values), sizeof(data->num_values), ht);
h = _hash_combine(h, ht);
for (size_t i = 0; i < data->num_values; i++) {
ht = _hash_datum(data->values + i);
h = _hash_combine(h, ht);
}
*hash_ret = h;
return CCS_SUCCESS;
}
#include "cconfigspace_internal.h"
#include "configuration_space_internal.h"
#include "configuration_internal.h"
#include "utlist.h"
static ccs_error_t
_generate_constraints(ccs_configuration_space_t configuration_space);
......
#ifndef _CONFIGURATION_SPACE_INTERNAL_H
#define _CONFIGURATION_SPACE_INTERNAL_H
#include "utarray.h"
#include "context_internal.h"
#define HASH_NONFATAL_OOM 1
#include "uthash.h"
#include "utlist.h"
#include "context_internal.h"
struct _ccs_distribution_wrapper_s;
typedef struct _ccs_distribution_wrapper_s _ccs_distribution_wrapper_t;
......
#ifndef _DATUMHASH_H
#define _DATUMHASH_H
#ifndef _DATUM_HASH_H
#define _DATUM_HASH_H
#define HASH_NONFATAL_OOM 1
#define HASH_FUNCTION(s,len,hashv) (hashv) = _hash_datum((ccs_datum_t *)(s))
......@@ -51,6 +51,7 @@ static inline int _datum_cmp(ccs_datum_t *a, ccs_datum_t *b) {
else
return strcmp(a->value.s, b->value.s);
case CCS_NONE:
case CCS_INACTIVE:
return 0;
break;
default:
......@@ -59,16 +60,27 @@ static inline int _datum_cmp(ccs_datum_t *a, ccs_datum_t *b) {
}
}
//from boost
static inline ccs_hash_t _hash_combine(ccs_hash_t h1, ccs_hash_t h2) {
h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
return h1;
}
static inline unsigned _hash_datum(ccs_datum_t *d) {
unsigned h;
switch(d->type) {
case CCS_STRING:
if (d->value.s)
HASH_JEN(d->value.s, strlen(d->value.s), h);
if (d->value.s) {
unsigned h1, h2;
HASH_JEN(&(d->type), sizeof(d->type), h1);
HASH_JEN(d->value.s, strlen(d->value.s), h2);
h = _hash_combine(h1, h2);
}
else
HASH_JEN(d, sizeof(ccs_datum_t), h);
break;
case CCS_NONE:
case CCS_INACTIVE:
HASH_JEN(&(d->type), sizeof(d->type), h);
break;
default:
......@@ -84,4 +96,4 @@ struct _ccs_hash_datum_s {
typedef struct _ccs_hash_datum_s _ccs_hash_datum_t;
#endif //_DATUMHASH_H
#endif //_DATUM_HASH_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment