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
}
// 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
}
Exemple #3
0
func (quartiles *Quartiles) Rollup(time int64, key string, samples *vector.IntVector) {
	if samples.Len() < 2 {
		return
	}
	sort.Sort(samples)
	lo := samples.At(0)
	hi := samples.At(samples.Len() - 1)
	number := samples.Len()
	lo_c := number / 2
	hi_c := number - lo_c
	data := &QuartilesItem{}
	if lo_c > 0 && hi_c > 0 {
		lo_samples := samples.Slice(0, lo_c)
		hi_samples := samples.Slice(lo_c, hi_c)
		lo_sum := 0
		hi_sum := 0
		lo_samples.Do(func(elem interface{}) { lo_sum += elem.(int) })
		hi_samples.Do(func(elem interface{}) { hi_sum += elem.(int) })
		q1 := lo_sum / lo_c
		q2 := (lo_sum + hi_sum) / (lo_c + hi_c)
		q3 := hi_sum / hi_c

		data.time = time
		data.lo = lo
		data.q1 = q1
		data.q2 = q2
		data.q3 = q3
		data.hi = hi
		data.total = number
	}
	quartiles.save(time, key, data)
}
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++
		}
	}
}
Exemple #5
0
func AreTwoIntVectorEquals(vector1, vector2 *vector.IntVector) bool {
	if vector1.Len() != vector2.Len() {
		return false
	}

	for i := 0; i < vector1.Len(); i++ {
		if vector1.At(i) != vector2.At(i) {
			return false
		}
	}

	return true
}
Exemple #6
0
func checkValues(trie *Trie, s string, v *vector.IntVector, t *testing.T) {
	value, ok := trie.GetValue(s)
	values := value.(*vector.IntVector)
	if !ok {
		t.Fatalf("No value returned for string '%s'", s)
	}

	if values.Len() != v.Len() {
		t.Fatalf("Length mismatch: Values for '%s' should be %v, but got %v", s, *v, *values)
	}
	for i := 0; i < values.Len(); i++ {
		if values.At(i) != v.At(i) {
			t.Fatalf("Content mismatch: Values for '%s' should be %v, but got %v", s, *v, *values)
		}
	}
}
Exemple #7
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()
}
Exemple #8
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
}
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
}
func generateFactorizedN(primes vector.IntVector, pos int, max int, currentN int, currentFactors map[int]int, returnChannel chan<- nWithFactors) {

	for ; pos < len(primes); pos++ {

		prime := primes.At(pos)

		pToThePowerOfKTimesN := currentN

		for k := 1; ; k++ {

			if int64(pToThePowerOfKTimesN)*int64(prime) > int64(max) {
				break
			}

			pToThePowerOfKTimesN *= prime

			copiedCurrentFactors := copyMapIntInt(currentFactors)
			copiedCurrentFactors[prime] = k

			returnChannel <- nWithFactors{pToThePowerOfKTimesN, copiedCurrentFactors}
			generateFactorizedN(primes, pos+1, max, pToThePowerOfKTimesN, copiedCurrentFactors, returnChannel)
		}
	}
}
Exemple #11
0
// apply color to vertex, modifying board and updating liberties of any go_adj strings
func (t *GoTracker) Play(color byte, vertex int) {
	if vertex != -1 {
		t.passes = 0

		if t.koVertex != -1 {
			t.weights.Set(t.koColor, t.koVertex, INIT_WEIGHT)
			t.koVertex = -1
			t.koColor = EMPTY
		}

		if t.superko {
			if t.history.Len() == 0 {
				t.history.Push(*NewHash(t.boardsize))
			}
			cp := t.Copy()
			cp.(*GoTracker).superko = false
			cp.Play(color, vertex)
			t.history.Push(*MakeHash(cp))
		}

		// modify the board
		t.board[vertex] = color

		// update parents and liberties of adjacent stones

		opp := Reverse(color)
		root := vertex
		for i := 0; i < 4; i++ {
			adj := t.adj[vertex][i]
			if adj != -1 && t.board[adj] == color {
				adj := find(adj, t.parent)
				// take adjacent chain out of atari (may be added back later)
				t.atari[color][adj] = 0, false
				// or in liberties to friendly chains
				new_root, old_root := union(root, adj, t.parent, t.rank)
				t.liberties[new_root][0] |= t.liberties[old_root][0]
				t.liberties[new_root][1] |= t.liberties[old_root][1]
				// xor out liberty from self
				t.liberties[new_root][0] &= ^t.mask[adj][0]
				t.liberties[new_root][1] &= ^t.mask[adj][1]
				root = new_root
			} else if adj != -1 && t.board[adj] == EMPTY {
				// xor out liberty from empty vertices
				t.liberties[adj][0] &= ^t.mask[vertex][0]
				t.liberties[adj][1] &= ^t.mask[vertex][1]
			} else if adj != -1 {
				// xor out liberties from enemy chains
				enemy := find(adj, t.parent)
				t.liberties[enemy][0] &= ^t.mask[vertex][0]
				t.liberties[enemy][1] &= ^t.mask[vertex][1]
			}
		}
		// xor out liberty from self
		t.liberties[root][0] &= ^t.mask[vertex][0]
		t.liberties[root][1] &= ^t.mask[vertex][1]

		// capture any adjacent enemies reduced to zero liberties
		var captured *vector.IntVector
		for i := 0; i < 4; i++ {
			adj := t.adj[vertex][i]
			if adj != -1 && t.board[adj] == opp {
				enemy := find(adj, t.parent)
				libs := t.libs(enemy)
				if libs == 0 {
					// take chain out of atari
					t.atari[opp][enemy] = 0, false
					if captured == nil {
						captured = t.capture(enemy)
					} else {
						captured.AppendVector(t.capture(enemy))
					}
				}
			}
		}

		// check for suicide of affected empty points
		for i := 0; i < 4; i++ {
			adj := t.adj[vertex][i]
			if adj != -1 && t.board[adj] == EMPTY && t.libs(adj) == 0 {
				t.check_suicide(adj)
			} else if adj != -1 && (t.board[adj] == BLACK || t.board[adj] == WHITE) {
				adj = find(adj, t.parent)
				if t.libs(adj) == 1 {
					last_liberty := t.lastliberty(adj)
					if t.libs(last_liberty) == 0 {
						t.check_suicide(last_liberty)
					}
				}
			}
		}

		// ko check
		if captured != nil && captured.Len() == 1 {
			capture := captured.At(0)
			t.check_suicide(capture)
			if t.libs(root) == 1 {
				t.koColor = opp
				t.koVertex = capture
			}
		}

		// check if capture took adjacent chains out of atari
		// if so, check suicide status of their previous last liberty
		for i := 0; captured != nil && i < captured.Len(); i++ {
			capture := captured.At(i)
			for j := 0; j < 4; j++ {
				adj := t.adj[capture][j]
				if adj != -1 && t.board[adj] == color {
					adj = find(adj, t.parent)
					if last_liberty, exists := t.atari[color][adj]; exists {
						t.check_suicide(last_liberty)
						t.atari[color][adj] = 0, false
					}
				}
			}
		}

		// cannot play on occupied vertex
		t.weights.Set(BLACK, vertex, 0)
		t.weights.Set(WHITE, vertex, 0)

		// update atari status of adjacent chains
		for i := 0; i < 4; i++ {
			adj := t.adj[vertex][i]
			if adj != -1 && (t.board[adj] == BLACK || t.board[adj] == WHITE) {
				adj = find(adj, t.parent)
				if t.libs(adj) == 1 {
					t.atari[t.board[adj]][adj] = t.lastliberty(adj)
				}
			}
		}

		// update atari status of current chain
		if t.libs(root) == 1 {
			t.atari[color][root] = t.lastliberty(root)
		}

		// apply patterns
		neighbors := t.neighbors[1][vertex]
		for i := range neighbors {
			if neighbors[i] != -1 && t.board[neighbors[i]] == EMPTY {
				t.updateWeights(neighbors[i])
			}
		}
		for i := 0; captured != nil && i < captured.Len(); i++ {
			t.updateWeights(captured.At(i))
		}

		// mark vertex as played for AMAF
		if t.played[vertex] == EMPTY {
			t.played[vertex] = color
		}

	} else {
		t.passes++
	}
	t.moves.Push(vertex)
}
Exemple #12
0
func (e *XMLConnector) Query(r *Relation) *vector.Vector {
	ret := new(vector.Vector)

	//	fmt.Println(r)
	switch r.kind {
	case SELECT:
		dat := copyVect(e.tables[r.table].data)
		if r.order_field.Len() > 0 {
			dat.sort = r.order_field
			dat.order_asc = r.order_direction
			sort.Sort(dat)
			//			fmt.Println(dat)
		}

		limit := math.MaxInt32
		if r.limit_count > 0 {
			limit = r.limit_offset + r.limit_count
		}
		//		fmt.Println(r.limit_offset)
		//		fmt.Println(limit)
		found := 0
		for i := 0; i < dat.Len(); i++ {
			tmp := dat.At(i).(map[string]Value)
			if e.match(r.conditions, tmp) {
				if found >= r.limit_offset {
					ret.Push(tmp)
				}
				found++
				if found == limit {
					return ret
				}
			}
		}
	case COUNT:
		for i := range r.attributes {
			k := r.attributes.At(i)
			r.Where(F(k).IsNotNull())
		}
		dat := e.tables[r.table].data
		count := 0
		for i := 0; i < dat.Len(); i++ {
			tmp := dat.At(i).(map[string]Value)
			if e.match(r.conditions, tmp) {
				count++
			}
		}
		re := make(map[string]Value)
		re["_count"] = SysInt(count).Value()
		ret.Push(re)
	case INSERT:
		ins := make(map[string]Value)
		for k, v := range r.values {
			ins[strings.ToLower(k)] = v
		}
		if _, ok := ins["id"]; ok {
			ins["id"] = SysInt(e.tables[r.table].data.Len() + 1).Value()
		}
		e.tables[r.table].data.Push(ins)
	case DELETE:
		var list vector.IntVector
		dat := e.tables[r.table].data
		limit := math.MaxInt32
		if r.limit_count > 0 {
			limit = r.limit_offset + r.limit_count
		}
		found := 0
		for i := 0; i < dat.Len(); i++ {
			tmp := dat.At(i).(map[string]Value)
			if e.match(r.conditions, tmp) {
				if found >= r.limit_offset {
					list.Push(i)
				}
				found++
				if found == limit {
					return ret
				}
			}
		}
		for i := range list {
			e.tables[r.table].data.Delete(list.At(i))
		}

	case UPDATE:
		dat := e.tables[r.table].data
		limit := math.MaxInt32
		if r.limit_count > 0 {
			limit = r.limit_offset + r.limit_count
		}
		found := 0
		for i := 0; i < dat.Len(); i++ {
			tmp := dat.At(i).(map[string]Value)
			if e.match(r.conditions, tmp) {
				if found >= r.limit_offset {
					for k, v := range r.values {
						tmp[strings.ToLower(k)] = v
					}
				}
				found++
				if found == limit {
					return ret
				}
			}
		}
	}
	return ret
}
Exemple #13
0
func TestTombStream(t *testing.T) {
	var v vec.IntVector
	ts := NewTombStream(&v)
	ts.InsertChars(7)
	if v.Len() != 1 || v.At(0) != 7 {
		t.Fatal("Error in TombStream 1")
	}

	ts = NewTombStream(&v)
	ts.InsertChars(1)
	chars, err1 := ts.Skip(2)
	if err1 != nil {
		t.Fatal("TombStream reached end too soon")
	}
	if chars != 2 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertChars(1)
	chars, err1 = ts.Skip(5)
	if err1 != nil {
		t.Fatal("TombStream reached end too soon")
	}
	if chars != 5 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertChars(1)
	_, err1 = ts.Skip(1)
	if err1 == nil {
		t.Fatal("TombStream did not detect end")
	}
	if v.Len() != 1 || v.At(0) != 10 {
		t.Fatal("Error in TombStream 2")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(3)
	if chars != 3 {
		t.Fatal("Invalid number of chars")
	}
	n, err1 := ts.Bury(2)
	if err1 != nil || n != 2 {
		t.Fatal("Cannot bury")
	}
	if v.Len() != 3 || v.At(0) != 3 || v.At(1) != -2 || v.At(2) != 5 {
		t.Fatal("Error in TombStream 3")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(5)
	if chars != 3 {
		t.Fatal("Invalid number of chars")
	}
	n, err2 := ts.Bury(2)
	if err2 != nil || n != 2 {
		t.Fatal("Cannot bury")
	}
	if v.Len() != 3 || v.At(0) != 3 || v.At(1) != -4 || v.At(2) != 3 {
		t.Fatal("Error in TombStream 4")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(2)
	if chars != 2 {
		t.Fatal("Invalid number of chars")
	}
	n, err3 := ts.Bury(1)
	if err3 != nil || n != 1 {
		t.Fatal("Cannot bury")
	}
	if v.Len() != 3 || v.At(0) != 2 || v.At(1) != -5 || v.At(2) != 3 {
		t.Fatal("Error in TombStream 5")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(1)
	if chars != 1 {
		t.Fatal("Invalid number of chars")
	}
	n, err4 := ts.Bury(7)
	if err4 != nil || n != 2 {
		t.Fatal("Cannot bury")
	}
	chars, _ = ts.Skip(2)
	if chars != 2 {
		t.Fatal("Invalid number of chars")
	}
	if v.Len() != 3 || v.At(0) != 1 || v.At(1) != -7 || v.At(2) != 2 {
		t.Fatal("Error in TombStream 6")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(9)
	if chars != 2 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertTombs(3) // Middle of char
	chars, _ = ts.Skip(1)
	if chars != 1 {
		t.Fatal("Invalid number of chars")
	}
	if v.Len() != 5 || v.At(0) != 1 || v.At(1) != -7 || v.At(2) != 1 || v.At(3) != -3 || v.At(4) != 1 {
		t.Fatal("Error in TombStream 7")
	}

	ts = NewTombStream(&v)
	ts.InsertTombs(2) // Beginning of seq
	if v.Len() != 6 || v.At(0) != -2 || v.At(1) != 1 || v.At(2) != -7 || v.At(3) != 1 || v.At(4) != -3 || v.At(5) != 1 {
		t.Fatal("Error in TombStream 8")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(15)
	if chars != 3 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertTombs(1) // End of seq
	if v.Len() != 7 || v.At(0) != -2 || v.At(1) != 1 || v.At(2) != -7 || v.At(3) != 1 || v.At(4) != -3 || v.At(5) != 1 || v.At(6) != -1 {
		t.Fatal("Error in TombStream 9")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(2)
	if chars != 0 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertTombs(1) // End of tomb
	if v.Len() != 7 || v.At(0) != -3 || v.At(1) != 1 || v.At(2) != -7 || v.At(3) != 1 || v.At(4) != -3 || v.At(5) != 1 || v.At(6) != -1 {
		t.Fatal("Error in TombStream 10")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(4)
	if chars != 1 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertTombs(1) // Beginning of tomb
	if v.Len() != 7 || v.At(0) != -3 || v.At(1) != 1 || v.At(2) != -8 || v.At(3) != 1 || v.At(4) != -3 || v.At(5) != 1 || v.At(6) != -1 {
		t.Fatal("Error in TombStream 11")
	}

	ts = NewTombStream(&v)
	chars, _ = ts.Skip(1)
	if chars != 0 {
		t.Fatal("Invalid number of chars")
	}
	ts.InsertTombs(1) // Midle of tomb
	if v.Len() != 7 || v.At(0) != -4 || v.At(1) != 1 || v.At(2) != -8 || v.At(3) != 1 || v.At(4) != -3 || v.At(5) != 1 || v.At(6) != -1 {
		t.Fatal("Error in TombStream 11")
	}
}