Example #1
1
func findInRing(in *ring.Ring, f func(r interface{}) bool) *ring.Ring {
	if f(in.Value) {
		return in
	}
	for p := in.Next(); p != in; p = p.Next() {
		if f(p.Value) {
			return p
		}
	}
	return nil
}
Example #2
0
func (s *CJKBigramFilter) outputBigram(r *ring.Ring, itemsInRing *int, pos int) *analysis.Token {
	if *itemsInRing == 2 {
		thisShingleRing := r.Move(-1)
		shingledBytes := make([]byte, 0)

		// do first token
		prev := thisShingleRing.Value.(*analysis.Token)
		shingledBytes = append(shingledBytes, prev.Term...)

		// do second token
		thisShingleRing = thisShingleRing.Next()
		curr := thisShingleRing.Value.(*analysis.Token)
		shingledBytes = append(shingledBytes, curr.Term...)

		token := analysis.Token{
			Type:     analysis.Double,
			Term:     shingledBytes,
			Position: pos,
			Start:    prev.Start,
			End:      curr.End,
		}
		return &token
	}
	return nil
}
Example #3
0
func (s *CJKBigramFilter) buildUnigram(r *ring.Ring, itemsInRing *int, pos int) *analysis.Token {
	if *itemsInRing == 2 {
		thisShingleRing := r.Move(-1)
		// do first token
		prev := thisShingleRing.Value.(*analysis.Token)
		token := analysis.Token{
			Type:     analysis.Single,
			Term:     prev.Term,
			Position: pos,
			Start:    prev.Start,
			End:      prev.End,
		}
		return &token
	} else if *itemsInRing == 1 {
		// do first token
		prev := r.Value.(*analysis.Token)
		token := analysis.Token{
			Type:     analysis.Single,
			Term:     prev.Term,
			Position: pos,
			Start:    prev.Start,
			End:      prev.End,
		}
		return &token
	}
	return nil
}
Example #4
0
func NewMainMenu(mapDir string, w, h int) (m *MainMenu) {
	file, err := os.Open(mapDir)
	defer file.Close()
	if err != nil {
		panic(err)
	}

	fis, err := file.Readdir(0)
	if err != nil {
		panic(err)
	}

	var maps *ring.Ring
	for _, fi := range fis {
		if strings.HasSuffix(fi.Name(), ".txt") {
			name := fi.Name()
			mapName := name[:len(name)-4]
			r := ring.New(1)
			r.Value = mapName
			if maps != nil {
				maps.Link(r)
			} else {
				maps = r
			}
		}
	}
	return &MainMenu{maps, time.Now(), w, h}
}
Example #5
0
func printRing(r *ring.Ring) {
	elements := make([]interface{}, 0, r.Len())
	r.Do(func(i interface{}) {
		elements = append(elements, i)
	})
	log.Printf("%v", elements)
}
Example #6
0
func slicifyRing(r *ring.Ring) {
	l := r.Len()
	s := make([]WindowData, 0, l)
	for i := 0; i < l; i++ {
		s = append(s, r.Value.(WindowData))
		r = r.Prev()
	}
}
Example #7
0
func slicifyRing(r *ring.Ring) {
	l := r.Len()
	s := make([]float64, 0, l)
	for i := 0; i < l; i++ {
		s = append(s, r.Value.(float64))
		r = r.Prev()
	}
}
Example #8
0
func iterateRing(rng *ring.Ring, out chan Pair) {
	i := 0

	rng.Do(func(ringCurrent interface{}) {
		out <- Pair{i, ringCurrent}
		i++
	})
}
Example #9
0
func removeFromRing(r *ring.Ring, s string) *ring.Ring {
	newRing := ring.New(0)
	r.Do(func(v interface{}) {
		if v != s {
			newRing = addToRing(newRing, v.(string))
		}
	})
	return newRing
}
Example #10
0
func current_extremes(r *ring.Ring) (int, int) {
	length := r.Len()
	if length%2 != 0 {
		fmt.Fprintln(os.Stderr, "error: Ring must be even")
		os.Exit(1)
	}
	fmt.Println(r.Value)

	return r.Value.(int), r.Move(length / 2).Value.(int)
}
Example #11
0
func containsTask(finishedTasks *ring.Ring, taskId string) bool {
	for i := 0; i < defaultFinishedTasksSize; i++ {
		value := finishedTasks.Next().Value
		if value == nil {
			continue
		}
		if value.(string) == taskId {
			return true
		}
	}
	return false
}
func (m *MessageBuffer) AddMessage(channel, message string) {
	if int32(m.buffer.Len()) >= m.maxsize { // buffer full?
		m.lostmsgs += 1
		return
	}
	var elem *ring.Ring = ring.New(1)
	var msg *bufferedMessage = &bufferedMessage{
		Channel: channel,
		Message: message,
	}
	elem.Value = msg
	// Add element elem to buffer.
	m.buffer.Prev().Link(elem)
}
Example #13
0
func seed(r *ring.Ring) *ring.Ring {
	if r.Value == nil {
		r.Value = new(bucket)
	} else {
		r.Value.(*bucket).Reset()
	}
	return r
}
Example #14
0
func (s *CJKBigramFilter) flush(r *ring.Ring, itemsInRing *int, pos int) *analysis.Token {
	var rv *analysis.Token
	if *itemsInRing == 1 {
		rv = s.buildUnigram(r, itemsInRing, pos)
	}
	r.Value = nil
	*itemsInRing = 0
	return rv
}
Example #15
0
func (c *Cache) meta_add(key string, r *ring.Ring) {

	c.evict()

	c.keys[key] = r
	r.Link(c.hand_hot)

	if c.hand_hot == nil {
		// first element
		c.hand_hot = r
		c.hand_cold = r
		c.hand_test = r
	}

	if c.hand_cold == c.hand_hot {
		c.hand_cold = c.hand_cold.Prev()
	}
}
Example #16
0
func (c *Cache) meta_del(r *ring.Ring) {

	delete(c.keys, r.Value.(*entry).key)

	if r == c.hand_hot {
		c.hand_hot = c.hand_hot.Prev()
	}

	if r == c.hand_cold {
		c.hand_cold = c.hand_cold.Prev()
	}

	if r == c.hand_test {
		c.hand_test = c.hand_test.Prev()
	}

	r.Prev().Unlink(1)
}
Example #17
0
func generadorTPS(nroMaxPeticiones uint64, lambda float64, q chan<- []byte, contadorPeticiones *uint64, forever bool, tiras *ring.Ring, procesoLlegada string) {
	if procesoLlegada == "M" {
		for ; forever || *contadorPeticiones < nroMaxPeticiones; *contadorPeticiones++ {
			time.Sleep(time.Duration(int64(1000000000.0 * rand.ExpFloat64() / lambda)))
			q <- (tiras.Value.([]byte))
			tiras = tiras.Next()
		}
	} else { /* procesoLlegada == "D" */
		// Utilizo un Ticker como referencia.
		// Lo encapsulo en generadorTPS porque el Ticker utiliza un canal sincrono.
		// aunque el ticker nunca se bloquea
		ticker := time.NewTicker(time.Duration(int64(1000000000.0 / lambda)))

		for ; forever || *contadorPeticiones < nroMaxPeticiones; *contadorPeticiones++ {
			<-ticker.C
			q <- (tiras.Value.([]byte))
			tiras = tiras.Next()
		}

		ticker.Stop()
	}
	close(q)
}
Example #18
0
func (rb *ringBuffer) read(lines int) []string {
	if lines <= 0 {
		return []string{}
	}
	// Only need a read lock because nothing we're about to do affects the internal state of the
	// ringBuffer.  Mutliple reads can happen in parallel.  Only writing requires an exclusive lock.
	rb.mutex.RLock()
	defer rb.mutex.RUnlock()
	var start *ring.Ring
	if lines < rb.ring.Len() {
		start = rb.ring.Move(-1 * (lines - 1))
	} else {
		start = rb.ring.Next()
	}
	data := make([]string, 0, lines)
	start.Do(func(line interface{}) {
		if line == nil || lines <= 0 {
			return
		}
		lines--
		data = append(data, line.(string))
	})
	return data
}
Example #19
0
func (s *ShingleFilter) shingleCurrentRingState(ring *ring.Ring, itemsInRing int) analysis.TokenStream {
	rv := make(analysis.TokenStream, 0)
	for shingleN := s.min; shingleN <= s.max; shingleN++ {
		// if there are enough items in the ring
		// to produce a shingle of this size
		if itemsInRing >= shingleN {
			thisShingleRing := ring.Move(-(shingleN - 1))
			shingledBytes := make([]byte, 0)
			pos := 0
			start := -1
			end := 0
			for i := 0; i < shingleN; i++ {
				if i != 0 {
					shingledBytes = append(shingledBytes, []byte(s.tokenSeparator)...)
				}
				curr := thisShingleRing.Value.(*analysis.Token)
				if pos == 0 && curr.Position != 0 {
					pos = curr.Position
				}
				if start == -1 && curr.Start != -1 {
					start = curr.Start
				}
				if curr.End != -1 {
					end = curr.End
				}
				shingledBytes = append(shingledBytes, curr.Term...)
				thisShingleRing = thisShingleRing.Next()
			}
			token := analysis.Token{
				Type: analysis.Shingle,
				Term: shingledBytes,
			}
			if pos != 0 {
				token.Position = pos
			}
			if start != -1 {
				token.Start = start
			}
			if end != -1 {
				token.End = end
			}
			rv = append(rv, &token)
		}
	}
	return rv
}
Example #20
0
func RingToStringInt64(r *ring.Ring, delim string, delta bool) string {
	bb := new(bytes.Buffer)
	prev := r.Value.(int64)
	r = r.Next()
	for i := 1; i < r.Len(); i++ {
		if delta {
			if r.Value.(int64) == int64(0) {
				bb.WriteString(fmt.Sprintf("0%s", delim))
			} else {
				d := r.Value.(int64) - prev
				bb.WriteString(fmt.Sprintf("%d%s", d, delim))
			}
			prev = r.Value.(int64)
		} else {
			bb.WriteString(fmt.Sprintf("%d%s", r.Value.(int64), delim))
		}
		r = r.Next()
	}
	return strings.TrimSuffix(bb.String(), delim)
}
Example #21
0
func rollup(r *ring.Ring) *ring.Ring {
	n := r.Next()
	return seed(n)
}
Example #22
0
File: yes.go Project: dpaneda/code
func main() {
	var n, m int

	f, _ := os.Create("yes.pprof")
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	fmt.Scan(&n)
	fmt.Scan(&m)

	bucket2number := make(map[int]*ring.Ring)
	number2bucket := map[int]int{0: 0, n - 1: n - 1}
	bucketlen := map[int]int{0: 1, n - 1: 1}

	bucket2number[0] = ring.New(1)
	bucket2number[0].Value = 0
	bucket2number[n-1] = ring.New(1)
	bucket2number[n-1].Value = n - 1

	var a, b int
	var la, lb *ring.Ring
	var debug = false

	for i := 0; i <= m; i++ {
		fmt.Scan(&a, &b)

		ba, ok := number2bucket[a]
		if !ok {
			number2bucket[a] = a
			bucketlen[a] = 1
			ba = a
			la = ring.New(1)
			la.Value = a
			bucket2number[ba] = la
		} else {
			la = bucket2number[ba]
		}

		bb, ok := number2bucket[b]
		if !ok {
			number2bucket[b] = b
			bucketlen[b] = 1
			bb = b
			lb = ring.New(1)
			lb.Value = b
			bucket2number[bb] = lb
		} else {
			lb = bucket2number[bb]
		}

		if ba != bb {
			// Not connected yet
			if bucketlen[ba] < bucketlen[bb] {
				la, lb = lb, la
				ba, bb = bb, ba
			}

			for j := 0; j < bucketlen[bb]; j++ {
				number2bucket[lb.Value.(int)] = ba
				lb = lb.Next()
			}

			la.Link(lb)
			bucketlen[ba] += bucketlen[bb]

			delete(bucket2number, bb)

			if number2bucket[0] == number2bucket[n-1] {
				fmt.Printf("Connected at %d\n", i)
				return
			}
		}

		if debug {
			fmt.Printf("%d -> %d\n", a, b)

			fmt.Print("Bucket2Number\n")
			for k, v := range bucket2number {
				fmt.Print(k)
				v.Do(
					func(x interface{}) {
						fmt.Printf(" %d", x.(int))
					})
				fmt.Println()
			}

			fmt.Print("Number2Bucket\n")
			for k, v := range number2bucket {
				fmt.Printf("%d: %d\n", k, v)
			}
		}
	}

	fmt.Println("Not connected")
}