Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Created August 16, 2025 00:32
Show Gist options
  • Select an option

  • Save altbodhi/24145426b91c0c6c8ef807bb8f592355 to your computer and use it in GitHub Desktop.

Select an option

Save altbodhi/24145426b91c0c6c8ef807bb8f592355 to your computer and use it in GitHub Desktop.
Search adplace by loc
namespace Company.AdPlaceSearchEngine;
public record Location(string Name);
public record AdPlace(string Name);
public abstract record SearchResult(Location Location)
{
public record NotFound(Location Location) : SearchResult(Location);
public record Success(Location Location, List<AdPlace> AdPlaces) : SearchResult(Location);
}
public class Locator
{
Dictionary<string, List<AdPlace>> _items = new();
SemaphoreSlim semaphoreSlim = new(1, 1);
public void Load(IEnumerable<string> lines)
{
try
{
semaphoreSlim.Wait();
Dictionary<string, List<AdPlace>> items = new();
foreach (var line in lines)
{
var data = line.Split(":", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var addPlace = new AdPlace(data[0]);
var locs = data[1].Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var loc in locs)
{
if (items.ContainsKey(loc))
items[loc].Add(addPlace);
else
items.Add(loc, [addPlace]);
}
}
_items = items;
}
finally { semaphoreSlim.Release(); }
}
public SearchResult Search(string location)
{
if (_items.TryGetValue(location, out var adPlaces))
return new SearchResult.Success(new Location(location), adPlaces);
return new SearchResult.NotFound(new Location(location));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment