2021-01-02 21:25:04 +00:00
|
|
|
package hyphae
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-02-04 17:06:37 +00:00
|
|
|
// Its value is number of all existing hyphae. NonEmptyHypha mutators are expected to manipulate the value. It is concurrent-safe.
|
2021-01-02 21:25:04 +00:00
|
|
|
var count = struct {
|
|
|
|
value int
|
|
|
|
sync.Mutex
|
|
|
|
}{}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// ResetCount sets the value of hyphae count to zero. Use when reloading hyphae.
|
2021-01-24 07:06:37 +00:00
|
|
|
func ResetCount() {
|
2021-02-20 14:03:54 +00:00
|
|
|
count.Lock()
|
|
|
|
count.value = 0
|
|
|
|
count.Unlock()
|
2021-01-24 07:06:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 08:26:38 +00:00
|
|
|
// Count how many hyphae there are. This is a O(1), the number of hyphae is stored in memory.
|
2021-12-20 20:59:23 +00:00
|
|
|
func Count() int {
|
|
|
|
count.Lock()
|
|
|
|
defer count.Unlock()
|
|
|
|
return count.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// incrementCount increments the value of the hyphae counter. Use when creating new hyphae or loading hyphae from disk.
|
|
|
|
func incrementCount() {
|
2021-01-02 21:25:04 +00:00
|
|
|
count.Lock()
|
|
|
|
count.value++
|
|
|
|
count.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:59:23 +00:00
|
|
|
// decrementCount decrements the value of the hyphae counter. Use when deleting existing hyphae.
|
|
|
|
func decrementCount() {
|
2021-01-02 21:25:04 +00:00
|
|
|
count.Lock()
|
|
|
|
count.value--
|
|
|
|
count.Unlock()
|
|
|
|
}
|