<commit_before>package rabinkarp

import "bytes"
import "fmt"

type Needle struct {
  text []byte
  length int
  hash uint
  m uint
  }

const prime uint = 101
const UINT_MAX = ^uint(0)

func hash(text []byte) (h uint, m uint) {
  var tmp uint = 1
  m = 1
  h = 0
  for i := len(text)-1; i >= 0; i-- {
    tmp = m
    h = (h + ((uint(text[i]) * m) % UINT_MAX)) % UINT_MAX
    m = (m * prime) % UINT_MAX
    }
  return h, tmp
  }

func update(hash uint, m uint, remove byte, add byte)  uint {
  hash = (hash - (((m * uint(remove)) % UINT_MAX) % UINT_MAX)) % UINT_MAX
  hash = (hash * prime) % UINT_MAX
  hash = (hash + uint(add)) % UINT_MAX
  return hash
  }

func NewNeedle(needle []byte) *Needle {
  n := new(Needle)
  n.text = needle
  n.length = len(needle)
  n.hash, n.m = hash(needle)
  return n
  }

func (self *Needle) Index(haystack []byte) (index int) {
  fmt.Println("Looking for: ", self.hash)
  l := len(haystack)
  fmt.Println("Length: ", l)
  max := l - self.length
  fmt.Println("Max: ", max)
  if l < self.length {
    return -1
    }
  h, _ := hash(haystack[0:self.length])
  for i := 0; i < max; i++ {
    fmt.Println("Current hash: ", h)
    if h == self.hash && bytes.Compare(haystack[i:i+self.length], self.text) == 0 {
      return i
      }
    h = update(h, self.m, haystack[i], haystack[i+self.length])
    }
  return -1
  }

/*
Return the index of the first instance of `needle` in `haystack`,
or -1 if not found.
*/
func Index(haystack, needle string) int { return NewNeedle([]byte(needle)).Index([]byte(haystack)) }<commit_msg>gofmt<commit_after>package rabinkarp

import "bytes"
import "fmt"

type Needle struct {
	text   []byte
	length int
	hash   uint
	m      uint
}

const prime uint = 101
const UINT_MAX = ^uint(0)

func hash(text []byte) (h uint, m uint) {
	var tmp uint = 1
	m = 1
	h = 0
	for i := len(text) - 1; i >= 0; i-- {
		tmp = m
		h = (h + ((uint(text[i]) * m) % UINT_MAX)) % UINT_MAX
		m = (m * prime) % UINT_MAX
	}
	return h, tmp
}

func update(hash uint, m uint, remove byte, add byte) uint {
	hash = (hash - (((m * uint(remove)) % UINT_MAX) % UINT_MAX)) % UINT_MAX
	hash = (hash * prime) % UINT_MAX
	hash = (hash + uint(add)) % UINT_MAX
	return hash
}

func NewNeedle(needle []byte) *Needle {
	n := new(Needle)
	n.text = needle
	n.length = len(needle)
	n.hash, n.m = hash(needle)
	return n
}

func (self *Needle) Index(haystack []byte) (index int) {
	fmt.Println("Looking for: ", self.hash)
	l := len(haystack)
	fmt.Println("Length: ", l)
	max := l - self.length
	fmt.Println("Max: ", max)
	if l < self.length {
		return -1
	}
	h, _ := hash(haystack[0:self.length])
	for i := 0; i < max; i++ {
		fmt.Println("Current hash: ", h)
		if h == self.hash && bytes.Compare(haystack[i:i+self.length], self.text) == 0 {
			return i
		}
		h = update(h, self.m, haystack[i], haystack[i+self.length])
	}
	return -1
}

/*
Return the index of the first instance of `needle` in `haystack`,
or -1 if not found.
*/
func Index(haystack, needle string) int { return NewNeedle([]byte(needle)).Index([]byte(haystack)) }
{'fraction_non_alphanumeric': 0.16683087027914614, 'fraction_numerical': 0.009852216748768473, 'mean_word_length': 3.105121293800539, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1817708', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1198, 'n_words': 351}