Last active
December 28, 2015 19:39
-
-
Save ramntry/7552002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = []; | |
for (var i = 0; i < 30000; i++) { | |
arr.push(Math.round(Math.random() * 1.0e9).toString()); | |
} | |
Array.prototype.diff = function(a) { | |
return this.filter(function(i) { return !(a.indexOf(i) > -1); }); | |
}; | |
for (var i = 0; i < 20; ++i) { | |
console.log(arr.diff(arr).length == 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
static inline size_t calc_hash(char const *str) | |
{ | |
size_t acc = 0; | |
while (*str) { | |
acc *= 223; | |
acc += *str++; | |
} | |
return acc; | |
} | |
static char **gen_array_of_random_strings(size_t size) | |
{ | |
char **array = (char **)malloc(size * sizeof(char *)); | |
for (size_t i = 0; i < size; ++i) { | |
array[i] = (char *)malloc(10); | |
sprintf(array[i], "%d", rand() % 1000000000); | |
} | |
return array; | |
} | |
static void print_array_of_strings(char **array, size_t size) | |
{ | |
putchar('['); | |
if (size) | |
printf(" '%s'", array[0]); | |
for (size_t i = 1; i < size; ++i) | |
printf(",\n '%s'", array[i]); | |
printf(" ]\n"); | |
} | |
static void free_array_of_strings(char **array, size_t size) | |
{ | |
for (size_t i = 0; i < size; ++i) | |
free(array[i]); | |
free(array); | |
} | |
struct Bucket | |
{ | |
char *str; | |
struct Bucket *next; | |
}; | |
static struct Bucket *create_hash(size_t numof_buckets) | |
{ | |
return (struct Bucket *)calloc(numof_buckets, sizeof(struct Bucket)); | |
} | |
static inline void insert(struct Bucket *hash, size_t numof_buckets, char *str) | |
{ | |
struct Bucket *bucket = &hash[calc_hash(str) % numof_buckets]; | |
if (bucket->str) { | |
struct Bucket *new_bucket = (struct Bucket *)malloc(sizeof(struct Bucket)); | |
new_bucket->str = str; | |
new_bucket->next = bucket->next; | |
bucket->next = new_bucket; | |
} else | |
bucket->str = str; | |
} | |
static inline int has(struct Bucket *hash, size_t numof_buckets, char *str) | |
{ | |
struct Bucket *bucket = &hash[calc_hash(str) % numof_buckets]; | |
if (bucket->str && strcmp(bucket->str, str) == 0) | |
return 1; | |
bucket = bucket->next; | |
while (bucket) { | |
if (strcmp(bucket->str, str) == 0) | |
return 1; | |
bucket = bucket->next; | |
} | |
return 0; | |
} | |
static void dump_hash(struct Bucket *hash, size_t numof_buckets) | |
{ | |
for (size_t i = 0; i < numof_buckets; ++i) { | |
printf("[%5zu]: [ ", i); | |
struct Bucket *bucket = &hash[i]; | |
if (bucket->str) | |
printf("%9s", bucket->str); | |
bucket = bucket->next; | |
while (bucket) { | |
printf(", %9s", bucket->str); | |
bucket = bucket->next; | |
} | |
printf(" ]\n"); | |
} | |
} | |
static size_t max_load(struct Bucket *hash, size_t numof_buckets) | |
{ | |
size_t acc = 0; | |
for (size_t i = 0; i < numof_buckets; ++i) { | |
size_t curr_load = 0; | |
struct Bucket *bucket = &hash[i]; | |
if (bucket->str) | |
++curr_load; | |
bucket = bucket->next; | |
while (bucket) { | |
++curr_load; | |
bucket = bucket->next; | |
} | |
acc = curr_load > acc ? curr_load : acc; | |
} | |
return acc; | |
} | |
static void free_hash(struct Bucket *hash, size_t numof_buckets) | |
{ | |
for (size_t i = 0; i < numof_buckets; ++i) { | |
struct Bucket *list = hash[i].next; | |
while (list) { | |
struct Bucket *to_free = list; | |
list = list->next; | |
free(to_free); | |
} | |
} | |
free(hash); | |
} | |
int hasAll(char **this, size_t this_size, char **a, size_t a_size) | |
{ | |
size_t const numof_buckets = this_size * 2; | |
struct Bucket *hash = create_hash(numof_buckets); | |
for (size_t i = 0; i < this_size; ++i) | |
insert(hash, numof_buckets, this[i]); | |
int result = 1; | |
for (size_t i = 0; i < a_size; ++i) | |
if (!has(hash, numof_buckets, a[i])) { | |
result = 0; | |
break; | |
} | |
free_hash(hash, numof_buckets); | |
return result; | |
} | |
int main() | |
{ | |
size_t const size = 30000; | |
size_t const numof_repetitions = 20; | |
char **array = gen_array_of_random_strings(size); | |
for (size_t i = 0; i < numof_repetitions; ++i) | |
printf("%s\n", hasAll(array, size, array, size) ? "true" : "false"); | |
free_array_of_strings(array, size); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
static inline size_t calc_hash(char const *str) | |
{ | |
size_t acc = 0; | |
while (*str) { | |
acc *= 223; | |
acc += *str++; | |
} | |
return acc; | |
} | |
static char **gen_array_of_random_strings(size_t size) | |
{ | |
char **array = (char **)malloc(size * sizeof(char *)); | |
for (size_t i = 0; i < size; ++i) { | |
array[i] = (char *)malloc(10); | |
sprintf(array[i], "%d", rand() % 1000000000); | |
} | |
return array; | |
} | |
static void print_array_of_strings(char **array, size_t size) | |
{ | |
putchar('['); | |
if (size) | |
printf(" '%s'", array[0]); | |
for (size_t i = 1; i < size; ++i) | |
printf(",\n '%s'", array[i]); | |
printf(" ]\n"); | |
} | |
static void free_array_of_strings(char **array, size_t size) | |
{ | |
for (size_t i = 0; i < size; ++i) | |
free(array[i]); | |
free(array); | |
} | |
struct Bucket | |
{ | |
char *str; | |
}; | |
static struct Bucket *create_hash(size_t numof_buckets) | |
{ | |
return (struct Bucket *)calloc(numof_buckets, sizeof(struct Bucket)); | |
} | |
static inline void insert(struct Bucket *hash, size_t numof_buckets, char *str) | |
{ | |
size_t pos = calc_hash(str) % numof_buckets; | |
while (hash[pos].str) | |
pos = (pos + 1) % numof_buckets; | |
hash[pos].str = str; | |
} | |
static inline int has(struct Bucket *hash, size_t numof_buckets, char *str) | |
{ | |
size_t pos = calc_hash(str) % numof_buckets; | |
while (hash[pos].str) { | |
if (strcmp(hash[pos].str, str) == 0) | |
return 1; | |
pos = (pos + 1) % numof_buckets; | |
} | |
return 0; | |
} | |
static void dump_hash(struct Bucket *hash, size_t numof_buckets) | |
{ | |
for (size_t i = 0; i < numof_buckets; ++i) { | |
printf("[%5zu]: [ ", i); | |
struct Bucket *bucket = &hash[i]; | |
if (bucket->str) | |
printf("%9s", bucket->str); | |
printf(" ]\n"); | |
} | |
} | |
static size_t max_load(struct Bucket *hash, size_t numof_buckets) | |
{ | |
size_t acc = 0; | |
size_t contiguous_used = 0; | |
for (size_t i = 0; i < numof_buckets; ++i) | |
if (hash[i].str) | |
++contiguous_used; | |
else { | |
acc = contiguous_used > acc ? contiguous_used : acc; | |
contiguous_used = 0; | |
} | |
return acc; | |
} | |
static void free_hash(struct Bucket *hash, size_t numof_buckets) | |
{ | |
free(hash); | |
} | |
int hasAll(char **this, size_t this_size, char **a, size_t a_size) | |
{ | |
size_t const numof_buckets = this_size * 4; | |
struct Bucket *hash = create_hash(numof_buckets); | |
for (size_t i = 0; i < this_size; ++i) | |
insert(hash, numof_buckets, this[i]); | |
int result = 1; | |
for (size_t i = 0; i < a_size; ++i) | |
if (!has(hash, numof_buckets, a[i])) { | |
result = 0; | |
break; | |
} | |
free_hash(hash, numof_buckets); | |
return result; | |
} | |
int main() | |
{ | |
size_t const size = 30000; | |
size_t const numof_repetitions = 20; | |
char **array = gen_array_of_random_strings(size); | |
for (size_t i = 0; i < numof_repetitions; ++i) | |
printf("%s\n", hasAll(array, size, array, size) ? "true" : "false"); | |
free_array_of_strings(array, size); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = []; | |
for (var i = 0; i < 30000; i++) { | |
arr.push(Math.round(Math.random() * 1.0e9).toString()); | |
} | |
Array.prototype.hasAll = function(a) { | |
var hash = this.reduce(function(acc, i) { acc[i] = true; return acc; }, {}); | |
return a.every(function(i) { return i in hash; }); | |
}; | |
for (var i = 0; i < 20; ++i) { | |
console.log(arr.hasAll(arr)); | |
} |
Author
ramntry
commented
Nov 19, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment