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.
36 typedef struct stat_s stat_t
;
43 #define MAX_ELM (8192 / sizeof (stat_t))
46 generate_frequencies (int fl
, char *buffer
, long buflen
,
47 GHashTable
*ht
, GTrashStack
**ts
, GPtrArray
*roots
, GStringChunk
*sc
)
52 if (fl
> buflen
) return 0;
53 if (fl
== 0) return 0;
55 for (i
= 0; i
< buflen
- fl
+ 1; ++i
)
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
);
68 stat
= g_trash_stack_pop(ts
);
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
);
82 g_hash_table_insert(ht
, key
, stat
);
87 buffer
[i
+ fl
] = nulled
;
90 return buflen
- fl
+ 1;
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
;
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
);
112 display_stat(gpointer data
, gpointer user_data
)
114 long *total
= user_data
;
117 printf("%s %.3f\n", st
->key
, 100 * (float) st
->stat
/ *total
);
121 write_frequencies (int fl
, char *buffer
, long buflen
, GTrashStack
**ts
, GPtrArray
*roots
)
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
);
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
);
141 g_hash_table_destroy(ht
);
142 g_string_chunk_free(sc
);
146 write_count (char *searchFor
, char *buffer
, long buflen
, GTrashStack
**ts
, GPtrArray
*roots
)
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
);
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
);
169 printf("%ld\t%s\n", result
? result
->stat
: 0, searchFor
);
171 g_hash_table_destroy(ht
);
172 g_string_chunk_free(sc
);
185 roots
= g_ptr_array_new();
188 while (fgets(buffer
, sizeof (buffer
), stdin
))
189 if (strncmp(buffer
, ">THREE", 6) == 0)
192 stuff
= g_string_new(NULL
);
194 while (fgets(buffer
, sizeof (buffer
), stdin
))
198 if (buffer
[0] == '>')
202 if (buffer
[sz
- 1] == '\n')
205 stuff
= g_string_append_len(stuff
, buffer
, sz
);
208 stuff
= g_string_ascii_up(stuff
);
210 s
= g_string_free(stuff
, FALSE
);
212 write_frequencies(1, s
, len
, &ts
, roots
);
214 write_frequencies(2, s
, len
, &ts
, roots
);
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
);
224 g_ptr_array_foreach(roots
, free
, NULL
);
225 g_ptr_array_free(roots
, TRUE
);