protohackers

package module
v0.0.0-...-c4e956a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 5 Imported by: 0

README

protohackers

Some (slightly overengineered) utilities and some solutions for the https://protohackers.com/ problems.

Documentation

Overview

Package protohackers provides several useful utility functions which can be used while solving problems on protohackers.com.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAcceptAndHandleParallel

func ListenAcceptAndHandleParallel(network, address string, handler ConnHandler) error

ListenAcceptAndHandleParallel listens on the given network and address, accepts an unlimited amount of connections in parallel from the given listener, calling the given handler in a goroutine for each connection.

func ListenAcceptAndHandleParallelStateful

func ListenAcceptAndHandleParallelStateful[State any](network, address string, initialState func() State, handler ConnHandlerStateful[State]) error

ListenAcceptAndHandleParallelStateful is like ListenAcceptAndHandleParallel, but allows the caller to handle per-connection state of type State.

Types

type BytesHandler

type BytesHandler func(request []byte) (response *[]byte, err error)

type BytesHandlerStateful

type BytesHandlerStateful[State any] func(state State, request []byte) (response *[]byte, err error)

type ConnHandler

type ConnHandler func(net.Conn) error

func ByteConnHandler

func ByteConnHandler(handler BytesHandler) ConnHandler

ByteConnHandler returns a connection handler that will continually:

  • read a byte
  • call the given handler function with that byte
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection

func LineConnHandler

func LineConnHandler(handler BytesHandler) ConnHandler

LineConnHandler returns a connection handler that will continually:

  • read a line of bytes (a sequence of bytes ending in a newline character)
  • call the given handler function with those bytes
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection, followed by a newline (otherwise do nothing).

Lines passed to the handler do not contain newlines.

func NBytesConnHandler

func NBytesConnHandler(n int64, handler BytesHandler) ConnHandler

NBytesConnHandler returns a connection handler that will continually:

  • read at most n bytes
  • call the given handler function with those bytes
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection

func SplitFuncConnHandler

func SplitFuncConnHandler(inputSplitter bufio.SplitFunc, responseDelimiter []byte, handler BytesHandler) ConnHandler

SplitFuncConnHandler returns a connection handler that will continually:

  • read a sequence of bytes, delimited as defined by the given bufio.SplitFunc
  • call the given handler function with those bytes
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection, followed by responseDelimiter

func WordConnHandler

func WordConnHandler(handler BytesHandler) ConnHandler

WordConnHandler returns a connection handler that will continually:

  • read a word of bytes (as defined by bufio.ScanWords)
  • call the given handler function with those bytes
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection, followed by a space (otherwise do nothing).

Lines passed to the handler do not contain spaces.

type ConnHandlerStateful

type ConnHandlerStateful[State any] func(State, net.Conn) error

func ByteConnHandlerStateful

func ByteConnHandlerStateful[State any](handler BytesHandlerStateful[State]) ConnHandlerStateful[State]

ByteConnHandlerStateful is a stateful version of ByteWordConnHandler

func LineConnHandlerStateful

func LineConnHandlerStateful[State any](handler BytesHandlerStateful[State]) ConnHandlerStateful[State]

LineConnHandlerStateful is a stateful version of LineConnHandler

func NBytesConnHandlerStateful

func NBytesConnHandlerStateful[State any](n int64, handler BytesHandlerStateful[State]) ConnHandlerStateful[State]

NBytesConnHandlerStateful is a stateful version of NBytesConnHandler

func SplitFuncConnHandlerStateful

func SplitFuncConnHandlerStateful[State any](inputSplitter bufio.SplitFunc, responseDelimiter []byte, handler BytesHandlerStateful[State]) ConnHandlerStateful[State]

SplitFuncConnHandler returns a connection handler that will continually:

  • read a sequence of bytes, delimited as defined by the given bufio.SplitFunc
  • call the given handler function with those bytes
  • if the handler function returns a non-nil pointer to a byte slice, write it to the connection, followed by responseDelimiter

func WordConnHandlerStateful

func WordConnHandlerStateful[State any](handler BytesHandlerStateful[State]) ConnHandlerStateful[State]

WordConnHandlerStateful is a stateful version of WordConnHandler

Directories

Path Synopsis
cmd
0
Solution to problem 0: [Smoke Test]
Solution to problem 0: [Smoke Test]
1
Solution to problem 1: Prime Time [Prime Time]: https://protohackers.com/problem/1
Solution to problem 1: Prime Time [Prime Time]: https://protohackers.com/problem/1
2
Solution to problem 2: Means to an End [Means to an End]: https://protohackers.com/problem/2
Solution to problem 2: Means to an End [Means to an End]: https://protohackers.com/problem/2
3
Solution to problem 3: Budget Chat [Budget Chat]: https://protohackers.com/problem/3
Solution to problem 3: Budget Chat [Budget Chat]: https://protohackers.com/problem/3

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL