1//  Copyright (c) 2014 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package util
11
12import (
13	"sync"
14)
15
16type BoolPool struct {
17	pool *sync.Pool
18	size int
19}
20
21func NewBoolPool(size int) *BoolPool {
22	rv := &BoolPool{
23		pool: &sync.Pool{
24			New: func() interface{} {
25				return make([]bool, 0, size)
26			},
27		},
28		size: size,
29	}
30
31	return rv
32}
33
34func (this *BoolPool) Get() []bool {
35	return this.pool.Get().([]bool)
36}
37
38func (this *BoolPool) GetCapped(capacity int) []bool {
39	if capacity > this.size {
40		return make([]bool, 0, capacity)
41	} else {
42		return this.Get()
43	}
44}
45
46func (this *BoolPool) GetSized(length int) []bool {
47	if length > this.size {
48		return make([]bool, length)
49	}
50
51	rv := this.Get()
52	rv = rv[0:length]
53	for i := 0; i < length; i++ {
54		rv[i] = false
55	}
56
57	return rv
58}
59
60func (this *BoolPool) Put(s []bool) {
61	if cap(s) != this.size {
62		return
63	}
64
65	this.pool.Put(s[0:0])
66}
67