Changeset bc216a0 in mainline for uspace/srv/hid/input/generic/gsp.c


Ignore:
Timestamp:
2012-08-07T22:13:44Z (12 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
da68871a
Parents:
b17518e
Message:

Refactored any users of hash_table to use opaque void* keys instead of the cumbersome unsigned long[] keys. Switched from the ad hoc computations of hashes of multiple values to hash_combine().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/input/generic/gsp.c

    rb17518e rbc216a0  
    5151#include <gsp.h>
    5252#include <adt/hash_table.h>
     53#include <adt/hash.h>
    5354#include <stdlib.h>
    5455#include <stdio.h>
    5556
    5657/*
    57  * Hash table operations for the transition function.
    58  */
    59 
    60 static size_t trans_op_hash(const link_t *item);
    61 static size_t trans_op_key_hash(unsigned long key[]);
    62 static bool trans_op_match(unsigned long key[], size_t keys, const link_t *item);
     58 * Transition function hash table operations.
     59 */
     60typedef struct {
     61        int old_state;
     62        int input;
     63} trans_key_t;
     64
     65static size_t trans_key_hash(void *key)
     66{
     67        trans_key_t *trans_key = (trans_key_t *)key;
     68        return hash_combine(trans_key->input, trans_key->old_state);
     69}
     70
     71static size_t trans_hash(const ht_link_t *item)
     72{
     73        gsp_trans_t *t = hash_table_get_inst(item, gsp_trans_t, link);
     74        return hash_combine(t->input, t->old_state);
     75}
     76
     77static bool trans_key_equal(void *key, const ht_link_t *item)
     78{
     79        trans_key_t *trans_key = (trans_key_t *)key;
     80        gsp_trans_t *t = hash_table_get_inst(item, gsp_trans_t, link);
     81       
     82        return trans_key->input == t->input && trans_key->old_state == t->old_state;
     83}
    6384
    6485static hash_table_ops_t trans_ops = {
    65         .hash = trans_op_hash,
    66         .key_hash = trans_op_key_hash,
    67         .match = trans_op_match,
     86        .hash = trans_hash,
     87        .key_hash = trans_key_hash,
     88        .key_equal = trans_key_equal,
    6889        .equal = 0,
    6990        .remove_callback = 0
    7091};
    7192
     93
    7294static gsp_trans_t *trans_lookup(gsp_t *p, int state, int input);
    7395static void trans_insert(gsp_t *p, gsp_trans_t *t);
     
    78100{
    79101        p->states = 1;
    80         hash_table_create(&p->trans, 0, 2, &trans_ops);
     102        hash_table_create(&p->trans, 0, 0, &trans_ops);
    81103}
    82104
     
    222244static gsp_trans_t *trans_lookup(gsp_t *p, int state, int input)
    223245{
    224         link_t *item;
    225         unsigned long key[2];
    226 
    227         key[0] = state;
    228         key[1] = input;
    229 
    230         item = hash_table_find(&p->trans, key);
     246        ht_link_t *item;
     247       
     248        trans_key_t key = {
     249                .input = input,
     250                .old_state = state
     251        };
     252
     253        item = hash_table_find(&p->trans, &key);
    231254        if (item == NULL) return NULL;
    232255
    233         return hash_table_get_instance(item, gsp_trans_t, link);
     256        return hash_table_get_inst(item, gsp_trans_t, link);
    234257}
    235258
     
    258281}
    259282
    260 /*
    261  * Transition function hash table operations.
    262  */
    263 
    264 static size_t trans_op_key_hash(unsigned long key[])
    265 {
    266         return (key[0] * 17 + key[1]);
    267 }
    268 
    269 static size_t trans_op_hash(const link_t *item)
    270 {
    271         gsp_trans_t *t = hash_table_get_instance(item, gsp_trans_t, link);
    272         unsigned long key[] = {
    273                 t->old_state,
    274                 t->input
    275         };
    276        
    277         return trans_op_key_hash(key);
    278 }
    279 
    280 static bool trans_op_match(unsigned long key[], size_t keys, const link_t *item)
    281 {
    282         gsp_trans_t *t = hash_table_get_instance(item, gsp_trans_t, link);
    283         return ((key[0] == (unsigned long) t->old_state)
    284             && (key[1] == (unsigned long) t->input));
    285 }
    286283
    287284/**
Note: See TracChangeset for help on using the changeset viewer.