Exemplo n.º 1
0
func main() {

	min := 113
	max := int(^uint32(0) >> 1)

	for currentSum := min; currentSum <= max; currentSum++ {
		var a Sum
		var initVector vector.IntVector

		for i := 0; i < currentSum; i += 1 {
			initVector.Push(1)
		}

		a.Init(initVector)

		count := recursiveMerging(a, currentSum, 0)

		fmt.Println(currentSum, count)

		if count%1000000 == 0 {
			panic("")
		}
	}

}
Exemplo n.º 2
0
// Partially mapped crossover.
func (a *GAOrderedIntGenome) Crossover(bi GAGenome, p1, p2 int) (GAGenome, GAGenome) {
	ca := a.Copy().(*GAOrderedIntGenome)
	b := bi.(*GAOrderedIntGenome)
	cb := b.Copy().(*GAOrderedIntGenome)
	copy(ca.Gene[p1:p2+1], b.Gene[p1:p2+1])
	copy(cb.Gene[p1:p2+1], a.Gene[p1:p2+1])
	//Proto child needs fixing
	amap := new(vector.IntVector)
	bmap := new(vector.IntVector)
	for i := p1; i <= p2; i++ {
		ma, found := ca.pmxmap(ca.Gene[i], p1, p2)
		if found {
			amap.Push(ma)
			if bmap.Len() > 0 {
				i1 := amap.Pop()
				i2 := bmap.Pop()
				ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
			}
		}
		mb, found := cb.pmxmap(cb.Gene[i], p1, p2)
		if found {
			bmap.Push(mb)
			if amap.Len() > 0 {
				i1 := amap.Pop()
				i2 := bmap.Pop()
				ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
			}
		}
	}
	ca.Reset()
	cb.Reset()
	return ca, cb
}
Exemplo n.º 3
0
func (t *GoTracker) dead() []int {
	dead := new(vector.IntVector)
	cp := t.Copy().(*GoTracker)
	color := BLACK
	move := 0
	for {
		vertex := cp.weights.Rand(color)
		cp.Play(color, vertex)
		move++
		if move > 3*t.sqsize || cp.Winner() != EMPTY {
			break
		}
		color = Reverse(color)
	}
	for i := 0; i < t.sqsize; i++ {
		if t.board[i] != EMPTY && cp.board[i] != t.board[i] {
			dead.Push(i)
		}
	}
	stones := make([]int, dead.Len())
	for i := 0; i < dead.Len(); i++ {
		stones[i] = dead.At(i)
	}
	return stones
}
Exemplo n.º 4
0
// capture any points connected to vertex, resetting their parent, rank and weight, and liberties
// check if chains adjacent to captured are now out of atari
func (t *GoTracker) capture(vertex int) *vector.IntVector {
	// do a linear search for connected points
	captured := new(vector.IntVector)
	for i := 0; i < t.sqsize; i++ {
		if find(i, t.parent) == vertex {
			captured.Push(i)
		}
	}
	// reset
	for i := 0; i < captured.Len(); i++ {
		capture := captured.At(i)
		t.parent[capture] = capture
		t.rank[capture] = 1
		t.liberties[capture][0] = 0
		t.liberties[capture][1] = 0
		t.board[capture] = EMPTY
		t.weights.Set(BLACK, capture, INIT_WEIGHT)
		t.weights.Set(WHITE, capture, INIT_WEIGHT)
	}
	// update liberties
	for i := 0; i < captured.Len(); i++ {
		capture := captured.At(i)
		for j := 0; j < 4; j++ {
			adj := t.adj[capture][j]
			if adj != -1 {
				root := find(adj, t.parent)
				t.liberties[root][0] |= t.mask[capture][0]
				t.liberties[root][1] |= t.mask[capture][1]
			}
		}
	}
	return captured
}
Exemplo n.º 5
0
// renvoie la liste des trolls avec qui le troll passé a un partage actif
func (store *MysqlStore) GetPartageurs(db *mysql.Client, trollId int) ([]int, os.Error) {
	st := strconv.Itoa(trollId)
	sql := "select troll_a, troll_b from partage where (troll_a=" + st + " or troll_b=" + st + ") and statut_a='on' and statut_b='on'"
	err := db.Query(sql)
	if err != nil {
		return nil, err
	}
	result, err := db.UseResult()
	if err != nil {
		return nil, err
	}
	defer result.Free()

	amis := new(vector.IntVector)
	for {
		row := result.FetchRow()
		if row == nil {
			break
		}
		r0 := fieldAsInt(row[0])
		r1 := fieldAsInt(row[1])
		if r0 == trollId {
			amis.Push(r1)
		} else {
			amis.Push(r0)
		}
	}

	return *amis, nil

}
Exemplo n.º 6
0
func GetPrimeFactors(number int) (*vector.IntVector, *vector.IntVector) {
	numbers := new(vector.IntVector)
	counts := new(vector.IntVector)

	primesChannel := GetPrimes()
	result := number
	for {
		prime := <-primesChannel

		if number < prime {
			break
		}

		if result%prime == 0 {
			numbers.Push(prime)
			count := 1

			for result = result / prime; result%prime == 0; result = result / prime {
				count++
			}

			counts.Push(count)
		}
	}

	return numbers, counts
}
Exemplo n.º 7
0
func (d *debugHistogram) PrintSorted() {
	if !printDebugOutput {
		return
	}
	if len(d.mapping) == 0 {
		return
	}
	var scores vector.IntVector
	inverseMapping := make(map[int]string)
	sum := 0
	for key, value := range d.mapping {
		inverseMapping[value] = key
		scores.Push(value)
		sum += value
	}
	scoresArray := sort.IntArray(scores)
	scoresArray.Sort()

	fmt.Fprintf(os.Stderr, "Debug histogram - sorted:\n")
	length := len(scoresArray)
	for i := 0; i < length; i++ {
		current := scoresArray[length-i-1]
		fmt.Fprintf(os.Stderr, "%s: %d (%2.1f%%)\n", inverseMapping[current], current, float(current)/float(sum)*100)
	}
}
Exemplo n.º 8
0
func removeZeros(c []int) []int {
	var ret vector.IntVector
	for _, v := range c {
		if v != 0 {
			ret.Push(v)
		}
	}
	return ret
}
Exemplo n.º 9
0
func BenchmarkIntVector(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var vec vector.IntVector
		for j := 0; j < vectorLength; j++ {
			vec.Push(j)
		}
		for j := 0; j < vectorLength; j++ {
			val := vec.At(j)
			val++
		}
	}
}
Exemplo n.º 10
0
func generatePrimes(min int, max int) vector.IntVector {

	var ret vector.IntVector

	for n := min; n <= max; n++ {
		if isPrime(n) {
			ret.Push(n)
		}
	}

	return ret
}
Exemplo n.º 11
0
func (connection *Connection) SearchPrefix(query string) (*vector.IntVector, os.Error) {
	var count _C_int
	resp := C.tcidbsearch(connection.Dystopia, C.CString(query), C.x_prefix(), &count)
	fmt.Printf("searched for %v, num results = %d, resp = %v\n", query, count, resp)

	var result vector.IntVector
	for i := 0; i < int(count); i++ {
		result.Push(int(C.x_get_result_item(resp, _C_int(i))))
	}

	//        return &result, nil;
	return &result, nil
}
// find all occurrences of s in source; report at most n occurences
func find(src, s string, n int) []int {
	var res vector.IntVector
	if s != "" && n != 0 {
		// find at most n occurrences of s in src
		for i := -1; n < 0 || len(res) < n; {
			j := strings.Index(src[i+1:], s)
			if j < 0 {
				break
			}
			i += j + 1
			res.Push(i)
		}
	}
	return res
}
Exemplo n.º 13
0
func readLexeme(state *LexerState,
	rune int,
	size int,
	err os.Error,
	predicate func(rune int) bool,
	reader *bufio.Reader) (string, os.Error) {
	var runes vector.IntVector
	for predicate(rune) && size > 0 && err == nil {
		runes.Push(rune)
		rune, size, err = readRune(state, reader)
	}
	if err != os.EOF {
		unreadRune(state, reader, rune)
	}
	return lexeme(runes), err
}
Exemplo n.º 14
0
func TestGetPrimeFactorsOfANumber(t *testing.T) {
	expectedPrimes := new(vector.IntVector)
	for _, integer := range []int{2, 3, 5} {
		expectedPrimes.Push(integer)
	}

	expectedCounts := new(vector.IntVector)
	for _, integer := range []int{1, 2, 1} {
		expectedCounts.Push(integer)
	}

	gotPrimes, gotCounts := GetPrimeFactors(90)
	if !AreTwoIntVectorEquals(expectedPrimes, gotPrimes) || !AreTwoIntVectorEquals(expectedCounts, gotCounts) {
		t.Errorf("Expected primes %q. Got primes %q\nExpected count %q. Got count %q", expectedPrimes, gotPrimes)
	}
}
Exemplo n.º 15
0
func generatePrimes(min int, max int) []int {

	var primes vector.IntVector

	for n := min; n <= max; n++ {
		if isPrime(n) {
			primes.Push(n)
		}
	}

	ret := make([]int, len(primes))
	for k, v := range primes {
		ret[k] = v
	}

	return ret
}
Exemplo n.º 16
0
func main() {

	var a Sum
	var initVector vector.IntVector

	for i := 0; i < 100; i += 1 {
		initVector.Push(1)
	}

	a.Init(initVector)

	var count uint64 = 0

	recursiveMerging(a, 100, 0, &count)

	fmt.Println(count - 1)
}
Exemplo n.º 17
0
func TestGridProductOfTheMainDiagonal(t *testing.T) {
	grid := new(vector.Vector)

	for i := 0; i < 5; i++ {
		line := new(vector.IntVector)
		for j := 0; j < 5; j++ {
			if i == j {
				line.Push(10)
			} else {
				line.Push(1)
			}
		}

		grid.Push(line)
	}

	AssertGridProduct(t, grid, 10000)
}
Exemplo n.º 18
0
func TestGridProductOfVerticalLine(t *testing.T) {
	grid := new(vector.Vector)

	for i := 0; i < 5; i++ {
		line := new(vector.IntVector)
		for j := 0; j < 5; j++ {
			if j == 0 {
				line.Push(10)
			} else {
				line.Push(1)
			}
		}

		grid.Push(line)
	}

	AssertGridProduct(t, grid, 10000)
}
Exemplo n.º 19
0
func ReadGridFromString(input string) *vector.Vector {
	grid := new(vector.Vector)
	lines := strings.Split(input, "\n")

	for _, line := range lines {
		lineVector := new(vector.IntVector)

		numbers := strings.Split(line, " ")
		for _, number := range numbers {
			number, _ := strconv.Atoi(number)
			lineVector.Push(number)
		}

		grid.Push(lineVector)
	}

	return grid
}
Exemplo n.º 20
0
func continuedFraction(a float64, dSquaredFloorTimesTwo int) vector.IntVector {

	aFloor := int(math.Floor(a))

	var ret vector.IntVector

	if dSquaredFloorTimesTwo == aFloor {
		return ret
	}

	ret.Push(aFloor)

	nextRet := continuedFraction(1/(a-float64(aFloor)), dSquaredFloorTimesTwo)

	ret.AppendVector(&nextRet)

	return ret
}
Exemplo n.º 21
0
func TestConvertGridToString(t *testing.T) {
	firstLine := new(vector.IntVector)
	firstLine.Push(10)
	firstLine.Push(15)

	secondLine := new(vector.IntVector)
	secondLine.Push(8)
	secondLine.Push(5)

	grid := new(vector.Vector)
	grid.Push(firstLine)
	grid.Push(secondLine)

	expected := "10 15\n8 5"
	got := ConvertGridToString(grid)

	if expected != got {
		t.Errorf("Expected: %q\nGot: %q", expected, got)
	}
}
Exemplo n.º 22
0
func TestConvertGridFromString(t *testing.T) {
	grid := "10 15\n8 5"

	firstLine := new(vector.IntVector)
	firstLine.Push(10)
	firstLine.Push(15)

	secondLine := new(vector.IntVector)
	secondLine.Push(8)
	secondLine.Push(5)

	expected := new(vector.Vector)
	expected.Push(firstLine)
	expected.Push(secondLine)
	got := ReadGridFromString(grid)

	if !AreGridEquals(expected, got) {
		t.Errorf("Expected: %q\nGot: %q", expected, got)
	}
}
Exemplo n.º 23
0
// Specialized function for TeX-style hyphenation patterns.  Accepts strings of the form '.hy2p'.
// The value it stores is of type vector.IntVector
func (p *Trie) AddPatternString(s string) {
	v := new(vector.IntVector)

	// precompute the Unicode rune for the character '0'
	rune0, _ := utf8.DecodeRune([]byte{'0'})

	strLen := len(s)

	// Using the range keyword will give us each Unicode rune.
	for pos, rune := range s {
		if unicode.IsDigit(rune) {
			if pos == 0 {
				// This is a prefix number
				v.Push(rune - rune0)
			}

			// this is a number referring to the previous character, and has
			// already been handled
			continue
		}

		if pos < strLen-1 {
			// look ahead to see if it's followed by a number
			next := int(s[pos+1])
			if unicode.IsDigit(next) {
				// next char is the hyphenation value for this char
				v.Push(next - rune0)
			} else {
				// hyphenation for this char is an implied zero
				v.Push(0)
			}
		} else {
			// last character gets an implied zero
			v.Push(0)
		}
	}

	pure := strings.Map(func(rune int) int {
		if unicode.IsDigit(rune) {
			return -1
		}
		return rune
	},
		s)
	leaf := p.addRunes(strings.NewReader(pure))
	if leaf == nil {
		return
	}

	leaf.value = v
}
Exemplo n.º 24
0
func TreeSort(data []int) {
	if len(data) <= 1 {
		return
	}
	root := NewTree(nil, data[0]) //root node
	for i := 1; i < len(data); i++ {
		root.NewNode(data[i]) //filling up the tree
	}
	var sorted vector.IntVector
	var sortIntoArray func(*Tree) //declared to use recursively
	sortIntoArray = func(node *Tree) {
		if node == nil {
			return
		}
		sortIntoArray(node.left)
		sorted.Push(node.val)
		sortIntoArray(node.right)
	}
	sortIntoArray(root)
	copy(data, sorted.Data()) //copy the sorted to the source
}
Exemplo n.º 25
0
func CountColor(pngR io.Reader) int {
	/* modify here */
	// uniq := new (vector.Vector[uint32])
	var colorVector vector.Vector
	var rVector vector.IntVector
	var gVector vector.IntVector
	var bVector vector.IntVector
	im, _ := png.Decode(pngR)
	for y := 0; y < im.Bounds().Dy(); y++ {
		for x := 0; x < im.Bounds().Dx(); x++ {
			color := im.At(x, y)
			unique := true
			r, g, b, _ := color.RGBA()
			for i := 0; i < colorVector.Len(); i++ {
				if r == uint32(rVector.At(i)) &&
					g == uint32(gVector.At(i)) &&
					b == uint32(bVector.At(i)) {
					unique = false
				}
			}
			if unique == true {
				colorVector.Push(color)
				rVector.Push(int(r))
				gVector.Push(int(g))
				bVector.Push(int(b))
			}
		}
	}
	return colorVector.Len()
}
Exemplo n.º 26
0
// Lookup returns an unsorted list of at most n indices where the byte string s
// occurs in the indexed data. If n < 0, all occurrences are returned.
// The result is nil if s is empty, s is not found, or n == 0.
// Lookup time is O((log(N) + len(result))*len(s)) where N is the
// size of the indexed data.
//
func (x *Index) Lookup(s []byte, n int) []int {
	var res vector.IntVector

	if len(s) > 0 && n != 0 {
		// find matching suffix index i
		i := x.search(s)
		// x.at(i) <= s < x.at(i+1)

		// ignore the first suffix if it is < s
		if i < len(x.sa) && bytes.Compare(x.at(i), s) < 0 {
			i++
		}

		// collect the following suffixes with matching prefixes
		for (n < 0 || len(res) < n) && i < len(x.sa) && bytes.HasPrefix(x.at(i), s) {
			res.Push(x.sa[i])
			i++
		}
	}

	return res
}
Exemplo n.º 27
0
func distinct(list vector.StringVector) [][2]string {
	if len(list) == 0 {
		return nil
	}
	var keys vector.StringVector
	var vals vector.IntVector
	for _, l := range list {
		index := search(keys, l)
		if index == -1 {
			keys.Push(l)
			vals.Push(1)
		} else {
			vals.Set(index, vals.At(index)+1)
		}
	}
	m := make([][2]string, len(keys))
	for i, k := range keys {
		m[i][0] = k
		m[i][1] = fmt.Sprint(vals.At(i))
	}
	return m
}
Exemplo n.º 28
0
// Adapted/ported the neat recipe/method by Manuel Ebert at
//   http://portwempreludium.tumblr.com/post/13108758604/instagram-unshredding
func FindShredWidth(img *image.Gray) {
	bounds := img.Bounds()
	width := bounds.Max.X
	height := bounds.Max.Y
	avg := make([]float64, width)
	for x := 0; x < width; x++ {
		tmp := 0
		for y := 0; y < height; y++ {
			tmp += int(img.Pix[x+y*img.Stride])
		}
		avg[x] = float64(tmp) / float64(height)
	}
	diff := make([]float64, width)
	for z := 1; z < width; z++ {
		diff[z] = math.Fabs(avg[z] - avg[z-1])
	}
	diff[0] = 0.0
	shred_width = 0
	for threshold := 1; threshold < 255; threshold++ {
		if shred_width >= 5 {
			break
		}
		var boundaries vector.IntVector
		for i, _ := range diff {
			if diff[i] > float64(threshold) {
				boundaries.Push(i)
			}
		}
		if len(boundaries) == 0 {
			shred_width = 0
			continue
		}
		tmp := boundaries[0]
		for b := 1; b < len(boundaries); b++ {
			tmp = gcd(tmp, boundaries[b])
		}
		shred_width = tmp
	}
}
Exemplo n.º 29
0
func (t *GoTracker) playHeuristicMove(color byte) int {
	if len(t.atari[color]) > 0 {
		// play a random saving move
		saves := new(vector.IntVector)
		for _, last_liberty := range t.atari[color] {
			if t.weights.Get(color, last_liberty) > 0 {
				saves.Push(last_liberty)
			}
		}
		if saves.Len() > 0 {
			return saves.At(rand.Intn(saves.Len()))
		}
	}
	if len(t.atari[Reverse(color)]) > 0 {
		// play a random capture move
		captures := new(vector.IntVector)
		for _, last_liberty := range t.atari[Reverse(color)] {
			captures.Push(last_liberty)
		}
		return captures.At(rand.Intn(captures.Len()))
	}
	return -1
}
Exemplo n.º 30
0
// Sorts channels in random order
func (dbs *DBS) RandomChan() {

	var SortCh vector.IntVector

	for i := 0; i < NCh; i++ {
		SortCh.Push(i)
		dbs.RndCh[i] = i
	}

	//randomizesd reserved top canals
	/*	for i := 10; i > 1; i-- {
			j := dbs.Rgen.Intn(i) + NCh-10
			SortCh.Swap(NCh-10+i-1, j)
			dbs.RndCh[NCh-10+i-1] =SortCh.Pop()
		}
		dbs.RndCh[NCh-10]=SortCh.Pop()
	*/
	//randomizes other canals
	/*	for i := NCh - 11; i > NChRes; i-- {
			j := dbs.Rgen.Intn(i-NChRes) + NChRes
			SortCh.Swap(i, j)
			dbs.RndCh[i] =SortCh.Pop()
		}
		dbs.RndCh[NChRes] = SortCh.Pop()
		dbs.RndCh[0] = 0
	*/
	//fmt.Println(dbs.RndCh);

	for i := NCh - 1; i > NChRes; i-- {
		j := dbs.Rgen.Intn(i-NChRes) + NChRes
		SortCh.Swap(i, j)
		dbs.RndCh[i] = SortCh.Pop()
	}
	dbs.RndCh[NChRes] = SortCh.Pop()
	dbs.RndCh[0] = 0

}