aboutsummaryrefslogtreecommitdiffstats
path: root/example_test.go
blob: f0105153a46ee1513819101f36e9bf5695314930 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package deque_test

import (
	"fmt"

	"roseh.moe/pkg/deque"
)

func ExampleDeque() {
	q := new(deque.Deque[int])
	for i := range 10 {
		q.PushBack(i)
	}
	for range 3 {
		q.PopFront()
	}
	fmt.Println(q)

	// Output:
	// [3 4 5 6 7 8 9]
}

func ExampleWithCapacity() {
	q := deque.WithCapacity[int](10)
	for i := range 100 {
		if q.Len() == q.Cap() {
			q.PopFront()
		}
		q.PushBack(i)
	}
	fmt.Println(q)

	// Output:
	// [90 91 92 93 94 95 96 97 98 99]
}

func ExampleFrom() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	fmt.Println(q.PopFront())

	// Output:
	// 1 true
}

func ExampleDeque_At() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	fmt.Println(q.At(3))

	// Output:
	// 4
}

func ExampleDeque_Cap() {
	q := deque.WithCapacity[int](10)
	q.PushBack(1, 2, 3, 4, 5)
	fmt.Println(q.Cap())

	// Output:
	// 10
}

func ExampleDeque_Len() {
	q := new(deque.Deque[int])
	q.PushBack(1, 2, 3, 4, 5)
	fmt.Println(q.Len())

	// Output:
	// 5
}

func ExampleDeque_PopFront() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	for range 3 {
		q.PopFront()
	}
	fmt.Println(q)

	// Output:
	// [4 5]
}

func ExampleDeque_PopBack() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	for range 3 {
		q.PopBack()
	}
	fmt.Println(q)

	// Output:
	// [1 2]
}

func ExampleDeque_PushFront() {
	q := deque.From([]int{6, 7, 8, 9, 10})
	q.PushFront(1, 2, 3, 4, 5)
	fmt.Println(q)

	// Output:
	// [1 2 3 4 5 6 7 8 9 10]
}

func ExampleDeque_PushBack() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	q.PushBack(6, 7, 8, 9, 10)
	fmt.Println(q)

	// Output:
	// [1 2 3 4 5 6 7 8 9 10]
}

func ExampleDeque_Reset() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	q.Reset()
	fmt.Println(q.Cap())

	// Output:
	// 5
}

func ExampleDeque_Grow() {
	q := new(deque.Deque[int])
	q.Grow(5)
	// PushBack will not allocate:
	q.PushBack(1, 2, 3, 4, 5)
}

func ExampleDeque_All() {
	q := new(deque.Deque[int])
	q.PushBack(1, 2, 3, 4, 5)
	q.PopFront()
	for _, x := range q.All() {
		fmt.Println(x)
	}

	// Output:
	// 2
	// 3
	// 4
	// 5
}

func ExampleDeque_PopAll() {
	q := deque.From([]int{1, 2, 3, 4, 5})
	for x := range q.PopAll() {
		fmt.Println(x)
	}
	fmt.Println(q)

	// Output:
	// 1
	// 2
	// 3
	// 4
	// 5
	// []
}