ba1d.c3 (921B) - raw


      1 // Pattern matching
      2 
      3 module ba1d;
      4 import std::io;
      5 import std::io::file;
      6 import std::collections;
      7 
      8 fn int main(String[] args) {
      9 	if (args.len != 2) {
     10 		io::eprintn("Please supply path to data file");
     11 		return 1;
     12 	}
     13 	File f = file::open(args[1], "rb")!!;
     14 	String pattern = io::treadline(&f)!!;
     15 	String genome = io::treadline(&f)!!;
     16 	int[] idxs = pattern_matching(pattern, genome);
     17 	foreach (idx : idxs) {
     18 		io::printf("%d ", idx);
     19 	}
     20 	return 0;
     21 }
     22 
     23 fn int[] pattern_matching(String pattern, String genome, Allocator alloc = allocator::heap()) {
     24 	if (pattern.len == 0 || genome.len == 0) return {};
     25 	List{int} res;
     26 	@pool() {
     27 		res.tinit();
     28 		for (int i = 0; i <= genome.len - pattern.len; i += 1) {
     29 			if (genome[i:pattern.len] == pattern) {
     30 				res.push(i);
     31 			}
     32 		}
     33 		return res.to_array(alloc);
     34 	};
     35 }
     36 
     37 fn void test_pattern_matching() @test {
     38 	assert(pattern_matching("ATAT", "GATATATGCATATACTT", tmem) == {1, 3, 9});
     39 }
     40 
     41