id
int32
0
167k
repo
stringlengths
5
54
path
stringlengths
4
155
func_name
stringlengths
1
118
original_string
stringlengths
52
85.5k
language
stringclasses
1 value
code
stringlengths
52
85.5k
code_tokens
listlengths
21
1.41k
docstring
stringlengths
6
2.61k
docstring_tokens
listlengths
3
215
sha
stringlengths
40
40
url
stringlengths
85
252
9,100
zmb3/spotify
track_attributes.go
MaxDuration
func (ta *TrackAttributes) MaxDuration(duration int) *TrackAttributes { ta.intAttributes["max_duration_ms"] = duration return ta }
go
func (ta *TrackAttributes) MaxDuration(duration int) *TrackAttributes { ta.intAttributes["max_duration_ms"] = duration return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MaxDuration", "(", "duration", "int", ")", "*", "TrackAttributes", "{", "ta", ".", "intAttributes", "[", "\"", "\"", "]", "=", "duration", "\n", "return", "ta", "\n", "}" ]
// MaxDuration sets the maximum length of the track in milliseconds
[ "MaxDuration", "sets", "the", "maximum", "length", "of", "the", "track", "in", "milliseconds" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L92-L95
9,101
zmb3/spotify
track_attributes.go
MinDuration
func (ta *TrackAttributes) MinDuration(duration int) *TrackAttributes { ta.intAttributes["min_duration_ms"] = duration return ta }
go
func (ta *TrackAttributes) MinDuration(duration int) *TrackAttributes { ta.intAttributes["min_duration_ms"] = duration return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MinDuration", "(", "duration", "int", ")", "*", "TrackAttributes", "{", "ta", ".", "intAttributes", "[", "\"", "\"", "]", "=", "duration", "\n", "return", "ta", "\n", "}" ]
// MinDuration sets the minimum length of the track in milliseconds
[ "MinDuration", "sets", "the", "minimum", "length", "of", "the", "track", "in", "milliseconds" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L98-L101
9,102
zmb3/spotify
track_attributes.go
TargetDuration
func (ta *TrackAttributes) TargetDuration(duration int) *TrackAttributes { ta.intAttributes["target_duration_ms"] = duration return ta }
go
func (ta *TrackAttributes) TargetDuration(duration int) *TrackAttributes { ta.intAttributes["target_duration_ms"] = duration return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "TargetDuration", "(", "duration", "int", ")", "*", "TrackAttributes", "{", "ta", ".", "intAttributes", "[", "\"", "\"", "]", "=", "duration", "\n", "return", "ta", "\n", "}" ]
// TargetDuration sets the target length of the track in milliseconds
[ "TargetDuration", "sets", "the", "target", "length", "of", "the", "track", "in", "milliseconds" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L104-L107
9,103
zmb3/spotify
track_attributes.go
MaxEnergy
func (ta *TrackAttributes) MaxEnergy(energy float64) *TrackAttributes { ta.floatAttributes["max_energy"] = energy return ta }
go
func (ta *TrackAttributes) MaxEnergy(energy float64) *TrackAttributes { ta.floatAttributes["max_energy"] = energy return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MaxEnergy", "(", "energy", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "energy", "\n", "return", "ta", "\n", "}" ]
// MaxEnergy sets the maximum energy // Energy is a measure from 0.0 to 1.0 and represents a perceptual mesaure // of intensity and activity. Typically, energetic tracks feel fast, loud, // and noisy.
[ "MaxEnergy", "sets", "the", "maximum", "energy", "Energy", "is", "a", "measure", "from", "0", ".", "0", "to", "1", ".", "0", "and", "represents", "a", "perceptual", "mesaure", "of", "intensity", "and", "activity", ".", "Typically", "energetic", "tracks", "feel", "fast", "loud", "and", "noisy", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L113-L116
9,104
zmb3/spotify
track_attributes.go
MinEnergy
func (ta *TrackAttributes) MinEnergy(energy float64) *TrackAttributes { ta.floatAttributes["min_energy"] = energy return ta }
go
func (ta *TrackAttributes) MinEnergy(energy float64) *TrackAttributes { ta.floatAttributes["min_energy"] = energy return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MinEnergy", "(", "energy", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "energy", "\n", "return", "ta", "\n", "}" ]
// MinEnergy sets the minimum energy // Energy is a measure from 0.0 to 1.0 and represents a perceptual mesaure // of intensity and activity. Typically, energetic tracks feel fast, loud, // and noisy.
[ "MinEnergy", "sets", "the", "minimum", "energy", "Energy", "is", "a", "measure", "from", "0", ".", "0", "to", "1", ".", "0", "and", "represents", "a", "perceptual", "mesaure", "of", "intensity", "and", "activity", ".", "Typically", "energetic", "tracks", "feel", "fast", "loud", "and", "noisy", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L122-L125
9,105
zmb3/spotify
track_attributes.go
TargetEnergy
func (ta *TrackAttributes) TargetEnergy(energy float64) *TrackAttributes { ta.floatAttributes["target_energy"] = energy return ta }
go
func (ta *TrackAttributes) TargetEnergy(energy float64) *TrackAttributes { ta.floatAttributes["target_energy"] = energy return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "TargetEnergy", "(", "energy", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "energy", "\n", "return", "ta", "\n", "}" ]
// TargetEnergy sets the target energy // Energy is a measure from 0.0 to 1.0 and represents a perceptual mesaure // of intensity and activity. Typically, energetic tracks feel fast, loud, // and noisy.
[ "TargetEnergy", "sets", "the", "target", "energy", "Energy", "is", "a", "measure", "from", "0", ".", "0", "to", "1", ".", "0", "and", "represents", "a", "perceptual", "mesaure", "of", "intensity", "and", "activity", ".", "Typically", "energetic", "tracks", "feel", "fast", "loud", "and", "noisy", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L131-L134
9,106
zmb3/spotify
track_attributes.go
MaxInstrumentalness
func (ta *TrackAttributes) MaxInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["max_instrumentalness"] = instrumentalness return ta }
go
func (ta *TrackAttributes) MaxInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["max_instrumentalness"] = instrumentalness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MaxInstrumentalness", "(", "instrumentalness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "instrumentalness", "\n", "return", "ta", "\n\n", "}" ]
// MaxInstrumentalness sets the maximum instrumentalness // Instrumentalness predicts whether a track contains no vocals. // "Ooh" and "aah" sounds are treated as instrumental in this context. // Rap or spoken word tracks are clearly "vocal". // The closer the instrumentalness value is to 1.0, // the greater likelihood the track contains no vocal content. // Values above 0.5 are intended to represent instrumental tracks, // but confidence is higher as the value approaches 1.0.
[ "MaxInstrumentalness", "sets", "the", "maximum", "instrumentalness", "Instrumentalness", "predicts", "whether", "a", "track", "contains", "no", "vocals", ".", "Ooh", "and", "aah", "sounds", "are", "treated", "as", "instrumental", "in", "this", "context", ".", "Rap", "or", "spoken", "word", "tracks", "are", "clearly", "vocal", ".", "The", "closer", "the", "instrumentalness", "value", "is", "to", "1", ".", "0", "the", "greater", "likelihood", "the", "track", "contains", "no", "vocal", "content", ".", "Values", "above", "0", ".", "5", "are", "intended", "to", "represent", "instrumental", "tracks", "but", "confidence", "is", "higher", "as", "the", "value", "approaches", "1", ".", "0", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L144-L148
9,107
zmb3/spotify
track_attributes.go
MinInstrumentalness
func (ta *TrackAttributes) MinInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["min_instrumentalness"] = instrumentalness return ta }
go
func (ta *TrackAttributes) MinInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["min_instrumentalness"] = instrumentalness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MinInstrumentalness", "(", "instrumentalness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "instrumentalness", "\n", "return", "ta", "\n\n", "}" ]
// MinInstrumentalness sets the minimum instrumentalness // Instrumentalness predicts whether a track contains no vocals. // "Ooh" and "aah" sounds are treated as instrumental in this context. // Rap or spoken word tracks are clearly "vocal". // The closer the instrumentalness value is to 1.0, // the greater likelihood the track contains no vocal content. // Values above 0.5 are intended to represent instrumental tracks, // but confidence is higher as the value approaches 1.0.
[ "MinInstrumentalness", "sets", "the", "minimum", "instrumentalness", "Instrumentalness", "predicts", "whether", "a", "track", "contains", "no", "vocals", ".", "Ooh", "and", "aah", "sounds", "are", "treated", "as", "instrumental", "in", "this", "context", ".", "Rap", "or", "spoken", "word", "tracks", "are", "clearly", "vocal", ".", "The", "closer", "the", "instrumentalness", "value", "is", "to", "1", ".", "0", "the", "greater", "likelihood", "the", "track", "contains", "no", "vocal", "content", ".", "Values", "above", "0", ".", "5", "are", "intended", "to", "represent", "instrumental", "tracks", "but", "confidence", "is", "higher", "as", "the", "value", "approaches", "1", ".", "0", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L158-L162
9,108
zmb3/spotify
track_attributes.go
TargetInstrumentalness
func (ta *TrackAttributes) TargetInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["target_instrumentalness"] = instrumentalness return ta }
go
func (ta *TrackAttributes) TargetInstrumentalness(instrumentalness float64) *TrackAttributes { ta.floatAttributes["target_instrumentalness"] = instrumentalness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "TargetInstrumentalness", "(", "instrumentalness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "instrumentalness", "\n", "return", "ta", "\n\n", "}" ]
// TargetInstrumentalness sets the target instrumentalness // Instrumentalness predicts whether a track contains no vocals. // "Ooh" and "aah" sounds are treated as instrumental in this context. // Rap or spoken word tracks are clearly "vocal". // The closer the instrumentalness value is to 1.0, // the greater likelihood the track contains no vocal content. // Values above 0.5 are intended to represent instrumental tracks, // but confidence is higher as the value approaches 1.0.
[ "TargetInstrumentalness", "sets", "the", "target", "instrumentalness", "Instrumentalness", "predicts", "whether", "a", "track", "contains", "no", "vocals", ".", "Ooh", "and", "aah", "sounds", "are", "treated", "as", "instrumental", "in", "this", "context", ".", "Rap", "or", "spoken", "word", "tracks", "are", "clearly", "vocal", ".", "The", "closer", "the", "instrumentalness", "value", "is", "to", "1", ".", "0", "the", "greater", "likelihood", "the", "track", "contains", "no", "vocal", "content", ".", "Values", "above", "0", ".", "5", "are", "intended", "to", "represent", "instrumental", "tracks", "but", "confidence", "is", "higher", "as", "the", "value", "approaches", "1", ".", "0", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L172-L176
9,109
zmb3/spotify
track_attributes.go
MaxLiveness
func (ta *TrackAttributes) MaxLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["max_liveness"] = liveness return ta }
go
func (ta *TrackAttributes) MaxLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["max_liveness"] = liveness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MaxLiveness", "(", "liveness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "liveness", "\n", "return", "ta", "\n", "}" ]
// MaxLiveness sets the maximum liveness // Detects the presence of an audience in the recording. Higher liveness // values represent an increased probability that the track was performed live. // A value above 0.8 provides strong likelihook that the track is live.
[ "MaxLiveness", "sets", "the", "maximum", "liveness", "Detects", "the", "presence", "of", "an", "audience", "in", "the", "recording", ".", "Higher", "liveness", "values", "represent", "an", "increased", "probability", "that", "the", "track", "was", "performed", "live", ".", "A", "value", "above", "0", ".", "8", "provides", "strong", "likelihook", "that", "the", "track", "is", "live", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L206-L209
9,110
zmb3/spotify
track_attributes.go
MinLiveness
func (ta *TrackAttributes) MinLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["min_liveness"] = liveness return ta }
go
func (ta *TrackAttributes) MinLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["min_liveness"] = liveness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MinLiveness", "(", "liveness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "liveness", "\n", "return", "ta", "\n", "}" ]
// MinLiveness sets the minimum liveness // Detects the presence of an audience in the recording. Higher liveness // values represent an increased probability that the track was performed live. // A value above 0.8 provides strong likelihook that the track is live.
[ "MinLiveness", "sets", "the", "minimum", "liveness", "Detects", "the", "presence", "of", "an", "audience", "in", "the", "recording", ".", "Higher", "liveness", "values", "represent", "an", "increased", "probability", "that", "the", "track", "was", "performed", "live", ".", "A", "value", "above", "0", ".", "8", "provides", "strong", "likelihook", "that", "the", "track", "is", "live", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L215-L218
9,111
zmb3/spotify
track_attributes.go
TargetLiveness
func (ta *TrackAttributes) TargetLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["target_liveness"] = liveness return ta }
go
func (ta *TrackAttributes) TargetLiveness(liveness float64) *TrackAttributes { ta.floatAttributes["target_liveness"] = liveness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "TargetLiveness", "(", "liveness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "liveness", "\n", "return", "ta", "\n", "}" ]
// TargetLiveness sets the target liveness // Detects the presence of an audience in the recording. Higher liveness // values represent an increased probability that the track was performed live. // A value above 0.8 provides strong likelihook that the track is live.
[ "TargetLiveness", "sets", "the", "target", "liveness", "Detects", "the", "presence", "of", "an", "audience", "in", "the", "recording", ".", "Higher", "liveness", "values", "represent", "an", "increased", "probability", "that", "the", "track", "was", "performed", "live", ".", "A", "value", "above", "0", ".", "8", "provides", "strong", "likelihook", "that", "the", "track", "is", "live", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L224-L227
9,112
zmb3/spotify
track_attributes.go
MaxSpeechiness
func (ta *TrackAttributes) MaxSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["max_speechiness"] = speechiness return ta }
go
func (ta *TrackAttributes) MaxSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["max_speechiness"] = speechiness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MaxSpeechiness", "(", "speechiness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "speechiness", "\n", "return", "ta", "\n\n", "}" ]
// MaxSpeechiness sets the maximum speechiness. // Speechiness detects the presence of spoken words in a track. // The more exclusively speech-like the recording, the closer to 1.0 // the speechiness will be. // Values above 0.66 describe tracks that are probably made entirely of // spoken words. Values between 0.33 and 0.66 describe tracks that may // contain both music and speech, including such cases as rap music. // Values below 0.33 most likely represent music and other non-speech-like tracks.
[ "MaxSpeechiness", "sets", "the", "maximum", "speechiness", ".", "Speechiness", "detects", "the", "presence", "of", "spoken", "words", "in", "a", "track", ".", "The", "more", "exclusively", "speech", "-", "like", "the", "recording", "the", "closer", "to", "1", ".", "0", "the", "speechiness", "will", "be", ".", "Values", "above", "0", ".", "66", "describe", "tracks", "that", "are", "probably", "made", "entirely", "of", "spoken", "words", ".", "Values", "between", "0", ".", "33", "and", "0", ".", "66", "describe", "tracks", "that", "may", "contain", "both", "music", "and", "speech", "including", "such", "cases", "as", "rap", "music", ".", "Values", "below", "0", ".", "33", "most", "likely", "represent", "music", "and", "other", "non", "-", "speech", "-", "like", "tracks", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L327-L331
9,113
zmb3/spotify
track_attributes.go
MinSpeechiness
func (ta *TrackAttributes) MinSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["min_speechiness"] = speechiness return ta }
go
func (ta *TrackAttributes) MinSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["min_speechiness"] = speechiness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "MinSpeechiness", "(", "speechiness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "speechiness", "\n", "return", "ta", "\n\n", "}" ]
// MinSpeechiness sets the minimum speechiness. // Speechiness detects the presence of spoken words in a track. // The more exclusively speech-like the recording, the closer to 1.0 // the speechiness will be. // Values above 0.66 describe tracks that are probably made entirely of // spoken words. Values between 0.33 and 0.66 describe tracks that may // contain both music and speech, including such cases as rap music. // Values below 0.33 most likely represent music and other non-speech-like tracks.
[ "MinSpeechiness", "sets", "the", "minimum", "speechiness", ".", "Speechiness", "detects", "the", "presence", "of", "spoken", "words", "in", "a", "track", ".", "The", "more", "exclusively", "speech", "-", "like", "the", "recording", "the", "closer", "to", "1", ".", "0", "the", "speechiness", "will", "be", ".", "Values", "above", "0", ".", "66", "describe", "tracks", "that", "are", "probably", "made", "entirely", "of", "spoken", "words", ".", "Values", "between", "0", ".", "33", "and", "0", ".", "66", "describe", "tracks", "that", "may", "contain", "both", "music", "and", "speech", "including", "such", "cases", "as", "rap", "music", ".", "Values", "below", "0", ".", "33", "most", "likely", "represent", "music", "and", "other", "non", "-", "speech", "-", "like", "tracks", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L341-L345
9,114
zmb3/spotify
track_attributes.go
TargetSpeechiness
func (ta *TrackAttributes) TargetSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["target_speechiness"] = speechiness return ta }
go
func (ta *TrackAttributes) TargetSpeechiness(speechiness float64) *TrackAttributes { ta.floatAttributes["target_speechiness"] = speechiness return ta }
[ "func", "(", "ta", "*", "TrackAttributes", ")", "TargetSpeechiness", "(", "speechiness", "float64", ")", "*", "TrackAttributes", "{", "ta", ".", "floatAttributes", "[", "\"", "\"", "]", "=", "speechiness", "\n", "return", "ta", "\n", "}" ]
// TargetSpeechiness sets the target speechiness. // Speechiness detects the presence of spoken words in a track. // The more exclusively speech-like the recording, the closer to 1.0 // the speechiness will be. // Values above 0.66 describe tracks that are probably made entirely of // spoken words. Values between 0.33 and 0.66 describe tracks that may // contain both music and speech, including such cases as rap music. // Values below 0.33 most likely represent music and other non-speech-like tracks.
[ "TargetSpeechiness", "sets", "the", "target", "speechiness", ".", "Speechiness", "detects", "the", "presence", "of", "spoken", "words", "in", "a", "track", ".", "The", "more", "exclusively", "speech", "-", "like", "the", "recording", "the", "closer", "to", "1", ".", "0", "the", "speechiness", "will", "be", ".", "Values", "above", "0", ".", "66", "describe", "tracks", "that", "are", "probably", "made", "entirely", "of", "spoken", "words", ".", "Values", "between", "0", ".", "33", "and", "0", ".", "66", "describe", "tracks", "that", "may", "contain", "both", "music", "and", "speech", "including", "such", "cases", "as", "rap", "music", ".", "Values", "below", "0", ".", "33", "most", "likely", "represent", "music", "and", "other", "non", "-", "speech", "-", "like", "tracks", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track_attributes.go#L355-L358
9,115
zmb3/spotify
audio_analysis.go
GetAudioAnalysis
func (c *Client) GetAudioAnalysis(id ID) (*AudioAnalysis, error) { url := fmt.Sprintf("%saudio-analysis/%s", c.baseURL, id) temp := AudioAnalysis{} err := c.get(url, &temp) if err != nil { return nil, err } return &temp, nil }
go
func (c *Client) GetAudioAnalysis(id ID) (*AudioAnalysis, error) { url := fmt.Sprintf("%saudio-analysis/%s", c.baseURL, id) temp := AudioAnalysis{} err := c.get(url, &temp) if err != nil { return nil, err } return &temp, nil }
[ "func", "(", "c", "*", "Client", ")", "GetAudioAnalysis", "(", "id", "ID", ")", "(", "*", "AudioAnalysis", ",", "error", ")", "{", "url", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "c", ".", "baseURL", ",", "id", ")", "\n\n", "temp", ":=", "AudioAnalysis", "{", "}", "\n\n", "err", ":=", "c", ".", "get", "(", "url", ",", "&", "temp", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "&", "temp", ",", "nil", "\n", "}" ]
// GetAudioAnalysis queries the Spotify web API for an audio analysis of a // single track.
[ "GetAudioAnalysis", "queries", "the", "Spotify", "web", "API", "for", "an", "audio", "analysis", "of", "a", "single", "track", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/audio_analysis.go#L99-L110
9,116
zmb3/spotify
search.go
NextArtistResults
func (c *Client) NextArtistResults(s *SearchResult) error { if s.Artists == nil || s.Artists.Next == "" { return ErrNoMorePages } return c.get(s.Artists.Next, s) }
go
func (c *Client) NextArtistResults(s *SearchResult) error { if s.Artists == nil || s.Artists.Next == "" { return ErrNoMorePages } return c.get(s.Artists.Next, s) }
[ "func", "(", "c", "*", "Client", ")", "NextArtistResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Artists", "==", "nil", "||", "s", ".", "Artists", ".", "Next", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Artists", ".", "Next", ",", "s", ")", "\n", "}" ]
// NextArtistResults loads the next page of artists into the specified search result.
[ "NextArtistResults", "loads", "the", "next", "page", "of", "artists", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L147-L152
9,117
zmb3/spotify
search.go
PreviousArtistResults
func (c *Client) PreviousArtistResults(s *SearchResult) error { if s.Artists == nil || s.Artists.Previous == "" { return ErrNoMorePages } return c.get(s.Artists.Previous, s) }
go
func (c *Client) PreviousArtistResults(s *SearchResult) error { if s.Artists == nil || s.Artists.Previous == "" { return ErrNoMorePages } return c.get(s.Artists.Previous, s) }
[ "func", "(", "c", "*", "Client", ")", "PreviousArtistResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Artists", "==", "nil", "||", "s", ".", "Artists", ".", "Previous", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Artists", ".", "Previous", ",", "s", ")", "\n", "}" ]
// PreviousArtistResults loads the previous page of artists into the specified search result.
[ "PreviousArtistResults", "loads", "the", "previous", "page", "of", "artists", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L155-L160
9,118
zmb3/spotify
search.go
NextAlbumResults
func (c *Client) NextAlbumResults(s *SearchResult) error { if s.Albums == nil || s.Albums.Next == "" { return ErrNoMorePages } return c.get(s.Albums.Next, s) }
go
func (c *Client) NextAlbumResults(s *SearchResult) error { if s.Albums == nil || s.Albums.Next == "" { return ErrNoMorePages } return c.get(s.Albums.Next, s) }
[ "func", "(", "c", "*", "Client", ")", "NextAlbumResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Albums", "==", "nil", "||", "s", ".", "Albums", ".", "Next", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Albums", ".", "Next", ",", "s", ")", "\n", "}" ]
// NextAlbumResults loads the next page of albums into the specified search result.
[ "NextAlbumResults", "loads", "the", "next", "page", "of", "albums", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L163-L168
9,119
zmb3/spotify
search.go
PreviousAlbumResults
func (c *Client) PreviousAlbumResults(s *SearchResult) error { if s.Albums == nil || s.Albums.Previous == "" { return ErrNoMorePages } return c.get(s.Albums.Previous, s) }
go
func (c *Client) PreviousAlbumResults(s *SearchResult) error { if s.Albums == nil || s.Albums.Previous == "" { return ErrNoMorePages } return c.get(s.Albums.Previous, s) }
[ "func", "(", "c", "*", "Client", ")", "PreviousAlbumResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Albums", "==", "nil", "||", "s", ".", "Albums", ".", "Previous", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Albums", ".", "Previous", ",", "s", ")", "\n", "}" ]
// PreviousAlbumResults loads the previous page of albums into the specified search result.
[ "PreviousAlbumResults", "loads", "the", "previous", "page", "of", "albums", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L171-L176
9,120
zmb3/spotify
search.go
NextPlaylistResults
func (c *Client) NextPlaylistResults(s *SearchResult) error { if s.Playlists == nil || s.Playlists.Next == "" { return ErrNoMorePages } return c.get(s.Playlists.Next, s) }
go
func (c *Client) NextPlaylistResults(s *SearchResult) error { if s.Playlists == nil || s.Playlists.Next == "" { return ErrNoMorePages } return c.get(s.Playlists.Next, s) }
[ "func", "(", "c", "*", "Client", ")", "NextPlaylistResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Playlists", "==", "nil", "||", "s", ".", "Playlists", ".", "Next", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Playlists", ".", "Next", ",", "s", ")", "\n", "}" ]
// NextPlaylistResults loads the next page of playlists into the specified search result.
[ "NextPlaylistResults", "loads", "the", "next", "page", "of", "playlists", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L179-L184
9,121
zmb3/spotify
search.go
PreviousPlaylistResults
func (c *Client) PreviousPlaylistResults(s *SearchResult) error { if s.Playlists == nil || s.Playlists.Previous == "" { return ErrNoMorePages } return c.get(s.Playlists.Previous, s) }
go
func (c *Client) PreviousPlaylistResults(s *SearchResult) error { if s.Playlists == nil || s.Playlists.Previous == "" { return ErrNoMorePages } return c.get(s.Playlists.Previous, s) }
[ "func", "(", "c", "*", "Client", ")", "PreviousPlaylistResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Playlists", "==", "nil", "||", "s", ".", "Playlists", ".", "Previous", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Playlists", ".", "Previous", ",", "s", ")", "\n", "}" ]
// PreviousPlaylistResults loads the previous page of playlists into the specified search result.
[ "PreviousPlaylistResults", "loads", "the", "previous", "page", "of", "playlists", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L187-L192
9,122
zmb3/spotify
search.go
PreviousTrackResults
func (c *Client) PreviousTrackResults(s *SearchResult) error { if s.Tracks == nil || s.Tracks.Previous == "" { return ErrNoMorePages } return c.get(s.Tracks.Previous, s) }
go
func (c *Client) PreviousTrackResults(s *SearchResult) error { if s.Tracks == nil || s.Tracks.Previous == "" { return ErrNoMorePages } return c.get(s.Tracks.Previous, s) }
[ "func", "(", "c", "*", "Client", ")", "PreviousTrackResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Tracks", "==", "nil", "||", "s", ".", "Tracks", ".", "Previous", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Tracks", ".", "Previous", ",", "s", ")", "\n", "}" ]
// PreviousTrackResults loads the previous page of tracks into the specified search result.
[ "PreviousTrackResults", "loads", "the", "previous", "page", "of", "tracks", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L195-L200
9,123
zmb3/spotify
search.go
NextTrackResults
func (c *Client) NextTrackResults(s *SearchResult) error { if s.Tracks == nil || s.Tracks.Next == "" { return ErrNoMorePages } return c.get(s.Tracks.Next, s) }
go
func (c *Client) NextTrackResults(s *SearchResult) error { if s.Tracks == nil || s.Tracks.Next == "" { return ErrNoMorePages } return c.get(s.Tracks.Next, s) }
[ "func", "(", "c", "*", "Client", ")", "NextTrackResults", "(", "s", "*", "SearchResult", ")", "error", "{", "if", "s", ".", "Tracks", "==", "nil", "||", "s", ".", "Tracks", ".", "Next", "==", "\"", "\"", "{", "return", "ErrNoMorePages", "\n", "}", "\n", "return", "c", ".", "get", "(", "s", ".", "Tracks", ".", "Next", ",", "s", ")", "\n", "}" ]
// NextTrackResults loads the next page of tracks into the specified search result.
[ "NextTrackResults", "loads", "the", "next", "page", "of", "tracks", "into", "the", "specified", "search", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/search.go#L203-L208
9,124
zmb3/spotify
track.go
TimeDuration
func (t *SimpleTrack) TimeDuration() time.Duration { return time.Duration(t.Duration) * time.Millisecond }
go
func (t *SimpleTrack) TimeDuration() time.Duration { return time.Duration(t.Duration) * time.Millisecond }
[ "func", "(", "t", "*", "SimpleTrack", ")", "TimeDuration", "(", ")", "time", ".", "Duration", "{", "return", "time", ".", "Duration", "(", "t", ".", "Duration", ")", "*", "time", ".", "Millisecond", "\n", "}" ]
// TimeDuration returns the track's duration as a time.Duration value.
[ "TimeDuration", "returns", "the", "track", "s", "duration", "as", "a", "time", ".", "Duration", "value", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track.go#L80-L82
9,125
zmb3/spotify
track.go
GetTrack
func (c *Client) GetTrack(id ID) (*FullTrack, error) { spotifyURL := c.baseURL + "tracks/" + string(id) var t FullTrack err := c.get(spotifyURL, &t) if err != nil { return nil, err } return &t, nil }
go
func (c *Client) GetTrack(id ID) (*FullTrack, error) { spotifyURL := c.baseURL + "tracks/" + string(id) var t FullTrack err := c.get(spotifyURL, &t) if err != nil { return nil, err } return &t, nil }
[ "func", "(", "c", "*", "Client", ")", "GetTrack", "(", "id", "ID", ")", "(", "*", "FullTrack", ",", "error", ")", "{", "spotifyURL", ":=", "c", ".", "baseURL", "+", "\"", "\"", "+", "string", "(", "id", ")", "\n\n", "var", "t", "FullTrack", "\n\n", "err", ":=", "c", ".", "get", "(", "spotifyURL", ",", "&", "t", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "&", "t", ",", "nil", "\n", "}" ]
// GetTrack gets Spotify catalog information for // a single track identified by its unique Spotify ID.
[ "GetTrack", "gets", "Spotify", "catalog", "information", "for", "a", "single", "track", "identified", "by", "its", "unique", "Spotify", "ID", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track.go#L86-L97
9,126
zmb3/spotify
track.go
GetTracks
func (c *Client) GetTracks(ids ...ID) ([]*FullTrack, error) { if len(ids) > 50 { return nil, errors.New("spotify: FindTracks supports up to 50 tracks") } spotifyURL := c.baseURL + "tracks?ids=" + strings.Join(toStringSlice(ids), ",") var t struct { Tracks []*FullTrack `json:"tracks"` } err := c.get(spotifyURL, &t) if err != nil { return nil, err } return t.Tracks, nil }
go
func (c *Client) GetTracks(ids ...ID) ([]*FullTrack, error) { if len(ids) > 50 { return nil, errors.New("spotify: FindTracks supports up to 50 tracks") } spotifyURL := c.baseURL + "tracks?ids=" + strings.Join(toStringSlice(ids), ",") var t struct { Tracks []*FullTrack `json:"tracks"` } err := c.get(spotifyURL, &t) if err != nil { return nil, err } return t.Tracks, nil }
[ "func", "(", "c", "*", "Client", ")", "GetTracks", "(", "ids", "...", "ID", ")", "(", "[", "]", "*", "FullTrack", ",", "error", ")", "{", "if", "len", "(", "ids", ")", ">", "50", "{", "return", "nil", ",", "errors", ".", "New", "(", "\"", "\"", ")", "\n", "}", "\n", "spotifyURL", ":=", "c", ".", "baseURL", "+", "\"", "\"", "+", "strings", ".", "Join", "(", "toStringSlice", "(", "ids", ")", ",", "\"", "\"", ")", "\n\n", "var", "t", "struct", "{", "Tracks", "[", "]", "*", "FullTrack", "`json:\"tracks\"`", "\n", "}", "\n\n", "err", ":=", "c", ".", "get", "(", "spotifyURL", ",", "&", "t", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "t", ".", "Tracks", ",", "nil", "\n", "}" ]
// GetTracks gets Spotify catalog information for multiple tracks based on their // Spotify IDs. It supports up to 50 tracks in a single call. Tracks are // returned in the order requested. If a track is not found, that position in the // result will be nil. Duplicate ids in the query will result in duplicate // tracks in the result.
[ "GetTracks", "gets", "Spotify", "catalog", "information", "for", "multiple", "tracks", "based", "on", "their", "Spotify", "IDs", ".", "It", "supports", "up", "to", "50", "tracks", "in", "a", "single", "call", ".", "Tracks", "are", "returned", "in", "the", "order", "requested", ".", "If", "a", "track", "is", "not", "found", "that", "position", "in", "the", "result", "will", "be", "nil", ".", "Duplicate", "ids", "in", "the", "query", "will", "result", "in", "duplicate", "tracks", "in", "the", "result", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/track.go#L104-L120
9,127
zmb3/spotify
audio_features.go
GetAudioFeatures
func (c *Client) GetAudioFeatures(ids ...ID) ([]*AudioFeatures, error) { url := fmt.Sprintf("%saudio-features?ids=%s", c.baseURL, strings.Join(toStringSlice(ids), ",")) temp := struct { F []*AudioFeatures `json:"audio_features"` }{} err := c.get(url, &temp) if err != nil { return nil, err } return temp.F, nil }
go
func (c *Client) GetAudioFeatures(ids ...ID) ([]*AudioFeatures, error) { url := fmt.Sprintf("%saudio-features?ids=%s", c.baseURL, strings.Join(toStringSlice(ids), ",")) temp := struct { F []*AudioFeatures `json:"audio_features"` }{} err := c.get(url, &temp) if err != nil { return nil, err } return temp.F, nil }
[ "func", "(", "c", "*", "Client", ")", "GetAudioFeatures", "(", "ids", "...", "ID", ")", "(", "[", "]", "*", "AudioFeatures", ",", "error", ")", "{", "url", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "c", ".", "baseURL", ",", "strings", ".", "Join", "(", "toStringSlice", "(", "ids", ")", ",", "\"", "\"", ")", ")", "\n\n", "temp", ":=", "struct", "{", "F", "[", "]", "*", "AudioFeatures", "`json:\"audio_features\"`", "\n", "}", "{", "}", "\n\n", "err", ":=", "c", ".", "get", "(", "url", ",", "&", "temp", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "temp", ".", "F", ",", "nil", "\n", "}" ]
// GetAudioFeatures queries the Spotify Web API for various // high-level acoustic attributes of audio tracks. // Objects are returned in the order requested. If an object // is not found, a nil value is returned in the appropriate position.
[ "GetAudioFeatures", "queries", "the", "Spotify", "Web", "API", "for", "various", "high", "-", "level", "acoustic", "attributes", "of", "audio", "tracks", ".", "Objects", "are", "returned", "in", "the", "order", "requested", ".", "If", "an", "object", "is", "not", "found", "a", "nil", "value", "is", "returned", "in", "the", "appropriate", "position", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/audio_features.go#L110-L123
9,128
zmb3/spotify
recommendation.go
count
func (s Seeds) count() int { return len(s.Artists) + len(s.Tracks) + len(s.Genres) }
go
func (s Seeds) count() int { return len(s.Artists) + len(s.Tracks) + len(s.Genres) }
[ "func", "(", "s", "Seeds", ")", "count", "(", ")", "int", "{", "return", "len", "(", "s", ".", "Artists", ")", "+", "len", "(", "s", ".", "Tracks", ")", "+", "len", "(", "s", ".", "Genres", ")", "\n", "}" ]
// count returns the total number of seeds contained in s
[ "count", "returns", "the", "total", "number", "of", "seeds", "contained", "in", "s" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/recommendation.go#L19-L21
9,129
zmb3/spotify
recommendation.go
setSeedValues
func setSeedValues(seeds Seeds, v url.Values) { if len(seeds.Artists) != 0 { v.Set("seed_artists", strings.Join(toStringSlice(seeds.Artists), ",")) } if len(seeds.Tracks) != 0 { v.Set("seed_tracks", strings.Join(toStringSlice(seeds.Tracks), ",")) } if len(seeds.Genres) != 0 { v.Set("seed_genres", strings.Join(seeds.Genres, ",")) } }
go
func setSeedValues(seeds Seeds, v url.Values) { if len(seeds.Artists) != 0 { v.Set("seed_artists", strings.Join(toStringSlice(seeds.Artists), ",")) } if len(seeds.Tracks) != 0 { v.Set("seed_tracks", strings.Join(toStringSlice(seeds.Tracks), ",")) } if len(seeds.Genres) != 0 { v.Set("seed_genres", strings.Join(seeds.Genres, ",")) } }
[ "func", "setSeedValues", "(", "seeds", "Seeds", ",", "v", "url", ".", "Values", ")", "{", "if", "len", "(", "seeds", ".", "Artists", ")", "!=", "0", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strings", ".", "Join", "(", "toStringSlice", "(", "seeds", ".", "Artists", ")", ",", "\"", "\"", ")", ")", "\n", "}", "\n", "if", "len", "(", "seeds", ".", "Tracks", ")", "!=", "0", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strings", ".", "Join", "(", "toStringSlice", "(", "seeds", ".", "Tracks", ")", ",", "\"", "\"", ")", ")", "\n", "}", "\n", "if", "len", "(", "seeds", ".", "Genres", ")", "!=", "0", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strings", ".", "Join", "(", "seeds", ".", "Genres", ",", "\"", "\"", ")", ")", "\n", "}", "\n", "}" ]
// setSeedValues sets url values into v for each seed in seeds
[ "setSeedValues", "sets", "url", "values", "into", "v", "for", "each", "seed", "in", "seeds" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/recommendation.go#L44-L54
9,130
zmb3/spotify
recommendation.go
setTrackAttributesValues
func setTrackAttributesValues(trackAttributes *TrackAttributes, values url.Values) { if trackAttributes == nil { return } for attr, val := range trackAttributes.intAttributes { values.Set(attr, strconv.Itoa(val)) } for attr, val := range trackAttributes.floatAttributes { values.Set(attr, strconv.FormatFloat(val, 'f', -1, 64)) } }
go
func setTrackAttributesValues(trackAttributes *TrackAttributes, values url.Values) { if trackAttributes == nil { return } for attr, val := range trackAttributes.intAttributes { values.Set(attr, strconv.Itoa(val)) } for attr, val := range trackAttributes.floatAttributes { values.Set(attr, strconv.FormatFloat(val, 'f', -1, 64)) } }
[ "func", "setTrackAttributesValues", "(", "trackAttributes", "*", "TrackAttributes", ",", "values", "url", ".", "Values", ")", "{", "if", "trackAttributes", "==", "nil", "{", "return", "\n", "}", "\n", "for", "attr", ",", "val", ":=", "range", "trackAttributes", ".", "intAttributes", "{", "values", ".", "Set", "(", "attr", ",", "strconv", ".", "Itoa", "(", "val", ")", ")", "\n", "}", "\n", "for", "attr", ",", "val", ":=", "range", "trackAttributes", ".", "floatAttributes", "{", "values", ".", "Set", "(", "attr", ",", "strconv", ".", "FormatFloat", "(", "val", ",", "'f'", ",", "-", "1", ",", "64", ")", ")", "\n", "}", "\n", "}" ]
// setTrackAttributesValues sets track attributes values to the given url values
[ "setTrackAttributesValues", "sets", "track", "attributes", "values", "to", "the", "given", "url", "values" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/recommendation.go#L57-L67
9,131
zmb3/spotify
recommendation.go
GetRecommendations
func (c *Client) GetRecommendations(seeds Seeds, trackAttributes *TrackAttributes, opt *Options) (*Recommendations, error) { v := url.Values{} if seeds.count() == 0 { return nil, fmt.Errorf("spotify: at least one seed is required") } if seeds.count() > MaxNumberOfSeeds { return nil, fmt.Errorf("spotify: exceeded maximum of %d seeds", MaxNumberOfSeeds) } setSeedValues(seeds, v) setTrackAttributesValues(trackAttributes, v) if opt != nil { if opt.Limit != nil { v.Set("limit", strconv.Itoa(*opt.Limit)) } if opt.Country != nil { v.Set("market", *opt.Country) } } spotifyURL := c.baseURL + "recommendations?" + v.Encode() var recommendations Recommendations err := c.get(spotifyURL, &recommendations) if err != nil { return nil, err } return &recommendations, err }
go
func (c *Client) GetRecommendations(seeds Seeds, trackAttributes *TrackAttributes, opt *Options) (*Recommendations, error) { v := url.Values{} if seeds.count() == 0 { return nil, fmt.Errorf("spotify: at least one seed is required") } if seeds.count() > MaxNumberOfSeeds { return nil, fmt.Errorf("spotify: exceeded maximum of %d seeds", MaxNumberOfSeeds) } setSeedValues(seeds, v) setTrackAttributesValues(trackAttributes, v) if opt != nil { if opt.Limit != nil { v.Set("limit", strconv.Itoa(*opt.Limit)) } if opt.Country != nil { v.Set("market", *opt.Country) } } spotifyURL := c.baseURL + "recommendations?" + v.Encode() var recommendations Recommendations err := c.get(spotifyURL, &recommendations) if err != nil { return nil, err } return &recommendations, err }
[ "func", "(", "c", "*", "Client", ")", "GetRecommendations", "(", "seeds", "Seeds", ",", "trackAttributes", "*", "TrackAttributes", ",", "opt", "*", "Options", ")", "(", "*", "Recommendations", ",", "error", ")", "{", "v", ":=", "url", ".", "Values", "{", "}", "\n\n", "if", "seeds", ".", "count", "(", ")", "==", "0", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ")", "\n", "}", "\n", "if", "seeds", ".", "count", "(", ")", ">", "MaxNumberOfSeeds", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "MaxNumberOfSeeds", ")", "\n", "}", "\n\n", "setSeedValues", "(", "seeds", ",", "v", ")", "\n", "setTrackAttributesValues", "(", "trackAttributes", ",", "v", ")", "\n\n", "if", "opt", "!=", "nil", "{", "if", "opt", ".", "Limit", "!=", "nil", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strconv", ".", "Itoa", "(", "*", "opt", ".", "Limit", ")", ")", "\n", "}", "\n", "if", "opt", ".", "Country", "!=", "nil", "{", "v", ".", "Set", "(", "\"", "\"", ",", "*", "opt", ".", "Country", ")", "\n", "}", "\n", "}", "\n\n", "spotifyURL", ":=", "c", ".", "baseURL", "+", "\"", "\"", "+", "v", ".", "Encode", "(", ")", "\n\n", "var", "recommendations", "Recommendations", "\n", "err", ":=", "c", ".", "get", "(", "spotifyURL", ",", "&", "recommendations", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "&", "recommendations", ",", "err", "\n", "}" ]
// GetRecommendations returns a list of recommended tracks based on the given seeds. // Recommendations are generated based on the available information for a given seed entity // and matched against similar artists and tracks. If there is sufficient information // about the provided seeds, a list of tracks will be returned together with pool size details. // For artists and tracks that are very new or obscure // there might not be enough data to generate a list of tracks.
[ "GetRecommendations", "returns", "a", "list", "of", "recommended", "tracks", "based", "on", "the", "given", "seeds", ".", "Recommendations", "are", "generated", "based", "on", "the", "available", "information", "for", "a", "given", "seed", "entity", "and", "matched", "against", "similar", "artists", "and", "tracks", ".", "If", "there", "is", "sufficient", "information", "about", "the", "provided", "seeds", "a", "list", "of", "tracks", "will", "be", "returned", "together", "with", "pool", "size", "details", ".", "For", "artists", "and", "tracks", "that", "are", "very", "new", "or", "obscure", "there", "might", "not", "be", "enough", "data", "to", "generate", "a", "list", "of", "tracks", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/recommendation.go#L75-L106
9,132
zmb3/spotify
recommendation.go
GetAvailableGenreSeeds
func (c *Client) GetAvailableGenreSeeds() ([]string, error) { spotifyURL := c.baseURL + "recommendations/available-genre-seeds" genreSeeds := make(map[string][]string) err := c.get(spotifyURL, &genreSeeds) if err != nil { return nil, err } return genreSeeds["genres"], nil }
go
func (c *Client) GetAvailableGenreSeeds() ([]string, error) { spotifyURL := c.baseURL + "recommendations/available-genre-seeds" genreSeeds := make(map[string][]string) err := c.get(spotifyURL, &genreSeeds) if err != nil { return nil, err } return genreSeeds["genres"], nil }
[ "func", "(", "c", "*", "Client", ")", "GetAvailableGenreSeeds", "(", ")", "(", "[", "]", "string", ",", "error", ")", "{", "spotifyURL", ":=", "c", ".", "baseURL", "+", "\"", "\"", "\n\n", "genreSeeds", ":=", "make", "(", "map", "[", "string", "]", "[", "]", "string", ")", "\n\n", "err", ":=", "c", ".", "get", "(", "spotifyURL", ",", "&", "genreSeeds", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "genreSeeds", "[", "\"", "\"", "]", ",", "nil", "\n", "}" ]
// GetAvailableGenreSeeds retrieves a list of available genres seed parameter values for // recommendations.
[ "GetAvailableGenreSeeds", "retrieves", "a", "list", "of", "available", "genres", "seed", "parameter", "values", "for", "recommendations", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/recommendation.go#L110-L121
9,133
zmb3/spotify
spotify.go
Download
func (i Image) Download(dst io.Writer) error { resp, err := http.Get(i.URL) if err != nil { return err } defer resp.Body.Close() // TODO: get Content-Type from header? if resp.StatusCode != http.StatusOK { return errors.New("Couldn't download image - HTTP" + strconv.Itoa(resp.StatusCode)) } _, err = io.Copy(dst, resp.Body) return err }
go
func (i Image) Download(dst io.Writer) error { resp, err := http.Get(i.URL) if err != nil { return err } defer resp.Body.Close() // TODO: get Content-Type from header? if resp.StatusCode != http.StatusOK { return errors.New("Couldn't download image - HTTP" + strconv.Itoa(resp.StatusCode)) } _, err = io.Copy(dst, resp.Body) return err }
[ "func", "(", "i", "Image", ")", "Download", "(", "dst", "io", ".", "Writer", ")", "error", "{", "resp", ",", "err", ":=", "http", ".", "Get", "(", "i", ".", "URL", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "resp", ".", "Body", ".", "Close", "(", ")", "\n", "// TODO: get Content-Type from header?", "if", "resp", ".", "StatusCode", "!=", "http", ".", "StatusOK", "{", "return", "errors", ".", "New", "(", "\"", "\"", "+", "strconv", ".", "Itoa", "(", "resp", ".", "StatusCode", ")", ")", "\n", "}", "\n", "_", ",", "err", "=", "io", ".", "Copy", "(", "dst", ",", "resp", ".", "Body", ")", "\n", "return", "err", "\n", "}" ]
// Download downloads the image and writes its data to the specified io.Writer.
[ "Download", "downloads", "the", "image", "and", "writes", "its", "data", "to", "the", "specified", "io", ".", "Writer", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L85-L97
9,134
zmb3/spotify
spotify.go
decodeError
func (c *Client) decodeError(resp *http.Response) error { responseBody, err := ioutil.ReadAll(resp.Body) if err != nil { return err } if len(responseBody) == 0 { return fmt.Errorf("spotify: HTTP %d: %s (body empty)", resp.StatusCode, http.StatusText(resp.StatusCode)) } buf := bytes.NewBuffer(responseBody) var e struct { E Error `json:"error"` } err = json.NewDecoder(buf).Decode(&e) if err != nil { return fmt.Errorf("spotify: couldn't decode error: (%d) [%s]", len(responseBody), responseBody) } if e.E.Message == "" { // Some errors will result in there being a useful status-code but an // empty message, which will confuse the user (who only has access to // the message and not the code). An example of this is when we send // some of the arguments directly in the HTTP query and the URL ends-up // being too long. e.E.Message = fmt.Sprintf("spotify: unexpected HTTP %d: %s (empty error)", resp.StatusCode, http.StatusText(resp.StatusCode)) } return e.E }
go
func (c *Client) decodeError(resp *http.Response) error { responseBody, err := ioutil.ReadAll(resp.Body) if err != nil { return err } if len(responseBody) == 0 { return fmt.Errorf("spotify: HTTP %d: %s (body empty)", resp.StatusCode, http.StatusText(resp.StatusCode)) } buf := bytes.NewBuffer(responseBody) var e struct { E Error `json:"error"` } err = json.NewDecoder(buf).Decode(&e) if err != nil { return fmt.Errorf("spotify: couldn't decode error: (%d) [%s]", len(responseBody), responseBody) } if e.E.Message == "" { // Some errors will result in there being a useful status-code but an // empty message, which will confuse the user (who only has access to // the message and not the code). An example of this is when we send // some of the arguments directly in the HTTP query and the URL ends-up // being too long. e.E.Message = fmt.Sprintf("spotify: unexpected HTTP %d: %s (empty error)", resp.StatusCode, http.StatusText(resp.StatusCode)) } return e.E }
[ "func", "(", "c", "*", "Client", ")", "decodeError", "(", "resp", "*", "http", ".", "Response", ")", "error", "{", "responseBody", ",", "err", ":=", "ioutil", ".", "ReadAll", "(", "resp", ".", "Body", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "if", "len", "(", "responseBody", ")", "==", "0", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "resp", ".", "StatusCode", ",", "http", ".", "StatusText", "(", "resp", ".", "StatusCode", ")", ")", "\n", "}", "\n\n", "buf", ":=", "bytes", ".", "NewBuffer", "(", "responseBody", ")", "\n\n", "var", "e", "struct", "{", "E", "Error", "`json:\"error\"`", "\n", "}", "\n", "err", "=", "json", ".", "NewDecoder", "(", "buf", ")", ".", "Decode", "(", "&", "e", ")", "\n", "if", "err", "!=", "nil", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "len", "(", "responseBody", ")", ",", "responseBody", ")", "\n", "}", "\n\n", "if", "e", ".", "E", ".", "Message", "==", "\"", "\"", "{", "// Some errors will result in there being a useful status-code but an", "// empty message, which will confuse the user (who only has access to", "// the message and not the code). An example of this is when we send", "// some of the arguments directly in the HTTP query and the URL ends-up", "// being too long.", "e", ".", "E", ".", "Message", "=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "resp", ".", "StatusCode", ",", "http", ".", "StatusText", "(", "resp", ".", "StatusCode", ")", ")", "\n", "}", "\n\n", "return", "e", ".", "E", "\n", "}" ]
// decodeError decodes an Error from an io.Reader.
[ "decodeError", "decodes", "an", "Error", "from", "an", "io", ".", "Reader", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L112-L144
9,135
zmb3/spotify
spotify.go
shouldRetry
func shouldRetry(status int) bool { return status == http.StatusAccepted || status == http.StatusTooManyRequests }
go
func shouldRetry(status int) bool { return status == http.StatusAccepted || status == http.StatusTooManyRequests }
[ "func", "shouldRetry", "(", "status", "int", ")", "bool", "{", "return", "status", "==", "http", ".", "StatusAccepted", "||", "status", "==", "http", ".", "StatusTooManyRequests", "\n", "}" ]
// shouldRetry determines whether the status code indicates that the // previous operation should be retried at a later time
[ "shouldRetry", "determines", "whether", "the", "status", "code", "indicates", "that", "the", "previous", "operation", "should", "be", "retried", "at", "a", "later", "time" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L148-L150
9,136
zmb3/spotify
spotify.go
isFailure
func isFailure(code int, validCodes []int) bool { for _, item := range validCodes { if item == code { return false } } return true }
go
func isFailure(code int, validCodes []int) bool { for _, item := range validCodes { if item == code { return false } } return true }
[ "func", "isFailure", "(", "code", "int", ",", "validCodes", "[", "]", "int", ")", "bool", "{", "for", "_", ",", "item", ":=", "range", "validCodes", "{", "if", "item", "==", "code", "{", "return", "false", "\n", "}", "\n", "}", "\n", "return", "true", "\n", "}" ]
// isFailure determines whether the code indicates failure
[ "isFailure", "determines", "whether", "the", "code", "indicates", "failure" ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L153-L160
9,137
zmb3/spotify
spotify.go
execute
func (c *Client) execute(req *http.Request, result interface{}, needsStatus ...int) error { for { resp, err := c.http.Do(req) if err != nil { return err } defer resp.Body.Close() if c.AutoRetry && shouldRetry(resp.StatusCode) { time.Sleep(retryDuration(resp)) continue } if resp.StatusCode != http.StatusOK && isFailure(resp.StatusCode, needsStatus) { return c.decodeError(resp) } if result != nil { if err := json.NewDecoder(resp.Body).Decode(result); err != nil { return err } } break } return nil }
go
func (c *Client) execute(req *http.Request, result interface{}, needsStatus ...int) error { for { resp, err := c.http.Do(req) if err != nil { return err } defer resp.Body.Close() if c.AutoRetry && shouldRetry(resp.StatusCode) { time.Sleep(retryDuration(resp)) continue } if resp.StatusCode != http.StatusOK && isFailure(resp.StatusCode, needsStatus) { return c.decodeError(resp) } if result != nil { if err := json.NewDecoder(resp.Body).Decode(result); err != nil { return err } } break } return nil }
[ "func", "(", "c", "*", "Client", ")", "execute", "(", "req", "*", "http", ".", "Request", ",", "result", "interface", "{", "}", ",", "needsStatus", "...", "int", ")", "error", "{", "for", "{", "resp", ",", "err", ":=", "c", ".", "http", ".", "Do", "(", "req", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "resp", ".", "Body", ".", "Close", "(", ")", "\n\n", "if", "c", ".", "AutoRetry", "&&", "shouldRetry", "(", "resp", ".", "StatusCode", ")", "{", "time", ".", "Sleep", "(", "retryDuration", "(", "resp", ")", ")", "\n", "continue", "\n", "}", "\n", "if", "resp", ".", "StatusCode", "!=", "http", ".", "StatusOK", "&&", "isFailure", "(", "resp", ".", "StatusCode", ",", "needsStatus", ")", "{", "return", "c", ".", "decodeError", "(", "resp", ")", "\n", "}", "\n\n", "if", "result", "!=", "nil", "{", "if", "err", ":=", "json", ".", "NewDecoder", "(", "resp", ".", "Body", ")", ".", "Decode", "(", "result", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n", "break", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// execute executes a non-GET request. `needsStatus` describes other HTTP status codes // that can represent success. Note that in all current usages of this function, // we need to still allow a 200 even if we'd also like to check for additional // success codes.
[ "execute", "executes", "a", "non", "-", "GET", "request", ".", "needsStatus", "describes", "other", "HTTP", "status", "codes", "that", "can", "represent", "success", ".", "Note", "that", "in", "all", "current", "usages", "of", "this", "function", "we", "need", "to", "still", "allow", "a", "200", "even", "if", "we", "d", "also", "like", "to", "check", "for", "additional", "success", "codes", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L166-L190
9,138
zmb3/spotify
spotify.go
NewReleasesOpt
func (c *Client) NewReleasesOpt(opt *Options) (albums *SimpleAlbumPage, err error) { spotifyURL := c.baseURL + "browse/new-releases" if opt != nil { v := url.Values{} if opt.Country != nil { v.Set("country", *opt.Country) } if opt.Limit != nil { v.Set("limit", strconv.Itoa(*opt.Limit)) } if opt.Offset != nil { v.Set("offset", strconv.Itoa(*opt.Offset)) } if params := v.Encode(); params != "" { spotifyURL += "?" + params } } var objmap map[string]*json.RawMessage err = c.get(spotifyURL, &objmap) if err != nil { return nil, err } var result SimpleAlbumPage err = json.Unmarshal(*objmap["albums"], &result) if err != nil { return nil, err } return &result, nil }
go
func (c *Client) NewReleasesOpt(opt *Options) (albums *SimpleAlbumPage, err error) { spotifyURL := c.baseURL + "browse/new-releases" if opt != nil { v := url.Values{} if opt.Country != nil { v.Set("country", *opt.Country) } if opt.Limit != nil { v.Set("limit", strconv.Itoa(*opt.Limit)) } if opt.Offset != nil { v.Set("offset", strconv.Itoa(*opt.Offset)) } if params := v.Encode(); params != "" { spotifyURL += "?" + params } } var objmap map[string]*json.RawMessage err = c.get(spotifyURL, &objmap) if err != nil { return nil, err } var result SimpleAlbumPage err = json.Unmarshal(*objmap["albums"], &result) if err != nil { return nil, err } return &result, nil }
[ "func", "(", "c", "*", "Client", ")", "NewReleasesOpt", "(", "opt", "*", "Options", ")", "(", "albums", "*", "SimpleAlbumPage", ",", "err", "error", ")", "{", "spotifyURL", ":=", "c", ".", "baseURL", "+", "\"", "\"", "\n", "if", "opt", "!=", "nil", "{", "v", ":=", "url", ".", "Values", "{", "}", "\n", "if", "opt", ".", "Country", "!=", "nil", "{", "v", ".", "Set", "(", "\"", "\"", ",", "*", "opt", ".", "Country", ")", "\n", "}", "\n", "if", "opt", ".", "Limit", "!=", "nil", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strconv", ".", "Itoa", "(", "*", "opt", ".", "Limit", ")", ")", "\n", "}", "\n", "if", "opt", ".", "Offset", "!=", "nil", "{", "v", ".", "Set", "(", "\"", "\"", ",", "strconv", ".", "Itoa", "(", "*", "opt", ".", "Offset", ")", ")", "\n", "}", "\n", "if", "params", ":=", "v", ".", "Encode", "(", ")", ";", "params", "!=", "\"", "\"", "{", "spotifyURL", "+=", "\"", "\"", "+", "params", "\n", "}", "\n", "}", "\n\n", "var", "objmap", "map", "[", "string", "]", "*", "json", ".", "RawMessage", "\n", "err", "=", "c", ".", "get", "(", "spotifyURL", ",", "&", "objmap", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "var", "result", "SimpleAlbumPage", "\n", "err", "=", "json", ".", "Unmarshal", "(", "*", "objmap", "[", "\"", "\"", "]", ",", "&", "result", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "&", "result", ",", "nil", "\n", "}" ]
// NewReleasesOpt is like NewReleases, but it accepts optional parameters // for filtering the results.
[ "NewReleasesOpt", "is", "like", "NewReleases", "but", "it", "accepts", "optional", "parameters", "for", "filtering", "the", "results", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L257-L288
9,139
zmb3/spotify
spotify.go
NewReleases
func (c *Client) NewReleases() (albums *SimpleAlbumPage, err error) { return c.NewReleasesOpt(nil) }
go
func (c *Client) NewReleases() (albums *SimpleAlbumPage, err error) { return c.NewReleasesOpt(nil) }
[ "func", "(", "c", "*", "Client", ")", "NewReleases", "(", ")", "(", "albums", "*", "SimpleAlbumPage", ",", "err", "error", ")", "{", "return", "c", ".", "NewReleasesOpt", "(", "nil", ")", "\n", "}" ]
// NewReleases gets a list of new album releases featured in Spotify. // This call requires bearer authorization.
[ "NewReleases", "gets", "a", "list", "of", "new", "album", "releases", "featured", "in", "Spotify", ".", "This", "call", "requires", "bearer", "authorization", "." ]
94cbe6dc5cc2eaa467e36f30b130b0f827e4262d
https://github.com/zmb3/spotify/blob/94cbe6dc5cc2eaa467e36f30b130b0f827e4262d/spotify.go#L292-L294
9,140
yourbasic/graph
graph.go
Check
func Check(g Iterator) Stats { if g, ok := g.(*Immutable); ok { return g.stats } _, mutable := g.(*Mutable) n := g.Order() degree := make([]int, n) type edge struct{ v, w int } edges := make(map[edge]bool) var stats Stats for v := 0; v < n; v++ { g.Visit(v, func(w int, c int64) (skip bool) { if w < 0 || w >= n { panic("vertex out of range: " + strconv.Itoa(w)) } if v == w { stats.Loops++ } if c != 0 { stats.Weighted++ } degree[v]++ if mutable { // A Mutable is never a multigraph. stats.Size++ return } if edges[edge{v, w}] { stats.Multi++ } else { stats.Size++ } edges[edge{v, w}] = true return }) } for _, deg := range degree { if deg == 0 { stats.Isolated++ } } return stats }
go
func Check(g Iterator) Stats { if g, ok := g.(*Immutable); ok { return g.stats } _, mutable := g.(*Mutable) n := g.Order() degree := make([]int, n) type edge struct{ v, w int } edges := make(map[edge]bool) var stats Stats for v := 0; v < n; v++ { g.Visit(v, func(w int, c int64) (skip bool) { if w < 0 || w >= n { panic("vertex out of range: " + strconv.Itoa(w)) } if v == w { stats.Loops++ } if c != 0 { stats.Weighted++ } degree[v]++ if mutable { // A Mutable is never a multigraph. stats.Size++ return } if edges[edge{v, w}] { stats.Multi++ } else { stats.Size++ } edges[edge{v, w}] = true return }) } for _, deg := range degree { if deg == 0 { stats.Isolated++ } } return stats }
[ "func", "Check", "(", "g", "Iterator", ")", "Stats", "{", "if", "g", ",", "ok", ":=", "g", ".", "(", "*", "Immutable", ")", ";", "ok", "{", "return", "g", ".", "stats", "\n", "}", "\n", "_", ",", "mutable", ":=", "g", ".", "(", "*", "Mutable", ")", "\n\n", "n", ":=", "g", ".", "Order", "(", ")", "\n", "degree", ":=", "make", "(", "[", "]", "int", ",", "n", ")", "\n", "type", "edge", "struct", "{", "v", ",", "w", "int", "}", "\n", "edges", ":=", "make", "(", "map", "[", "edge", "]", "bool", ")", "\n", "var", "stats", "Stats", "\n", "for", "v", ":=", "0", ";", "v", "<", "n", ";", "v", "++", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "if", "w", "<", "0", "||", "w", ">=", "n", "{", "panic", "(", "\"", "\"", "+", "strconv", ".", "Itoa", "(", "w", ")", ")", "\n", "}", "\n", "if", "v", "==", "w", "{", "stats", ".", "Loops", "++", "\n", "}", "\n", "if", "c", "!=", "0", "{", "stats", ".", "Weighted", "++", "\n", "}", "\n", "degree", "[", "v", "]", "++", "\n", "if", "mutable", "{", "// A Mutable is never a multigraph.", "stats", ".", "Size", "++", "\n", "return", "\n", "}", "\n", "if", "edges", "[", "edge", "{", "v", ",", "w", "}", "]", "{", "stats", ".", "Multi", "++", "\n", "}", "else", "{", "stats", ".", "Size", "++", "\n", "}", "\n", "edges", "[", "edge", "{", "v", ",", "w", "}", "]", "=", "true", "\n", "return", "\n", "}", ")", "\n", "}", "\n", "for", "_", ",", "deg", ":=", "range", "degree", "{", "if", "deg", "==", "0", "{", "stats", ".", "Isolated", "++", "\n", "}", "\n", "}", "\n", "return", "stats", "\n", "}" ]
// Check collects data about an Iterator.
[ "Check", "collects", "data", "about", "an", "Iterator", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/graph.go#L181-L223
9,141
yourbasic/graph
graph.go
Equal
func Equal(g, h Iterator) bool { if g.Order() != h.Order() { return false } edges := make(map[edge]int) for v := 0; v < g.Order(); v++ { g.Visit(v, func(w int, c int64) (skip bool) { edges[edge{v, w, c}]++ return }) if h.Visit(v, func(w int, c int64) (skip bool) { if edges[edge{v, w, c}] == 0 { return true } edges[edge{v, w, c}]-- return }) { return false } for _, n := range edges { if n > 0 { return false } } } return true }
go
func Equal(g, h Iterator) bool { if g.Order() != h.Order() { return false } edges := make(map[edge]int) for v := 0; v < g.Order(); v++ { g.Visit(v, func(w int, c int64) (skip bool) { edges[edge{v, w, c}]++ return }) if h.Visit(v, func(w int, c int64) (skip bool) { if edges[edge{v, w, c}] == 0 { return true } edges[edge{v, w, c}]-- return }) { return false } for _, n := range edges { if n > 0 { return false } } } return true }
[ "func", "Equal", "(", "g", ",", "h", "Iterator", ")", "bool", "{", "if", "g", ".", "Order", "(", ")", "!=", "h", ".", "Order", "(", ")", "{", "return", "false", "\n", "}", "\n", "edges", ":=", "make", "(", "map", "[", "edge", "]", "int", ")", "\n", "for", "v", ":=", "0", ";", "v", "<", "g", ".", "Order", "(", ")", ";", "v", "++", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "edges", "[", "edge", "{", "v", ",", "w", ",", "c", "}", "]", "++", "\n", "return", "\n", "}", ")", "\n", "if", "h", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "if", "edges", "[", "edge", "{", "v", ",", "w", ",", "c", "}", "]", "==", "0", "{", "return", "true", "\n", "}", "\n", "edges", "[", "edge", "{", "v", ",", "w", ",", "c", "}", "]", "--", "\n", "return", "\n", "}", ")", "{", "return", "false", "\n", "}", "\n", "for", "_", ",", "n", ":=", "range", "edges", "{", "if", "n", ">", "0", "{", "return", "false", "\n", "}", "\n", "}", "\n", "}", "\n", "return", "true", "\n", "}" ]
// Equal tells if g and h have the same number of vertices, // and the same edges with the same costs.
[ "Equal", "tells", "if", "g", "and", "h", "have", "the", "same", "number", "of", "vertices", "and", "the", "same", "edges", "with", "the", "same", "costs", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/graph.go#L227-L253
9,142
yourbasic/graph
build/subgraph.go
Subgraph
func (g *Virtual) Subgraph(s VertexSet) *Virtual { n := g.Order() s = s.And(Range(0, n)) m := s.size() switch { case m == 0: return null case m == 1: return singleton() case m == n: return g } cost := func(v, w int) int64 { return g.cost(s.get(v), s.get(w)) } res := generic(m, cost, func(v, w int) bool { return g.edge(s.get(v), s.get(w)) }) res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { if a >= m { return } v0 := s.get(v) s0 := s.And(Range(s.get(a), n)) for _, in := range s0.set { if more := false; g.visit(v0, in.a, func(w0 int, c int64) (skip bool) { switch { case w0 >= in.b: more, skip = true, true return case do(s.rank(w0), c): return true default: return } }) && !more { return true } } return } return res }
go
func (g *Virtual) Subgraph(s VertexSet) *Virtual { n := g.Order() s = s.And(Range(0, n)) m := s.size() switch { case m == 0: return null case m == 1: return singleton() case m == n: return g } cost := func(v, w int) int64 { return g.cost(s.get(v), s.get(w)) } res := generic(m, cost, func(v, w int) bool { return g.edge(s.get(v), s.get(w)) }) res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { if a >= m { return } v0 := s.get(v) s0 := s.And(Range(s.get(a), n)) for _, in := range s0.set { if more := false; g.visit(v0, in.a, func(w0 int, c int64) (skip bool) { switch { case w0 >= in.b: more, skip = true, true return case do(s.rank(w0), c): return true default: return } }) && !more { return true } } return } return res }
[ "func", "(", "g", "*", "Virtual", ")", "Subgraph", "(", "s", "VertexSet", ")", "*", "Virtual", "{", "n", ":=", "g", ".", "Order", "(", ")", "\n", "s", "=", "s", ".", "And", "(", "Range", "(", "0", ",", "n", ")", ")", "\n", "m", ":=", "s", ".", "size", "(", ")", "\n", "switch", "{", "case", "m", "==", "0", ":", "return", "null", "\n", "case", "m", "==", "1", ":", "return", "singleton", "(", ")", "\n", "case", "m", "==", "n", ":", "return", "g", "\n", "}", "\n\n", "cost", ":=", "func", "(", "v", ",", "w", "int", ")", "int64", "{", "return", "g", ".", "cost", "(", "s", ".", "get", "(", "v", ")", ",", "s", ".", "get", "(", "w", ")", ")", "\n", "}", "\n", "res", ":=", "generic", "(", "m", ",", "cost", ",", "func", "(", "v", ",", "w", "int", ")", "bool", "{", "return", "g", ".", "edge", "(", "s", ".", "get", "(", "v", ")", ",", "s", ".", "get", "(", "w", ")", ")", "\n", "}", ")", "\n", "res", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "if", "a", ">=", "m", "{", "return", "\n", "}", "\n", "v0", ":=", "s", ".", "get", "(", "v", ")", "\n", "s0", ":=", "s", ".", "And", "(", "Range", "(", "s", ".", "get", "(", "a", ")", ",", "n", ")", ")", "\n", "for", "_", ",", "in", ":=", "range", "s0", ".", "set", "{", "if", "more", ":=", "false", ";", "g", ".", "visit", "(", "v0", ",", "in", ".", "a", ",", "func", "(", "w0", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "switch", "{", "case", "w0", ">=", "in", ".", "b", ":", "more", ",", "skip", "=", "true", ",", "true", "\n", "return", "\n", "case", "do", "(", "s", ".", "rank", "(", "w0", ")", ",", "c", ")", ":", "return", "true", "\n", "default", ":", "return", "\n", "}", "\n", "}", ")", "&&", "!", "more", "{", "return", "true", "\n", "}", "\n", "}", "\n", "return", "\n", "}", "\n", "return", "res", "\n", "}" ]
// Subgraph returns a subgraph of g that consists of the vertices in s // and all edges whose endpoints are in s.
[ "Subgraph", "returns", "a", "subgraph", "of", "g", "that", "consists", "of", "the", "vertices", "in", "s", "and", "all", "edges", "whose", "endpoints", "are", "in", "s", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/subgraph.go#L5-L48
9,143
yourbasic/graph
strong.go
StrongComponents
func StrongComponents(g Iterator) [][]int { n := g.Order() s := &scc{ graph: g, visited: make([]bool, n), lowLink: make([]int, n), } components := [][]int{} for v := range s.visited { if !s.visited[v] { components = s.append(components, v) } } return components }
go
func StrongComponents(g Iterator) [][]int { n := g.Order() s := &scc{ graph: g, visited: make([]bool, n), lowLink: make([]int, n), } components := [][]int{} for v := range s.visited { if !s.visited[v] { components = s.append(components, v) } } return components }
[ "func", "StrongComponents", "(", "g", "Iterator", ")", "[", "]", "[", "]", "int", "{", "n", ":=", "g", ".", "Order", "(", ")", "\n", "s", ":=", "&", "scc", "{", "graph", ":", "g", ",", "visited", ":", "make", "(", "[", "]", "bool", ",", "n", ")", ",", "lowLink", ":", "make", "(", "[", "]", "int", ",", "n", ")", ",", "}", "\n", "components", ":=", "[", "]", "[", "]", "int", "{", "}", "\n", "for", "v", ":=", "range", "s", ".", "visited", "{", "if", "!", "s", ".", "visited", "[", "v", "]", "{", "components", "=", "s", ".", "append", "(", "components", ",", "v", ")", "\n", "}", "\n", "}", "\n", "return", "components", "\n", "}" ]
// StrongComponents produces a partition of g's vertices into its // strongly connected components. // // A component is strongly connected if all its vertices are reachable // from every other vertex in the component. // Each vertex of the graph appears in exactly one of the strongly // connected components, and any vertex that is not on a directed cycle // forms a strongly connected component all by itself.
[ "StrongComponents", "produces", "a", "partition", "of", "g", "s", "vertices", "into", "its", "strongly", "connected", "components", ".", "A", "component", "is", "strongly", "connected", "if", "all", "its", "vertices", "are", "reachable", "from", "every", "other", "vertex", "in", "the", "component", ".", "Each", "vertex", "of", "the", "graph", "appears", "in", "exactly", "one", "of", "the", "strongly", "connected", "components", "and", "any", "vertex", "that", "is", "not", "on", "a", "directed", "cycle", "forms", "a", "strongly", "connected", "component", "all", "by", "itself", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/strong.go#L11-L25
9,144
yourbasic/graph
strong.go
append
func (s *scc) append(components [][]int, v int) [][]int { // A vertex remains on this stack after it has been visited iff // there is a path from it to some vertex earlier on the stack. s.stack = append(s.stack, v) // lowLink[v] is the smallest vertex known to be reachable from v. s.lowLink[v] = s.time s.time++ newComponent := true s.visited[v] = true s.graph.Visit(v, func(w int, _ int64) (skip bool) { if !s.visited[w] { components = s.append(components, w) } if s.lowLink[v] > s.lowLink[w] { s.lowLink[v] = s.lowLink[w] newComponent = false } return }) if !newComponent { return components } var comp []int for { n := len(s.stack) - 1 w := s.stack[n] s.stack = s.stack[:n] s.lowLink[w] = int(^uint(0) >> 1) // maxint comp = append(comp, w) if v == w { return append(components, comp) } } }
go
func (s *scc) append(components [][]int, v int) [][]int { // A vertex remains on this stack after it has been visited iff // there is a path from it to some vertex earlier on the stack. s.stack = append(s.stack, v) // lowLink[v] is the smallest vertex known to be reachable from v. s.lowLink[v] = s.time s.time++ newComponent := true s.visited[v] = true s.graph.Visit(v, func(w int, _ int64) (skip bool) { if !s.visited[w] { components = s.append(components, w) } if s.lowLink[v] > s.lowLink[w] { s.lowLink[v] = s.lowLink[w] newComponent = false } return }) if !newComponent { return components } var comp []int for { n := len(s.stack) - 1 w := s.stack[n] s.stack = s.stack[:n] s.lowLink[w] = int(^uint(0) >> 1) // maxint comp = append(comp, w) if v == w { return append(components, comp) } } }
[ "func", "(", "s", "*", "scc", ")", "append", "(", "components", "[", "]", "[", "]", "int", ",", "v", "int", ")", "[", "]", "[", "]", "int", "{", "// A vertex remains on this stack after it has been visited iff", "// there is a path from it to some vertex earlier on the stack.", "s", ".", "stack", "=", "append", "(", "s", ".", "stack", ",", "v", ")", "\n\n", "// lowLink[v] is the smallest vertex known to be reachable from v.", "s", ".", "lowLink", "[", "v", "]", "=", "s", ".", "time", "\n", "s", ".", "time", "++", "\n\n", "newComponent", ":=", "true", "\n", "s", ".", "visited", "[", "v", "]", "=", "true", "\n", "s", ".", "graph", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "if", "!", "s", ".", "visited", "[", "w", "]", "{", "components", "=", "s", ".", "append", "(", "components", ",", "w", ")", "\n", "}", "\n", "if", "s", ".", "lowLink", "[", "v", "]", ">", "s", ".", "lowLink", "[", "w", "]", "{", "s", ".", "lowLink", "[", "v", "]", "=", "s", ".", "lowLink", "[", "w", "]", "\n", "newComponent", "=", "false", "\n", "}", "\n", "return", "\n", "}", ")", "\n", "if", "!", "newComponent", "{", "return", "components", "\n", "}", "\n", "var", "comp", "[", "]", "int", "\n", "for", "{", "n", ":=", "len", "(", "s", ".", "stack", ")", "-", "1", "\n", "w", ":=", "s", ".", "stack", "[", "n", "]", "\n", "s", ".", "stack", "=", "s", ".", "stack", "[", ":", "n", "]", "\n", "s", ".", "lowLink", "[", "w", "]", "=", "int", "(", "^", "uint", "(", "0", ")", ">>", "1", ")", "// maxint", "\n", "comp", "=", "append", "(", "comp", ",", "w", ")", "\n", "if", "v", "==", "w", "{", "return", "append", "(", "components", ",", "comp", ")", "\n", "}", "\n", "}", "\n", "}" ]
// Make a depth-first search starting at v and append all strongly // connected components of the visited subgraph to comps.
[ "Make", "a", "depth", "-", "first", "search", "starting", "at", "v", "and", "append", "all", "strongly", "connected", "components", "of", "the", "visited", "subgraph", "to", "comps", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/strong.go#L38-L73
9,145
yourbasic/graph
build/intersect.go
find
func (g *Virtual) find(v int, a int) (w int, n int) { prev := -1 g.visit(v, a, func(w0 int, c0 int64) (skip bool) { switch prev { case -1: w, n = w0, 1 prev = w0 return case w0 - 1: n += 1 prev = w0 return } return true }) return }
go
func (g *Virtual) find(v int, a int) (w int, n int) { prev := -1 g.visit(v, a, func(w0 int, c0 int64) (skip bool) { switch prev { case -1: w, n = w0, 1 prev = w0 return case w0 - 1: n += 1 prev = w0 return } return true }) return }
[ "func", "(", "g", "*", "Virtual", ")", "find", "(", "v", "int", ",", "a", "int", ")", "(", "w", "int", ",", "n", "int", ")", "{", "prev", ":=", "-", "1", "\n", "g", ".", "visit", "(", "v", ",", "a", ",", "func", "(", "w0", "int", ",", "c0", "int64", ")", "(", "skip", "bool", ")", "{", "switch", "prev", "{", "case", "-", "1", ":", "w", ",", "n", "=", "w0", ",", "1", "\n", "prev", "=", "w0", "\n", "return", "\n", "case", "w0", "-", "1", ":", "n", "+=", "1", "\n", "prev", "=", "w0", "\n", "return", "\n", "}", "\n", "return", "true", "\n", "}", ")", "\n", "return", "\n", "}" ]
// find returns the smallest n consecutive neighbors w, w+1, ..., w+n-1 // of v for which w >= a.
[ "find", "returns", "the", "smallest", "n", "consecutive", "neighbors", "w", "w", "+", "1", "...", "w", "+", "n", "-", "1", "of", "v", "for", "which", "w", ">", "=", "a", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/intersect.go#L64-L80
9,146
yourbasic/graph
mutable.go
New
func New(n int) *Mutable { return &Mutable{edges: make([]map[int]int64, n)} }
go
func New(n int) *Mutable { return &Mutable{edges: make([]map[int]int64, n)} }
[ "func", "New", "(", "n", "int", ")", "*", "Mutable", "{", "return", "&", "Mutable", "{", "edges", ":", "make", "(", "[", "]", "map", "[", "int", "]", "int64", ",", "n", ")", "}", "\n", "}" ]
// New constructs a new graph with n vertices, numbered from 0 to n-1, and no edges.
[ "New", "constructs", "a", "new", "graph", "with", "n", "vertices", "numbered", "from", "0", "to", "n", "-", "1", "and", "no", "edges", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L23-L25
9,147
yourbasic/graph
mutable.go
Copy
func Copy(g Iterator) *Mutable { switch g := g.(type) { case *Mutable: return copyMutable(g) case *Immutable: return copyImmutable(g) } n := g.Order() h := New(n) for v := 0; v < n; v++ { g.Visit(v, func(w int, c int64) (skip bool) { h.AddCost(v, w, c) return }) } return h }
go
func Copy(g Iterator) *Mutable { switch g := g.(type) { case *Mutable: return copyMutable(g) case *Immutable: return copyImmutable(g) } n := g.Order() h := New(n) for v := 0; v < n; v++ { g.Visit(v, func(w int, c int64) (skip bool) { h.AddCost(v, w, c) return }) } return h }
[ "func", "Copy", "(", "g", "Iterator", ")", "*", "Mutable", "{", "switch", "g", ":=", "g", ".", "(", "type", ")", "{", "case", "*", "Mutable", ":", "return", "copyMutable", "(", "g", ")", "\n", "case", "*", "Immutable", ":", "return", "copyImmutable", "(", "g", ")", "\n", "}", "\n", "n", ":=", "g", ".", "Order", "(", ")", "\n", "h", ":=", "New", "(", "n", ")", "\n", "for", "v", ":=", "0", ";", "v", "<", "n", ";", "v", "++", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "h", ".", "AddCost", "(", "v", ",", "w", ",", "c", ")", "\n", "return", "\n", "}", ")", "\n", "}", "\n", "return", "h", "\n", "}" ]
// Copy returns a copy of g. // If g is a multigraph, any duplicate edges in g will be lost.
[ "Copy", "returns", "a", "copy", "of", "g", ".", "If", "g", "is", "a", "multigraph", "any", "duplicate", "edges", "in", "g", "will", "be", "lost", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L29-L45
9,148
yourbasic/graph
mutable.go
Visit
func (g *Mutable) Visit(v int, do func(w int, c int64) bool) bool { for w, c := range g.edges[v] { if do(w, c) { return true } } return false }
go
func (g *Mutable) Visit(v int, do func(w int, c int64) bool) bool { for w, c := range g.edges[v] { if do(w, c) { return true } } return false }
[ "func", "(", "g", "*", "Mutable", ")", "Visit", "(", "v", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "bool", "{", "for", "w", ",", "c", ":=", "range", "g", ".", "edges", "[", "v", "]", "{", "if", "do", "(", "w", ",", "c", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "return", "false", "\n", "}" ]
// Visit calls the do function for each neighbor w of v, // with c equal to the cost of the edge from v to w. // If do returns true, Visit returns immediately, // skipping any remaining neighbors, and returns true. // // The iteration order is not specified and is not guaranteed // to be the same every time. // It is safe to delete, but not to add, edges adjacent to v // during a call to this method.
[ "Visit", "calls", "the", "do", "function", "for", "each", "neighbor", "w", "of", "v", "with", "c", "equal", "to", "the", "cost", "of", "the", "edge", "from", "v", "to", "w", ".", "If", "do", "returns", "true", "Visit", "returns", "immediately", "skipping", "any", "remaining", "neighbors", "and", "returns", "true", ".", "The", "iteration", "order", "is", "not", "specified", "and", "is", "not", "guaranteed", "to", "be", "the", "same", "every", "time", ".", "It", "is", "safe", "to", "delete", "but", "not", "to", "add", "edges", "adjacent", "to", "v", "during", "a", "call", "to", "this", "method", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L92-L99
9,149
yourbasic/graph
mutable.go
Add
func (g *Mutable) Add(v, w int) { g.AddCost(v, w, 0) }
go
func (g *Mutable) Add(v, w int) { g.AddCost(v, w, 0) }
[ "func", "(", "g", "*", "Mutable", ")", "Add", "(", "v", ",", "w", "int", ")", "{", "g", ".", "AddCost", "(", "v", ",", "w", ",", "0", ")", "\n", "}" ]
// Add inserts a directed edge from v to w with zero cost. // It removes the previous cost if this edge already exists.
[ "Add", "inserts", "a", "directed", "edge", "from", "v", "to", "w", "with", "zero", "cost", ".", "It", "removes", "the", "previous", "cost", "if", "this", "edge", "already", "exists", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L125-L127
9,150
yourbasic/graph
mutable.go
AddCost
func (g *Mutable) AddCost(v, w int, c int64) { // Make sure not to break internal state. if w < 0 || w >= len(g.edges) { panic("vertex out of range: " + strconv.Itoa(w)) } if g.edges[v] == nil { g.edges[v] = make(map[int]int64, initialMapSize) } g.edges[v][w] = c }
go
func (g *Mutable) AddCost(v, w int, c int64) { // Make sure not to break internal state. if w < 0 || w >= len(g.edges) { panic("vertex out of range: " + strconv.Itoa(w)) } if g.edges[v] == nil { g.edges[v] = make(map[int]int64, initialMapSize) } g.edges[v][w] = c }
[ "func", "(", "g", "*", "Mutable", ")", "AddCost", "(", "v", ",", "w", "int", ",", "c", "int64", ")", "{", "// Make sure not to break internal state.", "if", "w", "<", "0", "||", "w", ">=", "len", "(", "g", ".", "edges", ")", "{", "panic", "(", "\"", "\"", "+", "strconv", ".", "Itoa", "(", "w", ")", ")", "\n", "}", "\n", "if", "g", ".", "edges", "[", "v", "]", "==", "nil", "{", "g", ".", "edges", "[", "v", "]", "=", "make", "(", "map", "[", "int", "]", "int64", ",", "initialMapSize", ")", "\n", "}", "\n", "g", ".", "edges", "[", "v", "]", "[", "w", "]", "=", "c", "\n", "}" ]
// AddCost inserts a directed edge from v to w with cost c. // It overwrites the previous cost if this edge already exists.
[ "AddCost", "inserts", "a", "directed", "edge", "from", "v", "to", "w", "with", "cost", "c", ".", "It", "overwrites", "the", "previous", "cost", "if", "this", "edge", "already", "exists", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L131-L140
9,151
yourbasic/graph
mutable.go
AddBoth
func (g *Mutable) AddBoth(v, w int) { g.AddCost(v, w, 0) if v != w { g.AddCost(w, v, 0) } }
go
func (g *Mutable) AddBoth(v, w int) { g.AddCost(v, w, 0) if v != w { g.AddCost(w, v, 0) } }
[ "func", "(", "g", "*", "Mutable", ")", "AddBoth", "(", "v", ",", "w", "int", ")", "{", "g", ".", "AddCost", "(", "v", ",", "w", ",", "0", ")", "\n", "if", "v", "!=", "w", "{", "g", ".", "AddCost", "(", "w", ",", "v", ",", "0", ")", "\n", "}", "\n", "}" ]
// AddBoth inserts edges with zero cost between v and w. // It removes the previous costs if these edges already exist.
[ "AddBoth", "inserts", "edges", "with", "zero", "cost", "between", "v", "and", "w", ".", "It", "removes", "the", "previous", "costs", "if", "these", "edges", "already", "exist", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L144-L149
9,152
yourbasic/graph
mutable.go
AddBothCost
func (g *Mutable) AddBothCost(v, w int, c int64) { g.AddCost(v, w, c) if v != w { g.AddCost(w, v, c) } }
go
func (g *Mutable) AddBothCost(v, w int, c int64) { g.AddCost(v, w, c) if v != w { g.AddCost(w, v, c) } }
[ "func", "(", "g", "*", "Mutable", ")", "AddBothCost", "(", "v", ",", "w", "int", ",", "c", "int64", ")", "{", "g", ".", "AddCost", "(", "v", ",", "w", ",", "c", ")", "\n", "if", "v", "!=", "w", "{", "g", ".", "AddCost", "(", "w", ",", "v", ",", "c", ")", "\n", "}", "\n", "}" ]
// AddBothCost inserts edges with cost c between v and w. // It overwrites the previous costs if these edges already exist.
[ "AddBothCost", "inserts", "edges", "with", "cost", "c", "between", "v", "and", "w", ".", "It", "overwrites", "the", "previous", "costs", "if", "these", "edges", "already", "exist", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L153-L158
9,153
yourbasic/graph
mutable.go
Delete
func (g *Mutable) Delete(v, w int) { delete(g.edges[v], w) }
go
func (g *Mutable) Delete(v, w int) { delete(g.edges[v], w) }
[ "func", "(", "g", "*", "Mutable", ")", "Delete", "(", "v", ",", "w", "int", ")", "{", "delete", "(", "g", ".", "edges", "[", "v", "]", ",", "w", ")", "\n", "}" ]
// Delete removes an edge from v to w.
[ "Delete", "removes", "an", "edge", "from", "v", "to", "w", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L161-L163
9,154
yourbasic/graph
mutable.go
DeleteBoth
func (g *Mutable) DeleteBoth(v, w int) { g.Delete(v, w) if v != w { g.Delete(w, v) } }
go
func (g *Mutable) DeleteBoth(v, w int) { g.Delete(v, w) if v != w { g.Delete(w, v) } }
[ "func", "(", "g", "*", "Mutable", ")", "DeleteBoth", "(", "v", ",", "w", "int", ")", "{", "g", ".", "Delete", "(", "v", ",", "w", ")", "\n", "if", "v", "!=", "w", "{", "g", ".", "Delete", "(", "w", ",", "v", ")", "\n", "}", "\n", "}" ]
// DeleteBoth removes all edges between v and w.
[ "DeleteBoth", "removes", "all", "edges", "between", "v", "and", "w", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/mutable.go#L166-L171
9,155
yourbasic/graph
top.go
TopSort
func TopSort(g Iterator) (order []int, ok bool) { order, ok = topsort(g, true) return }
go
func TopSort(g Iterator) (order []int, ok bool) { order, ok = topsort(g, true) return }
[ "func", "TopSort", "(", "g", "Iterator", ")", "(", "order", "[", "]", "int", ",", "ok", "bool", ")", "{", "order", ",", "ok", "=", "topsort", "(", "g", ",", "true", ")", "\n", "return", "\n", "}" ]
// TopSort returns a topological ordering of the vertices in // a directed acyclic graph; if the graph is not acyclic, // no such ordering exists and ok is set to false. // // In a topological order v comes before w for every directed edge from v to w.
[ "TopSort", "returns", "a", "topological", "ordering", "of", "the", "vertices", "in", "a", "directed", "acyclic", "graph", ";", "if", "the", "graph", "is", "not", "acyclic", "no", "such", "ordering", "exists", "and", "ok", "is", "set", "to", "false", ".", "In", "a", "topological", "order", "v", "comes", "before", "w", "for", "every", "directed", "edge", "from", "v", "to", "w", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/top.go#L8-L11
9,156
yourbasic/graph
top.go
Acyclic
func Acyclic(g Iterator) bool { _, acyclic := topsort(g, false) return acyclic }
go
func Acyclic(g Iterator) bool { _, acyclic := topsort(g, false) return acyclic }
[ "func", "Acyclic", "(", "g", "Iterator", ")", "bool", "{", "_", ",", "acyclic", ":=", "topsort", "(", "g", ",", "false", ")", "\n", "return", "acyclic", "\n", "}" ]
// Acyclic tells if g has no cycles.
[ "Acyclic", "tells", "if", "g", "has", "no", "cycles", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/top.go#L14-L17
9,157
yourbasic/graph
top.go
topsort
func topsort(g Iterator, output bool) (order []int, acyclic bool) { indegree := make([]int, g.Order()) for v := range indegree { g.Visit(v, func(w int, _ int64) (skip bool) { indegree[w]++ return }) } // Invariant: this queue holds all vertices with indegree 0. var queue []int for v, degree := range indegree { if degree == 0 { queue = append(queue, v) } } order = []int{} vertexCount := 0 for len(queue) > 0 { v := queue[0] queue = queue[1:] if output { order = append(order, v) } vertexCount++ g.Visit(v, func(w int, _ int64) (skip bool) { indegree[w]-- if indegree[w] == 0 { queue = append(queue, w) } return }) } if vertexCount != g.Order() { return } acyclic = true return }
go
func topsort(g Iterator, output bool) (order []int, acyclic bool) { indegree := make([]int, g.Order()) for v := range indegree { g.Visit(v, func(w int, _ int64) (skip bool) { indegree[w]++ return }) } // Invariant: this queue holds all vertices with indegree 0. var queue []int for v, degree := range indegree { if degree == 0 { queue = append(queue, v) } } order = []int{} vertexCount := 0 for len(queue) > 0 { v := queue[0] queue = queue[1:] if output { order = append(order, v) } vertexCount++ g.Visit(v, func(w int, _ int64) (skip bool) { indegree[w]-- if indegree[w] == 0 { queue = append(queue, w) } return }) } if vertexCount != g.Order() { return } acyclic = true return }
[ "func", "topsort", "(", "g", "Iterator", ",", "output", "bool", ")", "(", "order", "[", "]", "int", ",", "acyclic", "bool", ")", "{", "indegree", ":=", "make", "(", "[", "]", "int", ",", "g", ".", "Order", "(", ")", ")", "\n", "for", "v", ":=", "range", "indegree", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "indegree", "[", "w", "]", "++", "\n", "return", "\n", "}", ")", "\n", "}", "\n\n", "// Invariant: this queue holds all vertices with indegree 0.", "var", "queue", "[", "]", "int", "\n", "for", "v", ",", "degree", ":=", "range", "indegree", "{", "if", "degree", "==", "0", "{", "queue", "=", "append", "(", "queue", ",", "v", ")", "\n", "}", "\n", "}", "\n\n", "order", "=", "[", "]", "int", "{", "}", "\n", "vertexCount", ":=", "0", "\n", "for", "len", "(", "queue", ")", ">", "0", "{", "v", ":=", "queue", "[", "0", "]", "\n", "queue", "=", "queue", "[", "1", ":", "]", "\n", "if", "output", "{", "order", "=", "append", "(", "order", ",", "v", ")", "\n", "}", "\n", "vertexCount", "++", "\n", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "indegree", "[", "w", "]", "--", "\n", "if", "indegree", "[", "w", "]", "==", "0", "{", "queue", "=", "append", "(", "queue", ",", "w", ")", "\n", "}", "\n", "return", "\n", "}", ")", "\n", "}", "\n", "if", "vertexCount", "!=", "g", ".", "Order", "(", ")", "{", "return", "\n", "}", "\n", "acyclic", "=", "true", "\n", "return", "\n", "}" ]
// Kahn's algorithm
[ "Kahn", "s", "algorithm" ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/top.go#L20-L59
9,158
yourbasic/graph
euler.go
EulerDirected
func EulerDirected(g Iterator) (walk []int, ok bool) { n := g.Order() degree := make([]int, n) // outdegree - indegree for each vertex edgeCount := 0 for v := range degree { g.Visit(v, func(w int, _ int64) (skip bool) { edgeCount++ degree[v]++ degree[w]-- return }) } if edgeCount == 0 { return []int{}, true } start, end := -1, -1 for v := range degree { switch { case degree[v] == 0: case degree[v] == 1 && start == -1: start = v case degree[v] == -1 && end == -1: end = v default: return []int{}, false } } // Make a copy of g h := make([][]int, n) for v := range h { g.Visit(v, func(w int, _ int64) (skip bool) { h[v] = append(h[v], w) return }) } // Find a starting point with neighbors. if start == -1 { for v, neighbors := range h { if len(neighbors) > 0 { start = v break } } } for stack := []int{start}; len(stack) > 0; { n := len(stack) v := stack[n-1] stack = stack[:n-1] for len(h[v]) > 0 { stack = append(stack, v) v, h[v] = h[v][0], h[v][1:] edgeCount-- } walk = append(walk, v) } if edgeCount > 0 { return []int{}, false } for i, j := 0, len(walk)-1; i < j; i, j = i+1, j-1 { walk[i], walk[j] = walk[j], walk[i] } return walk, true }
go
func EulerDirected(g Iterator) (walk []int, ok bool) { n := g.Order() degree := make([]int, n) // outdegree - indegree for each vertex edgeCount := 0 for v := range degree { g.Visit(v, func(w int, _ int64) (skip bool) { edgeCount++ degree[v]++ degree[w]-- return }) } if edgeCount == 0 { return []int{}, true } start, end := -1, -1 for v := range degree { switch { case degree[v] == 0: case degree[v] == 1 && start == -1: start = v case degree[v] == -1 && end == -1: end = v default: return []int{}, false } } // Make a copy of g h := make([][]int, n) for v := range h { g.Visit(v, func(w int, _ int64) (skip bool) { h[v] = append(h[v], w) return }) } // Find a starting point with neighbors. if start == -1 { for v, neighbors := range h { if len(neighbors) > 0 { start = v break } } } for stack := []int{start}; len(stack) > 0; { n := len(stack) v := stack[n-1] stack = stack[:n-1] for len(h[v]) > 0 { stack = append(stack, v) v, h[v] = h[v][0], h[v][1:] edgeCount-- } walk = append(walk, v) } if edgeCount > 0 { return []int{}, false } for i, j := 0, len(walk)-1; i < j; i, j = i+1, j-1 { walk[i], walk[j] = walk[j], walk[i] } return walk, true }
[ "func", "EulerDirected", "(", "g", "Iterator", ")", "(", "walk", "[", "]", "int", ",", "ok", "bool", ")", "{", "n", ":=", "g", ".", "Order", "(", ")", "\n", "degree", ":=", "make", "(", "[", "]", "int", ",", "n", ")", "// outdegree - indegree for each vertex", "\n", "edgeCount", ":=", "0", "\n", "for", "v", ":=", "range", "degree", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "edgeCount", "++", "\n", "degree", "[", "v", "]", "++", "\n", "degree", "[", "w", "]", "--", "\n", "return", "\n", "}", ")", "\n", "}", "\n", "if", "edgeCount", "==", "0", "{", "return", "[", "]", "int", "{", "}", ",", "true", "\n", "}", "\n\n", "start", ",", "end", ":=", "-", "1", ",", "-", "1", "\n", "for", "v", ":=", "range", "degree", "{", "switch", "{", "case", "degree", "[", "v", "]", "==", "0", ":", "case", "degree", "[", "v", "]", "==", "1", "&&", "start", "==", "-", "1", ":", "start", "=", "v", "\n", "case", "degree", "[", "v", "]", "==", "-", "1", "&&", "end", "==", "-", "1", ":", "end", "=", "v", "\n", "default", ":", "return", "[", "]", "int", "{", "}", ",", "false", "\n", "}", "\n", "}", "\n\n", "// Make a copy of g", "h", ":=", "make", "(", "[", "]", "[", "]", "int", ",", "n", ")", "\n", "for", "v", ":=", "range", "h", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "h", "[", "v", "]", "=", "append", "(", "h", "[", "v", "]", ",", "w", ")", "\n", "return", "\n", "}", ")", "\n", "}", "\n\n", "// Find a starting point with neighbors.", "if", "start", "==", "-", "1", "{", "for", "v", ",", "neighbors", ":=", "range", "h", "{", "if", "len", "(", "neighbors", ")", ">", "0", "{", "start", "=", "v", "\n", "break", "\n", "}", "\n", "}", "\n", "}", "\n\n", "for", "stack", ":=", "[", "]", "int", "{", "start", "}", ";", "len", "(", "stack", ")", ">", "0", ";", "{", "n", ":=", "len", "(", "stack", ")", "\n", "v", ":=", "stack", "[", "n", "-", "1", "]", "\n", "stack", "=", "stack", "[", ":", "n", "-", "1", "]", "\n", "for", "len", "(", "h", "[", "v", "]", ")", ">", "0", "{", "stack", "=", "append", "(", "stack", ",", "v", ")", "\n", "v", ",", "h", "[", "v", "]", "=", "h", "[", "v", "]", "[", "0", "]", ",", "h", "[", "v", "]", "[", "1", ":", "]", "\n", "edgeCount", "--", "\n", "}", "\n", "walk", "=", "append", "(", "walk", ",", "v", ")", "\n", "}", "\n", "if", "edgeCount", ">", "0", "{", "return", "[", "]", "int", "{", "}", ",", "false", "\n", "}", "\n", "for", "i", ",", "j", ":=", "0", ",", "len", "(", "walk", ")", "-", "1", ";", "i", "<", "j", ";", "i", ",", "j", "=", "i", "+", "1", ",", "j", "-", "1", "{", "walk", "[", "i", "]", ",", "walk", "[", "j", "]", "=", "walk", "[", "j", "]", ",", "walk", "[", "i", "]", "\n", "}", "\n", "return", "walk", ",", "true", "\n", "}" ]
// EulerDirected returns an Euler walk in a directed graph. // If no such walk exists, it returns an empty walk and sets ok to false.
[ "EulerDirected", "returns", "an", "Euler", "walk", "in", "a", "directed", "graph", ".", "If", "no", "such", "walk", "exists", "it", "returns", "an", "empty", "walk", "and", "sets", "ok", "to", "false", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/euler.go#L5-L71
9,159
yourbasic/graph
euler.go
EulerUndirected
func EulerUndirected(g Iterator) (walk []int, ok bool) { n := g.Order() out := make([]int, n) // outdegree for each vertex edgeCount := 0 for v := range out { g.Visit(v, func(w int, _ int64) (skip bool) { edgeCount++ if v != w { out[v]++ } return }) } if edgeCount == 0 { return []int{}, true } start, oddDeg := -1, 0 for v := range out { if out[v]&1 == 1 { start = v oddDeg++ } } if !(oddDeg == 0 || oddDeg == 2) { return []int{}, false } // Find a starting point with neighbors. if start == -1 { for v := 0; v < n; v++ { if g.Visit(v, func(w int, _ int64) (skip bool) { start = w return true }) { break } } } h := Copy(g) for stack := []int{start}; len(stack) > 0; { n := len(stack) v := stack[n-1] stack = stack[:n-1] for h.Degree(v) > 0 { stack = append(stack, v) var w int h.Visit(v, func(u int, _ int64) (skip bool) { w = u return true }) h.DeleteBoth(v, w) edgeCount-- if v != w { edgeCount-- } v = w } walk = append(walk, v) } if edgeCount > 0 { return []int{}, false } return walk, true }
go
func EulerUndirected(g Iterator) (walk []int, ok bool) { n := g.Order() out := make([]int, n) // outdegree for each vertex edgeCount := 0 for v := range out { g.Visit(v, func(w int, _ int64) (skip bool) { edgeCount++ if v != w { out[v]++ } return }) } if edgeCount == 0 { return []int{}, true } start, oddDeg := -1, 0 for v := range out { if out[v]&1 == 1 { start = v oddDeg++ } } if !(oddDeg == 0 || oddDeg == 2) { return []int{}, false } // Find a starting point with neighbors. if start == -1 { for v := 0; v < n; v++ { if g.Visit(v, func(w int, _ int64) (skip bool) { start = w return true }) { break } } } h := Copy(g) for stack := []int{start}; len(stack) > 0; { n := len(stack) v := stack[n-1] stack = stack[:n-1] for h.Degree(v) > 0 { stack = append(stack, v) var w int h.Visit(v, func(u int, _ int64) (skip bool) { w = u return true }) h.DeleteBoth(v, w) edgeCount-- if v != w { edgeCount-- } v = w } walk = append(walk, v) } if edgeCount > 0 { return []int{}, false } return walk, true }
[ "func", "EulerUndirected", "(", "g", "Iterator", ")", "(", "walk", "[", "]", "int", ",", "ok", "bool", ")", "{", "n", ":=", "g", ".", "Order", "(", ")", "\n", "out", ":=", "make", "(", "[", "]", "int", ",", "n", ")", "// outdegree for each vertex", "\n", "edgeCount", ":=", "0", "\n", "for", "v", ":=", "range", "out", "{", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "edgeCount", "++", "\n", "if", "v", "!=", "w", "{", "out", "[", "v", "]", "++", "\n", "}", "\n", "return", "\n", "}", ")", "\n", "}", "\n", "if", "edgeCount", "==", "0", "{", "return", "[", "]", "int", "{", "}", ",", "true", "\n", "}", "\n\n", "start", ",", "oddDeg", ":=", "-", "1", ",", "0", "\n", "for", "v", ":=", "range", "out", "{", "if", "out", "[", "v", "]", "&", "1", "==", "1", "{", "start", "=", "v", "\n", "oddDeg", "++", "\n", "}", "\n", "}", "\n", "if", "!", "(", "oddDeg", "==", "0", "||", "oddDeg", "==", "2", ")", "{", "return", "[", "]", "int", "{", "}", ",", "false", "\n", "}", "\n\n", "// Find a starting point with neighbors.", "if", "start", "==", "-", "1", "{", "for", "v", ":=", "0", ";", "v", "<", "n", ";", "v", "++", "{", "if", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "start", "=", "w", "\n", "return", "true", "\n", "}", ")", "{", "break", "\n", "}", "\n", "}", "\n", "}", "\n\n", "h", ":=", "Copy", "(", "g", ")", "\n", "for", "stack", ":=", "[", "]", "int", "{", "start", "}", ";", "len", "(", "stack", ")", ">", "0", ";", "{", "n", ":=", "len", "(", "stack", ")", "\n", "v", ":=", "stack", "[", "n", "-", "1", "]", "\n", "stack", "=", "stack", "[", ":", "n", "-", "1", "]", "\n", "for", "h", ".", "Degree", "(", "v", ")", ">", "0", "{", "stack", "=", "append", "(", "stack", ",", "v", ")", "\n", "var", "w", "int", "\n", "h", ".", "Visit", "(", "v", ",", "func", "(", "u", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "w", "=", "u", "\n", "return", "true", "\n", "}", ")", "\n", "h", ".", "DeleteBoth", "(", "v", ",", "w", ")", "\n", "edgeCount", "--", "\n", "if", "v", "!=", "w", "{", "edgeCount", "--", "\n", "}", "\n", "v", "=", "w", "\n", "}", "\n", "walk", "=", "append", "(", "walk", ",", "v", ")", "\n", "}", "\n", "if", "edgeCount", ">", "0", "{", "return", "[", "]", "int", "{", "}", ",", "false", "\n", "}", "\n", "return", "walk", ",", "true", "\n", "}" ]
// EulerUndirected returns an Euler walk following undirected edges // in only one direction. If no such walk exists, it returns an empty walk // and sets ok to false.
[ "EulerUndirected", "returns", "an", "Euler", "walk", "following", "undirected", "edges", "in", "only", "one", "direction", ".", "If", "no", "such", "walk", "exists", "it", "returns", "an", "empty", "walk", "and", "sets", "ok", "to", "false", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/euler.go#L76-L141
9,160
yourbasic/graph
build/edgeset.go
Add
func (g *Virtual) Add(e EdgeSet) *Virtual { return g.union(newEdges(g.Order(), e), true) }
go
func (g *Virtual) Add(e EdgeSet) *Virtual { return g.union(newEdges(g.Order(), e), true) }
[ "func", "(", "g", "*", "Virtual", ")", "Add", "(", "e", "EdgeSet", ")", "*", "Virtual", "{", "return", "g", ".", "union", "(", "newEdges", "(", "g", ".", "Order", "(", ")", ",", "e", ")", ",", "true", ")", "\n", "}" ]
// Add returns a graph containing all edges in g plus all edges in e. // Any edges belonging to both g and e will retain their cost from g.
[ "Add", "returns", "a", "graph", "containing", "all", "edges", "in", "g", "plus", "all", "edges", "in", "e", ".", "Any", "edges", "belonging", "to", "both", "g", "and", "e", "will", "retain", "their", "cost", "from", "g", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/edgeset.go#L54-L56
9,161
yourbasic/graph
build/edgeset.go
Delete
func (g *Virtual) Delete(e EdgeSet) *Virtual { return g.Keep(func(v, w int) bool { return !e.Contains(v, w) }) }
go
func (g *Virtual) Delete(e EdgeSet) *Virtual { return g.Keep(func(v, w int) bool { return !e.Contains(v, w) }) }
[ "func", "(", "g", "*", "Virtual", ")", "Delete", "(", "e", "EdgeSet", ")", "*", "Virtual", "{", "return", "g", ".", "Keep", "(", "func", "(", "v", ",", "w", "int", ")", "bool", "{", "return", "!", "e", ".", "Contains", "(", "v", ",", "w", ")", "\n", "}", ")", "\n", "}" ]
// Delete returns a graph containing all edges in g except those also found in e.
[ "Delete", "returns", "a", "graph", "containing", "all", "edges", "in", "g", "except", "those", "also", "found", "in", "e", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/edgeset.go#L59-L63
9,162
yourbasic/graph
immutable.go
Sort
func Sort(g Iterator) *Immutable { if g, ok := g.(*Immutable); ok { return g } return build(g, false) }
go
func Sort(g Iterator) *Immutable { if g, ok := g.(*Immutable); ok { return g } return build(g, false) }
[ "func", "Sort", "(", "g", "Iterator", ")", "*", "Immutable", "{", "if", "g", ",", "ok", ":=", "g", ".", "(", "*", "Immutable", ")", ";", "ok", "{", "return", "g", "\n", "}", "\n", "return", "build", "(", "g", ",", "false", ")", "\n", "}" ]
// Sort returns an immutable copy of g with a Visit method // that returns its neighbors in increasing numerical order.
[ "Sort", "returns", "an", "immutable", "copy", "of", "g", "with", "a", "Visit", "method", "that", "returns", "its", "neighbors", "in", "increasing", "numerical", "order", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/immutable.go#L26-L31
9,163
yourbasic/graph
build/build.go
singleton
func singleton() *Virtual { return &Virtual{ order: 1, edge: neverEdge, cost: zero, degree: degreeZero, visit: noNeighbors, } }
go
func singleton() *Virtual { return &Virtual{ order: 1, edge: neverEdge, cost: zero, degree: degreeZero, visit: noNeighbors, } }
[ "func", "singleton", "(", ")", "*", "Virtual", "{", "return", "&", "Virtual", "{", "order", ":", "1", ",", "edge", ":", "neverEdge", ",", "cost", ":", "zero", ",", "degree", ":", "degreeZero", ",", "visit", ":", "noNeighbors", ",", "}", "\n", "}" ]
// singleton returns a graph with one vertex.
[ "singleton", "returns", "a", "graph", "with", "one", "vertex", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L119-L127
9,164
yourbasic/graph
build/build.go
generic
func generic(n int, cost CostFunc, edge func(v, w int) bool) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } g := &Virtual{ order: n, edge: func(v, w int) bool { return v != w && edge(v, w) }, cost: cost, } g.degree = func(v int) (deg int) { g.visit(v, 0, func(int, int64) (skip bool) { deg++ return }) return } g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { for w := a; w < n; w++ { if g.edge(v, w) && do(w, cost(v, w)) { return true } } return } return g }
go
func generic(n int, cost CostFunc, edge func(v, w int) bool) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } g := &Virtual{ order: n, edge: func(v, w int) bool { return v != w && edge(v, w) }, cost: cost, } g.degree = func(v int) (deg int) { g.visit(v, 0, func(int, int64) (skip bool) { deg++ return }) return } g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { for w := a; w < n; w++ { if g.edge(v, w) && do(w, cost(v, w)) { return true } } return } return g }
[ "func", "generic", "(", "n", "int", ",", "cost", "CostFunc", ",", "edge", "func", "(", "v", ",", "w", "int", ")", "bool", ")", "*", "Virtual", "{", "switch", "{", "case", "n", "<", "0", ":", "return", "nil", "\n", "case", "n", "==", "0", ":", "return", "null", "\n", "case", "n", "==", "1", ":", "return", "singleton", "(", ")", "\n", "}", "\n", "g", ":=", "&", "Virtual", "{", "order", ":", "n", ",", "edge", ":", "func", "(", "v", ",", "w", "int", ")", "bool", "{", "return", "v", "!=", "w", "&&", "edge", "(", "v", ",", "w", ")", "}", ",", "cost", ":", "cost", ",", "}", "\n", "g", ".", "degree", "=", "func", "(", "v", "int", ")", "(", "deg", "int", ")", "{", "g", ".", "visit", "(", "v", ",", "0", ",", "func", "(", "int", ",", "int64", ")", "(", "skip", "bool", ")", "{", "deg", "++", "\n", "return", "\n", "}", ")", "\n", "return", "\n", "}", "\n", "g", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "for", "w", ":=", "a", ";", "w", "<", "n", ";", "w", "++", "{", "if", "g", ".", "edge", "(", "v", ",", "w", ")", "&&", "do", "(", "w", ",", "cost", "(", "v", ",", "w", ")", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "return", "\n", "}", "\n", "return", "g", "\n", "}" ]
// generic returns a standard implementation; cost and edge can't be nil.
[ "generic", "returns", "a", "standard", "implementation", ";", "cost", "and", "edge", "can", "t", "be", "nil", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L187-L217
9,165
yourbasic/graph
build/build.go
Specific
func Specific(g graph.Iterator) *Virtual { h := graph.Sort(g) stats := graph.Check(h) if stats.Multi != 0 || stats.Loops != 0 { panic("Virtual doesn't support multiple edges or self-loops") } res := &Virtual{ order: h.Order(), edge: h.Edge, visit: h.VisitFrom, degree: h.Degree, } if stats.Weighted == 0 { res.cost = zero return res } res.cost = func(v, w int) (cost int64) { if !h.Edge(v, w) { return 0 } h.VisitFrom(v, w, func(w int, c int64) (skip bool) { cost = c return true }) return } return res }
go
func Specific(g graph.Iterator) *Virtual { h := graph.Sort(g) stats := graph.Check(h) if stats.Multi != 0 || stats.Loops != 0 { panic("Virtual doesn't support multiple edges or self-loops") } res := &Virtual{ order: h.Order(), edge: h.Edge, visit: h.VisitFrom, degree: h.Degree, } if stats.Weighted == 0 { res.cost = zero return res } res.cost = func(v, w int) (cost int64) { if !h.Edge(v, w) { return 0 } h.VisitFrom(v, w, func(w int, c int64) (skip bool) { cost = c return true }) return } return res }
[ "func", "Specific", "(", "g", "graph", ".", "Iterator", ")", "*", "Virtual", "{", "h", ":=", "graph", ".", "Sort", "(", "g", ")", "\n", "stats", ":=", "graph", ".", "Check", "(", "h", ")", "\n", "if", "stats", ".", "Multi", "!=", "0", "||", "stats", ".", "Loops", "!=", "0", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "res", ":=", "&", "Virtual", "{", "order", ":", "h", ".", "Order", "(", ")", ",", "edge", ":", "h", ".", "Edge", ",", "visit", ":", "h", ".", "VisitFrom", ",", "degree", ":", "h", ".", "Degree", ",", "}", "\n", "if", "stats", ".", "Weighted", "==", "0", "{", "res", ".", "cost", "=", "zero", "\n", "return", "res", "\n", "}", "\n", "res", ".", "cost", "=", "func", "(", "v", ",", "w", "int", ")", "(", "cost", "int64", ")", "{", "if", "!", "h", ".", "Edge", "(", "v", ",", "w", ")", "{", "return", "0", "\n", "}", "\n", "h", ".", "VisitFrom", "(", "v", ",", "w", ",", "func", "(", "w", "int", ",", "c", "int64", ")", "(", "skip", "bool", ")", "{", "cost", "=", "c", "\n", "return", "true", "\n", "}", ")", "\n", "return", "\n", "}", "\n", "return", "res", "\n", "}" ]
// Specific returns a cached copy of g with constant time performance for // all basic operations. It uses space proportional to the size of the graph. // // This function does not accept multigraphs and graphs with self-loops.
[ "Specific", "returns", "a", "cached", "copy", "of", "g", "with", "constant", "time", "performance", "for", "all", "basic", "operations", ".", "It", "uses", "space", "proportional", "to", "the", "size", "of", "the", "graph", ".", "This", "function", "does", "not", "accept", "multigraphs", "and", "graphs", "with", "self", "-", "loops", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L272-L299
9,166
yourbasic/graph
build/build.go
Empty
func Empty(n int) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } return &Virtual{ order: n, edge: neverEdge, cost: zero, degree: degreeZero, visit: noNeighbors, } }
go
func Empty(n int) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } return &Virtual{ order: n, edge: neverEdge, cost: zero, degree: degreeZero, visit: noNeighbors, } }
[ "func", "Empty", "(", "n", "int", ")", "*", "Virtual", "{", "switch", "{", "case", "n", "<", "0", ":", "return", "nil", "\n", "case", "n", "==", "0", ":", "return", "null", "\n", "case", "n", "==", "1", ":", "return", "singleton", "(", ")", "\n", "}", "\n", "return", "&", "Virtual", "{", "order", ":", "n", ",", "edge", ":", "neverEdge", ",", "cost", ":", "zero", ",", "degree", ":", "degreeZero", ",", "visit", ":", "noNeighbors", ",", "}", "\n", "}" ]
// Empty returns a virtual graph with n vertices and no edges.
[ "Empty", "returns", "a", "virtual", "graph", "with", "n", "vertices", "and", "no", "edges", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L302-L318
9,167
yourbasic/graph
build/build.go
Kn
func Kn(n int) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } g := &Virtual{ order: n, edge: alwaysEdge, cost: zero, degree: func(v int) int { return n - 1 }, } g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { for w := a; w < g.order; w++ { if v != w && do(w, 0) { return true } } return } return g }
go
func Kn(n int) *Virtual { switch { case n < 0: return nil case n == 0: return null case n == 1: return singleton() } g := &Virtual{ order: n, edge: alwaysEdge, cost: zero, degree: func(v int) int { return n - 1 }, } g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { for w := a; w < g.order; w++ { if v != w && do(w, 0) { return true } } return } return g }
[ "func", "Kn", "(", "n", "int", ")", "*", "Virtual", "{", "switch", "{", "case", "n", "<", "0", ":", "return", "nil", "\n", "case", "n", "==", "0", ":", "return", "null", "\n", "case", "n", "==", "1", ":", "return", "singleton", "(", ")", "\n", "}", "\n", "g", ":=", "&", "Virtual", "{", "order", ":", "n", ",", "edge", ":", "alwaysEdge", ",", "cost", ":", "zero", ",", "degree", ":", "func", "(", "v", "int", ")", "int", "{", "return", "n", "-", "1", "}", ",", "}", "\n", "g", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "for", "w", ":=", "a", ";", "w", "<", "g", ".", "order", ";", "w", "++", "{", "if", "v", "!=", "w", "&&", "do", "(", "w", ",", "0", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "return", "\n", "}", "\n", "return", "g", "\n", "}" ]
// Kn returns a complete simple graph with n vertices.
[ "Kn", "returns", "a", "complete", "simple", "graph", "with", "n", "vertices", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L321-L345
9,168
yourbasic/graph
build/build.go
Complement
func (g *Virtual) Complement() *Virtual { n := g.order switch n { case 0: return null case 1: return singleton() } res := generic0(n, func(v, w int) (edge bool) { return v != w && !g.edge(v, w) }) res.degree = func(v int) int { return n - 1 - g.degree(v) } res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { prev := a if g.visit(v, a, func(w0 int, _ int64) (skip bool) { for w := prev; w < w0; w++ { if v != w && do(w, 0) { return true } } prev = w0 + 1 return }) { return true } for w := prev; w < n; w++ { if v != w && do(w, 0) { return true } } return } return res }
go
func (g *Virtual) Complement() *Virtual { n := g.order switch n { case 0: return null case 1: return singleton() } res := generic0(n, func(v, w int) (edge bool) { return v != w && !g.edge(v, w) }) res.degree = func(v int) int { return n - 1 - g.degree(v) } res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { prev := a if g.visit(v, a, func(w0 int, _ int64) (skip bool) { for w := prev; w < w0; w++ { if v != w && do(w, 0) { return true } } prev = w0 + 1 return }) { return true } for w := prev; w < n; w++ { if v != w && do(w, 0) { return true } } return } return res }
[ "func", "(", "g", "*", "Virtual", ")", "Complement", "(", ")", "*", "Virtual", "{", "n", ":=", "g", ".", "order", "\n", "switch", "n", "{", "case", "0", ":", "return", "null", "\n", "case", "1", ":", "return", "singleton", "(", ")", "\n", "}", "\n", "res", ":=", "generic0", "(", "n", ",", "func", "(", "v", ",", "w", "int", ")", "(", "edge", "bool", ")", "{", "return", "v", "!=", "w", "&&", "!", "g", ".", "edge", "(", "v", ",", "w", ")", "\n", "}", ")", "\n", "res", ".", "degree", "=", "func", "(", "v", "int", ")", "int", "{", "return", "n", "-", "1", "-", "g", ".", "degree", "(", "v", ")", "}", "\n", "res", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "prev", ":=", "a", "\n", "if", "g", ".", "visit", "(", "v", ",", "a", ",", "func", "(", "w0", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "for", "w", ":=", "prev", ";", "w", "<", "w0", ";", "w", "++", "{", "if", "v", "!=", "w", "&&", "do", "(", "w", ",", "0", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "prev", "=", "w0", "+", "1", "\n", "return", "\n", "}", ")", "{", "return", "true", "\n", "}", "\n", "for", "w", ":=", "prev", ";", "w", "<", "n", ";", "w", "++", "{", "if", "v", "!=", "w", "&&", "do", "(", "w", ",", "0", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "return", "\n", "}", "\n", "return", "res", "\n", "}" ]
// Complement returns the complement graph of g. // This graph has the same vertices as g, // but its edge set consists of the edges not present in g. // The edges of the complement graph will have zero cost.
[ "Complement", "returns", "the", "complement", "graph", "of", "g", ".", "This", "graph", "has", "the", "same", "vertices", "as", "g", "but", "its", "edge", "set", "consists", "of", "the", "edges", "not", "present", "in", "g", ".", "The", "edges", "of", "the", "complement", "graph", "will", "have", "zero", "cost", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L351-L384
9,169
yourbasic/graph
build/build.go
AddCost
func (g *Virtual) AddCost(c int64) *Virtual { res := *g res.cost = Cost(c) res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { return g.visit(v, a, func(w int, _ int64) bool { return do(w, c) }) } return &res }
go
func (g *Virtual) AddCost(c int64) *Virtual { res := *g res.cost = Cost(c) res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { return g.visit(v, a, func(w int, _ int64) bool { return do(w, c) }) } return &res }
[ "func", "(", "g", "*", "Virtual", ")", "AddCost", "(", "c", "int64", ")", "*", "Virtual", "{", "res", ":=", "*", "g", "\n", "res", ".", "cost", "=", "Cost", "(", "c", ")", "\n", "res", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "return", "g", ".", "visit", "(", "v", ",", "a", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "bool", "{", "return", "do", "(", "w", ",", "c", ")", "\n", "}", ")", "\n", "}", "\n", "return", "&", "res", "\n", "}" ]
// AddCost returns a copy of g with a new cost assigned to all edges.
[ "AddCost", "returns", "a", "copy", "of", "g", "with", "a", "new", "cost", "assigned", "to", "all", "edges", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L409-L418
9,170
yourbasic/graph
build/build.go
AddCostFunc
func (g *Virtual) AddCostFunc(c CostFunc) *Virtual { if c == nil { h := g.AddCost(0) return h } res := *g res.cost = c res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { return g.visit(v, a, func(w int, _ int64) bool { return do(w, c(v, w)) }) } return &res }
go
func (g *Virtual) AddCostFunc(c CostFunc) *Virtual { if c == nil { h := g.AddCost(0) return h } res := *g res.cost = c res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { return g.visit(v, a, func(w int, _ int64) bool { return do(w, c(v, w)) }) } return &res }
[ "func", "(", "g", "*", "Virtual", ")", "AddCostFunc", "(", "c", "CostFunc", ")", "*", "Virtual", "{", "if", "c", "==", "nil", "{", "h", ":=", "g", ".", "AddCost", "(", "0", ")", "\n", "return", "h", "\n", "}", "\n", "res", ":=", "*", "g", "\n", "res", ".", "cost", "=", "c", "\n", "res", ".", "visit", "=", "func", "(", "v", "int", ",", "a", "int", ",", "do", "func", "(", "w", "int", ",", "c", "int64", ")", "bool", ")", "(", "aborted", "bool", ")", "{", "return", "g", ".", "visit", "(", "v", ",", "a", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "bool", "{", "return", "do", "(", "w", ",", "c", "(", "v", ",", "w", ")", ")", "\n", "}", ")", "\n", "}", "\n", "return", "&", "res", "\n", "}" ]
// AddCostFunc returns a copy of g with a new cost function assigned.
[ "AddCostFunc", "returns", "a", "copy", "of", "g", "with", "a", "new", "cost", "function", "assigned", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/build.go#L421-L434
9,171
yourbasic/graph
bipart.go
Bipartition
func Bipartition(g Iterator) (part []int, ok bool) { type color byte const ( none color = iota white black ) colors := make([]color, g.Order()) whiteCount := 0 for v := range colors { if colors[v] != none { continue } colors[v] = white whiteCount++ for queue := []int{v}; len(queue) > 0; { v := queue[0] queue = queue[1:] if g.Visit(v, func(w int, _ int64) (skip bool) { switch { case colors[w] != none: if colors[v] == colors[w] { skip = true } return case colors[v] == white: colors[w] = black default: colors[w] = white whiteCount++ } queue = append(queue, w) return }) { return []int{}, false } } } part = make([]int, 0, whiteCount) for v, color := range colors { if color == white { part = append(part, v) } } return part, true }
go
func Bipartition(g Iterator) (part []int, ok bool) { type color byte const ( none color = iota white black ) colors := make([]color, g.Order()) whiteCount := 0 for v := range colors { if colors[v] != none { continue } colors[v] = white whiteCount++ for queue := []int{v}; len(queue) > 0; { v := queue[0] queue = queue[1:] if g.Visit(v, func(w int, _ int64) (skip bool) { switch { case colors[w] != none: if colors[v] == colors[w] { skip = true } return case colors[v] == white: colors[w] = black default: colors[w] = white whiteCount++ } queue = append(queue, w) return }) { return []int{}, false } } } part = make([]int, 0, whiteCount) for v, color := range colors { if color == white { part = append(part, v) } } return part, true }
[ "func", "Bipartition", "(", "g", "Iterator", ")", "(", "part", "[", "]", "int", ",", "ok", "bool", ")", "{", "type", "color", "byte", "\n", "const", "(", "none", "color", "=", "iota", "\n", "white", "\n", "black", "\n", ")", "\n", "colors", ":=", "make", "(", "[", "]", "color", ",", "g", ".", "Order", "(", ")", ")", "\n", "whiteCount", ":=", "0", "\n", "for", "v", ":=", "range", "colors", "{", "if", "colors", "[", "v", "]", "!=", "none", "{", "continue", "\n", "}", "\n", "colors", "[", "v", "]", "=", "white", "\n", "whiteCount", "++", "\n", "for", "queue", ":=", "[", "]", "int", "{", "v", "}", ";", "len", "(", "queue", ")", ">", "0", ";", "{", "v", ":=", "queue", "[", "0", "]", "\n", "queue", "=", "queue", "[", "1", ":", "]", "\n", "if", "g", ".", "Visit", "(", "v", ",", "func", "(", "w", "int", ",", "_", "int64", ")", "(", "skip", "bool", ")", "{", "switch", "{", "case", "colors", "[", "w", "]", "!=", "none", ":", "if", "colors", "[", "v", "]", "==", "colors", "[", "w", "]", "{", "skip", "=", "true", "\n", "}", "\n", "return", "\n", "case", "colors", "[", "v", "]", "==", "white", ":", "colors", "[", "w", "]", "=", "black", "\n", "default", ":", "colors", "[", "w", "]", "=", "white", "\n", "whiteCount", "++", "\n", "}", "\n", "queue", "=", "append", "(", "queue", ",", "w", ")", "\n", "return", "\n", "}", ")", "{", "return", "[", "]", "int", "{", "}", ",", "false", "\n", "}", "\n", "}", "\n", "}", "\n", "part", "=", "make", "(", "[", "]", "int", ",", "0", ",", "whiteCount", ")", "\n", "for", "v", ",", "color", ":=", "range", "colors", "{", "if", "color", "==", "white", "{", "part", "=", "append", "(", "part", ",", "v", ")", "\n", "}", "\n", "}", "\n", "return", "part", ",", "true", "\n", "}" ]
// Bipartition returns a subset U of g's vertices with the property // that every edge of g connects a vertex in U to one outside of U. // If g isn't bipartite, it returns an empty slice and sets ok to false.
[ "Bipartition", "returns", "a", "subset", "U", "of", "g", "s", "vertices", "with", "the", "property", "that", "every", "edge", "of", "g", "connects", "a", "vertex", "in", "U", "to", "one", "outside", "of", "U", ".", "If", "g", "isn", "t", "bipartite", "it", "returns", "an", "empty", "slice", "and", "sets", "ok", "to", "false", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/bipart.go#L6-L51
9,172
yourbasic/graph
build/vertexset.go
update
func (s VertexSet) update() { prev := 0 for i, in := range s.set { s.set[i].index = prev prev += in.b - in.a } }
go
func (s VertexSet) update() { prev := 0 for i, in := range s.set { s.set[i].index = prev prev += in.b - in.a } }
[ "func", "(", "s", "VertexSet", ")", "update", "(", ")", "{", "prev", ":=", "0", "\n", "for", "i", ",", "in", ":=", "range", "s", ".", "set", "{", "s", ".", "set", "[", "i", "]", ".", "index", "=", "prev", "\n", "prev", "+=", "in", ".", "b", "-", "in", ".", "a", "\n", "}", "\n", "}" ]
// update updates the index values.
[ "update", "updates", "the", "index", "values", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L26-L32
9,173
yourbasic/graph
build/vertexset.go
size
func (s VertexSet) size() (size int) { switch { case s.set == nil: return -1 case len(s.set) == 0: return 0 } in := s.set[len(s.set)-1] return in.index + in.b - in.a }
go
func (s VertexSet) size() (size int) { switch { case s.set == nil: return -1 case len(s.set) == 0: return 0 } in := s.set[len(s.set)-1] return in.index + in.b - in.a }
[ "func", "(", "s", "VertexSet", ")", "size", "(", ")", "(", "size", "int", ")", "{", "switch", "{", "case", "s", ".", "set", "==", "nil", ":", "return", "-", "1", "\n", "case", "len", "(", "s", ".", "set", ")", "==", "0", ":", "return", "0", "\n", "}", "\n", "in", ":=", "s", ".", "set", "[", "len", "(", "s", ".", "set", ")", "-", "1", "]", "\n", "return", "in", ".", "index", "+", "in", ".", "b", "-", "in", ".", "a", "\n", "}" ]
// size returns the number of elements in this set, or -1 for the universe.
[ "size", "returns", "the", "number", "of", "elements", "in", "this", "set", "or", "-", "1", "for", "the", "universe", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L53-L62
9,174
yourbasic/graph
build/vertexset.go
rank
func (s VertexSet) rank(n int) int { if len(s.set) == 0 || n < s.set[0].a { return -1 } // The smallest index i such that n < set[i].a i := sort.Search(len(s.set), func(i int) bool { return n < s.set[i].a }) in := s.set[i-1] if n >= in.b { return -1 } return in.index + n - in.a }
go
func (s VertexSet) rank(n int) int { if len(s.set) == 0 || n < s.set[0].a { return -1 } // The smallest index i such that n < set[i].a i := sort.Search(len(s.set), func(i int) bool { return n < s.set[i].a }) in := s.set[i-1] if n >= in.b { return -1 } return in.index + n - in.a }
[ "func", "(", "s", "VertexSet", ")", "rank", "(", "n", "int", ")", "int", "{", "if", "len", "(", "s", ".", "set", ")", "==", "0", "||", "n", "<", "s", ".", "set", "[", "0", "]", ".", "a", "{", "return", "-", "1", "\n", "}", "\n", "// The smallest index i such that n < set[i].a", "i", ":=", "sort", ".", "Search", "(", "len", "(", "s", ".", "set", ")", ",", "func", "(", "i", "int", ")", "bool", "{", "return", "n", "<", "s", ".", "set", "[", "i", "]", ".", "a", "\n", "}", ")", "\n", "in", ":=", "s", ".", "set", "[", "i", "-", "1", "]", "\n", "if", "n", ">=", "in", ".", "b", "{", "return", "-", "1", "\n", "}", "\n", "return", "in", ".", "index", "+", "n", "-", "in", ".", "a", "\n", "}" ]
// rank returns the position of n in the set, or -1 if not available.
[ "rank", "returns", "the", "position", "of", "n", "in", "the", "set", "or", "-", "1", "if", "not", "available", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L78-L91
9,175
yourbasic/graph
build/vertexset.go
Contains
func (s VertexSet) Contains(v int) bool { switch { case s.set == nil: return true case len(s.set) == 0 || v < s.set[0].a: return false } // The smallest index i such that v < set[i].a i := sort.Search(len(s.set), func(i int) bool { return v < s.set[i].a }) if v >= s.set[i-1].b { return false } return true }
go
func (s VertexSet) Contains(v int) bool { switch { case s.set == nil: return true case len(s.set) == 0 || v < s.set[0].a: return false } // The smallest index i such that v < set[i].a i := sort.Search(len(s.set), func(i int) bool { return v < s.set[i].a }) if v >= s.set[i-1].b { return false } return true }
[ "func", "(", "s", "VertexSet", ")", "Contains", "(", "v", "int", ")", "bool", "{", "switch", "{", "case", "s", ".", "set", "==", "nil", ":", "return", "true", "\n", "case", "len", "(", "s", ".", "set", ")", "==", "0", "||", "v", "<", "s", ".", "set", "[", "0", "]", ".", "a", ":", "return", "false", "\n", "}", "\n", "// The smallest index i such that v < set[i].a", "i", ":=", "sort", ".", "Search", "(", "len", "(", "s", ".", "set", ")", ",", "func", "(", "i", "int", ")", "bool", "{", "return", "v", "<", "s", ".", "set", "[", "i", "]", ".", "a", "\n", "}", ")", "\n", "if", "v", ">=", "s", ".", "set", "[", "i", "-", "1", "]", ".", "b", "{", "return", "false", "\n", "}", "\n", "return", "true", "\n", "}" ]
// Contains tells if v is a member of the set.
[ "Contains", "tells", "if", "v", "is", "a", "member", "of", "the", "set", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L94-L109
9,176
yourbasic/graph
build/vertexset.go
AndNot
func (s1 VertexSet) AndNot(s2 VertexSet) VertexSet { return s1.And(s2.complement()) }
go
func (s1 VertexSet) AndNot(s2 VertexSet) VertexSet { return s1.And(s2.complement()) }
[ "func", "(", "s1", "VertexSet", ")", "AndNot", "(", "s2", "VertexSet", ")", "VertexSet", "{", "return", "s1", ".", "And", "(", "s2", ".", "complement", "(", ")", ")", "\n", "}" ]
// AndNot returns the set of all vertices belonging to s1 but not to s2.
[ "AndNot", "returns", "the", "set", "of", "all", "vertices", "belonging", "to", "s1", "but", "not", "to", "s2", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L112-L114
9,177
yourbasic/graph
build/vertexset.go
complement
func (s VertexSet) complement() VertexSet { switch { case s.set == nil: return empty() case len(s.set) == 0: return VertexSet{} } t := empty() prev := minInt for _, in := range s.set { if prev != in.a { t.set = append(t.set, interval{prev, in.a, 0}) } prev = in.b } if prev < maxInt { t.set = append(t.set, interval{prev, maxInt, 0}) } t.update() return t }
go
func (s VertexSet) complement() VertexSet { switch { case s.set == nil: return empty() case len(s.set) == 0: return VertexSet{} } t := empty() prev := minInt for _, in := range s.set { if prev != in.a { t.set = append(t.set, interval{prev, in.a, 0}) } prev = in.b } if prev < maxInt { t.set = append(t.set, interval{prev, maxInt, 0}) } t.update() return t }
[ "func", "(", "s", "VertexSet", ")", "complement", "(", ")", "VertexSet", "{", "switch", "{", "case", "s", ".", "set", "==", "nil", ":", "return", "empty", "(", ")", "\n", "case", "len", "(", "s", ".", "set", ")", "==", "0", ":", "return", "VertexSet", "{", "}", "\n", "}", "\n", "t", ":=", "empty", "(", ")", "\n", "prev", ":=", "minInt", "\n", "for", "_", ",", "in", ":=", "range", "s", ".", "set", "{", "if", "prev", "!=", "in", ".", "a", "{", "t", ".", "set", "=", "append", "(", "t", ".", "set", ",", "interval", "{", "prev", ",", "in", ".", "a", ",", "0", "}", ")", "\n", "}", "\n", "prev", "=", "in", ".", "b", "\n", "}", "\n", "if", "prev", "<", "maxInt", "{", "t", ".", "set", "=", "append", "(", "t", ".", "set", ",", "interval", "{", "prev", ",", "maxInt", ",", "0", "}", ")", "\n", "}", "\n", "t", ".", "update", "(", ")", "\n", "return", "t", "\n", "}" ]
// complement returns the set of all vertices not belonging to s.
[ "complement", "returns", "the", "set", "of", "all", "vertices", "not", "belonging", "to", "s", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L117-L137
9,178
yourbasic/graph
build/vertexset.go
And
func (s1 VertexSet) And(s2 VertexSet) VertexSet { switch { case s1.set == nil: return s2 case s2.set == nil: return s1 } type point struct { x int a bool // Tells if x is a of [a, b). } points := make([]point, 0, 2*(len(s1.set)+len(s2.set))) for _, in := range s1.set { points = append(points, point{in.a, true}) points = append(points, point{in.b, false}) } for _, in := range s2.set { points = append(points, point{in.a, true}) points = append(points, point{in.b, false}) } sort.Slice(points, func(i, j int) bool { if points[i].x == points[j].x { return !points[i].a } return points[i].x < points[j].x }) s := empty() start, count := 0, 0 for _, p := range points { switch count { case 0: count++ case 1: if p.a { start = p.x count++ } else { count-- } case 2: s.set = append(s.set, interval{start, p.x, 0}) count-- } } s.update() return s }
go
func (s1 VertexSet) And(s2 VertexSet) VertexSet { switch { case s1.set == nil: return s2 case s2.set == nil: return s1 } type point struct { x int a bool // Tells if x is a of [a, b). } points := make([]point, 0, 2*(len(s1.set)+len(s2.set))) for _, in := range s1.set { points = append(points, point{in.a, true}) points = append(points, point{in.b, false}) } for _, in := range s2.set { points = append(points, point{in.a, true}) points = append(points, point{in.b, false}) } sort.Slice(points, func(i, j int) bool { if points[i].x == points[j].x { return !points[i].a } return points[i].x < points[j].x }) s := empty() start, count := 0, 0 for _, p := range points { switch count { case 0: count++ case 1: if p.a { start = p.x count++ } else { count-- } case 2: s.set = append(s.set, interval{start, p.x, 0}) count-- } } s.update() return s }
[ "func", "(", "s1", "VertexSet", ")", "And", "(", "s2", "VertexSet", ")", "VertexSet", "{", "switch", "{", "case", "s1", ".", "set", "==", "nil", ":", "return", "s2", "\n", "case", "s2", ".", "set", "==", "nil", ":", "return", "s1", "\n", "}", "\n\n", "type", "point", "struct", "{", "x", "int", "\n", "a", "bool", "// Tells if x is a of [a, b).", "\n", "}", "\n", "points", ":=", "make", "(", "[", "]", "point", ",", "0", ",", "2", "*", "(", "len", "(", "s1", ".", "set", ")", "+", "len", "(", "s2", ".", "set", ")", ")", ")", "\n", "for", "_", ",", "in", ":=", "range", "s1", ".", "set", "{", "points", "=", "append", "(", "points", ",", "point", "{", "in", ".", "a", ",", "true", "}", ")", "\n", "points", "=", "append", "(", "points", ",", "point", "{", "in", ".", "b", ",", "false", "}", ")", "\n", "}", "\n", "for", "_", ",", "in", ":=", "range", "s2", ".", "set", "{", "points", "=", "append", "(", "points", ",", "point", "{", "in", ".", "a", ",", "true", "}", ")", "\n", "points", "=", "append", "(", "points", ",", "point", "{", "in", ".", "b", ",", "false", "}", ")", "\n", "}", "\n", "sort", ".", "Slice", "(", "points", ",", "func", "(", "i", ",", "j", "int", ")", "bool", "{", "if", "points", "[", "i", "]", ".", "x", "==", "points", "[", "j", "]", ".", "x", "{", "return", "!", "points", "[", "i", "]", ".", "a", "\n", "}", "\n", "return", "points", "[", "i", "]", ".", "x", "<", "points", "[", "j", "]", ".", "x", "\n", "}", ")", "\n\n", "s", ":=", "empty", "(", ")", "\n", "start", ",", "count", ":=", "0", ",", "0", "\n", "for", "_", ",", "p", ":=", "range", "points", "{", "switch", "count", "{", "case", "0", ":", "count", "++", "\n", "case", "1", ":", "if", "p", ".", "a", "{", "start", "=", "p", ".", "x", "\n", "count", "++", "\n", "}", "else", "{", "count", "--", "\n", "}", "\n", "case", "2", ":", "s", ".", "set", "=", "append", "(", "s", ".", "set", ",", "interval", "{", "start", ",", "p", ".", "x", ",", "0", "}", ")", "\n", "count", "--", "\n", "}", "\n", "}", "\n", "s", ".", "update", "(", ")", "\n", "return", "s", "\n", "}" ]
// And returns the set of all vertices belonging to both s1 and s2.
[ "And", "returns", "the", "set", "of", "all", "vertices", "belonging", "to", "both", "s1", "and", "s2", "." ]
40eb135c0b2618a87b653037e2697f58436d4666
https://github.com/yourbasic/graph/blob/40eb135c0b2618a87b653037e2697f58436d4666/build/vertexset.go#L140-L188
9,179
brutella/hc
gen/golang/service.go
ServiceGoCode
func ServiceGoCode(svc *gen.ServiceMetadata, chars []*gen.CharacteristicMetadata) ([]byte, error) { var err error var buf bytes.Buffer data := ServiceDecl(svc, chars) t := template.New("Test Template") t, err = t.Parse(ServiceStructTemplate) t.Execute(&buf, data) return buf.Bytes(), err }
go
func ServiceGoCode(svc *gen.ServiceMetadata, chars []*gen.CharacteristicMetadata) ([]byte, error) { var err error var buf bytes.Buffer data := ServiceDecl(svc, chars) t := template.New("Test Template") t, err = t.Parse(ServiceStructTemplate) t.Execute(&buf, data) return buf.Bytes(), err }
[ "func", "ServiceGoCode", "(", "svc", "*", "gen", ".", "ServiceMetadata", ",", "chars", "[", "]", "*", "gen", ".", "CharacteristicMetadata", ")", "(", "[", "]", "byte", ",", "error", ")", "{", "var", "err", "error", "\n", "var", "buf", "bytes", ".", "Buffer", "\n\n", "data", ":=", "ServiceDecl", "(", "svc", ",", "chars", ")", "\n\n", "t", ":=", "template", ".", "New", "(", "\"", "\"", ")", "\n\n", "t", ",", "err", "=", "t", ".", "Parse", "(", "ServiceStructTemplate", ")", "\n", "t", ".", "Execute", "(", "&", "buf", ",", "data", ")", "\n\n", "return", "buf", ".", "Bytes", "(", ")", ",", "err", "\n", "}" ]
// ServiceGoCode returns the o code for a characteristic file
[ "ServiceGoCode", "returns", "the", "o", "code", "for", "a", "characteristic", "file" ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/gen/golang/service.go#L73-L85
9,180
brutella/hc
accessory/bridge.go
NewBridge
func NewBridge(info Info) *Bridge { acc := Bridge{} acc.Accessory = New(info, TypeBridge) return &acc }
go
func NewBridge(info Info) *Bridge { acc := Bridge{} acc.Accessory = New(info, TypeBridge) return &acc }
[ "func", "NewBridge", "(", "info", "Info", ")", "*", "Bridge", "{", "acc", ":=", "Bridge", "{", "}", "\n", "acc", ".", "Accessory", "=", "New", "(", "info", ",", "TypeBridge", ")", "\n\n", "return", "&", "acc", "\n", "}" ]
// NewBridge returns a bridge which implements model.Bridge.
[ "NewBridge", "returns", "a", "bridge", "which", "implements", "model", ".", "Bridge", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/accessory/bridge.go#L8-L13
9,181
brutella/hc
hap/pair/setup_client_controller.go
NewSetupClientController
func NewSetupClientController(pin string, client hap.Device, database db.Database) *SetupClientController { session := NewSetupClientSession("Pair-Setup", pin) controller := SetupClientController{ client: client, session: session, database: database, } return &controller }
go
func NewSetupClientController(pin string, client hap.Device, database db.Database) *SetupClientController { session := NewSetupClientSession("Pair-Setup", pin) controller := SetupClientController{ client: client, session: session, database: database, } return &controller }
[ "func", "NewSetupClientController", "(", "pin", "string", ",", "client", "hap", ".", "Device", ",", "database", "db", ".", "Database", ")", "*", "SetupClientController", "{", "session", ":=", "NewSetupClientSession", "(", "\"", "\"", ",", "pin", ")", "\n", "controller", ":=", "SetupClientController", "{", "client", ":", "client", ",", "session", ":", "session", ",", "database", ":", "database", ",", "}", "\n", "return", "&", "controller", "\n", "}" ]
// NewSetupClientController returns a new setup client controller.
[ "NewSetupClientController", "returns", "a", "new", "setup", "client", "controller", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/pair/setup_client_controller.go#L26-L34
9,182
brutella/hc
hap/pair/setup_client_controller.go
InitialPairingRequest
func (setup *SetupClientController) InitialPairingRequest() io.Reader { out := util.NewTLV8Container() out.SetByte(TagPairingMethod, 0) out.SetByte(TagSequence, PairStepStartRequest.Byte()) return out.BytesBuffer() }
go
func (setup *SetupClientController) InitialPairingRequest() io.Reader { out := util.NewTLV8Container() out.SetByte(TagPairingMethod, 0) out.SetByte(TagSequence, PairStepStartRequest.Byte()) return out.BytesBuffer() }
[ "func", "(", "setup", "*", "SetupClientController", ")", "InitialPairingRequest", "(", ")", "io", ".", "Reader", "{", "out", ":=", "util", ".", "NewTLV8Container", "(", ")", "\n", "out", ".", "SetByte", "(", "TagPairingMethod", ",", "0", ")", "\n", "out", ".", "SetByte", "(", "TagSequence", ",", "PairStepStartRequest", ".", "Byte", "(", ")", ")", "\n\n", "return", "out", ".", "BytesBuffer", "(", ")", "\n", "}" ]
// InitialPairingRequest returns the first request the client sends to an accessory to start the paring process. // The request contains the sequence set to PairStepStartRequest.
[ "InitialPairingRequest", "returns", "the", "first", "request", "the", "client", "sends", "to", "an", "accessory", "to", "start", "the", "paring", "process", ".", "The", "request", "contains", "the", "sequence", "set", "to", "PairStepStartRequest", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/pair/setup_client_controller.go#L38-L44
9,183
brutella/hc
accessory/thermometer.go
NewTemperatureSensor
func NewTemperatureSensor(info Info, temp, min, max, steps float64) *Thermometer { acc := Thermometer{} acc.Accessory = New(info, TypeThermostat) acc.TempSensor = service.NewTemperatureSensor() acc.TempSensor.CurrentTemperature.SetValue(temp) acc.TempSensor.CurrentTemperature.SetMinValue(min) acc.TempSensor.CurrentTemperature.SetMaxValue(max) acc.TempSensor.CurrentTemperature.SetStepValue(steps) acc.AddService(acc.TempSensor.Service) return &acc }
go
func NewTemperatureSensor(info Info, temp, min, max, steps float64) *Thermometer { acc := Thermometer{} acc.Accessory = New(info, TypeThermostat) acc.TempSensor = service.NewTemperatureSensor() acc.TempSensor.CurrentTemperature.SetValue(temp) acc.TempSensor.CurrentTemperature.SetMinValue(min) acc.TempSensor.CurrentTemperature.SetMaxValue(max) acc.TempSensor.CurrentTemperature.SetStepValue(steps) acc.AddService(acc.TempSensor.Service) return &acc }
[ "func", "NewTemperatureSensor", "(", "info", "Info", ",", "temp", ",", "min", ",", "max", ",", "steps", "float64", ")", "*", "Thermometer", "{", "acc", ":=", "Thermometer", "{", "}", "\n", "acc", ".", "Accessory", "=", "New", "(", "info", ",", "TypeThermostat", ")", "\n", "acc", ".", "TempSensor", "=", "service", ".", "NewTemperatureSensor", "(", ")", "\n", "acc", ".", "TempSensor", ".", "CurrentTemperature", ".", "SetValue", "(", "temp", ")", "\n", "acc", ".", "TempSensor", ".", "CurrentTemperature", ".", "SetMinValue", "(", "min", ")", "\n", "acc", ".", "TempSensor", ".", "CurrentTemperature", ".", "SetMaxValue", "(", "max", ")", "\n", "acc", ".", "TempSensor", ".", "CurrentTemperature", ".", "SetStepValue", "(", "steps", ")", "\n\n", "acc", ".", "AddService", "(", "acc", ".", "TempSensor", ".", "Service", ")", "\n\n", "return", "&", "acc", "\n", "}" ]
// NewTemperatureSensor returns a Thermometer which implements model.Thermometer.
[ "NewTemperatureSensor", "returns", "a", "Thermometer", "which", "implements", "model", ".", "Thermometer", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/accessory/thermometer.go#L14-L26
9,184
brutella/hc
crypto/curve25519/curve25519.go
GeneratePrivateKey
func GeneratePrivateKey() [keySize]byte { var b [keySize]byte rand.Read(b[:]) return b }
go
func GeneratePrivateKey() [keySize]byte { var b [keySize]byte rand.Read(b[:]) return b }
[ "func", "GeneratePrivateKey", "(", ")", "[", "keySize", "]", "byte", "{", "var", "b", "[", "keySize", "]", "byte", "\n", "rand", ".", "Read", "(", "b", "[", ":", "]", ")", "\n\n", "return", "b", "\n", "}" ]
// GeneratePrivateKey returns random bytes.
[ "GeneratePrivateKey", "returns", "random", "bytes", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/crypto/curve25519/curve25519.go#L13-L18
9,185
brutella/hc
crypto/curve25519/curve25519.go
PublicKey
func PublicKey(privateKey [keySize]byte) [keySize]byte { var k [keySize]byte curve25519.ScalarBaseMult(&k, &privateKey) return k }
go
func PublicKey(privateKey [keySize]byte) [keySize]byte { var k [keySize]byte curve25519.ScalarBaseMult(&k, &privateKey) return k }
[ "func", "PublicKey", "(", "privateKey", "[", "keySize", "]", "byte", ")", "[", "keySize", "]", "byte", "{", "var", "k", "[", "keySize", "]", "byte", "\n", "curve25519", ".", "ScalarBaseMult", "(", "&", "k", ",", "&", "privateKey", ")", "\n\n", "return", "k", "\n", "}" ]
// PublicKey returns a Curve25519 public key derived from privateKey.
[ "PublicKey", "returns", "a", "Curve25519", "public", "key", "derived", "from", "privateKey", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/crypto/curve25519/curve25519.go#L21-L26
9,186
brutella/hc
crypto/curve25519/curve25519.go
SharedSecret
func SharedSecret(privateKey, otherPublicKey [keySize]byte) [keySize]byte { var k [keySize]byte curve25519.ScalarMult(&k, &privateKey, &otherPublicKey) return k }
go
func SharedSecret(privateKey, otherPublicKey [keySize]byte) [keySize]byte { var k [keySize]byte curve25519.ScalarMult(&k, &privateKey, &otherPublicKey) return k }
[ "func", "SharedSecret", "(", "privateKey", ",", "otherPublicKey", "[", "keySize", "]", "byte", ")", "[", "keySize", "]", "byte", "{", "var", "k", "[", "keySize", "]", "byte", "\n", "curve25519", ".", "ScalarMult", "(", "&", "k", ",", "&", "privateKey", ",", "&", "otherPublicKey", ")", "\n\n", "return", "k", "\n", "}" ]
// SharedSecret returns a Curve25519 shared secret derived from privateKey and otherPublicKey.
[ "SharedSecret", "returns", "a", "Curve25519", "shared", "secret", "derived", "from", "privateKey", "and", "otherPublicKey", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/crypto/curve25519/curve25519.go#L29-L34
9,187
brutella/hc
hap/keep_alive.go
NewKeepAlive
func NewKeepAlive(timeout time.Duration, context Context) *KeepAlive { k := KeepAlive{ timeout: timeout, context: context, } return &k }
go
func NewKeepAlive(timeout time.Duration, context Context) *KeepAlive { k := KeepAlive{ timeout: timeout, context: context, } return &k }
[ "func", "NewKeepAlive", "(", "timeout", "time", ".", "Duration", ",", "context", "Context", ")", "*", "KeepAlive", "{", "k", ":=", "KeepAlive", "{", "timeout", ":", "timeout", ",", "context", ":", "context", ",", "}", "\n\n", "return", "&", "k", "\n", "}" ]
// NewKeepAlive returns a new keep alive for a specific timeout.
[ "NewKeepAlive", "returns", "a", "new", "keep", "alive", "for", "a", "specific", "timeout", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/keep_alive.go#L20-L27
9,188
brutella/hc
hap/keep_alive.go
Start
func (k *KeepAlive) Start(ctx gocontext.Context) { for { select { case <-ctx.Done(): return case <-time.After(k.timeout): k.sendKeepAlive() } } }
go
func (k *KeepAlive) Start(ctx gocontext.Context) { for { select { case <-ctx.Done(): return case <-time.After(k.timeout): k.sendKeepAlive() } } }
[ "func", "(", "k", "*", "KeepAlive", ")", "Start", "(", "ctx", "gocontext", ".", "Context", ")", "{", "for", "{", "select", "{", "case", "<-", "ctx", ".", "Done", "(", ")", ":", "return", "\n", "case", "<-", "time", ".", "After", "(", "k", ".", "timeout", ")", ":", "k", ".", "sendKeepAlive", "(", ")", "\n", "}", "\n", "}", "\n", "}" ]
// Start starts sending keep alive messages. This method blocks until the context is canceled.
[ "Start", "starts", "sending", "keep", "alive", "messages", ".", "This", "method", "blocks", "until", "the", "context", "is", "canceled", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/keep_alive.go#L30-L39
9,189
brutella/hc
hap/pair/verify_client_controller.go
NewVerifyClientController
func NewVerifyClientController(client hap.Device, database db.Database) *VerifyClientController { controller := VerifyClientController{ client: client, database: database, session: NewVerifySession(), } return &controller }
go
func NewVerifyClientController(client hap.Device, database db.Database) *VerifyClientController { controller := VerifyClientController{ client: client, database: database, session: NewVerifySession(), } return &controller }
[ "func", "NewVerifyClientController", "(", "client", "hap", ".", "Device", ",", "database", "db", ".", "Database", ")", "*", "VerifyClientController", "{", "controller", ":=", "VerifyClientController", "{", "client", ":", "client", ",", "database", ":", "database", ",", "session", ":", "NewVerifySession", "(", ")", ",", "}", "\n\n", "return", "&", "controller", "\n", "}" ]
// NewVerifyClientController returns a new verify client controller.
[ "NewVerifyClientController", "returns", "a", "new", "verify", "client", "controller", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/pair/verify_client_controller.go#L28-L36
9,190
brutella/hc
hap/pair/verify_client_controller.go
Handle
func (verify *VerifyClientController) Handle(in util.Container) (util.Container, error) { var out util.Container var err error method := PairMethodType(in.GetByte(TagPairingMethod)) // It is valid that method is not sent // If method is sent then it must be 0x00 if method != PairingMethodDefault { return nil, errInvalidPairMethod(method) } seq := VerifyStepType(in.GetByte(TagSequence)) switch seq { case VerifyStepStartResponse: out, err = verify.handlePairStepVerifyResponse(in) case VerifyStepFinishResponse: out, err = verify.handlePairVerifyStepFinishResponse(in) default: return nil, errInvalidVerifyStep(seq) } return out, err }
go
func (verify *VerifyClientController) Handle(in util.Container) (util.Container, error) { var out util.Container var err error method := PairMethodType(in.GetByte(TagPairingMethod)) // It is valid that method is not sent // If method is sent then it must be 0x00 if method != PairingMethodDefault { return nil, errInvalidPairMethod(method) } seq := VerifyStepType(in.GetByte(TagSequence)) switch seq { case VerifyStepStartResponse: out, err = verify.handlePairStepVerifyResponse(in) case VerifyStepFinishResponse: out, err = verify.handlePairVerifyStepFinishResponse(in) default: return nil, errInvalidVerifyStep(seq) } return out, err }
[ "func", "(", "verify", "*", "VerifyClientController", ")", "Handle", "(", "in", "util", ".", "Container", ")", "(", "util", ".", "Container", ",", "error", ")", "{", "var", "out", "util", ".", "Container", "\n", "var", "err", "error", "\n\n", "method", ":=", "PairMethodType", "(", "in", ".", "GetByte", "(", "TagPairingMethod", ")", ")", "\n\n", "// It is valid that method is not sent", "// If method is sent then it must be 0x00", "if", "method", "!=", "PairingMethodDefault", "{", "return", "nil", ",", "errInvalidPairMethod", "(", "method", ")", "\n", "}", "\n\n", "seq", ":=", "VerifyStepType", "(", "in", ".", "GetByte", "(", "TagSequence", ")", ")", "\n", "switch", "seq", "{", "case", "VerifyStepStartResponse", ":", "out", ",", "err", "=", "verify", ".", "handlePairStepVerifyResponse", "(", "in", ")", "\n", "case", "VerifyStepFinishResponse", ":", "out", ",", "err", "=", "verify", ".", "handlePairVerifyStepFinishResponse", "(", "in", ")", "\n", "default", ":", "return", "nil", ",", "errInvalidVerifyStep", "(", "seq", ")", "\n", "}", "\n\n", "return", "out", ",", "err", "\n", "}" ]
// Handle processes a container to verify if an accessory is paired correctly.
[ "Handle", "processes", "a", "container", "to", "verify", "if", "an", "accessory", "is", "paired", "correctly", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/pair/verify_client_controller.go#L39-L62
9,191
brutella/hc
hap/pair/verify_client_controller.go
InitialKeyVerifyRequest
func (verify *VerifyClientController) InitialKeyVerifyRequest() io.Reader { out := util.NewTLV8Container() out.SetByte(TagPairingMethod, 0) out.SetByte(TagSequence, VerifyStepStartRequest.Byte()) out.SetBytes(TagPublicKey, verify.session.PublicKey[:]) fmt.Println("<- A:", hex.EncodeToString(out.GetBytes(TagPublicKey))) return out.BytesBuffer() }
go
func (verify *VerifyClientController) InitialKeyVerifyRequest() io.Reader { out := util.NewTLV8Container() out.SetByte(TagPairingMethod, 0) out.SetByte(TagSequence, VerifyStepStartRequest.Byte()) out.SetBytes(TagPublicKey, verify.session.PublicKey[:]) fmt.Println("<- A:", hex.EncodeToString(out.GetBytes(TagPublicKey))) return out.BytesBuffer() }
[ "func", "(", "verify", "*", "VerifyClientController", ")", "InitialKeyVerifyRequest", "(", ")", "io", ".", "Reader", "{", "out", ":=", "util", ".", "NewTLV8Container", "(", ")", "\n", "out", ".", "SetByte", "(", "TagPairingMethod", ",", "0", ")", "\n", "out", ".", "SetByte", "(", "TagSequence", ",", "VerifyStepStartRequest", ".", "Byte", "(", ")", ")", "\n", "out", ".", "SetBytes", "(", "TagPublicKey", ",", "verify", ".", "session", ".", "PublicKey", "[", ":", "]", ")", "\n\n", "fmt", ".", "Println", "(", "\"", "\"", ",", "hex", ".", "EncodeToString", "(", "out", ".", "GetBytes", "(", "TagPublicKey", ")", ")", ")", "\n\n", "return", "out", ".", "BytesBuffer", "(", ")", "\n", "}" ]
// InitialKeyVerifyRequest returns the first request the client sends to an accessory to start the paring verifcation process. // The request contains the client public key and sequence set to VerifyStepStartRequest.
[ "InitialKeyVerifyRequest", "returns", "the", "first", "request", "the", "client", "sends", "to", "an", "accessory", "to", "start", "the", "paring", "verifcation", "process", ".", "The", "request", "contains", "the", "client", "public", "key", "and", "sequence", "set", "to", "VerifyStepStartRequest", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/pair/verify_client_controller.go#L66-L75
9,192
brutella/hc
hap/context.go
NewContextForSecuredDevice
func NewContextForSecuredDevice(b SecuredDevice) Context { ctx := context{ storage: map[interface{}]interface{}{}, mutex: &sync.Mutex{}, } ctx.SetSecuredDevice(b) return &ctx }
go
func NewContextForSecuredDevice(b SecuredDevice) Context { ctx := context{ storage: map[interface{}]interface{}{}, mutex: &sync.Mutex{}, } ctx.SetSecuredDevice(b) return &ctx }
[ "func", "NewContextForSecuredDevice", "(", "b", "SecuredDevice", ")", "Context", "{", "ctx", ":=", "context", "{", "storage", ":", "map", "[", "interface", "{", "}", "]", "interface", "{", "}", "{", "}", ",", "mutex", ":", "&", "sync", ".", "Mutex", "{", "}", ",", "}", "\n", "ctx", ".", "SetSecuredDevice", "(", "b", ")", "\n", "return", "&", "ctx", "\n", "}" ]
// NewContextForSecuredDevice returns a new Context
[ "NewContextForSecuredDevice", "returns", "a", "new", "Context" ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/context.go#L50-L57
9,193
brutella/hc
hap/context.go
ActiveConnections
func (ctx *context) ActiveConnections() []net.Conn { var connections []net.Conn ctx.mutex.Lock() defer ctx.mutex.Unlock() for _, v := range ctx.storage { if s, ok := v.(Session); ok == true { connections = append(connections, s.Connection()) } } return connections }
go
func (ctx *context) ActiveConnections() []net.Conn { var connections []net.Conn ctx.mutex.Lock() defer ctx.mutex.Unlock() for _, v := range ctx.storage { if s, ok := v.(Session); ok == true { connections = append(connections, s.Connection()) } } return connections }
[ "func", "(", "ctx", "*", "context", ")", "ActiveConnections", "(", ")", "[", "]", "net", ".", "Conn", "{", "var", "connections", "[", "]", "net", ".", "Conn", "\n", "ctx", ".", "mutex", ".", "Lock", "(", ")", "\n", "defer", "ctx", ".", "mutex", ".", "Unlock", "(", ")", "\n", "for", "_", ",", "v", ":=", "range", "ctx", ".", "storage", "{", "if", "s", ",", "ok", ":=", "v", ".", "(", "Session", ")", ";", "ok", "==", "true", "{", "connections", "=", "append", "(", "connections", ",", "s", ".", "Connection", "(", ")", ")", "\n", "}", "\n", "}", "\n\n", "return", "connections", "\n", "}" ]
// Returns a list of active connections
[ "Returns", "a", "list", "of", "active", "connections" ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/hap/context.go#L110-L121
9,194
brutella/hc
characteristic/characteristic.go
noWritePerms
func noWritePerms(permissions []string) bool { for _, value := range permissions { if value == PermWrite { return false } } return true }
go
func noWritePerms(permissions []string) bool { for _, value := range permissions { if value == PermWrite { return false } } return true }
[ "func", "noWritePerms", "(", "permissions", "[", "]", "string", ")", "bool", "{", "for", "_", ",", "value", ":=", "range", "permissions", "{", "if", "value", "==", "PermWrite", "{", "return", "false", "\n", "}", "\n", "}", "\n", "return", "true", "\n", "}" ]
// noWritePerms returns true when permissions include no write permission
[ "noWritePerms", "returns", "true", "when", "permissions", "include", "no", "write", "permission" ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/characteristic/characteristic.go#L47-L54
9,195
brutella/hc
characteristic/characteristic.go
Equal
func (c *Characteristic) Equal(other interface{}) bool { if characteristic, ok := other.(*Characteristic); ok == true { // The value type (e.g. float32, bool,...) of property `Value` may be different even though // they look the same. They are equal when they have the same string representation. value := fmt.Sprintf("%+v", c.Value) otherValue := fmt.Sprintf("%+v", characteristic.Value) return value == otherValue && c.ID == characteristic.ID && c.Type == characteristic.Type && len(c.Perms) == len(characteristic.Perms) && c.Description == characteristic.Description && c.Format == characteristic.Format && c.Unit == characteristic.Unit && c.MaxLen == characteristic.MaxLen && c.MaxValue == characteristic.MaxValue && c.MinValue == characteristic.MinValue && c.StepValue == characteristic.StepValue && c.Events == characteristic.Events } return false }
go
func (c *Characteristic) Equal(other interface{}) bool { if characteristic, ok := other.(*Characteristic); ok == true { // The value type (e.g. float32, bool,...) of property `Value` may be different even though // they look the same. They are equal when they have the same string representation. value := fmt.Sprintf("%+v", c.Value) otherValue := fmt.Sprintf("%+v", characteristic.Value) return value == otherValue && c.ID == characteristic.ID && c.Type == characteristic.Type && len(c.Perms) == len(characteristic.Perms) && c.Description == characteristic.Description && c.Format == characteristic.Format && c.Unit == characteristic.Unit && c.MaxLen == characteristic.MaxLen && c.MaxValue == characteristic.MaxValue && c.MinValue == characteristic.MinValue && c.StepValue == characteristic.StepValue && c.Events == characteristic.Events } return false }
[ "func", "(", "c", "*", "Characteristic", ")", "Equal", "(", "other", "interface", "{", "}", ")", "bool", "{", "if", "characteristic", ",", "ok", ":=", "other", ".", "(", "*", "Characteristic", ")", ";", "ok", "==", "true", "{", "// The value type (e.g. float32, bool,...) of property `Value` may be different even though", "// they look the same. They are equal when they have the same string representation.", "value", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "c", ".", "Value", ")", "\n", "otherValue", ":=", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "characteristic", ".", "Value", ")", "\n\n", "return", "value", "==", "otherValue", "&&", "c", ".", "ID", "==", "characteristic", ".", "ID", "&&", "c", ".", "Type", "==", "characteristic", ".", "Type", "&&", "len", "(", "c", ".", "Perms", ")", "==", "len", "(", "characteristic", ".", "Perms", ")", "&&", "c", ".", "Description", "==", "characteristic", ".", "Description", "&&", "c", ".", "Format", "==", "characteristic", ".", "Format", "&&", "c", ".", "Unit", "==", "characteristic", ".", "Unit", "&&", "c", ".", "MaxLen", "==", "characteristic", ".", "MaxLen", "&&", "c", ".", "MaxValue", "==", "characteristic", ".", "MaxValue", "&&", "c", ".", "MinValue", "==", "characteristic", ".", "MinValue", "&&", "c", ".", "StepValue", "==", "characteristic", ".", "StepValue", "&&", "c", ".", "Events", "==", "characteristic", ".", "Events", "\n", "}", "\n\n", "return", "false", "\n", "}" ]
// Equal returns true when receiver has the values as the argument.
[ "Equal", "returns", "true", "when", "receiver", "has", "the", "values", "as", "the", "argument", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/characteristic/characteristic.go#L107-L118
9,196
brutella/hc
service/service.go
New
func New(typ string) *Service { s := Service{ Type: typ, Characteristics: []*characteristic.Characteristic{}, Linked: []int64{}, } return &s }
go
func New(typ string) *Service { s := Service{ Type: typ, Characteristics: []*characteristic.Characteristic{}, Linked: []int64{}, } return &s }
[ "func", "New", "(", "typ", "string", ")", "*", "Service", "{", "s", ":=", "Service", "{", "Type", ":", "typ", ",", "Characteristics", ":", "[", "]", "*", "characteristic", ".", "Characteristic", "{", "}", ",", "Linked", ":", "[", "]", "int64", "{", "}", ",", "}", "\n\n", "return", "&", "s", "\n", "}" ]
// New returns a new service.
[ "New", "returns", "a", "new", "service", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/service/service.go#L18-L26
9,197
brutella/hc
service/service.go
GetCharacteristics
func (s *Service) GetCharacteristics() []*characteristic.Characteristic { var result []*characteristic.Characteristic for _, c := range s.Characteristics { result = append(result, c) } return result }
go
func (s *Service) GetCharacteristics() []*characteristic.Characteristic { var result []*characteristic.Characteristic for _, c := range s.Characteristics { result = append(result, c) } return result }
[ "func", "(", "s", "*", "Service", ")", "GetCharacteristics", "(", ")", "[", "]", "*", "characteristic", ".", "Characteristic", "{", "var", "result", "[", "]", "*", "characteristic", ".", "Characteristic", "\n", "for", "_", ",", "c", ":=", "range", "s", ".", "Characteristics", "{", "result", "=", "append", "(", "result", ",", "c", ")", "\n", "}", "\n", "return", "result", "\n", "}" ]
// GetCharacteristics returns the characteristics which represent the service.
[ "GetCharacteristics", "returns", "the", "characteristics", "which", "represent", "the", "service", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/service/service.go#L39-L45
9,198
brutella/hc
service/service.go
Equal
func (s *Service) Equal(other interface{}) bool { if service, ok := other.(*Service); ok == true { if len(s.Characteristics) != len(service.Characteristics) { println("Number of chars wrong") return false } for i, c := range s.Characteristics { other := service.Characteristics[i] if c.Equal(other) == false { return false } } return s.ID == service.ID && s.Type == service.Type } return false }
go
func (s *Service) Equal(other interface{}) bool { if service, ok := other.(*Service); ok == true { if len(s.Characteristics) != len(service.Characteristics) { println("Number of chars wrong") return false } for i, c := range s.Characteristics { other := service.Characteristics[i] if c.Equal(other) == false { return false } } return s.ID == service.ID && s.Type == service.Type } return false }
[ "func", "(", "s", "*", "Service", ")", "Equal", "(", "other", "interface", "{", "}", ")", "bool", "{", "if", "service", ",", "ok", ":=", "other", ".", "(", "*", "Service", ")", ";", "ok", "==", "true", "{", "if", "len", "(", "s", ".", "Characteristics", ")", "!=", "len", "(", "service", ".", "Characteristics", ")", "{", "println", "(", "\"", "\"", ")", "\n", "return", "false", "\n", "}", "\n\n", "for", "i", ",", "c", ":=", "range", "s", ".", "Characteristics", "{", "other", ":=", "service", ".", "Characteristics", "[", "i", "]", "\n", "if", "c", ".", "Equal", "(", "other", ")", "==", "false", "{", "return", "false", "\n", "}", "\n", "}", "\n\n", "return", "s", ".", "ID", "==", "service", ".", "ID", "&&", "s", ".", "Type", "==", "service", ".", "Type", "\n", "}", "\n\n", "return", "false", "\n", "}" ]
// Equal returns true when receiver has the same characteristics, service id and service type as the argument.
[ "Equal", "returns", "true", "when", "receiver", "has", "the", "same", "characteristics", "service", "id", "and", "service", "type", "as", "the", "argument", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/service/service.go#L70-L88
9,199
brutella/hc
crypto/secure_session.go
NewSecureSessionFromSharedKey
func NewSecureSessionFromSharedKey(sharedKey [32]byte) (Cryptographer, error) { salt := []byte("Control-Salt") out := []byte("Control-Read-Encryption-Key") in := []byte("Control-Write-Encryption-Key") var s = new(secureSession) var err error s.encryptKey, err = hkdf.Sha512(sharedKey[:], salt, out) s.encryptCount = 0 if err != nil { return nil, err } s.decryptKey, err = hkdf.Sha512(sharedKey[:], salt, in) s.decryptCount = 0 return s, err }
go
func NewSecureSessionFromSharedKey(sharedKey [32]byte) (Cryptographer, error) { salt := []byte("Control-Salt") out := []byte("Control-Read-Encryption-Key") in := []byte("Control-Write-Encryption-Key") var s = new(secureSession) var err error s.encryptKey, err = hkdf.Sha512(sharedKey[:], salt, out) s.encryptCount = 0 if err != nil { return nil, err } s.decryptKey, err = hkdf.Sha512(sharedKey[:], salt, in) s.decryptCount = 0 return s, err }
[ "func", "NewSecureSessionFromSharedKey", "(", "sharedKey", "[", "32", "]", "byte", ")", "(", "Cryptographer", ",", "error", ")", "{", "salt", ":=", "[", "]", "byte", "(", "\"", "\"", ")", "\n", "out", ":=", "[", "]", "byte", "(", "\"", "\"", ")", "\n", "in", ":=", "[", "]", "byte", "(", "\"", "\"", ")", "\n\n", "var", "s", "=", "new", "(", "secureSession", ")", "\n", "var", "err", "error", "\n", "s", ".", "encryptKey", ",", "err", "=", "hkdf", ".", "Sha512", "(", "sharedKey", "[", ":", "]", ",", "salt", ",", "out", ")", "\n", "s", ".", "encryptCount", "=", "0", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "s", ".", "decryptKey", ",", "err", "=", "hkdf", ".", "Sha512", "(", "sharedKey", "[", ":", "]", ",", "salt", ",", "in", ")", "\n", "s", ".", "decryptCount", "=", "0", "\n\n", "return", "s", ",", "err", "\n", "}" ]
// NewSecureSessionFromSharedKey returns a session from a shared private key.
[ "NewSecureSessionFromSharedKey", "returns", "a", "session", "from", "a", "shared", "private", "key", "." ]
e301c8e55cb063ac61007bd632f9bda2d700557e
https://github.com/brutella/hc/blob/e301c8e55cb063ac61007bd632f9bda2d700557e/crypto/secure_session.go#L24-L41