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" { ...@@ -10,9 +10,11 @@ extern "C" {
#define likely(x) __builtin_expect(!!(x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0) #define unlikely(x) __builtin_expect(!!(x), 0)
typedef double ccs_float_t; typedef double ccs_float_t;
typedef int64_t ccs_int_t; typedef int64_t ccs_int_t;
typedef int32_t ccs_bool_t; typedef int32_t ccs_bool_t;
typedef uint32_t ccs_hash_t;
typedef struct { typedef struct {
uint16_t major; uint16_t major;
uint16_t minor; uint16_t minor;
......
...@@ -46,7 +46,11 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration, ...@@ -46,7 +46,11 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
ccs_datum_t *value_ret); ccs_datum_t *value_ret);
extern ccs_error_t 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 #ifdef __cplusplus
} }
......
#include "cconfigspace_internal.h" #include "cconfigspace_internal.h"
#include "configuration_internal.h" #include "configuration_internal.h"
#include "datum_hash.h"
#include <string.h> #include <string.h>
static inline _ccs_configuration_ops_t * static inline _ccs_configuration_ops_t *
...@@ -154,9 +155,29 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration, ...@@ -154,9 +155,29 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
} }
ccs_error_t ccs_error_t
ccs_configuration_check(ccs_configuration_t configuration) { ccs_configuration_check(ccs_configuration_t configuration) {
if (!configuration || !configuration->data) if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT; return -CCS_INVALID_OBJECT;
return ccs_configuration_space_check_configuration( return ccs_configuration_space_check_configuration(
configuration->data->configuration_space, 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 "cconfigspace_internal.h"
#include "configuration_space_internal.h" #include "configuration_space_internal.h"
#include "configuration_internal.h" #include "configuration_internal.h"
#include "utlist.h"
static ccs_error_t static ccs_error_t
_generate_constraints(ccs_configuration_space_t configuration_space); _generate_constraints(ccs_configuration_space_t configuration_space);
......
#ifndef _CONFIGURATION_SPACE_INTERNAL_H #ifndef _CONFIGURATION_SPACE_INTERNAL_H
#define _CONFIGURATION_SPACE_INTERNAL_H #define _CONFIGURATION_SPACE_INTERNAL_H
#include "utarray.h" #include "utarray.h"
#include "context_internal.h"
#define HASH_NONFATAL_OOM 1 #define HASH_NONFATAL_OOM 1
#include "uthash.h" #include "uthash.h"
#include "utlist.h"
#include "context_internal.h"
struct _ccs_distribution_wrapper_s; struct _ccs_distribution_wrapper_s;
typedef struct _ccs_distribution_wrapper_s _ccs_distribution_wrapper_t; typedef struct _ccs_distribution_wrapper_s _ccs_distribution_wrapper_t;
......
#ifndef _DATUMHASH_H #ifndef _DATUM_HASH_H
#define _DATUMHASH_H #define _DATUM_HASH_H
#define HASH_NONFATAL_OOM 1 #define HASH_NONFATAL_OOM 1
#define HASH_FUNCTION(s,len,hashv) (hashv) = _hash_datum((ccs_datum_t *)(s)) #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) { ...@@ -51,6 +51,7 @@ static inline int _datum_cmp(ccs_datum_t *a, ccs_datum_t *b) {
else else
return strcmp(a->value.s, b->value.s); return strcmp(a->value.s, b->value.s);
case CCS_NONE: case CCS_NONE:
case CCS_INACTIVE:
return 0; return 0;
break; break;
default: default:
...@@ -59,16 +60,27 @@ static inline int _datum_cmp(ccs_datum_t *a, ccs_datum_t *b) { ...@@ -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) { static inline unsigned _hash_datum(ccs_datum_t *d) {
unsigned h; unsigned h;
switch(d->type) { switch(d->type) {
case CCS_STRING: case CCS_STRING:
if (d->value.s) if (d->value.s) {
HASH_JEN(d->value.s, strlen(d->value.s), h); 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 else
HASH_JEN(d, sizeof(ccs_datum_t), h); HASH_JEN(d, sizeof(ccs_datum_t), h);
break; break;
case CCS_NONE: case CCS_NONE:
case CCS_INACTIVE:
HASH_JEN(&(d->type), sizeof(d->type), h); HASH_JEN(&(d->type), sizeof(d->type), h);
break; break;
default: default:
...@@ -84,4 +96,4 @@ struct _ccs_hash_datum_s { ...@@ -84,4 +96,4 @@ struct _ccs_hash_datum_s {
typedef struct _ccs_hash_datum_s _ccs_hash_datum_t; 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