<commit_before>package device

import (
	"hash/fnv"
	"sync"
)

// idShard represents a single shard of mappings for devices keyed by ID.  Each idShard
// permits duplicate devices mapped to the same ID.
type idShard struct {
	sync.RWMutex
	data map[ID][]*device
}

func (is *idShard) add(id ID, d *device) {
	is.Lock()
	defer is.Unlock()
	duplicates := is.data[id]

	// just do a simple linear search, as the number of duplicate devices
	// for a given ID is likely to be very small
	for _, candidate := range duplicates {
		if d == candidate {
			// this device is already here
			return
		}
	}

	is.data[id] = append(duplicates, d)
}

func (is *idShard) removeOne(id ID, d *device) bool {
	is.Lock()
	defer is.Unlock()

	duplicates := is.data[id]
	for i, candidate := range duplicates {
		if d == candidate {
			last := len(duplicates) - 1

			duplicates[i] = duplicates[last]
			duplicates[last] = nil
			duplicates = duplicates[:last]

			if len(duplicates) > 0 {
				is.data[id] = duplicates
			} else {
				delete(is.data, id)
			}

			return true
		}
	}

	return false
}

func (is *idShard) removeAll(id ID) []*device {
	is.Lock()
	defer is.Unlock()
	duplicates := is.data[id]
	delete(is.data, id)
	return duplicates
}

func (is *idShard) visitID(id ID, visitor func(*device)) int {
	is.RLock()
	defer is.RUnlock()

	duplicates := is.data[id]
	for _, d := range duplicates {
		visitor(d)
	}

	return len(duplicates)
}

func (is *idShard) visitIf(filter func(ID) bool, visitor func(*device)) (count int) {
	is.RLock()
	defer is.RUnlock()

	for id, duplicates := range is.data {
		if filter(id) {
			count += len(duplicates)
			for _, d := range duplicates {
				visitor(d)
			}
		}
	}

	return
}

func (is *idShard) removeIf(filter func(ID) bool, visitor func(*device)) (count int) {
	is.RLock()
	defer is.RUnlock()

	for id, duplicates := range is.data {
		if filter(id) {
			delete(is.data, id)
			count += len(duplicates)

			for _, d := range duplicates {
				visitor(d)
			}
		}
	}

	return
}

// keyShard represents a single shard of mappings for devices keyed by their unique Keys.
type keyShard struct {
	sync.RWMutex
	data map[Key]*device
}

func (ks *keyShard) add(key Key, d *device) error {
	ks.Lock()
	defer ks.Unlock()
	if _, ok := ks.data[key]; ok {
		return ErrorDuplicateKey
	}

	ks.data[key] = d
	return nil
}

func (ks *keyShard) remove(key Key) *device {
	ks.Lock()
	defer ks.Unlock()
	d := ks.data[key]
	delete(ks.data, key)
	return d
}

func (ks *keyShard) visitKey(key Key, visitor func(*device)) int {
	ks.RLock()
	defer ks.RUnlock()

	if d, ok := ks.data[key]; ok {
		visitor(d)
		return 1
	}

	return 0
}

func (ks *keyShard) visitAll(visitor func(*device)) int {
	ks.RLock()
	defer ks.RUnlock()

	for _, d := range ks.data {
		visitor(d)
	}

	return len(ks.data)
}

// registry is a fully sharded concurrent-safe mapping of connected devices.  Devices are mapped by both
// ID and Key.  A registry allows duplicate devices for the same ID, but only (1) device may be mapped to
// a given Key.
type registry struct {
	byID  []idShard
	byKey []keyShard
}

func newRegistry(shards, initialCapacity uint32) *registry {
	r := &registry{<commit_msg>removed the defers for unlocking<commit_after>package device

import (
	"hash/fnv"
	"sync"
)

// idShard represents a single shard of mappings for devices keyed by ID.  Each idShard
// permits duplicate devices mapped to the same ID.
type idShard struct {
	sync.RWMutex
	data map[ID][]*device
}

func (is *idShard) add(id ID, d *device) {
	is.Lock()
	duplicates := is.data[id]

	// just do a simple linear search, as the number of duplicate devices
	// for a given ID is likely to be very small
	for _, candidate := range duplicates {
		if d == candidate {
			// this device is already here
			is.Unlock()
			return
		}
	}

	is.data[id] = append(duplicates, d)
	is.Unlock()
}

func (is *idShard) removeOne(id ID, d *device) bool {
	is.Lock()
	duplicates := is.data[id]

	for i, candidate := range duplicates {
		if d == candidate {
			last := len(duplicates) - 1

			duplicates[i] = duplicates[last]
			duplicates[last] = nil
			duplicates = duplicates[:last]

			if len(duplicates) > 0 {
				is.data[id] = duplicates
			} else {
				delete(is.data, id)
			}

			is.Unlock()
			return true
		}
	}

	is.Unlock()
	return false
}

func (is *idShard) removeAll(id ID) []*device {
	is.Lock()
	duplicates := is.data[id]
	delete(is.data, id)
	is.Unlock()

	return duplicates
}

func (is *idShard) visitID(id ID, visitor func(*device)) int {
	is.RLock()

	duplicates := is.data[id]
	for _, d := range duplicates {
		visitor(d)
	}

	is.RUnlock()
	return len(duplicates)
}

func (is *idShard) visitIf(filter func(ID) bool, visitor func(*device)) (count int) {
	is.RLock()

	for id, duplicates := range is.data {
		if filter(id) {
			count += len(duplicates)
			for _, d := range duplicates {
				visitor(d)
			}
		}
	}

	is.RUnlock()
	return
}

func (is *idShard) removeIf(filter func(ID) bool, visitor func(*device)) (count int) {
	is.RLock()

	for id, duplicates := range is.data {
		if filter(id) {
			delete(is.data, id)
			count += len(duplicates)

			for _, d := range duplicates {
				visitor(d)
			}
		}
	}

	is.RUnlock()
	return
}

// keyShard represents a single shard of mappings for devices keyed by their unique Keys.
type keyShard struct {
	sync.RWMutex
	data map[Key]*device
}

func (ks *keyShard) add(key Key, d *device) error {
	ks.Lock()
	if _, ok := ks.data[key]; ok {
		ks.Unlock()
		return ErrorDuplicateKey
	}

	ks.data[key] = d
	ks.Unlock()
	return nil
}

func (ks *keyShard) remove(key Key) *device {
	ks.Lock()
	d := ks.data[key]
	delete(ks.data, key)
	ks.Unlock()

	return d
}

func (ks *keyShard) visitKey(key Key, visitor func(*device)) int {
	ks.RLock()

	if d, ok := ks.data[key]; ok {
		visitor(d)
		ks.RUnlock()
		return 1
	}

	ks.RUnlock()
	return 0
}

func (ks *keyShard) visitAll(visitor func(*device)) int {
	ks.RLock()

	for _, d := range ks.data {
		visitor(d)
	}

	ks.RUnlock()
	return len(ks.data)
}

// registry is a fully sharded concurrent-safe mapping of connected devices.  Devices are mapped by both
// ID and Key.  A registry allows duplicate devices for the same ID, but only (1) device may be mapped to
// a given Key.
type registry struct {
	byID  []idShard
	byKey []keyShard
}

func newRegistry(shards, initialCapacity uint32) *registry {
	r := &registry{
{'fraction_non_alphanumeric': 0.13214061519146264, 'fraction_numerical': 0.002197112366603892, 'mean_word_length': 3.320677966101695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2802806', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2370, 'n_words': 723}