<commit_before>package crdt

type Counter struct {
	Process string

	IncAtoms map[string]int64
	DecAtoms map[string]int64
}

// retrieves the current value of the distributed counter
func (c *Counter) Value() int64 {
	x := int64(0)
	for _, value := range c.IncAtoms {
		x = x + value
	}
	for _, value := range c.DecAtoms {
		x = x - value
	}
	return x
}

// called when we want to merge our counter with the state of the same
// counter from another node.
func (c *Counter) Merge(other *Counter) {
	for key, value := range other.IncAtoms {
		ourValue := c.IncAtoms[key]
		if value > ourValue {
			c.IncAtoms[key] = value
		}
	}

	for key, value := range other.DecAtoms {
		ourValue := c.DecAtoms[key]
		if value > ourValue {
			c.DecAtoms[key] = value
		}
	}
}

func (c *Counter) Increment() {
	c.IncAtoms[c.Process] += 1
}

func (c *Counter) Decrement() {
	c.DecAtoms[c.Process] += 1
}

// creates a new counter, each node that has a replica of the counter
// state must have a unique identity
func NewCounter(Process string) *Counter {
	return &Counter{Process, make(map[string]int64), make(map[string]int64)}
}
<commit_msg>ensure access to counter values is synchronised<commit_after>package crdt

import (
	"sync"
)

type Counter struct {
	Process string

	IncAtoms map[string]int64
	DecAtoms map[string]int64

	lock *sync.RWMutex
}

// retrieves the current value of the distributed counter
func (c *Counter) Value() int64 {
	c.lock.RLock()
	defer c.lock.RUnlock()

	x := int64(0)
	for _, value := range c.IncAtoms {
		x = x + value
	}
	for _, value := range c.DecAtoms {
		x = x - value
	}
	return x
}

// called when we want to merge our counter with the state of the same
// counter from another node.
func (c *Counter) Merge(other *Counter) {
	c.lock.Lock()
	defer c.lock.Unlock()

	for key, value := range other.IncAtoms {
		ourValue := c.IncAtoms[key]
		if value > ourValue {
			c.IncAtoms[key] = value
		}
	}

	for key, value := range other.DecAtoms {
		ourValue := c.DecAtoms[key]
		if value > ourValue {
			c.DecAtoms[key] = value
		}
	}
}

func (c *Counter) Increment() {
	c.lock.Lock()
	defer c.lock.Unlock()

	c.IncAtoms[c.Process] += 1
}

func (c *Counter) Decrement() {
	c.lock.Lock()
	defer c.lock.Unlock()

	c.DecAtoms[c.Process] += 1
}

// creates a new counter, each node that has a replica of the counter
// state must have a unique identity
func NewCounter(process string) *Counter {
	return &Counter{
		Process:  process,
		IncAtoms: make(map[string]int64),
		DecAtoms: make(map[string]int64),
		lock:     &sync.RWMutex{},
	}
}

{'fraction_non_alphanumeric': 0.12529365700861395, 'fraction_numerical': 0.011746280344557557, 'mean_word_length': 3.4667832167832167, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5844357', 'n_tokens_mistral': 996, 'n_tokens_neox': 927, 'n_words': 284}