Skip to content

Commit

Permalink
ref #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simo981 committed Apr 16, 2024
1 parent 50d6d7f commit ec42a71
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 82 deletions.
100 changes: 25 additions & 75 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static size_t max_len = 0;
static size_t min_len = 0;
static char **dict;
static size_t word_size;
static Queue_t **all_queues = NULL;
static Queue_t *global_queue = NULL;
static char **connectors = NULL;
static unsigned short ***delim_bins = NULL;
static char leet_map[128] = {0x0};
Expand Down Expand Up @@ -45,14 +45,8 @@ void gen_bin_to_arr(unsigned short *arr, size_t size, size_t idx, size_t max, si

void free_inputs_optind(void)
{
if (connectors)
{
free(connectors);
}
if (last)
{
free(last);
}
FREE_P(connectors);
FREE_P(last);
}

static struct option long_options[] =
Expand Down Expand Up @@ -108,7 +102,7 @@ inline void print_out(char **arr, size_t size)
char finalString[BUFF] = {0x0};
char *all_strings[BUFF] = {0x0};
size_t run_len = 0, strings_len = 0;
size_t lengths[BUFF];
size_t lengths[size];
size_t saved_len = 0;
size_t reverse_make_sense = false;
for (size_t i = 0; i < size; i++)
Expand Down Expand Up @@ -225,7 +219,7 @@ inline void print_out(char **arr, size_t size)
{
printf("%s\n", all_strings[i]);
}
free(all_strings[i]);
FREE_P(all_strings[i]);
}
}

Expand Down Expand Up @@ -292,28 +286,23 @@ void seq_perm(char **arr, size_t size)
P[i++] = 0;
}
}
free(P);
FREE_P(P);
}

void *thread_perm(void *in)
{
size_t queuePos = (size_t)in;
input_t *words;
for (;;)
{
words = pop_queue(all_queues[queuePos]);
words = pop_queue(global_queue);
if (words == NULL)
{
break;
}
print_out(words->aux, words->len);
seq_perm(words->aux, words->len);
for (size_t i = 0; i < words->len; i++)
{
free(words->aux[i]);
}
free(words->aux);
free(words);
FREE_PP(words->aux, 0, words->len);
FREE_P(words);
}
return NULL;
}
Expand Down Expand Up @@ -376,25 +365,18 @@ void gen_bin_perms(unsigned short *arr, size_t size, size_t idx, size_t max, siz
delim_t *delim_word = &delim_words[j];
full_dict[size_dict++] = x[j] ? strdup(delim_word->p2) : strdup(delim_word->p1);
}
push_queue(all_queues[cur - min], full_dict, cur);
push_queue(global_queue, full_dict, cur);
}
for (size_t i = 0; i < delim; i++)
{
free(delim_words[i].p1);
free(delim_words[i].p2);
}
for (size_t i = 0; i < cur; i++)
{
if (auxPerm[i] != NULL)
{
free(auxPerm[i]);
}
}
free(auxPerm);
FREE_PP(auxPerm, 0, cur);
}
else
{
push_queue(all_queues[cur - min], auxPerm, pointer);
push_queue(global_queue, auxPerm, pointer);
}
}
return;
Expand All @@ -411,7 +393,6 @@ void gen_bin_perms(unsigned short *arr, size_t size, size_t idx, size_t max, siz
int main(int argc, char **argv)
{
int c, option_index = 0;
size_t thread_n, queue_n;
while ((c = getopt_long(argc, argv, "u:l:pk:c:s:e:r:", long_options, &option_index)) != -1)
{
switch (c)
Expand Down Expand Up @@ -507,9 +488,7 @@ int main(int argc, char **argv)
}

word_size = (size_t)argc - (size_t)optind;
queue_n = max_len - min_len + 1;
thread_n = (size_t)(max_len * (max_len + 1)) / 2;
all_queues = (Queue_t **)malloc(sizeof(Queue_t *) * queue_n);
global_queue = default_init_queue();
size_t n_delim = 0;
size_t optind_copy = optind;
for (size_t i = 0; i < word_size; i++)
Expand All @@ -526,13 +505,9 @@ int main(int argc, char **argv)
unsigned short *bin = (unsigned short *)calloc(i, sizeof(unsigned short));
size_t bd_size = 0;
gen_bin_to_arr(bin, i, 0, i, 0, 0, bin_delim, &bd_size);
free(bin);
FREE_P(bin);
}
delim_bins = bin_delim;
for (size_t i = 0; i < queue_n; i++)
{
all_queues[i] = default_init_queue();
}
char **input_words = (char **)malloc(sizeof(char *) * word_size);
for (size_t i = 0; i < word_size; i++)
{
Expand All @@ -546,48 +521,23 @@ int main(int argc, char **argv)
bin[i] = 1;
}
gen_bin_perms(bin, word_size, 0, max_len, 0, min_len);
free(bin);
pthread_t tworker[thread_n];
FREE_P(bin);
pthread_t tworker[N_THREAD];
memset(&tworker, 0x0, sizeof(tworker));
size_t pos = 0;
for (size_t i = 0; i < queue_n; i++)
for (size_t i = 0; i < N_THREAD; i++)
{
size_t j = i + 1;
while (j > 0)
{
pthread_create(&tworker[pos], NULL, thread_perm, (void *)i);
j--;
pos++;
}
pthread_create(&tworker[pos], NULL, thread_perm, NULL);
}
for (size_t i = 0; i < thread_n; i++)
for (size_t i = 0; i < N_THREAD; i++)
{
pthread_join(tworker[i], NULL);
}
for (size_t i = 1; i < n_delim + 1; i++)
{
for (size_t j = 0; j < (1 << i); j++)
{
free(bin_delim[i][j]);
}
free(bin_delim[i]);
}
free(bin_delim);
for (size_t i = 0; i < queue_n; i++)
{
free_queue(all_queues[i]);
free(all_queues[i]);
}
for (size_t i = 0; i < connectors_size; i++)
{
free(connectors[i]);
}
for (size_t i = 0; i < last_size; i++)
{
free(last[i]);
}
free_inputs_optind();
free(all_queues);
free(input_words);
free_queue(global_queue);
FREE_P(global_queue);
FREE_P(input_words);
FREE_PP(connectors, 0, connectors_size);
FREE_PP(last, 0, last_size);
FREE_PPP(bin_delim, 1, n_delim + 1, 0, (1 << i));
return 0;
}
43 changes: 36 additions & 7 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,44 @@
#include <stdbool.h>
#include <string.h>
#define BUFF 512
#define N_THREAD 8

#define COPY_STRING(dest, src) \
({ \
size_t copy_len = strlen(src); \
memccpy(dest, src, '\0', copy_len); \
dest[copy_len] = '\0'; \
copy_len; \
#define FREE_PPP(p, fc, size, sc, size2) \
({ \
for (size_t i = fc; i < size; i++) \
{ \
FREE_PP(p[i], sc, size2); \
} \
FREE_P(p); \
})

#define FREE_PP(p, fc, size) \
({ \
if (p) \
{ \
for (size_t i = fc; i < size; i++) \
{ \
FREE_P(p[i]); \
} \
FREE_P(p); \
} \
})

#define FREE_P(p) \
({ \
if (p) \
{ \
free(p); \
} \
})

#define COPY_STRING(dest, src) ({ \
size_t copy_len = strlen(src); \
memccpy(dest, src, '\0', copy_len); \
dest[copy_len] = '\0'; \
copy_len; \
})

#define CHECK_TRUE(var, message) \
({ \
if (var) \
Expand Down Expand Up @@ -71,7 +100,7 @@ bool palindrome(char *str, size_t len);

void exit_usage(char *plus)
{
printf("%s\n./seqperm --upper full/first --leet full/vowel --only_transformations y/n --start <min words> --end <max words> --last N1,N2,... --connectors ... w1 w2 w3 w4\n", plus);
printf("%s\n./seqperm --upper full/first --leet full/vowel --only_transformations --start <min words> --end <max words> --last N1,N2,... --connectors ... w1 w2 w3 w4\n", plus);
exit(EXIT_FAILURE);
}

Expand Down

0 comments on commit ec42a71

Please sign in to comment.