当前位置: 首页>>代码示例>>Golang>>正文


Golang ring.New函数代码示例

本文整理汇总了Golang中container/ring.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: addToKillRing

// addToKillRing adds some text to the kill ring. If mode is 0 it adds it to a
// new node in the end of the kill ring, and move the current pointer to the new
// node. If mode is 1 or 2 it appends or prepends the text to the current entry
// of the killRing.
func (s *State) addToKillRing(text []rune, mode int) {
	// Don't use the same underlying array as text
	killLine := make([]rune, len(text))
	copy(killLine, text)

	// Point killRing to a newNode, procedure depends on the killring state and
	// append mode.
	if mode == 0 { // Add new node to killRing
		if s.killRing == nil { // if killring is empty, create a new one
			s.killRing = ring.New(1)
		} else if s.killRing.Len() >= KillRingMax { // if killring is "full"
			s.killRing = s.killRing.Next()
		} else { // Normal case
			s.killRing.Link(ring.New(1))
			s.killRing = s.killRing.Next()
		}
	} else {
		if s.killRing == nil { // if killring is empty, create a new one
			s.killRing = ring.New(1)
			s.killRing.Value = []rune{}
		}
		if mode == 1 { // Append to last entry
			killLine = append(s.killRing.Value.([]rune), killLine...)
		} else if mode == 2 { // Prepend to last entry
			killLine = append(killLine, s.killRing.Value.([]rune)...)
		}
	}

	// Save text in the current killring node
	s.killRing.Value = killLine
}
开发者ID:onlyafly,项目名称:galapagos,代码行数:35,代码来源:line.go

示例2: ThreeDRotate

func ThreeDRotate(op ThreeDOperation) error {
	newFaceRing := ring.New(8)
	newEdgeRing := ring.New(12)
	trx := ThreeDTransformer{
		newFaceRing, newEdgeRing}
	for _, faceColor := range op.ent[op.cubeId].faceMap[op.face] {
		trx.faceRing.Value = faceColor
		trx.faceRing = trx.faceRing.Next()
	}
	for _, edgeColorPtr := range op.ent[op.cubeId].edgeMap[op.face] {
		trx.edgeRing.Value = *edgeColorPtr
		trx.edgeRing = trx.edgeRing.Next()
	}

	trx.faceRing = trx.faceRing.Move(2 * op.direction)
	trx.edgeRing = trx.edgeRing.Move(3 * op.direction)

	for i, _ := range op.ent[op.cubeId].faceMap[op.face] {
		if v, ok := trx.faceRing.Value.(Color); ok {
			op.ent[op.cubeId].faceMap[op.face][i] = v
		}
		trx.faceRing = trx.faceRing.Next()
	}
	for i, _ := range op.ent[op.cubeId].edgeMap[op.face] {
		if v, ok := trx.edgeRing.Value.(Color); ok {
			*op.ent[op.cubeId].edgeMap[op.face][i] = v
		}
		trx.edgeRing = trx.edgeRing.Next()
	}

	return nil
}
开发者ID:rwbogy,项目名称:innercube,代码行数:32,代码来源:transformations.go

示例3: valueBuffer

// Process values coming from ADC
func valueBuffer(d *EKReceiver) {
	last_sample := make([]time.Time, 31)

	for i := 0; i < 31; i++ {
		d.ValueBufferRaw[i] = ring.New(RAW_BUFFER_SIZE)
		d.ValueBuffer[i] = ring.New(BUFFER_SIZE)
	}
	for {
		v := <-d.buffer_chan

		//Move head forwards
		d.ValueBufferRaw[v.Channel] = d.ValueBufferRaw[v.Channel].Next()
		d.ValueBufferRaw[v.Channel].Value = ChannelData{v.Abstimestamp, v.Value} //Set value

		// FIR Calculation
		p0 := d.ValueBufferRaw[v.Channel]
		i := 0
		out := float64(0)
		for ; i < d.FIRTaps && p0.Value != nil; i++ {
			out += p0.Value.(ChannelData).Value
			p0 = p0.Prev()
		}
		out = out / float64(i)
		if v.Abstimestamp.Sub(last_sample[v.Channel]) >= d.SampleTime {
			d.ValueBuffer[v.Channel] = d.ValueBuffer[v.Channel].Next()
			d.ValueBuffer[v.Channel].Value = ChannelData{v.Abstimestamp, out}
			last_sample[v.Channel] = v.Abstimestamp
		}
	}
}
开发者ID:thoj,项目名称:Delphin-EK200C,代码行数:31,代码来源:expertkey.go

示例4: sendproxy

// Return a channel which serves as a sending proxy to `out`.
// Use a goroutine to receive values from `out` and store them
// in an expanding buffer, so that sending to `out` never blocks.
// See this discussion:
// <http://rogpeppe.wordpress.com/2010/02/10/unlimited-buffering-with-low-overhead>
func sendproxy(out chan<- uint64) chan<- uint64 {
	proxy := make(chan uint64, 1024)
	go func() {
		n := 1024 // the allocated size of the circular queue
		first := ring.New(n)
		last := first
		var c chan<- uint64
		var e uint64
		for {
			c = out
			if first == last {
				// buffer empty: disable output
				c = nil
			} else {
				e = first.Value.(uint64)
			}
			select {
			case e = <-proxy:
				last.Value = e
				if last.Next() == first {
					// buffer full: expand it
					last.Link(ring.New(n))
					n *= 2
				}
				last = last.Next()
			case c <- e:
				first = first.Next()
			}
		}
	}()
	return proxy
}
开发者ID:attilaolah,项目名称:prcert,代码行数:37,代码来源:sieve.go

示例5: main

func main() {
	r := ring.New(5)
	r.Value = 1
	r.Next().Value = 2
	r.Prev().Value = 5

	plus := ring.New(1)
	plus.Value = 10
	r.Link(plus)
	fmt.Println("len: ", r.Len())
	r.Do(printe)
}
开发者ID:hyndio,项目名称:hyd.me,代码行数:12,代码来源:ring.go

示例6: init

func init() {

	testBackend.groups["alt.test"] = &groupStorage{
		group: &nntp.Group{"alt.test", "A test.",
			0, 0, 0, nntp.PostingNotPermitted},
		articles: ring.New(maxArticles),
	}

	testBackend.groups["misc.test"] = &groupStorage{
		group: &nntp.Group{"misc.test", "More testing.",
			0, 0, 0, nntp.PostingPermitted},
		articles: ring.New(maxArticles),
	}

}
开发者ID:welterde,项目名称:go-nntp,代码行数:15,代码来源:exampleserver.go

示例7: NewSimpleMovingPercentile

// Create a new simple moving percentile expvar.Var. It will be
// published under `name` and maintain `size` values for
// calculating the percentile.
//
// percentile must be between 0 and 1
//
// An empty name will cause it to not be published
func NewSimpleMovingPercentile(name string, percentile float64, size int) *SimpleMovingStat {
	sm := new(SimpleMovingStat)
	sm.size = size
	sm.mutex = new(sync.Mutex)
	sm.values = ring.New(size)

	sm.calculate = func(s *SimpleMovingStat) float64 {
		ary := make([]float64, 0)
		s.values.Do(func(val interface{}) {
			if val != nil {
				ary = append(ary, val.(float64))
			}
		})
		length := len(ary)
		if length == 0 {
			return 0.0
		}
		sort.Float64s(ary)
		mid := int(float64(len(ary)) * percentile)
		return ary[mid]
	}

	if name != "" {
		expvar.Publish(name, sm)
	}
	return sm

}
开发者ID:brianm,项目名称:variant,代码行数:35,代码来源:smav.go

示例8: main

func main() {
	sourceAddress := ":3000"

	ports := []string{
		":3333",
		":3334",
	}
	hostRing := ring.New(len(ports))
	for _, port := range ports {
		url, _ := url.Parse("http://127.0.0.1" + port)
		hostRing.Value = url
		hostRing = hostRing.Next()
	}

	mutex := sync.Mutex{}
	director := func(request *http.Request) {
		mutex.Lock()
		defer mutex.Unlock()
		request.URL.Scheme = "http"
		request.URL.Host = hostRing.Value.(*url.URL).Host
		hostRing = hostRing.Next()
		fmt.Println(hostRing)
	}
	proxy := &httputil.ReverseProxy{Director: director}
	server := http.Server{
		Addr:    sourceAddress,
		Handler: proxy,
	}
	server.ListenAndServe()
}
开发者ID:yanjinzh6,项目名称:reverse-proxy,代码行数:30,代码来源:main.go

示例9: testRing

func testRing() {
	fmt.Println("testRing -----------------------------------------------------")
	// 循环列表
	size := 10
	// 声明和初始化
	r := ring.New(size)

	fmt.Println("len=", r.Len(), ",r=", r, ",r.Prev()=", r.Prev())

	for num, e := 0, r.Prev(); e != nil; e = e.Next() {
		e.Value = num
		num++
		if num >= size {
			break
		}
	}
	for num, e := 0, r.Prev(); e != nil; e = e.Next() {
		fmt.Println("num =", num, ",e.value=", e.Value)
		num++
		if num >= size+1 {
			break
		}
	}

}
开发者ID:shawnpan,项目名称:learnGo,代码行数:25,代码来源:testContainer.go

示例10: NewLoadBalancer

//NewLoadBalancer creates a new instance of a Round Robin load balancer
func (rrf *RoundRobinLoadBalancerFactory) NewLoadBalancer(backendName, caCertPath string, servers []config.ServerConfig) (LoadBalancer, error) {
	var rrlb RoundRobinLoadBalancer

	if backendName == "" {
		return nil, fmt.Errorf("Expected non-empty backend name")
	}

	if len(servers) == 0 {
		return nil, fmt.Errorf("Expected at least one server in servers argument")
	}

	rrlb.backend = backendName
	rrlb.servers = ring.New(len(servers))

	for _, s := range servers {

		lbEndpoint := new(LoadBalancerEndpoint)
		lbEndpoint.Address = fmt.Sprintf("%s:%d", s.Address, s.Port)
		metrics.SetGauge([]string{"endpoint", lbEndpoint.Address}, 1.0)
		lbEndpoint.PingURI = s.PingURI
		lbEndpoint.Up = true
		lbEndpoint.CACertPath = caCertPath

		log.Info("Spawing health check for address ", lbEndpoint.Address)
		healthCheckFunction := MakeHealthCheck(lbEndpoint, s, true)
		go healthCheckFunction()

		log.Info("Adding server with address ", lbEndpoint.Address)
		rrlb.servers.Value = lbEndpoint
		rrlb.servers = rrlb.servers.Next()
	}

	return &rrlb, nil
}
开发者ID:xtracdev,项目名称:xavi,代码行数:35,代码来源:roundrobin.go

示例11: initRedis

//初始化redis连接池
func initRedis(confs []*config.Redis) {
	ids = ring.New(len(confs))
	for _, conf := range confs {
		if !conf.Enable {
			continue
		}
		id := &IdAvailable{Id: conf.Id}
		ids.Value = id
		spec := redis.DefaultSpec().Db(conf.DB).Password(conf.Password).Host(conf.Host).Port(conf.Port)
		//log.Info("redis init at ", spec)
		c := make(chan *RedisClient, PoolSize)
		for j := 0; j < PoolSize; j++ {
			if client, err := redis.NewSynchClientWithSpec(spec); err != nil {
				goto err
			} else {
				c <- &RedisClient{Id: conf.Id, Redis: client}
			}
		}
		pool[conf.Id] = c
		id.IsAvailable = true
		ids = ids.Next()
	err: //如果创建CLIENT时出错,就抛弃这个台机器
		errRedis[conf.Id] = conf
		ids = ids.Next()
	}
}
开发者ID:sdgdsffdsfff,项目名称:monitor-1,代码行数:27,代码来源:redis.go

示例12: main

func main() {
	// Creates a circular list for round-robin HTTP roundtrip.
	hosts := []string{
		":4000",
		":5000",
		":6000",
	}
	hostRing := ring.New(len(hosts))
	for _, host := range hosts {
		hostRing.Value = host
		hostRing = hostRing.Next()
	}

	// Locks by mutex because hostConverter will be executed in parallel.
	mutex := sync.Mutex{}
	hostConverter := func(originalHost string) string {
		mutex.Lock()
		defer mutex.Unlock()
		host := hostRing.Value.(string)
		hostRing = hostRing.Next()
		return host
	}

	// Runs a reverse-proxy server on http://localhost:3000/
	proxy := entoverse.NewProxyWithHostConverter(hostConverter)
	http.ListenAndServe(":3000", proxy)
}
开发者ID:leobcn,项目名称:entoverse,代码行数:27,代码来源:roundrobin.go

示例13: TestDo

func TestDo(t *testing.T) {
	r := ring.New(5) // 5個の要素

	if r.Len() != 5 {
		t.Errorf("length = %d", r.Len())
	}

	i := 0
	for initialValue := r.Next(); initialValue != r; initialValue = initialValue.Next() {
		value := HogeValue(i)
		initialValue.Value = &value
		i++
	}

	for p := r.Next(); p != r; p = p.Next() {
		t.Logf("value = %d", *p.Value.(*HogeValue))
	}

	r.Do(func(v interface{}) {
		hoge, ok := v.(*HogeValue)
		if ok {
			hoge.Add(1)
		}
	})

	i = 1
	for p := r.Next(); p != r; p = p.Next() {
		check := p.Value.(*HogeValue)
		if int(*check) != i {
			t.Errorf("check = %d, i = %d", *check, i)
		}
		i++
	}
}
开发者ID:qt-luigi,项目名称:golangcafe,代码行数:34,代码来源:ringsample_test.go

示例14: main

func main() {
	myArr := string([]rune{
		0x49, 0x53, 0x54, 0x53, 0x2d, 0x59, 0x41, 0x52,
		0x41, 0x2d, 0x45, 0x4e, 0x55, 0x52, 0x0a,
	})
	myStr := "Haha!!!!  Strings won't find this flag. You need to use something else!!"
	promiseRing := ring.New(15)

	result := make([]byte, len(myStr))
	restore := result

	fmt.Println(myStr)

	for i := 0; i < promiseRing.Len(); i++ {
		result = restore
		for j := 0; j < len(myStr); j++ {
			result[j] = myStr[j] ^ myArr[i]
		}
		promiseRing.Value = b64.StdEncoding.EncodeToString(result)
		promiseRing = promiseRing.Next()
	}

	fmt.Println("============")
	promiseRing.Do(func(x interface{}) {
		fmt.Print(x, "\n============\n")
	})

}
开发者ID:RITSPARSA,项目名称:ISTS14_CTF,代码行数:28,代码来源:GoReversin.go

示例15: main

func main() {
	//创建10个元素的闭环
	r := ring.New(10)

	//给闭环中的元素赋值
	for i := 1; i <= r.Len(); i++ {
		r.Value = i
		r = r.Next()
	}

	//循环打印闭环中的元素值
	r.Do(
		func(p interface{}) {
			println(p.(int))
		})

	//获得当前元素之后的第5个元素
	r5 := r.Move(5)
	fmt.Println(r5.Value)
	fmt.Println(r.Value)

	//链接当前元素r与r5,相当于删除了r与r5之间的元素
	r1 := r.Link(r5)
	fmt.Println(r1.Value)
	fmt.Println(r.Value)
}
开发者ID:quchunguang,项目名称:test,代码行数:26,代码来源:test_ring.go


注:本文中的container/ring.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。