Example #1
0
void *hfind(htable_t *ht, void *key, int key_len)
{
  uint32_t index;
  hash_entry_t *hen;

  null_if_null(ht);

  index = h_index(ht, key, key_len);
  hen = h_entry(ht, index, key, key_len);
  return hen ? hen->data : NULL;
}
Example #2
0
void *
hinsert(htable_t *ht, void *key, int key_len, void *data, hcallback_func_t *destructor, 
                      hcallback_func_t *can_replace, hcallback_func_t *do_delete, int *did_delete)
{
  uint32_t index;
  hash_entry_t *hen;
  hash_entry_t *hen_old;
  void *data_old = NULL;

  null_if_null(ht);
  
  index = h_index(ht, key, key_len);
  hen = h_entry(ht, index, key, key_len);

  hen_old = hen;
  data_old = hen ? hen->data : NULL;

  if (did_delete) { *did_delete = 0; }

  if(can_replace != NULL && !can_replace(key, key_len, data_old, data)) {
    return data;
  }
  if (NULL == (hen = h_alloc_hen(key_len))) {
    return data;
  }
  if (hen_old) {
    /*
     * entry already exists - we are about to replace it.
     *
     * If not for the iterators, we'd be all fine and dandy.
     *
     * But there can be iterators referencing this item
     * (hence, can't wipe out key/data).
     */
    h_try_delete(ht, hen_old, do_delete, did_delete);
  } else {
    if (did_delete) { *did_delete = 0; }
  }

  /* 
   * fill in the new hen
   */

  memcpy(hen->key, key, key_len);
  hen->data = data;
  hen->destructor = destructor;
  LIST_INSERT_HEAD(&ht->buckets[index], hen, entries);
  // inserting in the head of all entries ensures we don't have
  // to mess with the iterators
  LIST_INSERT_HEAD(&ht->all_entries, hen, all_entries);
  // successfully inserted 
  ht->total_items++;
  return data_old;
}
Example #3
0
void shuffle(
    std::shared_ptr<particle_type> particle
  , std::shared_ptr<halmd::random::gpu::random<rng_type> > random
)
{
    // generate random permutation
    cuda::vector<unsigned int> g_index(particle->nparticle());
    cuda::host::vector<unsigned int> h_index(g_index.size());
    std::iota(h_index.begin(), h_index.end(), 0);
    cuda::copy(h_index.begin(), h_index.end(), g_index.begin());
    random->shuffle(g_index);

    // shuffle particles
    particle->rearrange(g_index);
}
Example #4
0
int main(void) {
    int c[] = {0, 1, 3, 5, 6};
    printf("hindex = %d\n", h_index(c, 5));
    return 0;
}