<commit_before>package server

import (
	"math"
	"sync"
)

// RegionArea is the area (in coordinate degrees)
const RegionArea = 1.0

// Client is the representation of chat clients
type Client struct {
	ID string
	Lat,
	Long float64
}

//NewClient creates a new client and returns its reference
func NewClient(lat, long float64) (client *Client) {
	client = new(Client)
	client.Lat = lat
	client.Long = long
	return
}

//ClientNetwork is the representation of the Network of all client regions
type ClientNetwork struct {
	root            *clientRegion
	allRegions      []*clientRegion
	latRange        [2]float64
	longRange       [2]float64
	modificationMux *sync.Mutex
}

// AddClient adds a client to the network in the appropriate region
func (c *ClientNetwork) AddClient(client *Client) (connected bool) {
	lat := math.Floor(client.Lat)
	long := math.Floor(client.Long)
	// we want to also track possible connecting regions in the case that
	// we need to add a region. Key as follows:
	// 0: Up
	// 1: Left
	// 2: Down
	// 3: Right
	var possibleRegionConnects = [4]*clientRegion{nil, nil, nil, nil}
	for _, region := range c.allRegions {
		if region.Lat == lat && region.Long == long {
			region.AddClient(client)
			connected = true
			return
		}
		if (region.Lat-lat) == RegionArea && (region.Long-long) == 0 {
			possibleRegionConnects[0] = region
			connected = true
		}
		if (region.Lat-lat) == -RegionArea && (region.Long-long) == 0 {
			possibleRegionConnects[3] = region
			connected = true
		}
		if (region.Lat-lat) == 0 && (region.Long-long) == -RegionArea {
			possibleRegionConnects[1] = region
			connected = true
		}
		if (region.Lat-lat) == 0 && (region.Long-long) == RegionArea {
			possibleRegionConnects[3] = region
			connected = true
		}
	}

	if !connected {
		return
	}

	newRegion := newClientRegion(client.Lat, client.Long)
	newRegion.AddClient(client)

	for i, r := range possibleRegionConnects {
		if r == nil {
			continue
		}
		switch i {
		case 0:
			newRegion.Up = r
			r.Down = newRegion
		case 1:
			newRegion.Left = r
			r.Right = newRegion
		case 2:
			newRegion.Down = r
			r.Up = newRegion
		case 3:
			newRegion.Right = r
			r.Left = newRegion
		}
	}
	return
}

// NewClientNetwork creates a new network of client regions
func NewClientNetwork(root *clientRegion) (network *ClientNetwork) {
	network = new(ClientNetwork)
	network.root = root
	network.allRegions = []*clientRegion{root}

	network.latRange = [2]float64{root.Lat, root.Lat + RegionArea}
	network.longRange = [2]float64{root.Long, root.Long + RegionArea}

	network.modificationMux = new(sync.Mutex)
	return
}

type clientRegion struct {
	Up,<commit_msg>Fixed missing append of new region to the network client slice<commit_after>package server

import (
	"math"
	"sync"
)

// RegionArea is the area (in coordinate degrees)
const RegionArea = 1.0

// Client is the representation of chat clients
type Client struct {
	ID string
	Lat,
	Long float64
}

//NewClient creates a new client and returns its reference
func NewClient(lat, long float64) (client *Client) {
	client = new(Client)
	client.Lat = lat
	client.Long = long
	return
}

//ClientNetwork is the representation of the Network of all client regions
type ClientNetwork struct {
	root            *clientRegion
	allRegions      []*clientRegion
	latRange        [2]float64
	longRange       [2]float64
	modificationMux *sync.Mutex
}

// AddClient adds a client to the network in the appropriate region
func (c *ClientNetwork) AddClient(client *Client) (connected bool) {
	lat := math.Floor(client.Lat)
	long := math.Floor(client.Long)
	// we want to also track possible connecting regions in the case that
	// we need to add a region. Key as follows:
	// 0: Up
	// 1: Left
	// 2: Down
	// 3: Right
	var possibleRegionConnects = [4]*clientRegion{nil, nil, nil, nil}
	for _, region := range c.allRegions {
		if region.Lat == lat && region.Long == long {
			region.AddClient(client)
			connected = true
			return
		}
		if (region.Lat-lat) == RegionArea && (region.Long-long) == 0 {
			possibleRegionConnects[0] = region
			connected = true
		}
		if (region.Lat-lat) == -RegionArea && (region.Long-long) == 0 {
			possibleRegionConnects[3] = region
			connected = true
		}
		if (region.Lat-lat) == 0 && (region.Long-long) == -RegionArea {
			possibleRegionConnects[1] = region
			connected = true
		}
		if (region.Lat-lat) == 0 && (region.Long-long) == RegionArea {
			possibleRegionConnects[3] = region
			connected = true
		}
	}

	if !connected {
		return
	}

	newRegion := newClientRegion(client.Lat, client.Long)
	newRegion.AddClient(client)

	for i, r := range possibleRegionConnects {
		if r == nil {
			continue
		}
		switch i {
		case 0:
			newRegion.Up = r
			r.Down = newRegion
		case 1:
			newRegion.Left = r
			r.Right = newRegion
		case 2:
			newRegion.Down = r
			r.Up = newRegion
		case 3:
			newRegion.Right = r
			r.Left = newRegion
		}
	}

	// Append the region to the region array
	c.allRegions = append(c.allRegions, newRegion)
	return
}

// NewClientNetwork creates a new network of client regions
func NewClientNetwork(root *clientRegion) (network *ClientNetwork) {
	network = new(ClientNetwork)
	network.root = root
	network.allRegions = []*clientRegion{root}

	network.latRange = [2]float64{root.Lat, root.Lat + RegionArea}
	network.longRange = [2]float64{root.Long, root.Long + RegionArea}

	network.modificationMux = new(sync.Mutex)
	return
}

type clientRegion struct {
	Up,
{'fraction_non_alphanumeric': 0.11033724340175953, 'fraction_numerical': 0.012829912023460411, 'mean_word_length': 3.3725961538461537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5860346', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1786, 'n_words': 537}