<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Bioinformatika
{
    class RabinKarp
    {

        private String genome;
        private String pattern;
        private int patternSize;
        private long prime;

        private int genomeHash;
        private int patternHash;

        public RabinKarp(String genome, String pattern)
        {
            this.genome = genome;
            this.pattern = pattern;
            this.patternSize = pattern.Length;
            this.prime = 11;

            hash(genome, pattern, patternSize, prime);
        }

        public static void Main(string[] args)
        {
            String genome = "ACAGTACGUTACGA";
            String pattern = "CGU";

            //var filePath = args[0];
            //ReadGenomeFromFile(filePath);

            RabinKarp rc = new RabinKarp(genome, pattern);
            var index = rc.search();
            Console.WriteLine("Done");
        }


        static void ReadGenomeFromFile(string filePath)
        {
            using (var reader = new StreamReader(filePath))
            {
                var line = "";
                do
                {
                    line = reader.ReadLine();
                    if (string.IsNullOrEmpty(line))
                        continue;
                    //line = line.Trim();
                    if (string.IsNullOrEmpty(line))
                        continue;
                    if (line[0] == '>')
                    {
                        Console.WriteLine("found '>'");
                        continue;
                    }
                    Console.WriteLine("line ->  " + line);
                    
                } while (!reader.EndOfStream);
            }
        }

        private int search() 
        {
            /*
            if (substring == None)
                return -1;
            if (substring == "")
                return -1;
            */
            int genomeSize = genome.Length;
            if (genomeSize < patternSize) return genomeSize;
            Console.Write("genome length: ");
            Console.WriteLine(genome.Length.ToString());

            // check for match at offset 0
            if (genomeHash == patternHash) {
                if (verifyHash(0))
                    return 0;
            }
            int h = (int)(Math.Pow(prime, patternSize - 1));
            Console.WriteLine("genomeHash: 0 " + genomeHash);
            for (int i = patternSize; i < genomeSize; i++)
            {
                genomeHash = (int)(prime*(genomeHash - h * (int)genome[i - patternSize]) + (int)genome[i]);
                Console.Write("genomeHash: " + i + " ");
                Console.WriteLine(genomeHash.ToString());
                if (genomeHash == patternHash)
                {
                    if (verifyHash(i - patternSize + 1)) 
                    {
                        Console.WriteLine("index: " + i);
                        return i;
                    }
                }
            }
            return 0;
        }

        private bool verifyHash(int index)
        {
            for (int j = 0; j < patternSize; j++)
                if (genome[index + j] != pattern[j]) return false;
            return true;
        }


        private void hash(String genome, String pattern, int patternSize, long prime)
        {
            for (int i = 0; i < patternSize; i++)
            {
                genomeHash += (int)((int)genome[i]*(Math.Pow(prime, (patternSize - i - 1))));
                patternHash += (int)((int)pattern[i]*(Math.Pow(prime, (patternSize - i - 1))));
            }
            Console.WriteLine(genomeHash.ToString());
            Console.WriteLine(patternHash.ToString());

        }

	}
}<commit_msg>promjena u funkciji za izracun hash vrijednosti<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Bioinformatika
{
    static class RabinKarp
    {
        const ulong BASE = 11;
        //const ulong HASH_SIZE = (2 << 20);

        private static Tuple<ulong,ulong> CalculateInitialHash(string genome, string pattern, ulong patternSize, ulong pBase)
        {
            ulong genomeHash = 0;
            ulong patternHash = 0;
            ulong basePower = 1;
            for (int i = (int)patternSize - 1; i >= 0; i--, basePower*= pBase)
            {
                genomeHash += (genome[i] * basePower);// % HASH_SIZE;
                patternHash += (pattern[i] * basePower);// % HASH_SIZE;
            }
            return new Tuple<ulong, ulong>(genomeHash, patternHash);
        }

        private static ulong CalculateFactor(ulong patternSize, ulong pBase)
        {
            ulong power = 1;
            for (int i = 0; i < (int) patternSize - 1; i++)
                power = (power * pBase);// % HASH_SIZE;
            return power;
        }
	}
}
{'fraction_non_alphanumeric': 0.10682492581602374, 'fraction_numerical': 0.005934718100890208, 'mean_word_length': 1.377056887635167, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2056577', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1285, 'n_words': 328}