]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/liblfds700/src/lfds700_hash_addonly/lfds700_hash_addonly_query.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / liblfds700 / src / lfds700_hash_addonly / lfds700_hash_addonly_query.c
1 /***** includes *****/
2 #include "lfds700_hash_addonly_internal.h"
3
4 /***** private prototypes *****/
5 static void lfds700_hash_a_internal_validate( struct lfds700_hash_a_state *has, struct lfds700_misc_validation_info *vi, enum lfds700_misc_validity *lfds700_hash_a_validity );
6
7
8
9
10
11 /****************************************************************************/
12 void lfds700_hash_a_query( struct lfds700_hash_a_state *has, enum lfds700_hash_a_query query_type, void *query_input, void *query_output )
13 {
14   LFDS700_PAL_ASSERT( has != NULL );
15   // TRD : query_type can be any value in its range
16
17   LFDS700_MISC_BARRIER_LOAD;
18
19   switch( query_type )
20   {
21     case LFDS700_HASH_A_QUERY_GET_POTENTIALLY_INACCURATE_COUNT:
22     {
23       struct lfds700_hash_a_iterate
24         ai;
25
26       struct lfds700_hash_a_element
27         *hae;
28
29       LFDS700_PAL_ASSERT( query_input == NULL );
30       LFDS700_PAL_ASSERT( query_output != NULL );
31
32       *(lfds700_pal_uint_t *) query_output = 0;
33
34       lfds700_hash_a_iterate_init( has, &ai );
35
36       while( lfds700_hash_a_iterate(&ai, &hae) )
37         ( *(lfds700_pal_uint_t *) query_output )++;
38     }
39     break;
40
41     case LFDS700_HASH_A_QUERY_SINGLETHREADED_VALIDATE:
42       // TRD: query_input can be any value in its range
43       LFDS700_PAL_ASSERT( query_output != NULL );
44
45       lfds700_hash_a_internal_validate( has, (struct lfds700_misc_validation_info *) query_input, (enum lfds700_misc_validity *) query_output );
46     break;
47   }
48
49   return;
50 }
51
52
53
54
55
56 /****************************************************************************/
57 static void lfds700_hash_a_internal_validate( struct lfds700_hash_a_state *has, struct lfds700_misc_validation_info *vi, enum lfds700_misc_validity *lfds700_hash_a_validity )
58 {
59   lfds700_pal_uint_t
60     lfds700_hash_a_total_number_elements = 0,
61     lfds700_btree_au_total_number_elements = 0,
62     number_elements;
63
64   lfds700_pal_uint_t
65     loop;
66
67   LFDS700_PAL_ASSERT( has!= NULL );
68   // TRD : vi can be NULL
69   LFDS700_PAL_ASSERT( lfds700_hash_a_validity != NULL );
70
71   /* TRD : validate every btree_addonly_unbalanced in the addonly_hash
72            sum elements in each btree_addonly_unbalanced
73            check matches expected element counts (if vi is provided)
74   */
75
76   *lfds700_hash_a_validity = LFDS700_MISC_VALIDITY_VALID;
77
78   for( loop = 0 ; *lfds700_hash_a_validity == LFDS700_MISC_VALIDITY_VALID and loop < has->array_size ; loop++ )
79     lfds700_btree_au_query( has->baus_array+loop, LFDS700_BTREE_AU_QUERY_SINGLETHREADED_VALIDATE, NULL, (void *) lfds700_hash_a_validity );
80
81   if( *lfds700_hash_a_validity == LFDS700_MISC_VALIDITY_VALID )
82   {
83     for( loop = 0 ; loop < has->array_size ; loop++ )
84     {
85       lfds700_btree_au_query( has->baus_array+loop, LFDS700_BTREE_AU_QUERY_GET_POTENTIALLY_INACCURATE_COUNT, NULL, (void *) &number_elements );
86       lfds700_btree_au_total_number_elements += number_elements;
87     }
88
89     // TRD : first, check btree_addonly_unbalanced total vs the addonly_hash total
90     lfds700_hash_a_query( has, LFDS700_HASH_A_QUERY_GET_POTENTIALLY_INACCURATE_COUNT, NULL, &lfds700_hash_a_total_number_elements );
91
92     // TRD : the btree_addonly_unbalanceds are assumed to speak the truth
93     if( lfds700_hash_a_total_number_elements < lfds700_btree_au_total_number_elements )
94       *lfds700_hash_a_validity = LFDS700_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
95
96     if( lfds700_hash_a_total_number_elements > lfds700_btree_au_total_number_elements )
97       *lfds700_hash_a_validity = LFDS700_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
98
99     // TRD : second, if we're still valid and vi is provided, check the btree_addonly_unbalanced total against vi
100     if( *lfds700_hash_a_validity == LFDS700_MISC_VALIDITY_VALID and vi != NULL )
101     {
102       if( lfds700_btree_au_total_number_elements < vi->min_elements )
103         *lfds700_hash_a_validity = LFDS700_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
104
105       if( lfds700_btree_au_total_number_elements > vi->max_elements )
106         *lfds700_hash_a_validity = LFDS700_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
107     }
108   }
109
110   return;
111 }
112