array_indexer.c 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/****************************************************************************
 * Array indexing functions                                                  *
 ****************************************************************************/

/**
 * @file array_indexer.c
 * @author Celine Mercier
 * @date October 5th 2017
 * @brief Functions handling the indexing and retrieval of arrays of any type.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>

#include "obiblob.h"
#include "obiblob_indexer.h"
#include "obidebug.h"
#include "obitypes.h"


#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)


index_t obi_index_array(Obi_indexer_p indexer, const void* value, uint8_t elt_size, int value_length)
{
	Obi_blob_p  value_b;
	index_t 	idx;
	int32_t     length_bytes;

33 34 35
	//fprintf(stderr, "\nelt size in C: %u, value len = %d\n", elt_size, value_length);
	//for (int i=0; i<value_length; i++)
	//	fprintf(stderr, "%d - ", ((obiint_t*)value)[i]);
36

37
	length_bytes = value_length * elt_size / 8;
38

39 40
	//fprintf(stderr, "\nlength_bytes: %d", length_bytes);

41 42 43 44 45
	// Encode value
	value_b = obi_blob((byte_t*)value, elt_size, length_bytes, length_bytes);
	if (value_b == NULL)
		return -1;

46 47
	//for (int i=0; i<value_length; i++)
	//	fprintf(stderr, "\nin blob: value %d\n", ((obiint_t*)(value_b->value))[i]);
48 49 50 51 52 53 54 55 56 57

	// Add in the indexer
	idx = obi_indexer_add(indexer, value_b);

	free(value_b);

	return idx;
}


58
const void* obi_retrieve_array(Obi_indexer_p indexer, index_t idx, int32_t* value_length_p)
59 60 61 62 63 64
{
	Obi_blob_p  value_b;

	// Get encoded value
	value_b = obi_indexer_get(indexer, idx);

65 66
	//fprintf(stderr, "\nlen in blob: %d, elt size %u", value_b->length_decoded_value, value_b->element_size);

67
	// Store array length
68 69 70 71 72
	*value_length_p = (value_b->length_decoded_value) * 8 / (value_b->element_size);

	//fprintf(stderr, "\ngetting length: %d\n", *value_length_p);
	//for (int i=0; i<*value_length_p; i++)
	//	fprintf(stderr, "\nin blob getting: value %d\n", ((obiint_t*)(value_b->value))[i]);
73 74 75 76 77

	// Return pointer on mapped array
	return ((void*) (value_b->value));
}