Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / bench / k-nucleotide.c
blob3bace391c4d7c69a47d480183695d1553f7b6a72
1 /*
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright
6 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
12 * Neither the name of "The Computer Language Benchmarks Game" nor the
13 name of "The Computer Language Shootout Benchmarks" nor the names of
14 its contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <glib.h>
36 typedef struct stat_s stat_t;
37 struct stat_s
39 const gchar *key;
40 long stat;
43 #define MAX_ELM (8192 / sizeof (stat_t))
45 static int
46 generate_frequencies (int fl, char *buffer, long buflen,
47 GHashTable *ht, GTrashStack **ts, GPtrArray *roots, GStringChunk *sc)
49 gchar *key;
50 long i;
52 if (fl > buflen) return 0;
53 if (fl == 0) return 0;
55 for (i = 0; i < buflen - fl + 1; ++i)
57 char nulled;
58 stat_t *stat;
60 nulled = buffer[i + fl];
61 buffer[i + fl] = '\0';
63 key = g_string_chunk_insert_const(sc, buffer + i);
65 stat = g_hash_table_lookup(ht, key);
66 if (!stat)
68 stat = g_trash_stack_pop(ts);
69 if (!stat)
71 int j;
73 stat = malloc(sizeof (stat_t) * MAX_ELM);
74 g_ptr_array_add(roots, stat);
76 for (j = 1; j < MAX_ELM; ++j)
77 g_trash_stack_push(ts, stat + j);
79 stat->stat = 1;
80 stat->key = key;
82 g_hash_table_insert(ht, key, stat);
84 else
85 stat->stat++;
87 buffer[i + fl] = nulled;
90 return buflen - fl + 1;
93 static int
94 cmp_func(gconstpointer a, gconstpointer b)
96 const stat_t *left = a;
97 const stat_t *right = b;
99 return right->stat - left->stat;
102 static void
103 sorted_list(gpointer key, gpointer value, gpointer user_data)
105 stat_t *data = value;
106 GList **lst = user_data;
108 *lst = g_list_insert_sorted(*lst, data, cmp_func);
111 static void
112 display_stat(gpointer data, gpointer user_data)
114 long *total = user_data;
115 stat_t *st = data;
117 printf("%s %.3f\n", st->key, 100 * (float) st->stat / *total);
120 void
121 write_frequencies (int fl, char *buffer, long buflen, GTrashStack **ts, GPtrArray *roots)
123 GStringChunk *sc;
124 GHashTable *ht;
125 GList *lst;
126 long total;
128 ht = g_hash_table_new_full(g_str_hash, g_str_equal, NULL /* free key */, NULL /* free value */);
129 sc = g_string_chunk_new(buflen);
130 lst = NULL;
132 total = generate_frequencies (fl, buffer, buflen, ht, ts, roots, sc);
134 if (!total) goto on_error;
136 g_hash_table_foreach(ht, sorted_list, &lst);
137 g_list_foreach(lst, display_stat, &total);
138 g_list_free(lst);
140 on_error:
141 g_hash_table_destroy(ht);
142 g_string_chunk_free(sc);
145 void
146 write_count (char *searchFor, char *buffer, long buflen, GTrashStack **ts, GPtrArray *roots)
148 GStringChunk *sc;
149 GHashTable *ht;
150 stat_t *result;
151 GList *lst;
152 long total;
153 long fl;
155 fl = strlen(searchFor);
157 ht = g_hash_table_new_full(g_str_hash, g_str_equal, NULL /* free key */, NULL /* free value */);
158 sc = g_string_chunk_new(buflen);
159 lst = NULL;
160 result = NULL;
162 total = generate_frequencies (fl, buffer, buflen, ht, ts, roots, sc);
164 if (!total) goto on_error;
166 result = g_hash_table_lookup(ht, searchFor);
168 on_error:
169 printf("%ld\t%s\n", result ? result->stat : 0, searchFor);
171 g_hash_table_destroy(ht);
172 g_string_chunk_free(sc);
176 main ()
178 char buffer[4096];
179 GTrashStack *ts;
180 GPtrArray *roots;
181 GString *stuff;
182 gchar *s;
183 int len;
185 roots = g_ptr_array_new();
186 ts = NULL;
188 while (fgets(buffer, sizeof (buffer), stdin))
189 if (strncmp(buffer, ">THREE", 6) == 0)
190 break;
192 stuff = g_string_new(NULL);
194 while (fgets(buffer, sizeof (buffer), stdin))
196 size_t sz;
198 if (buffer[0] == '>')
199 break;
201 sz = strlen(buffer);
202 if (buffer[sz - 1] == '\n')
203 --sz;
205 stuff = g_string_append_len(stuff, buffer, sz);
208 stuff = g_string_ascii_up(stuff);
209 len = stuff->len;
210 s = g_string_free(stuff, FALSE);
212 write_frequencies(1, s, len, &ts, roots);
213 printf("\n");
214 write_frequencies(2, s, len, &ts, roots);
215 printf("\n");
216 write_count("GGT", s, len, &ts, roots);
217 write_count("GGTA", s, len, &ts, roots);
218 write_count("GGTATT", s, len, &ts, roots);
219 write_count("GGTATTTTAATT", s, len, &ts, roots);
220 write_count("GGTATTTTAATTTATAGT", s, len, &ts, roots);
222 free(s);
224 g_ptr_array_foreach(roots, free, NULL);
225 g_ptr_array_free(roots, TRUE);
227 return 0;