<commit_before>//
//  hashtable.c
//
//  Created by <NAME> on 23/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

#include "hashtable.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>


typedef struct hashtable_item_s {
  hashtable_hash_t fullhash;
  void *key;
  void *value;
  struct hashtable_item_s *next;
} hashtable_item_t;

typedef struct hashtable_rootItem_s {
  hashtable_item_t item;
  struct hashtable_rootItem_s *enumPrev;
  struct hashtable_rootItem_s *enumNext;
} hashtable_rootItem_t;

struct hashtable_s {
  size_t cbuckets;
  hashtable_fcompare compare;
  hashtable_fhash hash;
  hashtable_ffree free;
  hashtable_rootItem_t *enumHead;
  hashtable_rootItem_t *buckets;
};


hashtable_t *hashtable_make(size_t cbuckets, hashtable_fcompare c, hashtable_fhash h, hashtable_ffree f) {
  hashtable_t *ht;
  
  if (!h || !c)
    return NULL;
  if (cbuckets < 1)
    cbuckets = 127;
  
  ht = calloc(1, sizeof(hashtable_t));
  ht->cbuckets = cbuckets;
  ht->compare = c;
  ht->hash = h;
  ht->free = f;
  ht->buckets = calloc(cbuckets, sizeof(hashtable_item_t));
  return ht;
}


void hashtable_free(hashtable_t *ht) {
  hashtable_rootItem_t *thisHead;
  hashtable_item_t *this, *next;
  
  thisHead = ht->enumHead;
  while (thisHead) {
    this = thisHead->item.next;
    while (this) {
      next = this->next;
      ht->free(this->key);
      ht->free(this->value);
      free(this);
      this = next;
    }
    thisHead = thisHead->enumNext;
  }
  
  free(ht->buckets);
  free(ht);
}


void hashtable_insert(hashtable_t *ht, void *key, void *value) {
  hashtable_hash_t hash;
  size_t ph;
  hashtable_rootItem_t *rthis;
  hashtable_item_t *this, *next;
  
  if (!key) return;
  
  hash = ht->hash(key);<commit_msg>Move the buckets array at the end of the main hashtable struct, instead of doing a separate allocation for it.<commit_after>//
//  hashtable.c
//
//  Created by <NAME> on 23/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

#include "hashtable.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>


typedef struct hashtable_item_s {
  hashtable_hash_t fullhash;
  void *key;
  void *value;
  struct hashtable_item_s *next;
} hashtable_item_t;

typedef struct hashtable_rootItem_s {
  hashtable_item_t item;
  struct hashtable_rootItem_s *enumPrev;
  struct hashtable_rootItem_s *enumNext;
} hashtable_rootItem_t;

struct hashtable_s {
  size_t cbuckets;
  hashtable_fcompare compare;
  hashtable_fhash hash;
  hashtable_ffree free;
  hashtable_rootItem_t *enumHead;
  hashtable_rootItem_t buckets[1];  /* buckets are trailing */
};


hashtable_t *hashtable_make(size_t cbuckets, hashtable_fcompare c, hashtable_fhash h, hashtable_ffree f) {
  hashtable_t *ht;
  
  if (!h || !c)
    return NULL;
  if (cbuckets < 1)
    cbuckets = 127;
  
  ht = calloc(1, sizeof(hashtable_t) + sizeof(hashtable_rootItem_t) * (cbuckets - 1));
  ht->cbuckets = cbuckets;
  ht->compare = c;
  ht->hash = h;
  ht->free = f;
  return ht;
}


void hashtable_free(hashtable_t *ht) {
  hashtable_rootItem_t *thisHead;
  hashtable_item_t *this, *next;
  
  thisHead = ht->enumHead;
  while (thisHead) {
    this = thisHead->item.next;
    while (this) {
      next = this->next;
      ht->free(this->key);
      ht->free(this->value);
      free(this);
      this = next;
    }
    thisHead = thisHead->enumNext;
  }
  
  free(ht);
}


void hashtable_insert(hashtable_t *ht, void *key, void *value) {
  hashtable_hash_t hash;
  size_t ph;
  hashtable_rootItem_t *rthis;
  hashtable_item_t *this, *next;
  
  if (!key) return;
  
  hash = ht->hash(key);
{'fraction_non_alphanumeric': 0.12081274025260846, 'fraction_numerical': 0.008786381109280615, 'mean_word_length': 3.652618135376756, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4385027', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1450, 'n_words': 316}