Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 说明:New创建并返回cap元素的环形结构。可选参数safe指定是否在并发安全中使用此结构,默认情况下为false



  • Code Block
    languagego
    func ExampleNew() {
    	// Non concurrent safety
    	gring.New(10)
    
    	// Concurrent safety
    	gring.New(10, true)
    
    	// Output:
    }


(r *Ring) Cap() int

  • 说明:Cap返回Ring的容量。

  • Code Block
    languagego
    func ExampleRing_Cap() {
    	r1 := gring.New(10)
    	for i := 0; i < 5; i++ {
    		r1.Set(i).Next()
    	}
    	fmt.Println("Cap:", r1.Cap())
    
    	r2 := gring.New(10, true)
    	for i := 0; i < 10; i++ {
    		r2.Set(i).Next()
    	}
    	fmt.Println("Cap:", r2.Cap())
    
    	// Output:
    	// Cap: 10
    	// Cap: 10
    }


(r *Ring) Len() int

  • 说明:Len返回Ring的大小。

  • Code Block
    languagego
    func ExampleRing_Len() {
    	r1 := gring.New(10)
    	for i := 0; i < 5; i++ {
    		r1.Set(i).Next()
    	}
    	fmt.Println("Len:", r1.Len())
    
    	r2 := gring.New(10, true)
    	for i := 0; i < 10; i++ {
    		r2.Set(i).Next()
    	}
    	fmt.Println("Len:", r2.Len())
    
    	// Output:
    	// Len: 5
    	// Len: 10
    }



...

使用示例,约瑟夫问题

我们使用ring来模拟一下约瑟夫问题

...