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
list
docstring
stringlengths
6
2.61k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
85
252
partition
stringclasses
1 value
pilosa/pilosa
stats/stats.go
Close
func (a MultiStatsClient) Close() error { for _, c := range a { err := c.Close() if err != nil { return err } } return nil }
go
func (a MultiStatsClient) Close() error { for _, c := range a { err := c.Close() if err != nil { return err } } return nil }
[ "func", "(", "a", "MultiStatsClient", ")", "Close", "(", ")", "error", "{", "for", "_", ",", "c", ":=", "range", "a", "{", "err", ":=", "c", ".", "Close", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// Close shuts down the stats clients.
[ "Close", "shuts", "down", "the", "stats", "clients", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/stats/stats.go#L240-L248
train
pilosa/pilosa
server/server.go
NewCommand
func NewCommand(stdin io.Reader, stdout, stderr io.Writer, opts ...CommandOption) *Command { c := &Command{ Config: NewConfig(), CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), Started: make(chan struct{}), done: make(chan struct{}), } for _, opt := range opts { err := opt(c) if err != nil { panic(err) // TODO: Return error instead of panic? } } return c }
go
func NewCommand(stdin io.Reader, stdout, stderr io.Writer, opts ...CommandOption) *Command { c := &Command{ Config: NewConfig(), CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), Started: make(chan struct{}), done: make(chan struct{}), } for _, opt := range opts { err := opt(c) if err != nil { panic(err) // TODO: Return error instead of panic? } } return c }
[ "func", "NewCommand", "(", "stdin", "io", ".", "Reader", ",", "stdout", ",", "stderr", "io", ".", "Writer", ",", "opts", "...", "CommandOption", ")", "*", "Command", "{", "c", ":=", "&", "Command", "{", "Config", ":", "NewConfig", "(", ")", ",", "CmdIO", ":", "pilosa", ".", "NewCmdIO", "(", "stdin", ",", "stdout", ",", "stderr", ")", ",", "Started", ":", "make", "(", "chan", "struct", "{", "}", ")", ",", "done", ":", "make", "(", "chan", "struct", "{", "}", ")", ",", "}", "\n\n", "for", "_", ",", "opt", ":=", "range", "opts", "{", "err", ":=", "opt", "(", "c", ")", "\n", "if", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "// TODO: Return error instead of panic?", "}", "\n", "}", "\n\n", "return", "c", "\n", "}" ]
// NewCommand returns a new instance of Main.
[ "NewCommand", "returns", "a", "new", "instance", "of", "Main", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L107-L126
train
pilosa/pilosa
server/server.go
Start
func (m *Command) Start() (err error) { defer close(m.Started) // Seed random number generator rand.Seed(time.Now().UTC().UnixNano()) // SetupServer err = m.SetupServer() if err != nil { return errors.Wrap(err, "setting up server") } // SetupNetworking err = m.setupNetworking() if err != nil { return errors.Wrap(err, "setting up networking") } go func() { err := m.Handler.Serve() if err != nil { m.logger.Printf("handler serve error: %v", err) } }() // Initialize server. if err = m.Server.Open(); err != nil { return errors.Wrap(err, "opening server") } m.logger.Printf("listening as %s\n", m.listenURI) return nil }
go
func (m *Command) Start() (err error) { defer close(m.Started) // Seed random number generator rand.Seed(time.Now().UTC().UnixNano()) // SetupServer err = m.SetupServer() if err != nil { return errors.Wrap(err, "setting up server") } // SetupNetworking err = m.setupNetworking() if err != nil { return errors.Wrap(err, "setting up networking") } go func() { err := m.Handler.Serve() if err != nil { m.logger.Printf("handler serve error: %v", err) } }() // Initialize server. if err = m.Server.Open(); err != nil { return errors.Wrap(err, "opening server") } m.logger.Printf("listening as %s\n", m.listenURI) return nil }
[ "func", "(", "m", "*", "Command", ")", "Start", "(", ")", "(", "err", "error", ")", "{", "defer", "close", "(", "m", ".", "Started", ")", "\n\n", "// Seed random number generator", "rand", ".", "Seed", "(", "time", ".", "Now", "(", ")", ".", "UTC", "(", ")", ".", "UnixNano", "(", ")", ")", "\n\n", "// SetupServer", "err", "=", "m", ".", "SetupServer", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "// SetupNetworking", "err", "=", "m", ".", "setupNetworking", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "go", "func", "(", ")", "{", "err", ":=", "m", ".", "Handler", ".", "Serve", "(", ")", "\n", "if", "err", "!=", "nil", "{", "m", ".", "logger", ".", "Printf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "}", "(", ")", "\n\n", "// Initialize server.", "if", "err", "=", "m", ".", "Server", ".", "Open", "(", ")", ";", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "m", ".", "logger", ".", "Printf", "(", "\"", "\\n", "\"", ",", "m", ".", "listenURI", ")", "\n\n", "return", "nil", "\n", "}" ]
// Start starts the pilosa server - it returns once the server is running.
[ "Start", "starts", "the", "pilosa", "server", "-", "it", "returns", "once", "the", "server", "is", "running", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L129-L161
train
pilosa/pilosa
server/server.go
Wait
func (m *Command) Wait() error { // First SIGKILL causes server to shut down gracefully. c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) select { case sig := <-c: m.logger.Printf("received signal '%s', gracefully shutting down...\n", sig.String()) // Second signal causes a hard shutdown. go func() { <-c; os.Exit(1) }() return errors.Wrap(m.Close(), "closing command") case <-m.done: m.logger.Printf("server closed externally") return nil } }
go
func (m *Command) Wait() error { // First SIGKILL causes server to shut down gracefully. c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) select { case sig := <-c: m.logger.Printf("received signal '%s', gracefully shutting down...\n", sig.String()) // Second signal causes a hard shutdown. go func() { <-c; os.Exit(1) }() return errors.Wrap(m.Close(), "closing command") case <-m.done: m.logger.Printf("server closed externally") return nil } }
[ "func", "(", "m", "*", "Command", ")", "Wait", "(", ")", "error", "{", "// First SIGKILL causes server to shut down gracefully.", "c", ":=", "make", "(", "chan", "os", ".", "Signal", ",", "2", ")", "\n", "signal", ".", "Notify", "(", "c", ",", "os", ".", "Interrupt", ",", "syscall", ".", "SIGTERM", ")", "\n", "select", "{", "case", "sig", ":=", "<-", "c", ":", "m", ".", "logger", ".", "Printf", "(", "\"", "\\n", "\"", ",", "sig", ".", "String", "(", ")", ")", "\n\n", "// Second signal causes a hard shutdown.", "go", "func", "(", ")", "{", "<-", "c", ";", "os", ".", "Exit", "(", "1", ")", "}", "(", ")", "\n", "return", "errors", ".", "Wrap", "(", "m", ".", "Close", "(", ")", ",", "\"", "\"", ")", "\n", "case", "<-", "m", ".", "done", ":", "m", ".", "logger", ".", "Printf", "(", "\"", "\"", ")", "\n", "return", "nil", "\n", "}", "\n", "}" ]
// Wait waits for the server to be closed or interrupted.
[ "Wait", "waits", "for", "the", "server", "to", "be", "closed", "or", "interrupted", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L164-L179
train
pilosa/pilosa
server/server.go
setupNetworking
func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { return nil } gossipPort, err := strconv.Atoi(m.Config.Gossip.Port) if err != nil { return errors.Wrap(err, "parsing port") } // get the host portion of addr to use for binding gossipHost := m.listenURI.Host m.gossipTransport, err = gossip.NewTransport(gossipHost, gossipPort, m.logger.Logger()) if err != nil { return errors.Wrap(err, "getting transport") } gossipMemberSet, err := gossip.NewMemberSet( m.Config.Gossip, m.API, gossip.WithLogOutput(&filteredWriter{logOutput: m.logOutput, v: m.Config.Verbose}), gossip.WithPilosaLogger(m.logger), gossip.WithTransport(m.gossipTransport), ) if err != nil { return errors.Wrap(err, "getting memberset") } m.gossipMemberSet = gossipMemberSet return errors.Wrap(gossipMemberSet.Open(), "opening gossip memberset") }
go
func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { return nil } gossipPort, err := strconv.Atoi(m.Config.Gossip.Port) if err != nil { return errors.Wrap(err, "parsing port") } // get the host portion of addr to use for binding gossipHost := m.listenURI.Host m.gossipTransport, err = gossip.NewTransport(gossipHost, gossipPort, m.logger.Logger()) if err != nil { return errors.Wrap(err, "getting transport") } gossipMemberSet, err := gossip.NewMemberSet( m.Config.Gossip, m.API, gossip.WithLogOutput(&filteredWriter{logOutput: m.logOutput, v: m.Config.Verbose}), gossip.WithPilosaLogger(m.logger), gossip.WithTransport(m.gossipTransport), ) if err != nil { return errors.Wrap(err, "getting memberset") } m.gossipMemberSet = gossipMemberSet return errors.Wrap(gossipMemberSet.Open(), "opening gossip memberset") }
[ "func", "(", "m", "*", "Command", ")", "setupNetworking", "(", ")", "error", "{", "if", "m", ".", "Config", ".", "Cluster", ".", "Disabled", "{", "return", "nil", "\n", "}", "\n\n", "gossipPort", ",", "err", ":=", "strconv", ".", "Atoi", "(", "m", ".", "Config", ".", "Gossip", ".", "Port", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "// get the host portion of addr to use for binding", "gossipHost", ":=", "m", ".", "listenURI", ".", "Host", "\n", "m", ".", "gossipTransport", ",", "err", "=", "gossip", ".", "NewTransport", "(", "gossipHost", ",", "gossipPort", ",", "m", ".", "logger", ".", "Logger", "(", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "gossipMemberSet", ",", "err", ":=", "gossip", ".", "NewMemberSet", "(", "m", ".", "Config", ".", "Gossip", ",", "m", ".", "API", ",", "gossip", ".", "WithLogOutput", "(", "&", "filteredWriter", "{", "logOutput", ":", "m", ".", "logOutput", ",", "v", ":", "m", ".", "Config", ".", "Verbose", "}", ")", ",", "gossip", ".", "WithPilosaLogger", "(", "m", ".", "logger", ")", ",", "gossip", ".", "WithTransport", "(", "m", ".", "gossipTransport", ")", ",", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "m", ".", "gossipMemberSet", "=", "gossipMemberSet", "\n\n", "return", "errors", ".", "Wrap", "(", "gossipMemberSet", ".", "Open", "(", ")", ",", "\"", "\"", ")", "\n", "}" ]
// setupNetworking sets up internode communication based on the configuration.
[ "setupNetworking", "sets", "up", "internode", "communication", "based", "on", "the", "configuration", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L332-L362
train
pilosa/pilosa
server/server.go
newStatsClient
func newStatsClient(name string, host string) (stats.StatsClient, error) { switch name { case "expvar": return stats.NewExpvarStatsClient(), nil case "statsd": return statsd.NewStatsClient(host) case "nop", "none": return stats.NopStatsClient, nil default: return nil, errors.Errorf("'%v' not a valid stats client, choose from [expvar, statsd, none].", name) } }
go
func newStatsClient(name string, host string) (stats.StatsClient, error) { switch name { case "expvar": return stats.NewExpvarStatsClient(), nil case "statsd": return statsd.NewStatsClient(host) case "nop", "none": return stats.NopStatsClient, nil default: return nil, errors.Errorf("'%v' not a valid stats client, choose from [expvar, statsd, none].", name) } }
[ "func", "newStatsClient", "(", "name", "string", ",", "host", "string", ")", "(", "stats", ".", "StatsClient", ",", "error", ")", "{", "switch", "name", "{", "case", "\"", "\"", ":", "return", "stats", ".", "NewExpvarStatsClient", "(", ")", ",", "nil", "\n", "case", "\"", "\"", ":", "return", "statsd", ".", "NewStatsClient", "(", "host", ")", "\n", "case", "\"", "\"", ",", "\"", "\"", ":", "return", "stats", ".", "NopStatsClient", ",", "nil", "\n", "default", ":", "return", "nil", ",", "errors", ".", "Errorf", "(", "\"", "\"", ",", "name", ")", "\n", "}", "\n", "}" ]
// newStatsClient creates a stats client from the config
[ "newStatsClient", "creates", "a", "stats", "client", "from", "the", "config" ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L392-L403
train
pilosa/pilosa
server/server.go
getListener
func getListener(uri pilosa.URI, tlsconf *tls.Config) (ln net.Listener, err error) { // If bind URI has the https scheme, enable TLS if uri.Scheme == "https" && tlsconf != nil { ln, err = tls.Listen("tcp", uri.HostPort(), tlsconf) if err != nil { return nil, errors.Wrap(err, "tls.Listener") } } else if uri.Scheme == "http" { // Open HTTP listener to determine port (if specified as :0). ln, err = net.Listen("tcp", uri.HostPort()) if err != nil { return nil, errors.Wrap(err, "net.Listen") } } else { return nil, errors.Errorf("unsupported scheme: %s", uri.Scheme) } return ln, nil }
go
func getListener(uri pilosa.URI, tlsconf *tls.Config) (ln net.Listener, err error) { // If bind URI has the https scheme, enable TLS if uri.Scheme == "https" && tlsconf != nil { ln, err = tls.Listen("tcp", uri.HostPort(), tlsconf) if err != nil { return nil, errors.Wrap(err, "tls.Listener") } } else if uri.Scheme == "http" { // Open HTTP listener to determine port (if specified as :0). ln, err = net.Listen("tcp", uri.HostPort()) if err != nil { return nil, errors.Wrap(err, "net.Listen") } } else { return nil, errors.Errorf("unsupported scheme: %s", uri.Scheme) } return ln, nil }
[ "func", "getListener", "(", "uri", "pilosa", ".", "URI", ",", "tlsconf", "*", "tls", ".", "Config", ")", "(", "ln", "net", ".", "Listener", ",", "err", "error", ")", "{", "// If bind URI has the https scheme, enable TLS", "if", "uri", ".", "Scheme", "==", "\"", "\"", "&&", "tlsconf", "!=", "nil", "{", "ln", ",", "err", "=", "tls", ".", "Listen", "(", "\"", "\"", ",", "uri", ".", "HostPort", "(", ")", ",", "tlsconf", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "}", "else", "if", "uri", ".", "Scheme", "==", "\"", "\"", "{", "// Open HTTP listener to determine port (if specified as :0).", "ln", ",", "err", "=", "net", ".", "Listen", "(", "\"", "\"", ",", "uri", ".", "HostPort", "(", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "}", "else", "{", "return", "nil", ",", "errors", ".", "Errorf", "(", "\"", "\"", ",", "uri", ".", "Scheme", ")", "\n", "}", "\n\n", "return", "ln", ",", "nil", "\n", "}" ]
// getListener gets a net.Listener based on the config.
[ "getListener", "gets", "a", "net", ".", "Listener", "based", "on", "the", "config", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/server/server.go#L406-L424
train
pilosa/pilosa
roaring/roaring.go
NewBitmap
func NewBitmap(a ...uint64) *Bitmap { b := &Bitmap{ Containers: newSliceContainers(), } // TODO: We have no way to report this. We aren't in a server context // so we haven't got a logger, nothing is checking for nil returns // from this... _, _ = b.AddN(a...) return b }
go
func NewBitmap(a ...uint64) *Bitmap { b := &Bitmap{ Containers: newSliceContainers(), } // TODO: We have no way to report this. We aren't in a server context // so we haven't got a logger, nothing is checking for nil returns // from this... _, _ = b.AddN(a...) return b }
[ "func", "NewBitmap", "(", "a", "...", "uint64", ")", "*", "Bitmap", "{", "b", ":=", "&", "Bitmap", "{", "Containers", ":", "newSliceContainers", "(", ")", ",", "}", "\n", "// TODO: We have no way to report this. We aren't in a server context", "// so we haven't got a logger, nothing is checking for nil returns", "// from this...", "_", ",", "_", "=", "b", ".", "AddN", "(", "a", "...", ")", "\n", "return", "b", "\n", "}" ]
// NewBitmap returns a Bitmap with an initial set of values.
[ "NewBitmap", "returns", "a", "Bitmap", "with", "an", "initial", "set", "of", "values", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L136-L145
train
pilosa/pilosa
roaring/roaring.go
DirectRemoveN
func (b *Bitmap) DirectRemoveN(a ...uint64) (changed int) { return b.directOpN((*Container).remove, a...) }
go
func (b *Bitmap) DirectRemoveN(a ...uint64) (changed int) { return b.directOpN((*Container).remove, a...) }
[ "func", "(", "b", "*", "Bitmap", ")", "DirectRemoveN", "(", "a", "...", "uint64", ")", "(", "changed", "int", ")", "{", "return", "b", ".", "directOpN", "(", "(", "*", "Container", ")", ".", "remove", ",", "a", "...", ")", "\n", "}" ]
// DirectRemoveN behaves analgously to DirectAddN.
[ "DirectRemoveN", "behaves", "analgously", "to", "DirectAddN", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L224-L226
train
pilosa/pilosa
roaring/roaring.go
directOpN
func (b *Bitmap) directOpN(op func(c *Container, v uint16) bool, a ...uint64) (changed int) { hb := uint64(0xFFFFFFFFFFFFFFFF) // impossible sentinel value var cont *Container for _, v := range a { if newhb := highbits(v); newhb != hb { hb = newhb cont = b.Containers.GetOrCreate(hb) } if op(cont, lowbits(v)) { a[changed] = v changed++ } } return changed }
go
func (b *Bitmap) directOpN(op func(c *Container, v uint16) bool, a ...uint64) (changed int) { hb := uint64(0xFFFFFFFFFFFFFFFF) // impossible sentinel value var cont *Container for _, v := range a { if newhb := highbits(v); newhb != hb { hb = newhb cont = b.Containers.GetOrCreate(hb) } if op(cont, lowbits(v)) { a[changed] = v changed++ } } return changed }
[ "func", "(", "b", "*", "Bitmap", ")", "directOpN", "(", "op", "func", "(", "c", "*", "Container", ",", "v", "uint16", ")", "bool", ",", "a", "...", "uint64", ")", "(", "changed", "int", ")", "{", "hb", ":=", "uint64", "(", "0xFFFFFFFFFFFFFFFF", ")", "// impossible sentinel value", "\n", "var", "cont", "*", "Container", "\n", "for", "_", ",", "v", ":=", "range", "a", "{", "if", "newhb", ":=", "highbits", "(", "v", ")", ";", "newhb", "!=", "hb", "{", "hb", "=", "newhb", "\n", "cont", "=", "b", ".", "Containers", ".", "GetOrCreate", "(", "hb", ")", "\n", "}", "\n", "if", "op", "(", "cont", ",", "lowbits", "(", "v", ")", ")", "{", "a", "[", "changed", "]", "=", "v", "\n", "changed", "++", "\n", "}", "\n", "}", "\n", "return", "changed", "\n", "}" ]
// directOpN contains the logic for DirectAddN and DirectRemoveN. Theoretically, // it could be used by anything that wanted to apply a boolean-returning // container level operation across a list of values and return the number of // trues while modifying the list of values in place to contain the // true-returning values in order.
[ "directOpN", "contains", "the", "logic", "for", "DirectAddN", "and", "DirectRemoveN", ".", "Theoretically", "it", "could", "be", "used", "by", "anything", "that", "wanted", "to", "apply", "a", "boolean", "-", "returning", "container", "level", "operation", "across", "a", "list", "of", "values", "and", "return", "the", "number", "of", "trues", "while", "modifying", "the", "list", "of", "values", "in", "place", "to", "contain", "the", "true", "-", "returning", "values", "in", "order", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L233-L247
train
pilosa/pilosa
roaring/roaring.go
Contains
func (b *Bitmap) Contains(v uint64) bool { c := b.Containers.Get(highbits(v)) if c == nil { return false } return c.Contains(lowbits(v)) }
go
func (b *Bitmap) Contains(v uint64) bool { c := b.Containers.Get(highbits(v)) if c == nil { return false } return c.Contains(lowbits(v)) }
[ "func", "(", "b", "*", "Bitmap", ")", "Contains", "(", "v", "uint64", ")", "bool", "{", "c", ":=", "b", ".", "Containers", ".", "Get", "(", "highbits", "(", "v", ")", ")", "\n", "if", "c", "==", "nil", "{", "return", "false", "\n", "}", "\n", "return", "c", ".", "Contains", "(", "lowbits", "(", "v", ")", ")", "\n", "}" ]
// Contains returns true if v is in the bitmap.
[ "Contains", "returns", "true", "if", "v", "is", "in", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L257-L263
train
pilosa/pilosa
roaring/roaring.go
RemoveN
func (b *Bitmap) RemoveN(a ...uint64) (changed int, err error) { if len(a) == 0 { return 0, nil } changed = b.DirectRemoveN(a...) // modifies a in-place if b.OpWriter != nil { op := &op{ typ: opTypeRemoveBatch, values: a[:changed], } if err := b.writeOp(op); err != nil { b.DirectAddN(op.values...) // reset data since we're returning an error return 0, errors.Wrap(err, "writing to op log") } } return changed, nil }
go
func (b *Bitmap) RemoveN(a ...uint64) (changed int, err error) { if len(a) == 0 { return 0, nil } changed = b.DirectRemoveN(a...) // modifies a in-place if b.OpWriter != nil { op := &op{ typ: opTypeRemoveBatch, values: a[:changed], } if err := b.writeOp(op); err != nil { b.DirectAddN(op.values...) // reset data since we're returning an error return 0, errors.Wrap(err, "writing to op log") } } return changed, nil }
[ "func", "(", "b", "*", "Bitmap", ")", "RemoveN", "(", "a", "...", "uint64", ")", "(", "changed", "int", ",", "err", "error", ")", "{", "if", "len", "(", "a", ")", "==", "0", "{", "return", "0", ",", "nil", "\n", "}", "\n\n", "changed", "=", "b", ".", "DirectRemoveN", "(", "a", "...", ")", "// modifies a in-place", "\n\n", "if", "b", ".", "OpWriter", "!=", "nil", "{", "op", ":=", "&", "op", "{", "typ", ":", "opTypeRemoveBatch", ",", "values", ":", "a", "[", ":", "changed", "]", ",", "}", "\n", "if", "err", ":=", "b", ".", "writeOp", "(", "op", ")", ";", "err", "!=", "nil", "{", "b", ".", "DirectAddN", "(", "op", ".", "values", "...", ")", "// reset data since we're returning an error", "\n", "return", "0", ",", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "}", "\n\n", "return", "changed", ",", "nil", "\n", "}" ]
// RemoveN behaves analagously to AddN.
[ "RemoveN", "behaves", "analagously", "to", "AddN", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L288-L307
train
pilosa/pilosa
roaring/roaring.go
Max
func (b *Bitmap) Max() uint64 { if b.Containers.Size() == 0 { return 0 } hb, c := b.Containers.Last() lb := c.max() return hb<<16 | uint64(lb) }
go
func (b *Bitmap) Max() uint64 { if b.Containers.Size() == 0 { return 0 } hb, c := b.Containers.Last() lb := c.max() return hb<<16 | uint64(lb) }
[ "func", "(", "b", "*", "Bitmap", ")", "Max", "(", ")", "uint64", "{", "if", "b", ".", "Containers", ".", "Size", "(", ")", "==", "0", "{", "return", "0", "\n", "}", "\n\n", "hb", ",", "c", ":=", "b", ".", "Containers", ".", "Last", "(", ")", "\n", "lb", ":=", "c", ".", "max", "(", ")", "\n", "return", "hb", "<<", "16", "|", "uint64", "(", "lb", ")", "\n", "}" ]
// Max returns the highest value in the bitmap. // Returns zero if the bitmap is empty.
[ "Max", "returns", "the", "highest", "value", "in", "the", "bitmap", ".", "Returns", "zero", "if", "the", "bitmap", "is", "empty", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L320-L328
train
pilosa/pilosa
roaring/roaring.go
Size
func (b *Bitmap) Size() int { numbytes := 0 citer, _ := b.Containers.Iterator(0) for citer.Next() { _, c := citer.Value() numbytes += c.size() } return numbytes }
go
func (b *Bitmap) Size() int { numbytes := 0 citer, _ := b.Containers.Iterator(0) for citer.Next() { _, c := citer.Value() numbytes += c.size() } return numbytes }
[ "func", "(", "b", "*", "Bitmap", ")", "Size", "(", ")", "int", "{", "numbytes", ":=", "0", "\n", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "_", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "numbytes", "+=", "c", ".", "size", "(", ")", "\n\n", "}", "\n", "return", "numbytes", "\n", "}" ]
// Size returns the number of bytes required for the bitmap.
[ "Size", "returns", "the", "number", "of", "bytes", "required", "for", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L351-L360
train
pilosa/pilosa
roaring/roaring.go
CountRange
func (b *Bitmap) CountRange(start, end uint64) (n uint64) { if b.Containers.Size() == 0 { return } skey := highbits(start) ekey := highbits(end) citer, found := b.Containers.Iterator(highbits(start)) // If range is entirely in one container then just count that range. if found && skey == ekey { citer.Next() _, c := citer.Value() return uint64(c.countRange(int32(lowbits(start)), int32(lowbits(end)))) } for citer.Next() { k, c := citer.Value() if k < skey { // TODO remove once we've validated this stuff works panic("should be impossible for k to be less than skey") } if k == skey { n += uint64(c.countRange(int32(lowbits(start)), maxContainerVal+1)) continue } if k < ekey { n += uint64(c.n) continue } if k == ekey { n += uint64(c.countRange(0, int32(lowbits(end)))) break } if k > ekey { break } } return n }
go
func (b *Bitmap) CountRange(start, end uint64) (n uint64) { if b.Containers.Size() == 0 { return } skey := highbits(start) ekey := highbits(end) citer, found := b.Containers.Iterator(highbits(start)) // If range is entirely in one container then just count that range. if found && skey == ekey { citer.Next() _, c := citer.Value() return uint64(c.countRange(int32(lowbits(start)), int32(lowbits(end)))) } for citer.Next() { k, c := citer.Value() if k < skey { // TODO remove once we've validated this stuff works panic("should be impossible for k to be less than skey") } if k == skey { n += uint64(c.countRange(int32(lowbits(start)), maxContainerVal+1)) continue } if k < ekey { n += uint64(c.n) continue } if k == ekey { n += uint64(c.countRange(0, int32(lowbits(end)))) break } if k > ekey { break } } return n }
[ "func", "(", "b", "*", "Bitmap", ")", "CountRange", "(", "start", ",", "end", "uint64", ")", "(", "n", "uint64", ")", "{", "if", "b", ".", "Containers", ".", "Size", "(", ")", "==", "0", "{", "return", "\n", "}", "\n\n", "skey", ":=", "highbits", "(", "start", ")", "\n", "ekey", ":=", "highbits", "(", "end", ")", "\n\n", "citer", ",", "found", ":=", "b", ".", "Containers", ".", "Iterator", "(", "highbits", "(", "start", ")", ")", "\n", "// If range is entirely in one container then just count that range.", "if", "found", "&&", "skey", "==", "ekey", "{", "citer", ".", "Next", "(", ")", "\n", "_", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "return", "uint64", "(", "c", ".", "countRange", "(", "int32", "(", "lowbits", "(", "start", ")", ")", ",", "int32", "(", "lowbits", "(", "end", ")", ")", ")", ")", "\n", "}", "\n\n", "for", "citer", ".", "Next", "(", ")", "{", "k", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "if", "k", "<", "skey", "{", "// TODO remove once we've validated this stuff works", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "if", "k", "==", "skey", "{", "n", "+=", "uint64", "(", "c", ".", "countRange", "(", "int32", "(", "lowbits", "(", "start", ")", ")", ",", "maxContainerVal", "+", "1", ")", ")", "\n", "continue", "\n", "}", "\n", "if", "k", "<", "ekey", "{", "n", "+=", "uint64", "(", "c", ".", "n", ")", "\n", "continue", "\n", "}", "\n", "if", "k", "==", "ekey", "{", "n", "+=", "uint64", "(", "c", ".", "countRange", "(", "0", ",", "int32", "(", "lowbits", "(", "end", ")", ")", ")", ")", "\n", "break", "\n", "}", "\n", "if", "k", ">", "ekey", "{", "break", "\n", "}", "\n", "}", "\n", "return", "n", "\n", "}" ]
// CountRange returns the number of bits set between [start, end).
[ "CountRange", "returns", "the", "number", "of", "bits", "set", "between", "[", "start", "end", ")", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L363-L402
train
pilosa/pilosa
roaring/roaring.go
Slice
func (b *Bitmap) Slice() []uint64 { var a []uint64 itr := b.Iterator() itr.Seek(0) for v, eof := itr.Next(); !eof; v, eof = itr.Next() { a = append(a, v) } return a }
go
func (b *Bitmap) Slice() []uint64 { var a []uint64 itr := b.Iterator() itr.Seek(0) for v, eof := itr.Next(); !eof; v, eof = itr.Next() { a = append(a, v) } return a }
[ "func", "(", "b", "*", "Bitmap", ")", "Slice", "(", ")", "[", "]", "uint64", "{", "var", "a", "[", "]", "uint64", "\n", "itr", ":=", "b", ".", "Iterator", "(", ")", "\n", "itr", ".", "Seek", "(", "0", ")", "\n\n", "for", "v", ",", "eof", ":=", "itr", ".", "Next", "(", ")", ";", "!", "eof", ";", "v", ",", "eof", "=", "itr", ".", "Next", "(", ")", "{", "a", "=", "append", "(", "a", ",", "v", ")", "\n", "}", "\n", "return", "a", "\n", "}" ]
// Slice returns a slice of all integers in the bitmap.
[ "Slice", "returns", "a", "slice", "of", "all", "integers", "in", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L405-L414
train
pilosa/pilosa
roaring/roaring.go
SliceRange
func (b *Bitmap) SliceRange(start, end uint64) []uint64 { var a []uint64 itr := b.Iterator() itr.Seek(start) for v, eof := itr.Next(); !eof && v < end; v, eof = itr.Next() { a = append(a, v) } return a }
go
func (b *Bitmap) SliceRange(start, end uint64) []uint64 { var a []uint64 itr := b.Iterator() itr.Seek(start) for v, eof := itr.Next(); !eof && v < end; v, eof = itr.Next() { a = append(a, v) } return a }
[ "func", "(", "b", "*", "Bitmap", ")", "SliceRange", "(", "start", ",", "end", "uint64", ")", "[", "]", "uint64", "{", "var", "a", "[", "]", "uint64", "\n", "itr", ":=", "b", ".", "Iterator", "(", ")", "\n", "itr", ".", "Seek", "(", "start", ")", "\n", "for", "v", ",", "eof", ":=", "itr", ".", "Next", "(", ")", ";", "!", "eof", "&&", "v", "<", "end", ";", "v", ",", "eof", "=", "itr", ".", "Next", "(", ")", "{", "a", "=", "append", "(", "a", ",", "v", ")", "\n", "}", "\n", "return", "a", "\n", "}" ]
// SliceRange returns a slice of integers between [start, end).
[ "SliceRange", "returns", "a", "slice", "of", "integers", "between", "[", "start", "end", ")", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L417-L425
train
pilosa/pilosa
roaring/roaring.go
ForEach
func (b *Bitmap) ForEach(fn func(uint64)) { itr := b.Iterator() itr.Seek(0) for v, eof := itr.Next(); !eof; v, eof = itr.Next() { fn(v) } }
go
func (b *Bitmap) ForEach(fn func(uint64)) { itr := b.Iterator() itr.Seek(0) for v, eof := itr.Next(); !eof; v, eof = itr.Next() { fn(v) } }
[ "func", "(", "b", "*", "Bitmap", ")", "ForEach", "(", "fn", "func", "(", "uint64", ")", ")", "{", "itr", ":=", "b", ".", "Iterator", "(", ")", "\n", "itr", ".", "Seek", "(", "0", ")", "\n", "for", "v", ",", "eof", ":=", "itr", ".", "Next", "(", ")", ";", "!", "eof", ";", "v", ",", "eof", "=", "itr", ".", "Next", "(", ")", "{", "fn", "(", "v", ")", "\n", "}", "\n", "}" ]
// ForEach executes fn for each value in the bitmap.
[ "ForEach", "executes", "fn", "for", "each", "value", "in", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L428-L434
train
pilosa/pilosa
roaring/roaring.go
ForEachRange
func (b *Bitmap) ForEachRange(start, end uint64, fn func(uint64)) { itr := b.Iterator() itr.Seek(start) for v, eof := itr.Next(); !eof && v < end; v, eof = itr.Next() { fn(v) } }
go
func (b *Bitmap) ForEachRange(start, end uint64, fn func(uint64)) { itr := b.Iterator() itr.Seek(start) for v, eof := itr.Next(); !eof && v < end; v, eof = itr.Next() { fn(v) } }
[ "func", "(", "b", "*", "Bitmap", ")", "ForEachRange", "(", "start", ",", "end", "uint64", ",", "fn", "func", "(", "uint64", ")", ")", "{", "itr", ":=", "b", ".", "Iterator", "(", ")", "\n", "itr", ".", "Seek", "(", "start", ")", "\n", "for", "v", ",", "eof", ":=", "itr", ".", "Next", "(", ")", ";", "!", "eof", "&&", "v", "<", "end", ";", "v", ",", "eof", "=", "itr", ".", "Next", "(", ")", "{", "fn", "(", "v", ")", "\n", "}", "\n", "}" ]
// ForEachRange executes fn for each value in the bitmap between [start, end).
[ "ForEachRange", "executes", "fn", "for", "each", "value", "in", "the", "bitmap", "between", "[", "start", "end", ")", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L437-L443
train
pilosa/pilosa
roaring/roaring.go
OffsetRange
func (b *Bitmap) OffsetRange(offset, start, end uint64) *Bitmap { if lowbits(offset) != 0 { panic("offset must not contain low bits") } if lowbits(start) != 0 { panic("range start must not contain low bits") } if lowbits(end) != 0 { panic("range end must not contain low bits") } off := highbits(offset) hi0, hi1 := highbits(start), highbits(end) citer, _ := b.Containers.Iterator(hi0) other := NewBitmap() for citer.Next() { k, c := citer.Value() if k >= hi1 { break } other.Containers.Put(off+(k-hi0), c) } return other }
go
func (b *Bitmap) OffsetRange(offset, start, end uint64) *Bitmap { if lowbits(offset) != 0 { panic("offset must not contain low bits") } if lowbits(start) != 0 { panic("range start must not contain low bits") } if lowbits(end) != 0 { panic("range end must not contain low bits") } off := highbits(offset) hi0, hi1 := highbits(start), highbits(end) citer, _ := b.Containers.Iterator(hi0) other := NewBitmap() for citer.Next() { k, c := citer.Value() if k >= hi1 { break } other.Containers.Put(off+(k-hi0), c) } return other }
[ "func", "(", "b", "*", "Bitmap", ")", "OffsetRange", "(", "offset", ",", "start", ",", "end", "uint64", ")", "*", "Bitmap", "{", "if", "lowbits", "(", "offset", ")", "!=", "0", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "if", "lowbits", "(", "start", ")", "!=", "0", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "if", "lowbits", "(", "end", ")", "!=", "0", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n\n", "off", ":=", "highbits", "(", "offset", ")", "\n", "hi0", ",", "hi1", ":=", "highbits", "(", "start", ")", ",", "highbits", "(", "end", ")", "\n", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "hi0", ")", "\n", "other", ":=", "NewBitmap", "(", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "k", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "if", "k", ">=", "hi1", "{", "break", "\n", "}", "\n", "other", ".", "Containers", ".", "Put", "(", "off", "+", "(", "k", "-", "hi0", ")", ",", "c", ")", "\n", "}", "\n", "return", "other", "\n", "}" ]
// OffsetRange returns a new bitmap with a containers offset by start.
[ "OffsetRange", "returns", "a", "new", "bitmap", "with", "a", "containers", "offset", "by", "start", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L446-L469
train
pilosa/pilosa
roaring/roaring.go
container
func (b *Bitmap) container(key uint64) *Container { return b.Containers.Get(key) }
go
func (b *Bitmap) container(key uint64) *Container { return b.Containers.Get(key) }
[ "func", "(", "b", "*", "Bitmap", ")", "container", "(", "key", "uint64", ")", "*", "Container", "{", "return", "b", ".", "Containers", ".", "Get", "(", "key", ")", "\n", "}" ]
// container returns the container with the given key.
[ "container", "returns", "the", "container", "with", "the", "given", "key", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L472-L474
train
pilosa/pilosa
roaring/roaring.go
IntersectionCount
func (b *Bitmap) IntersectionCount(other *Bitmap) uint64 { var n uint64 iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i && j { if ki < kj { i = iiter.Next() ki, ci = iiter.Value() } else if ki > kj { j = jiter.Next() kj, cj = jiter.Value() } else { n += uint64(intersectionCount(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return n }
go
func (b *Bitmap) IntersectionCount(other *Bitmap) uint64 { var n uint64 iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i && j { if ki < kj { i = iiter.Next() ki, ci = iiter.Value() } else if ki > kj { j = jiter.Next() kj, cj = jiter.Value() } else { n += uint64(intersectionCount(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return n }
[ "func", "(", "b", "*", "Bitmap", ")", "IntersectionCount", "(", "other", "*", "Bitmap", ")", "uint64", "{", "var", "n", "uint64", "\n", "iiter", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "jiter", ",", "_", ":=", "other", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "i", ",", "j", ":=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", ":=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", ":=", "jiter", ".", "Value", "(", ")", "\n", "for", "i", "&&", "j", "{", "if", "ki", "<", "kj", "{", "i", "=", "iiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "}", "else", "if", "ki", ">", "kj", "{", "j", "=", "jiter", ".", "Next", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "else", "{", "n", "+=", "uint64", "(", "intersectionCount", "(", "ci", ",", "cj", ")", ")", "\n", "i", ",", "j", "=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "\n", "}", "\n", "return", "n", "\n", "}" ]
// IntersectionCount returns the number of set bits that would result in an // intersection between b and other. It is more efficient than actually // intersecting the two and counting the result.
[ "IntersectionCount", "returns", "the", "number", "of", "set", "bits", "that", "would", "result", "in", "an", "intersection", "between", "b", "and", "other", ".", "It", "is", "more", "efficient", "than", "actually", "intersecting", "the", "two", "and", "counting", "the", "result", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L479-L501
train
pilosa/pilosa
roaring/roaring.go
Intersect
func (b *Bitmap) Intersect(other *Bitmap) *Bitmap { output := NewBitmap() iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i && j { if ki < kj { i = iiter.Next() ki, ci = iiter.Value() } else if ki > kj { j = jiter.Next() kj, cj = jiter.Value() } else { // ki == kj output.Containers.Put(ki, intersect(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return output }
go
func (b *Bitmap) Intersect(other *Bitmap) *Bitmap { output := NewBitmap() iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i && j { if ki < kj { i = iiter.Next() ki, ci = iiter.Value() } else if ki > kj { j = jiter.Next() kj, cj = jiter.Value() } else { // ki == kj output.Containers.Put(ki, intersect(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return output }
[ "func", "(", "b", "*", "Bitmap", ")", "Intersect", "(", "other", "*", "Bitmap", ")", "*", "Bitmap", "{", "output", ":=", "NewBitmap", "(", ")", "\n", "iiter", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "jiter", ",", "_", ":=", "other", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "i", ",", "j", ":=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", ":=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", ":=", "jiter", ".", "Value", "(", ")", "\n", "for", "i", "&&", "j", "{", "if", "ki", "<", "kj", "{", "i", "=", "iiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "}", "else", "if", "ki", ">", "kj", "{", "j", "=", "jiter", ".", "Next", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "else", "{", "// ki == kj", "output", ".", "Containers", ".", "Put", "(", "ki", ",", "intersect", "(", "ci", ",", "cj", ")", ")", "\n", "i", ",", "j", "=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "\n", "}", "\n", "return", "output", "\n", "}" ]
// Intersect returns the intersection of b and other.
[ "Intersect", "returns", "the", "intersection", "of", "b", "and", "other", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L504-L526
train
pilosa/pilosa
roaring/roaring.go
Union
func (b *Bitmap) Union(others ...*Bitmap) *Bitmap { if len(others) == 1 { output := NewBitmap() b.unionIntoTargetSingle(output, others[0]) return output } output := b.Clone() output.UnionInPlace(others...) return output }
go
func (b *Bitmap) Union(others ...*Bitmap) *Bitmap { if len(others) == 1 { output := NewBitmap() b.unionIntoTargetSingle(output, others[0]) return output } output := b.Clone() output.UnionInPlace(others...) return output }
[ "func", "(", "b", "*", "Bitmap", ")", "Union", "(", "others", "...", "*", "Bitmap", ")", "*", "Bitmap", "{", "if", "len", "(", "others", ")", "==", "1", "{", "output", ":=", "NewBitmap", "(", ")", "\n", "b", ".", "unionIntoTargetSingle", "(", "output", ",", "others", "[", "0", "]", ")", "\n", "return", "output", "\n", "}", "\n", "output", ":=", "b", ".", "Clone", "(", ")", "\n", "output", ".", "UnionInPlace", "(", "others", "...", ")", "\n", "return", "output", "\n", "}" ]
// Union returns the bitwise union of b and others as a new bitmap.
[ "Union", "returns", "the", "bitwise", "union", "of", "b", "and", "others", "as", "a", "new", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L529-L538
train
pilosa/pilosa
roaring/roaring.go
Difference
func (b *Bitmap) Difference(other *Bitmap) *Bitmap { output := NewBitmap() iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i || j { if i && (!j || ki < kj) { output.Containers.Put(ki, ci.Clone()) i = iiter.Next() ki, ci = iiter.Value() } else if j && (!i || ki > kj) { j = jiter.Next() kj, cj = jiter.Value() } else { // ki == kj output.Containers.Put(ki, difference(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return output }
go
func (b *Bitmap) Difference(other *Bitmap) *Bitmap { output := NewBitmap() iiter, _ := b.Containers.Iterator(0) jiter, _ := other.Containers.Iterator(0) i, j := iiter.Next(), jiter.Next() ki, ci := iiter.Value() kj, cj := jiter.Value() for i || j { if i && (!j || ki < kj) { output.Containers.Put(ki, ci.Clone()) i = iiter.Next() ki, ci = iiter.Value() } else if j && (!i || ki > kj) { j = jiter.Next() kj, cj = jiter.Value() } else { // ki == kj output.Containers.Put(ki, difference(ci, cj)) i, j = iiter.Next(), jiter.Next() ki, ci = iiter.Value() kj, cj = jiter.Value() } } return output }
[ "func", "(", "b", "*", "Bitmap", ")", "Difference", "(", "other", "*", "Bitmap", ")", "*", "Bitmap", "{", "output", ":=", "NewBitmap", "(", ")", "\n\n", "iiter", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "jiter", ",", "_", ":=", "other", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "i", ",", "j", ":=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", ":=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", ":=", "jiter", ".", "Value", "(", ")", "\n", "for", "i", "||", "j", "{", "if", "i", "&&", "(", "!", "j", "||", "ki", "<", "kj", ")", "{", "output", ".", "Containers", ".", "Put", "(", "ki", ",", "ci", ".", "Clone", "(", ")", ")", "\n", "i", "=", "iiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "}", "else", "if", "j", "&&", "(", "!", "i", "||", "ki", ">", "kj", ")", "{", "j", "=", "jiter", ".", "Next", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "else", "{", "// ki == kj", "output", ".", "Containers", ".", "Put", "(", "ki", ",", "difference", "(", "ci", ",", "cj", ")", ")", "\n", "i", ",", "j", "=", "iiter", ".", "Next", "(", ")", ",", "jiter", ".", "Next", "(", ")", "\n", "ki", ",", "ci", "=", "iiter", ".", "Value", "(", ")", "\n", "kj", ",", "cj", "=", "jiter", ".", "Value", "(", ")", "\n", "}", "\n", "}", "\n", "return", "output", "\n", "}" ]
// Difference returns the difference of b and other.
[ "Difference", "returns", "the", "difference", "of", "b", "and", "other", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L794-L818
train
pilosa/pilosa
roaring/roaring.go
Shift
func (b *Bitmap) Shift(n int) (*Bitmap, error) { if n != 1 { return nil, errors.New("cannot shift by a value other than 1") } output := NewBitmap() iiter, _ := b.Containers.Iterator(0) lastCarry := false lastKey := uint64(0) for iiter.Next() { ki, ci := iiter.Value() o, carry := shift(ci) if lastCarry { o.add(0) } if o.n > 0 { output.Containers.Put(ki, o) } lastCarry = carry lastKey = ki } // As long as the carry wasn't from the max container, // append a new container and add the carried bit. if lastCarry && lastKey != maxContainerKey { extra := NewContainerArray([]uint16{0}) output.Containers.Put(lastKey+1, extra) } return output, nil }
go
func (b *Bitmap) Shift(n int) (*Bitmap, error) { if n != 1 { return nil, errors.New("cannot shift by a value other than 1") } output := NewBitmap() iiter, _ := b.Containers.Iterator(0) lastCarry := false lastKey := uint64(0) for iiter.Next() { ki, ci := iiter.Value() o, carry := shift(ci) if lastCarry { o.add(0) } if o.n > 0 { output.Containers.Put(ki, o) } lastCarry = carry lastKey = ki } // As long as the carry wasn't from the max container, // append a new container and add the carried bit. if lastCarry && lastKey != maxContainerKey { extra := NewContainerArray([]uint16{0}) output.Containers.Put(lastKey+1, extra) } return output, nil }
[ "func", "(", "b", "*", "Bitmap", ")", "Shift", "(", "n", "int", ")", "(", "*", "Bitmap", ",", "error", ")", "{", "if", "n", "!=", "1", "{", "return", "nil", ",", "errors", ".", "New", "(", "\"", "\"", ")", "\n", "}", "\n", "output", ":=", "NewBitmap", "(", ")", "\n", "iiter", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "lastCarry", ":=", "false", "\n", "lastKey", ":=", "uint64", "(", "0", ")", "\n", "for", "iiter", ".", "Next", "(", ")", "{", "ki", ",", "ci", ":=", "iiter", ".", "Value", "(", ")", "\n", "o", ",", "carry", ":=", "shift", "(", "ci", ")", "\n", "if", "lastCarry", "{", "o", ".", "add", "(", "0", ")", "\n", "}", "\n", "if", "o", ".", "n", ">", "0", "{", "output", ".", "Containers", ".", "Put", "(", "ki", ",", "o", ")", "\n", "}", "\n", "lastCarry", "=", "carry", "\n", "lastKey", "=", "ki", "\n", "}", "\n", "// As long as the carry wasn't from the max container,", "// append a new container and add the carried bit.", "if", "lastCarry", "&&", "lastKey", "!=", "maxContainerKey", "{", "extra", ":=", "NewContainerArray", "(", "[", "]", "uint16", "{", "0", "}", ")", "\n", "output", ".", "Containers", ".", "Put", "(", "lastKey", "+", "1", ",", "extra", ")", "\n", "}", "\n\n", "return", "output", ",", "nil", "\n", "}" ]
// Shift shifts the contents of b by 1.
[ "Shift", "shifts", "the", "contents", "of", "b", "by", "1", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L849-L877
train
pilosa/pilosa
roaring/roaring.go
removeEmptyContainers
func (b *Bitmap) removeEmptyContainers() { citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() if c.n == 0 { b.Containers.Remove(k) } } }
go
func (b *Bitmap) removeEmptyContainers() { citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() if c.n == 0 { b.Containers.Remove(k) } } }
[ "func", "(", "b", "*", "Bitmap", ")", "removeEmptyContainers", "(", ")", "{", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "k", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "if", "c", ".", "n", "==", "0", "{", "b", ".", "Containers", ".", "Remove", "(", "k", ")", "\n", "}", "\n", "}", "\n", "}" ]
// removeEmptyContainers deletes all containers that have a count of zero.
[ "removeEmptyContainers", "deletes", "all", "containers", "that", "have", "a", "count", "of", "zero", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L880-L888
train
pilosa/pilosa
roaring/roaring.go
Optimize
func (b *Bitmap) Optimize() { citer, _ := b.Containers.Iterator(0) for citer.Next() { _, c := citer.Value() c.optimize() } }
go
func (b *Bitmap) Optimize() { citer, _ := b.Containers.Iterator(0) for citer.Next() { _, c := citer.Value() c.optimize() } }
[ "func", "(", "b", "*", "Bitmap", ")", "Optimize", "(", ")", "{", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "_", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "c", ".", "optimize", "(", ")", "\n", "}", "\n", "}" ]
// Optimize converts array and bitmap containers to run containers as necessary.
[ "Optimize", "converts", "array", "and", "bitmap", "containers", "to", "run", "containers", "as", "necessary", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L902-L908
train
pilosa/pilosa
roaring/roaring.go
WriteTo
func (b *Bitmap) WriteTo(w io.Writer) (n int64, err error) { b.Optimize() return b.writeToUnoptimized(w) }
go
func (b *Bitmap) WriteTo(w io.Writer) (n int64, err error) { b.Optimize() return b.writeToUnoptimized(w) }
[ "func", "(", "b", "*", "Bitmap", ")", "WriteTo", "(", "w", "io", ".", "Writer", ")", "(", "n", "int64", ",", "err", "error", ")", "{", "b", ".", "Optimize", "(", ")", "\n", "return", "b", ".", "writeToUnoptimized", "(", "w", ")", "\n", "}" ]
// WriteTo writes b to w.
[ "WriteTo", "writes", "b", "to", "w", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L946-L949
train
pilosa/pilosa
roaring/roaring.go
writeOp
func (b *Bitmap) writeOp(op *op) error { if b.OpWriter == nil { return nil } if _, err := op.WriteTo(b.OpWriter); err != nil { return err } b.opN += op.count() return nil }
go
func (b *Bitmap) writeOp(op *op) error { if b.OpWriter == nil { return nil } if _, err := op.WriteTo(b.OpWriter); err != nil { return err } b.opN += op.count() return nil }
[ "func", "(", "b", "*", "Bitmap", ")", "writeOp", "(", "op", "*", "op", ")", "error", "{", "if", "b", ".", "OpWriter", "==", "nil", "{", "return", "nil", "\n", "}", "\n\n", "if", "_", ",", "err", ":=", "op", ".", "WriteTo", "(", "b", ".", "OpWriter", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "b", ".", "opN", "+=", "op", ".", "count", "(", ")", "\n\n", "return", "nil", "\n", "}" ]
// writeOp writes op to the OpWriter, if available.
[ "writeOp", "writes", "op", "to", "the", "OpWriter", "if", "available", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1113-L1124
train
pilosa/pilosa
roaring/roaring.go
Iterator
func (b *Bitmap) Iterator() *Iterator { itr := &Iterator{bitmap: b} itr.Seek(0) return itr }
go
func (b *Bitmap) Iterator() *Iterator { itr := &Iterator{bitmap: b} itr.Seek(0) return itr }
[ "func", "(", "b", "*", "Bitmap", ")", "Iterator", "(", ")", "*", "Iterator", "{", "itr", ":=", "&", "Iterator", "{", "bitmap", ":", "b", "}", "\n", "itr", ".", "Seek", "(", "0", ")", "\n", "return", "itr", "\n", "}" ]
// Iterator returns a new iterator for the bitmap.
[ "Iterator", "returns", "a", "new", "iterator", "for", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1127-L1131
train
pilosa/pilosa
roaring/roaring.go
Info
func (b *Bitmap) Info() bitmapInfo { info := bitmapInfo{ OpN: b.opN, Containers: make([]containerInfo, 0, b.Containers.Size()), } citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() ci := c.info() ci.Key = k info.Containers = append(info.Containers, ci) } return info }
go
func (b *Bitmap) Info() bitmapInfo { info := bitmapInfo{ OpN: b.opN, Containers: make([]containerInfo, 0, b.Containers.Size()), } citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() ci := c.info() ci.Key = k info.Containers = append(info.Containers, ci) } return info }
[ "func", "(", "b", "*", "Bitmap", ")", "Info", "(", ")", "bitmapInfo", "{", "info", ":=", "bitmapInfo", "{", "OpN", ":", "b", ".", "opN", ",", "Containers", ":", "make", "(", "[", "]", "containerInfo", ",", "0", ",", "b", ".", "Containers", ".", "Size", "(", ")", ")", ",", "}", "\n\n", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "k", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "ci", ":=", "c", ".", "info", "(", ")", "\n", "ci", ".", "Key", "=", "k", "\n", "info", ".", "Containers", "=", "append", "(", "info", ".", "Containers", ",", "ci", ")", "\n", "}", "\n", "return", "info", "\n", "}" ]
// Info returns stats for the bitmap.
[ "Info", "returns", "stats", "for", "the", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1134-L1148
train
pilosa/pilosa
roaring/roaring.go
Check
func (b *Bitmap) Check() error { var a ErrorList // Check each container. citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() if err := c.check(); err != nil { a.AppendWithPrefix(err, fmt.Sprintf("%d/", k)) } } if len(a) == 0 { return nil } return a }
go
func (b *Bitmap) Check() error { var a ErrorList // Check each container. citer, _ := b.Containers.Iterator(0) for citer.Next() { k, c := citer.Value() if err := c.check(); err != nil { a.AppendWithPrefix(err, fmt.Sprintf("%d/", k)) } } if len(a) == 0 { return nil } return a }
[ "func", "(", "b", "*", "Bitmap", ")", "Check", "(", ")", "error", "{", "var", "a", "ErrorList", "\n\n", "// Check each container.", "citer", ",", "_", ":=", "b", ".", "Containers", ".", "Iterator", "(", "0", ")", "\n", "for", "citer", ".", "Next", "(", ")", "{", "k", ",", "c", ":=", "citer", ".", "Value", "(", ")", "\n", "if", "err", ":=", "c", ".", "check", "(", ")", ";", "err", "!=", "nil", "{", "a", ".", "AppendWithPrefix", "(", "err", ",", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "k", ")", ")", "\n", "}", "\n", "}", "\n\n", "if", "len", "(", "a", ")", "==", "0", "{", "return", "nil", "\n", "}", "\n", "return", "a", "\n", "}" ]
// Check performs a consistency check on the bitmap. Returns nil if consistent.
[ "Check", "performs", "a", "consistency", "check", "on", "the", "bitmap", ".", "Returns", "nil", "if", "consistent", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1151-L1167
train
pilosa/pilosa
roaring/roaring.go
Seek
func (itr *Iterator) Seek(seek uint64) { // k should always be -1 unless we're seeking into a run container. Then the // "if c.isRun" section will take care of it. itr.k = -1 // Move to the correct container. itr.citer, _ = itr.bitmap.Containers.Iterator(highbits(seek)) if !itr.citer.Next() { itr.c = nil return // eof } itr.key, itr.c = itr.citer.Value() // Move to the correct value index inside the container. lb := lowbits(seek) if itr.c.isArray() { // Find index in the container. itr.j = search32(itr.c.array(), lb) if itr.j < 0 { itr.j = -itr.j - 1 } if itr.j < int32(len(itr.c.array())) { itr.j-- return } // If it's at the end of the container then move to the next one. if !itr.citer.Next() { itr.c = nil return } itr.key, itr.c = itr.citer.Value() itr.j = -1 return } if itr.c.isRun() { if seek == 0 { itr.j, itr.k = 0, -1 } j, contains := binSearchRuns(lb, itr.c.runs()) if contains { itr.j = j itr.k = int32(lb) - int32(itr.c.runs()[j].start) - 1 } else { // Set iterator to next value in the Bitmap. itr.j = j itr.k = -1 } return } // If it's a bitmap container then move to index before the value and call next(). itr.j = int32(lb) - 1 }
go
func (itr *Iterator) Seek(seek uint64) { // k should always be -1 unless we're seeking into a run container. Then the // "if c.isRun" section will take care of it. itr.k = -1 // Move to the correct container. itr.citer, _ = itr.bitmap.Containers.Iterator(highbits(seek)) if !itr.citer.Next() { itr.c = nil return // eof } itr.key, itr.c = itr.citer.Value() // Move to the correct value index inside the container. lb := lowbits(seek) if itr.c.isArray() { // Find index in the container. itr.j = search32(itr.c.array(), lb) if itr.j < 0 { itr.j = -itr.j - 1 } if itr.j < int32(len(itr.c.array())) { itr.j-- return } // If it's at the end of the container then move to the next one. if !itr.citer.Next() { itr.c = nil return } itr.key, itr.c = itr.citer.Value() itr.j = -1 return } if itr.c.isRun() { if seek == 0 { itr.j, itr.k = 0, -1 } j, contains := binSearchRuns(lb, itr.c.runs()) if contains { itr.j = j itr.k = int32(lb) - int32(itr.c.runs()[j].start) - 1 } else { // Set iterator to next value in the Bitmap. itr.j = j itr.k = -1 } return } // If it's a bitmap container then move to index before the value and call next(). itr.j = int32(lb) - 1 }
[ "func", "(", "itr", "*", "Iterator", ")", "Seek", "(", "seek", "uint64", ")", "{", "// k should always be -1 unless we're seeking into a run container. Then the", "// \"if c.isRun\" section will take care of it.", "itr", ".", "k", "=", "-", "1", "\n\n", "// Move to the correct container.", "itr", ".", "citer", ",", "_", "=", "itr", ".", "bitmap", ".", "Containers", ".", "Iterator", "(", "highbits", "(", "seek", ")", ")", "\n", "if", "!", "itr", ".", "citer", ".", "Next", "(", ")", "{", "itr", ".", "c", "=", "nil", "\n", "return", "// eof", "\n", "}", "\n", "itr", ".", "key", ",", "itr", ".", "c", "=", "itr", ".", "citer", ".", "Value", "(", ")", "\n\n", "// Move to the correct value index inside the container.", "lb", ":=", "lowbits", "(", "seek", ")", "\n", "if", "itr", ".", "c", ".", "isArray", "(", ")", "{", "// Find index in the container.", "itr", ".", "j", "=", "search32", "(", "itr", ".", "c", ".", "array", "(", ")", ",", "lb", ")", "\n", "if", "itr", ".", "j", "<", "0", "{", "itr", ".", "j", "=", "-", "itr", ".", "j", "-", "1", "\n", "}", "\n", "if", "itr", ".", "j", "<", "int32", "(", "len", "(", "itr", ".", "c", ".", "array", "(", ")", ")", ")", "{", "itr", ".", "j", "--", "\n", "return", "\n", "}", "\n\n", "// If it's at the end of the container then move to the next one.", "if", "!", "itr", ".", "citer", ".", "Next", "(", ")", "{", "itr", ".", "c", "=", "nil", "\n", "return", "\n", "}", "\n", "itr", ".", "key", ",", "itr", ".", "c", "=", "itr", ".", "citer", ".", "Value", "(", ")", "\n", "itr", ".", "j", "=", "-", "1", "\n", "return", "\n", "}", "\n\n", "if", "itr", ".", "c", ".", "isRun", "(", ")", "{", "if", "seek", "==", "0", "{", "itr", ".", "j", ",", "itr", ".", "k", "=", "0", ",", "-", "1", "\n", "}", "\n\n", "j", ",", "contains", ":=", "binSearchRuns", "(", "lb", ",", "itr", ".", "c", ".", "runs", "(", ")", ")", "\n", "if", "contains", "{", "itr", ".", "j", "=", "j", "\n", "itr", ".", "k", "=", "int32", "(", "lb", ")", "-", "int32", "(", "itr", ".", "c", ".", "runs", "(", ")", "[", "j", "]", ".", "start", ")", "-", "1", "\n", "}", "else", "{", "// Set iterator to next value in the Bitmap.", "itr", ".", "j", "=", "j", "\n", "itr", ".", "k", "=", "-", "1", "\n", "}", "\n\n", "return", "\n", "}", "\n\n", "// If it's a bitmap container then move to index before the value and call next().", "itr", ".", "j", "=", "int32", "(", "lb", ")", "-", "1", "\n", "}" ]
// Seek moves to the first value equal to or greater than `seek`.
[ "Seek", "moves", "to", "the", "first", "value", "equal", "to", "or", "greater", "than", "seek", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1213-L1269
train
pilosa/pilosa
roaring/roaring.go
peek
func (itr *Iterator) peek() uint64 { if itr.c == nil { return 0 } if itr.c.isArray() { return itr.key<<16 | uint64(itr.c.array()[itr.j]) } if itr.c.isRun() { return itr.key<<16 | uint64(itr.c.runs()[itr.j].start+uint16(itr.k)) } return itr.key<<16 | uint64(itr.j) }
go
func (itr *Iterator) peek() uint64 { if itr.c == nil { return 0 } if itr.c.isArray() { return itr.key<<16 | uint64(itr.c.array()[itr.j]) } if itr.c.isRun() { return itr.key<<16 | uint64(itr.c.runs()[itr.j].start+uint16(itr.k)) } return itr.key<<16 | uint64(itr.j) }
[ "func", "(", "itr", "*", "Iterator", ")", "peek", "(", ")", "uint64", "{", "if", "itr", ".", "c", "==", "nil", "{", "return", "0", "\n", "}", "\n", "if", "itr", ".", "c", ".", "isArray", "(", ")", "{", "return", "itr", ".", "key", "<<", "16", "|", "uint64", "(", "itr", ".", "c", ".", "array", "(", ")", "[", "itr", ".", "j", "]", ")", "\n", "}", "\n", "if", "itr", ".", "c", ".", "isRun", "(", ")", "{", "return", "itr", ".", "key", "<<", "16", "|", "uint64", "(", "itr", ".", "c", ".", "runs", "(", ")", "[", "itr", ".", "j", "]", ".", "start", "+", "uint16", "(", "itr", ".", "k", ")", ")", "\n", "}", "\n", "return", "itr", ".", "key", "<<", "16", "|", "uint64", "(", "itr", ".", "j", ")", "\n", "}" ]
// peek returns the current value.
[ "peek", "returns", "the", "current", "value", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1380-L1391
train
pilosa/pilosa
roaring/roaring.go
countRange
func (c *Container) countRange(start, end int32) (n int32) { if c.isArray() { return c.arrayCountRange(start, end) } else if c.isRun() { return c.runCountRange(start, end) } return c.bitmapCountRange(start, end) }
go
func (c *Container) countRange(start, end int32) (n int32) { if c.isArray() { return c.arrayCountRange(start, end) } else if c.isRun() { return c.runCountRange(start, end) } return c.bitmapCountRange(start, end) }
[ "func", "(", "c", "*", "Container", ")", "countRange", "(", "start", ",", "end", "int32", ")", "(", "n", "int32", ")", "{", "if", "c", ".", "isArray", "(", ")", "{", "return", "c", ".", "arrayCountRange", "(", "start", ",", "end", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "return", "c", ".", "runCountRange", "(", "start", ",", "end", ")", "\n", "}", "\n", "return", "c", ".", "bitmapCountRange", "(", "start", ",", "end", ")", "\n", "}" ]
// countRange counts the number of bits set between [start, end).
[ "countRange", "counts", "the", "number", "of", "bits", "set", "between", "[", "start", "end", ")", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1415-L1422
train
pilosa/pilosa
roaring/roaring.go
add
func (c *Container) add(v uint16) (added bool) { if c.isArray() { added = c.arrayAdd(v) } else if c.isRun() { added = c.runAdd(v) } else { added = c.bitmapAdd(v) } if added { c.n++ } return added }
go
func (c *Container) add(v uint16) (added bool) { if c.isArray() { added = c.arrayAdd(v) } else if c.isRun() { added = c.runAdd(v) } else { added = c.bitmapAdd(v) } if added { c.n++ } return added }
[ "func", "(", "c", "*", "Container", ")", "add", "(", "v", "uint16", ")", "(", "added", "bool", ")", "{", "if", "c", ".", "isArray", "(", ")", "{", "added", "=", "c", ".", "arrayAdd", "(", "v", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "added", "=", "c", ".", "runAdd", "(", "v", ")", "\n", "}", "else", "{", "added", "=", "c", ".", "bitmapAdd", "(", "v", ")", "\n", "}", "\n", "if", "added", "{", "c", ".", "n", "++", "\n", "}", "\n", "return", "added", "\n", "}" ]
// add adds a value to the container.
[ "add", "adds", "a", "value", "to", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1500-L1513
train
pilosa/pilosa
roaring/roaring.go
Contains
func (c *Container) Contains(v uint16) bool { if c.isArray() { return c.arrayContains(v) } else if c.isRun() { return c.runContains(v) } else { return c.bitmapContains(v) } }
go
func (c *Container) Contains(v uint16) bool { if c.isArray() { return c.arrayContains(v) } else if c.isRun() { return c.runContains(v) } else { return c.bitmapContains(v) } }
[ "func", "(", "c", "*", "Container", ")", "Contains", "(", "v", "uint16", ")", "bool", "{", "if", "c", ".", "isArray", "(", ")", "{", "return", "c", ".", "arrayContains", "(", "v", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "return", "c", ".", "runContains", "(", "v", ")", "\n", "}", "else", "{", "return", "c", ".", "bitmapContains", "(", "v", ")", "\n", "}", "\n", "}" ]
// Contains returns true if v is in the container.
[ "Contains", "returns", "true", "if", "v", "is", "in", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1612-L1620
train
pilosa/pilosa
roaring/roaring.go
optimize
func (c *Container) optimize() { if c.n == 0 { statsHit("optimize/empty") return } runs := c.countRuns() var newType byte if runs <= runMaxSize && runs <= c.n/2 { newType = containerRun } else if c.n < ArrayMaxSize { newType = containerArray } else { newType = containerBitmap } // Then convert accordingly. if c.isArray() { if newType == containerBitmap { statsHit("optimize/arrayToBitmap") c.arrayToBitmap() } else if newType == containerRun { statsHit("optimize/arrayToRun") c.arrayToRun(runs) } else { statsHit("optimize/arrayUnchanged") } } else if c.isBitmap() { if newType == containerArray { statsHit("optimize/bitmapToArray") c.bitmapToArray() } else if newType == containerRun { statsHit("optimize/bitmapToRun") c.bitmapToRun(runs) } else { statsHit("optimize/bitmapUnchanged") } } else if c.isRun() { if newType == containerBitmap { statsHit("optimize/runToBitmap") c.runToBitmap() } else if newType == containerArray { statsHit("optimize/runToArray") c.runToArray() } else { statsHit("optimize/runUnchanged") } } }
go
func (c *Container) optimize() { if c.n == 0 { statsHit("optimize/empty") return } runs := c.countRuns() var newType byte if runs <= runMaxSize && runs <= c.n/2 { newType = containerRun } else if c.n < ArrayMaxSize { newType = containerArray } else { newType = containerBitmap } // Then convert accordingly. if c.isArray() { if newType == containerBitmap { statsHit("optimize/arrayToBitmap") c.arrayToBitmap() } else if newType == containerRun { statsHit("optimize/arrayToRun") c.arrayToRun(runs) } else { statsHit("optimize/arrayUnchanged") } } else if c.isBitmap() { if newType == containerArray { statsHit("optimize/bitmapToArray") c.bitmapToArray() } else if newType == containerRun { statsHit("optimize/bitmapToRun") c.bitmapToRun(runs) } else { statsHit("optimize/bitmapUnchanged") } } else if c.isRun() { if newType == containerBitmap { statsHit("optimize/runToBitmap") c.runToBitmap() } else if newType == containerArray { statsHit("optimize/runToArray") c.runToArray() } else { statsHit("optimize/runUnchanged") } } }
[ "func", "(", "c", "*", "Container", ")", "optimize", "(", ")", "{", "if", "c", ".", "n", "==", "0", "{", "statsHit", "(", "\"", "\"", ")", "\n", "return", "\n", "}", "\n", "runs", ":=", "c", ".", "countRuns", "(", ")", "\n\n", "var", "newType", "byte", "\n", "if", "runs", "<=", "runMaxSize", "&&", "runs", "<=", "c", ".", "n", "/", "2", "{", "newType", "=", "containerRun", "\n", "}", "else", "if", "c", ".", "n", "<", "ArrayMaxSize", "{", "newType", "=", "containerArray", "\n", "}", "else", "{", "newType", "=", "containerBitmap", "\n", "}", "\n\n", "// Then convert accordingly.", "if", "c", ".", "isArray", "(", ")", "{", "if", "newType", "==", "containerBitmap", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "arrayToBitmap", "(", ")", "\n", "}", "else", "if", "newType", "==", "containerRun", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "arrayToRun", "(", "runs", ")", "\n", "}", "else", "{", "statsHit", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "else", "if", "c", ".", "isBitmap", "(", ")", "{", "if", "newType", "==", "containerArray", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "bitmapToArray", "(", ")", "\n", "}", "else", "if", "newType", "==", "containerRun", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "bitmapToRun", "(", "runs", ")", "\n", "}", "else", "{", "statsHit", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "if", "newType", "==", "containerBitmap", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "runToBitmap", "(", ")", "\n", "}", "else", "if", "newType", "==", "containerArray", "{", "statsHit", "(", "\"", "\"", ")", "\n", "c", ".", "runToArray", "(", ")", "\n", "}", "else", "{", "statsHit", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "}" ]
// optimize converts the container to the type which will take up the least // amount of space.
[ "optimize", "converts", "the", "container", "to", "the", "type", "which", "will", "take", "up", "the", "least", "amount", "of", "space", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1666-L1714
train
pilosa/pilosa
roaring/roaring.go
binSearchRuns
func binSearchRuns(v uint16, a []interval16) (int32, bool) { i := int32(sort.Search(len(a), func(i int) bool { return a[i].last >= v })) if i < int32(len(a)) { return i, (v >= a[i].start) && (v <= a[i].last) } return i, false }
go
func binSearchRuns(v uint16, a []interval16) (int32, bool) { i := int32(sort.Search(len(a), func(i int) bool { return a[i].last >= v })) if i < int32(len(a)) { return i, (v >= a[i].start) && (v <= a[i].last) } return i, false }
[ "func", "binSearchRuns", "(", "v", "uint16", ",", "a", "[", "]", "interval16", ")", "(", "int32", ",", "bool", ")", "{", "i", ":=", "int32", "(", "sort", ".", "Search", "(", "len", "(", "a", ")", ",", "func", "(", "i", "int", ")", "bool", "{", "return", "a", "[", "i", "]", ".", "last", ">=", "v", "}", ")", ")", "\n", "if", "i", "<", "int32", "(", "len", "(", "a", ")", ")", "{", "return", "i", ",", "(", "v", ">=", "a", "[", "i", "]", ".", "start", ")", "&&", "(", "v", "<=", "a", "[", "i", "]", ".", "last", ")", "\n", "}", "\n\n", "return", "i", ",", "false", "\n", "}" ]
// binSearchRuns returns the index of the run containing v, and true, when v is contained; // or the index of the next run starting after v, and false, when v is not contained.
[ "binSearchRuns", "returns", "the", "index", "of", "the", "run", "containing", "v", "and", "true", "when", "v", "is", "contained", ";", "or", "the", "index", "of", "the", "next", "run", "starting", "after", "v", "and", "false", "when", "v", "is", "not", "contained", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1767-L1775
train
pilosa/pilosa
roaring/roaring.go
runContains
func (c *Container) runContains(v uint16) bool { _, found := binSearchRuns(v, c.runs()) return found }
go
func (c *Container) runContains(v uint16) bool { _, found := binSearchRuns(v, c.runs()) return found }
[ "func", "(", "c", "*", "Container", ")", "runContains", "(", "v", "uint16", ")", "bool", "{", "_", ",", "found", ":=", "binSearchRuns", "(", "v", ",", "c", ".", "runs", "(", ")", ")", "\n", "return", "found", "\n", "}" ]
// runContains determines if v is in the container assuming c is a run // container.
[ "runContains", "determines", "if", "v", "is", "in", "the", "container", "assuming", "c", "is", "a", "run", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1779-L1782
train
pilosa/pilosa
roaring/roaring.go
remove
func (c *Container) remove(v uint16) (removed bool) { if c.isArray() { removed = c.arrayRemove(v) } else if c.isRun() { removed = c.runRemove(v) } else { removed = c.bitmapRemove(v) } return removed }
go
func (c *Container) remove(v uint16) (removed bool) { if c.isArray() { removed = c.arrayRemove(v) } else if c.isRun() { removed = c.runRemove(v) } else { removed = c.bitmapRemove(v) } return removed }
[ "func", "(", "c", "*", "Container", ")", "remove", "(", "v", "uint16", ")", "(", "removed", "bool", ")", "{", "if", "c", ".", "isArray", "(", ")", "{", "removed", "=", "c", ".", "arrayRemove", "(", "v", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "removed", "=", "c", ".", "runRemove", "(", "v", ")", "\n", "}", "else", "{", "removed", "=", "c", ".", "bitmapRemove", "(", "v", ")", "\n", "}", "\n", "return", "removed", "\n", "}" ]
// remove removes a value from the container.
[ "remove", "removes", "a", "value", "from", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1785-L1794
train
pilosa/pilosa
roaring/roaring.go
runRemove
func (c *Container) runRemove(v uint16) bool { runs := c.runs() i, contains := binSearchRuns(v, runs) if !contains { return false } c.unmapRun() runs = c.runs() if v == runs[i].last && v == runs[i].start { runs = append(runs[:i], runs[i+1:]...) } else if v == runs[i].last { runs[i].last-- } else if v == runs[i].start { runs[i].start++ } else if v > runs[i].start { last := runs[i].last runs[i].last = v - 1 runs = append(runs, interval16{}) copy(runs[i+2:], runs[i+1:]) runs[i+1] = interval16{start: v + 1, last: last} // runs = append(runs[:i+1], append([]interval16{{start: v + 1, last: last}}, runs[i+1:]...)...) } c.n-- c.setRuns(runs) return true }
go
func (c *Container) runRemove(v uint16) bool { runs := c.runs() i, contains := binSearchRuns(v, runs) if !contains { return false } c.unmapRun() runs = c.runs() if v == runs[i].last && v == runs[i].start { runs = append(runs[:i], runs[i+1:]...) } else if v == runs[i].last { runs[i].last-- } else if v == runs[i].start { runs[i].start++ } else if v > runs[i].start { last := runs[i].last runs[i].last = v - 1 runs = append(runs, interval16{}) copy(runs[i+2:], runs[i+1:]) runs[i+1] = interval16{start: v + 1, last: last} // runs = append(runs[:i+1], append([]interval16{{start: v + 1, last: last}}, runs[i+1:]...)...) } c.n-- c.setRuns(runs) return true }
[ "func", "(", "c", "*", "Container", ")", "runRemove", "(", "v", "uint16", ")", "bool", "{", "runs", ":=", "c", ".", "runs", "(", ")", "\n", "i", ",", "contains", ":=", "binSearchRuns", "(", "v", ",", "runs", ")", "\n", "if", "!", "contains", "{", "return", "false", "\n", "}", "\n", "c", ".", "unmapRun", "(", ")", "\n", "runs", "=", "c", ".", "runs", "(", ")", "\n", "if", "v", "==", "runs", "[", "i", "]", ".", "last", "&&", "v", "==", "runs", "[", "i", "]", ".", "start", "{", "runs", "=", "append", "(", "runs", "[", ":", "i", "]", ",", "runs", "[", "i", "+", "1", ":", "]", "...", ")", "\n", "}", "else", "if", "v", "==", "runs", "[", "i", "]", ".", "last", "{", "runs", "[", "i", "]", ".", "last", "--", "\n", "}", "else", "if", "v", "==", "runs", "[", "i", "]", ".", "start", "{", "runs", "[", "i", "]", ".", "start", "++", "\n", "}", "else", "if", "v", ">", "runs", "[", "i", "]", ".", "start", "{", "last", ":=", "runs", "[", "i", "]", ".", "last", "\n", "runs", "[", "i", "]", ".", "last", "=", "v", "-", "1", "\n", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "}", ")", "\n", "copy", "(", "runs", "[", "i", "+", "2", ":", "]", ",", "runs", "[", "i", "+", "1", ":", "]", ")", "\n", "runs", "[", "i", "+", "1", "]", "=", "interval16", "{", "start", ":", "v", "+", "1", ",", "last", ":", "last", "}", "\n", "// runs = append(runs[:i+1], append([]interval16{{start: v + 1, last: last}}, runs[i+1:]...)...)", "}", "\n", "c", ".", "n", "--", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "return", "true", "\n", "}" ]
// runRemove removes v from a run container, and returns true if v was removed.
[ "runRemove", "removes", "v", "from", "a", "run", "container", "and", "returns", "true", "if", "v", "was", "removed", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1830-L1855
train
pilosa/pilosa
roaring/roaring.go
max
func (c *Container) max() uint16 { if c.isArray() { return c.arrayMax() } else if c.isRun() { return c.runMax() } else { return c.bitmapMax() } }
go
func (c *Container) max() uint16 { if c.isArray() { return c.arrayMax() } else if c.isRun() { return c.runMax() } else { return c.bitmapMax() } }
[ "func", "(", "c", "*", "Container", ")", "max", "(", ")", "uint16", "{", "if", "c", ".", "isArray", "(", ")", "{", "return", "c", ".", "arrayMax", "(", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "return", "c", ".", "runMax", "(", ")", "\n", "}", "else", "{", "return", "c", ".", "bitmapMax", "(", ")", "\n", "}", "\n", "}" ]
// max returns the maximum value in the container.
[ "max", "returns", "the", "maximum", "value", "in", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1858-L1866
train
pilosa/pilosa
roaring/roaring.go
bitmapToArray
func (c *Container) bitmapToArray() { statsHit("bitmapToArray") bitmap := c.bitmap() c.setBitmap(nil) c.typ = containerArray c.mapped = false // return early if empty if c.n == 0 { c.setArray(nil) return } n := int32(0) array := make([]uint16, c.n) for i, word := range bitmap { for word != 0 { t := word & -word if roaringParanoia { if n >= c.n { panic("bitmap has more bits set than container.n") } } array[n] = uint16((i*64 + int(popcount(t-1)))) n++ word ^= t } } if roaringParanoia { if n != c.n { panic("bitmap has fewer bits set than container.n") } } c.setArray(array) }
go
func (c *Container) bitmapToArray() { statsHit("bitmapToArray") bitmap := c.bitmap() c.setBitmap(nil) c.typ = containerArray c.mapped = false // return early if empty if c.n == 0 { c.setArray(nil) return } n := int32(0) array := make([]uint16, c.n) for i, word := range bitmap { for word != 0 { t := word & -word if roaringParanoia { if n >= c.n { panic("bitmap has more bits set than container.n") } } array[n] = uint16((i*64 + int(popcount(t-1)))) n++ word ^= t } } if roaringParanoia { if n != c.n { panic("bitmap has fewer bits set than container.n") } } c.setArray(array) }
[ "func", "(", "c", "*", "Container", ")", "bitmapToArray", "(", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "bitmap", ":=", "c", ".", "bitmap", "(", ")", "\n", "c", ".", "setBitmap", "(", "nil", ")", "\n", "c", ".", "typ", "=", "containerArray", "\n", "c", ".", "mapped", "=", "false", "\n\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "c", ".", "setArray", "(", "nil", ")", "\n", "return", "\n", "}", "\n", "n", ":=", "int32", "(", "0", ")", "\n\n", "array", ":=", "make", "(", "[", "]", "uint16", ",", "c", ".", "n", ")", "\n", "for", "i", ",", "word", ":=", "range", "bitmap", "{", "for", "word", "!=", "0", "{", "t", ":=", "word", "&", "-", "word", "\n", "if", "roaringParanoia", "{", "if", "n", ">=", "c", ".", "n", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "array", "[", "n", "]", "=", "uint16", "(", "(", "i", "*", "64", "+", "int", "(", "popcount", "(", "t", "-", "1", ")", ")", ")", ")", "\n", "n", "++", "\n", "word", "^=", "t", "\n", "}", "\n", "}", "\n", "if", "roaringParanoia", "{", "if", "n", "!=", "c", ".", "n", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "c", ".", "setArray", "(", "array", ")", "\n", "}" ]
// bitmapToArray converts from bitmap format to array format.
[ "bitmapToArray", "converts", "from", "bitmap", "format", "to", "array", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1900-L1934
train
pilosa/pilosa
roaring/roaring.go
arrayToBitmap
func (c *Container) arrayToBitmap() { statsHit("arrayToBitmap") array := c.array() c.typ = containerBitmap bitmap := make([]uint64, bitmapN) c.setBitmap(bitmap) c.mapped = false // return early if empty if c.n == 0 { return } for _, v := range array { bitmap[int(v)/64] |= (uint64(1) << uint(v%64)) } }
go
func (c *Container) arrayToBitmap() { statsHit("arrayToBitmap") array := c.array() c.typ = containerBitmap bitmap := make([]uint64, bitmapN) c.setBitmap(bitmap) c.mapped = false // return early if empty if c.n == 0 { return } for _, v := range array { bitmap[int(v)/64] |= (uint64(1) << uint(v%64)) } }
[ "func", "(", "c", "*", "Container", ")", "arrayToBitmap", "(", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "array", ":=", "c", ".", "array", "(", ")", "\n", "c", ".", "typ", "=", "containerBitmap", "\n", "bitmap", ":=", "make", "(", "[", "]", "uint64", ",", "bitmapN", ")", "\n", "c", ".", "setBitmap", "(", "bitmap", ")", "\n", "c", ".", "mapped", "=", "false", "\n\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "return", "\n", "}", "\n\n", "for", "_", ",", "v", ":=", "range", "array", "{", "bitmap", "[", "int", "(", "v", ")", "/", "64", "]", "|=", "(", "uint64", "(", "1", ")", "<<", "uint", "(", "v", "%", "64", ")", ")", "\n", "}", "\n", "}" ]
// arrayToBitmap converts from array format to bitmap format.
[ "arrayToBitmap", "converts", "from", "array", "format", "to", "bitmap", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1937-L1953
train
pilosa/pilosa
roaring/roaring.go
runToBitmap
func (c *Container) runToBitmap() { statsHit("runToBitmap") runs := c.runs() bitmap := make([]uint64, bitmapN) c.typ = containerBitmap c.setBitmap(bitmap) c.mapped = false // return early if empty if c.n == 0 { return } for _, r := range runs { // TODO this can be ~64x faster for long runs by setting maxBitmap instead of single bits //note v must be int or will overflow for v := int(r.start); v <= int(r.last); v++ { bitmap[v/64] |= (uint64(1) << uint(v%64)) } } }
go
func (c *Container) runToBitmap() { statsHit("runToBitmap") runs := c.runs() bitmap := make([]uint64, bitmapN) c.typ = containerBitmap c.setBitmap(bitmap) c.mapped = false // return early if empty if c.n == 0 { return } for _, r := range runs { // TODO this can be ~64x faster for long runs by setting maxBitmap instead of single bits //note v must be int or will overflow for v := int(r.start); v <= int(r.last); v++ { bitmap[v/64] |= (uint64(1) << uint(v%64)) } } }
[ "func", "(", "c", "*", "Container", ")", "runToBitmap", "(", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "runs", ":=", "c", ".", "runs", "(", ")", "\n", "bitmap", ":=", "make", "(", "[", "]", "uint64", ",", "bitmapN", ")", "\n", "c", ".", "typ", "=", "containerBitmap", "\n", "c", ".", "setBitmap", "(", "bitmap", ")", "\n\n", "c", ".", "mapped", "=", "false", "\n\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "return", "\n", "}", "\n\n", "for", "_", ",", "r", ":=", "range", "runs", "{", "// TODO this can be ~64x faster for long runs by setting maxBitmap instead of single bits", "//note v must be int or will overflow", "for", "v", ":=", "int", "(", "r", ".", "start", ")", ";", "v", "<=", "int", "(", "r", ".", "last", ")", ";", "v", "++", "{", "bitmap", "[", "v", "/", "64", "]", "|=", "(", "uint64", "(", "1", ")", "<<", "uint", "(", "v", "%", "64", ")", ")", "\n", "}", "\n", "}", "\n", "}" ]
// runToBitmap converts from RLE format to bitmap format.
[ "runToBitmap", "converts", "from", "RLE", "format", "to", "bitmap", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1956-L1977
train
pilosa/pilosa
roaring/roaring.go
bitmapToRun
func (c *Container) bitmapToRun(numRuns int32) { statsHit("bitmapToRun") bitmap := c.bitmap() c.mapped = false c.typ = containerRun // return early if empty if c.n == 0 { c.setRuns(nil) return } if numRuns == 0 { numRuns = bitmapCountRuns(bitmap) } runs := make([]interval16, 0, numRuns) current := bitmap[0] var i, start, last uint16 for { // skip while empty for current == 0 && i < bitmapN-1 { i++ current = bitmap[i] } if current == 0 { break } currentStart := uint16(trailingZeroN(current)) start = 64*i + currentStart // pad LSBs with 1s current = current | (current - 1) // find next 0 for current == maxBitmap && i < bitmapN-1 { i++ current = bitmap[i] } if current == maxBitmap { // bitmap[1023] == maxBitmap runs = append(runs, interval16{start, maxContainerVal}) break } currentLast := uint16(trailingZeroN(^current)) last = 64*i + currentLast runs = append(runs, interval16{start, last - 1}) // pad LSBs with 0s current = current & (current + 1) } c.setRuns(runs) }
go
func (c *Container) bitmapToRun(numRuns int32) { statsHit("bitmapToRun") bitmap := c.bitmap() c.mapped = false c.typ = containerRun // return early if empty if c.n == 0 { c.setRuns(nil) return } if numRuns == 0 { numRuns = bitmapCountRuns(bitmap) } runs := make([]interval16, 0, numRuns) current := bitmap[0] var i, start, last uint16 for { // skip while empty for current == 0 && i < bitmapN-1 { i++ current = bitmap[i] } if current == 0 { break } currentStart := uint16(trailingZeroN(current)) start = 64*i + currentStart // pad LSBs with 1s current = current | (current - 1) // find next 0 for current == maxBitmap && i < bitmapN-1 { i++ current = bitmap[i] } if current == maxBitmap { // bitmap[1023] == maxBitmap runs = append(runs, interval16{start, maxContainerVal}) break } currentLast := uint16(trailingZeroN(^current)) last = 64*i + currentLast runs = append(runs, interval16{start, last - 1}) // pad LSBs with 0s current = current & (current + 1) } c.setRuns(runs) }
[ "func", "(", "c", "*", "Container", ")", "bitmapToRun", "(", "numRuns", "int32", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "bitmap", ":=", "c", ".", "bitmap", "(", ")", "\n", "c", ".", "mapped", "=", "false", "\n", "c", ".", "typ", "=", "containerRun", "\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "c", ".", "setRuns", "(", "nil", ")", "\n", "return", "\n", "}", "\n", "if", "numRuns", "==", "0", "{", "numRuns", "=", "bitmapCountRuns", "(", "bitmap", ")", "\n", "}", "\n\n", "runs", ":=", "make", "(", "[", "]", "interval16", ",", "0", ",", "numRuns", ")", "\n\n", "current", ":=", "bitmap", "[", "0", "]", "\n", "var", "i", ",", "start", ",", "last", "uint16", "\n", "for", "{", "// skip while empty", "for", "current", "==", "0", "&&", "i", "<", "bitmapN", "-", "1", "{", "i", "++", "\n", "current", "=", "bitmap", "[", "i", "]", "\n", "}", "\n\n", "if", "current", "==", "0", "{", "break", "\n", "}", "\n", "currentStart", ":=", "uint16", "(", "trailingZeroN", "(", "current", ")", ")", "\n", "start", "=", "64", "*", "i", "+", "currentStart", "\n\n", "// pad LSBs with 1s", "current", "=", "current", "|", "(", "current", "-", "1", ")", "\n\n", "// find next 0", "for", "current", "==", "maxBitmap", "&&", "i", "<", "bitmapN", "-", "1", "{", "i", "++", "\n", "current", "=", "bitmap", "[", "i", "]", "\n", "}", "\n\n", "if", "current", "==", "maxBitmap", "{", "// bitmap[1023] == maxBitmap", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ",", "maxContainerVal", "}", ")", "\n", "break", "\n", "}", "\n", "currentLast", ":=", "uint16", "(", "trailingZeroN", "(", "^", "current", ")", ")", "\n", "last", "=", "64", "*", "i", "+", "currentLast", "\n", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ",", "last", "-", "1", "}", ")", "\n\n", "// pad LSBs with 0s", "current", "=", "current", "&", "(", "current", "+", "1", ")", "\n", "}", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "}" ]
// bitmapToRun converts from bitmap format to RLE format.
[ "bitmapToRun", "converts", "from", "bitmap", "format", "to", "RLE", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L1980-L2034
train
pilosa/pilosa
roaring/roaring.go
arrayToRun
func (c *Container) arrayToRun(numRuns int32) { statsHit("arrayToRun") array := c.array() c.typ = containerRun c.mapped = false // return early if empty if c.n == 0 { c.setRuns(nil) return } if numRuns == 0 { numRuns = arrayCountRuns(array) } runs := make([]interval16, 0, numRuns) start := array[0] for i, v := range array[1:] { if v-array[i] > 1 { // if current-previous > 1, one run ends and another begins runs = append(runs, interval16{start, array[i]}) start = v } } // append final run runs = append(runs, interval16{start, array[c.n-1]}) c.setRuns(runs) }
go
func (c *Container) arrayToRun(numRuns int32) { statsHit("arrayToRun") array := c.array() c.typ = containerRun c.mapped = false // return early if empty if c.n == 0 { c.setRuns(nil) return } if numRuns == 0 { numRuns = arrayCountRuns(array) } runs := make([]interval16, 0, numRuns) start := array[0] for i, v := range array[1:] { if v-array[i] > 1 { // if current-previous > 1, one run ends and another begins runs = append(runs, interval16{start, array[i]}) start = v } } // append final run runs = append(runs, interval16{start, array[c.n-1]}) c.setRuns(runs) }
[ "func", "(", "c", "*", "Container", ")", "arrayToRun", "(", "numRuns", "int32", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "array", ":=", "c", ".", "array", "(", ")", "\n", "c", ".", "typ", "=", "containerRun", "\n", "c", ".", "mapped", "=", "false", "\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "c", ".", "setRuns", "(", "nil", ")", "\n", "return", "\n", "}", "\n", "if", "numRuns", "==", "0", "{", "numRuns", "=", "arrayCountRuns", "(", "array", ")", "\n", "}", "\n\n", "runs", ":=", "make", "(", "[", "]", "interval16", ",", "0", ",", "numRuns", ")", "\n", "start", ":=", "array", "[", "0", "]", "\n", "for", "i", ",", "v", ":=", "range", "array", "[", "1", ":", "]", "{", "if", "v", "-", "array", "[", "i", "]", ">", "1", "{", "// if current-previous > 1, one run ends and another begins", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ",", "array", "[", "i", "]", "}", ")", "\n", "start", "=", "v", "\n", "}", "\n", "}", "\n", "// append final run", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ",", "array", "[", "c", ".", "n", "-", "1", "]", "}", ")", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "}" ]
// arrayToRun converts from array format to RLE format.
[ "arrayToRun", "converts", "from", "array", "format", "to", "RLE", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2037-L2063
train
pilosa/pilosa
roaring/roaring.go
runToArray
func (c *Container) runToArray() { statsHit("runToArray") runs := c.runs() c.typ = containerArray c.mapped = false // return early if empty if c.n == 0 { c.setArray(nil) return } array := make([]uint16, c.n) n := int32(0) for _, r := range runs { for v := int(r.start); v <= int(r.last); v++ { array[n] = uint16(v) n++ } } if roaringParanoia { if n != c.n { panic("run has fewer bits set than container.n") } } c.setArray(array) }
go
func (c *Container) runToArray() { statsHit("runToArray") runs := c.runs() c.typ = containerArray c.mapped = false // return early if empty if c.n == 0 { c.setArray(nil) return } array := make([]uint16, c.n) n := int32(0) for _, r := range runs { for v := int(r.start); v <= int(r.last); v++ { array[n] = uint16(v) n++ } } if roaringParanoia { if n != c.n { panic("run has fewer bits set than container.n") } } c.setArray(array) }
[ "func", "(", "c", "*", "Container", ")", "runToArray", "(", ")", "{", "statsHit", "(", "\"", "\"", ")", "\n", "runs", ":=", "c", ".", "runs", "(", ")", "\n", "c", ".", "typ", "=", "containerArray", "\n", "c", ".", "mapped", "=", "false", "\n\n", "// return early if empty", "if", "c", ".", "n", "==", "0", "{", "c", ".", "setArray", "(", "nil", ")", "\n", "return", "\n", "}", "\n\n", "array", ":=", "make", "(", "[", "]", "uint16", ",", "c", ".", "n", ")", "\n", "n", ":=", "int32", "(", "0", ")", "\n", "for", "_", ",", "r", ":=", "range", "runs", "{", "for", "v", ":=", "int", "(", "r", ".", "start", ")", ";", "v", "<=", "int", "(", "r", ".", "last", ")", ";", "v", "++", "{", "array", "[", "n", "]", "=", "uint16", "(", "v", ")", "\n", "n", "++", "\n", "}", "\n", "}", "\n", "if", "roaringParanoia", "{", "if", "n", "!=", "c", ".", "n", "{", "panic", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "c", ".", "setArray", "(", "array", ")", "\n", "}" ]
// runToArray converts from RLE format to array format.
[ "runToArray", "converts", "from", "RLE", "format", "to", "array", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2066-L2092
train
pilosa/pilosa
roaring/roaring.go
WriteTo
func (c *Container) WriteTo(w io.Writer) (n int64, err error) { if c.isArray() { return c.arrayWriteTo(w) } else if c.isRun() { return c.runWriteTo(w) } else { return c.bitmapWriteTo(w) } }
go
func (c *Container) WriteTo(w io.Writer) (n int64, err error) { if c.isArray() { return c.arrayWriteTo(w) } else if c.isRun() { return c.runWriteTo(w) } else { return c.bitmapWriteTo(w) } }
[ "func", "(", "c", "*", "Container", ")", "WriteTo", "(", "w", "io", ".", "Writer", ")", "(", "n", "int64", ",", "err", "error", ")", "{", "if", "c", ".", "isArray", "(", ")", "{", "return", "c", ".", "arrayWriteTo", "(", "w", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "return", "c", ".", "runWriteTo", "(", "w", ")", "\n", "}", "else", "{", "return", "c", ".", "bitmapWriteTo", "(", "w", ")", "\n", "}", "\n", "}" ]
// WriteTo writes c to w.
[ "WriteTo", "writes", "c", "to", "w", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2126-L2134
train
pilosa/pilosa
roaring/roaring.go
size
func (c *Container) size() int { if c.isArray() { return len(c.array()) * 2 // sizeof(uint16) } else if c.isRun() { return len(c.runs())*interval16Size + runCountHeaderSize } else { return len(c.bitmap()) * 8 // sizeof(uint64) } }
go
func (c *Container) size() int { if c.isArray() { return len(c.array()) * 2 // sizeof(uint16) } else if c.isRun() { return len(c.runs())*interval16Size + runCountHeaderSize } else { return len(c.bitmap()) * 8 // sizeof(uint64) } }
[ "func", "(", "c", "*", "Container", ")", "size", "(", ")", "int", "{", "if", "c", ".", "isArray", "(", ")", "{", "return", "len", "(", "c", ".", "array", "(", ")", ")", "*", "2", "// sizeof(uint16)", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "return", "len", "(", "c", ".", "runs", "(", ")", ")", "*", "interval16Size", "+", "runCountHeaderSize", "\n", "}", "else", "{", "return", "len", "(", "c", ".", "bitmap", "(", ")", ")", "*", "8", "// sizeof(uint64)", "\n", "}", "\n", "}" ]
// size returns the encoded size of the container, in bytes.
[ "size", "returns", "the", "encoded", "size", "of", "the", "container", "in", "bytes", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2179-L2187
train
pilosa/pilosa
roaring/roaring.go
info
func (c *Container) info() containerInfo { info := containerInfo{N: c.n} if c.isArray() { info.Type = "array" info.Alloc = len(c.array()) * 2 // sizeof(uint16) } else if c.isRun() { info.Type = "run" info.Alloc = len(c.runs())*interval16Size + runCountHeaderSize } else { info.Type = "bitmap" info.Alloc = len(c.bitmap()) * 8 // sizeof(uint64) } if c.mapped { if c.isArray() { info.Pointer = unsafe.Pointer(&c.array()[0]) } else if c.isRun() { info.Pointer = unsafe.Pointer(&c.runs()[0]) } else { info.Pointer = unsafe.Pointer(&c.bitmap()[0]) } } return info }
go
func (c *Container) info() containerInfo { info := containerInfo{N: c.n} if c.isArray() { info.Type = "array" info.Alloc = len(c.array()) * 2 // sizeof(uint16) } else if c.isRun() { info.Type = "run" info.Alloc = len(c.runs())*interval16Size + runCountHeaderSize } else { info.Type = "bitmap" info.Alloc = len(c.bitmap()) * 8 // sizeof(uint64) } if c.mapped { if c.isArray() { info.Pointer = unsafe.Pointer(&c.array()[0]) } else if c.isRun() { info.Pointer = unsafe.Pointer(&c.runs()[0]) } else { info.Pointer = unsafe.Pointer(&c.bitmap()[0]) } } return info }
[ "func", "(", "c", "*", "Container", ")", "info", "(", ")", "containerInfo", "{", "info", ":=", "containerInfo", "{", "N", ":", "c", ".", "n", "}", "\n\n", "if", "c", ".", "isArray", "(", ")", "{", "info", ".", "Type", "=", "\"", "\"", "\n", "info", ".", "Alloc", "=", "len", "(", "c", ".", "array", "(", ")", ")", "*", "2", "// sizeof(uint16)", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "info", ".", "Type", "=", "\"", "\"", "\n", "info", ".", "Alloc", "=", "len", "(", "c", ".", "runs", "(", ")", ")", "*", "interval16Size", "+", "runCountHeaderSize", "\n", "}", "else", "{", "info", ".", "Type", "=", "\"", "\"", "\n", "info", ".", "Alloc", "=", "len", "(", "c", ".", "bitmap", "(", ")", ")", "*", "8", "// sizeof(uint64)", "\n", "}", "\n\n", "if", "c", ".", "mapped", "{", "if", "c", ".", "isArray", "(", ")", "{", "info", ".", "Pointer", "=", "unsafe", ".", "Pointer", "(", "&", "c", ".", "array", "(", ")", "[", "0", "]", ")", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "info", ".", "Pointer", "=", "unsafe", ".", "Pointer", "(", "&", "c", ".", "runs", "(", ")", "[", "0", "]", ")", "\n", "}", "else", "{", "info", ".", "Pointer", "=", "unsafe", ".", "Pointer", "(", "&", "c", ".", "bitmap", "(", ")", "[", "0", "]", ")", "\n", "}", "\n", "}", "\n\n", "return", "info", "\n", "}" ]
// info returns the current stats about the container.
[ "info", "returns", "the", "current", "stats", "about", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2190-L2215
train
pilosa/pilosa
roaring/roaring.go
check
func (c *Container) check() error { var a ErrorList if c.isArray() { array := c.array() if int32(len(array)) != c.n { a.Append(fmt.Errorf("array count mismatch: count=%d, n=%d", len(array), c.n)) } } else if c.isRun() { n := c.runCountRange(0, maxContainerVal+1) if n != c.n { a.Append(fmt.Errorf("run count mismatch: count=%d, n=%d", n, c.n)) } } else if c.isBitmap() { if n := c.bitmapCountRange(0, maxContainerVal+1); n != c.n { a.Append(fmt.Errorf("bitmap count mismatch: count=%d, n=%d", n, c.n)) } } else { a.Append(fmt.Errorf("empty container")) if c.n != 0 { a.Append(fmt.Errorf("empty container with nonzero count: n=%d", c.n)) } } if a == nil { return nil } return a }
go
func (c *Container) check() error { var a ErrorList if c.isArray() { array := c.array() if int32(len(array)) != c.n { a.Append(fmt.Errorf("array count mismatch: count=%d, n=%d", len(array), c.n)) } } else if c.isRun() { n := c.runCountRange(0, maxContainerVal+1) if n != c.n { a.Append(fmt.Errorf("run count mismatch: count=%d, n=%d", n, c.n)) } } else if c.isBitmap() { if n := c.bitmapCountRange(0, maxContainerVal+1); n != c.n { a.Append(fmt.Errorf("bitmap count mismatch: count=%d, n=%d", n, c.n)) } } else { a.Append(fmt.Errorf("empty container")) if c.n != 0 { a.Append(fmt.Errorf("empty container with nonzero count: n=%d", c.n)) } } if a == nil { return nil } return a }
[ "func", "(", "c", "*", "Container", ")", "check", "(", ")", "error", "{", "var", "a", "ErrorList", "\n\n", "if", "c", ".", "isArray", "(", ")", "{", "array", ":=", "c", ".", "array", "(", ")", "\n", "if", "int32", "(", "len", "(", "array", ")", ")", "!=", "c", ".", "n", "{", "a", ".", "Append", "(", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "len", "(", "array", ")", ",", "c", ".", "n", ")", ")", "\n", "}", "\n", "}", "else", "if", "c", ".", "isRun", "(", ")", "{", "n", ":=", "c", ".", "runCountRange", "(", "0", ",", "maxContainerVal", "+", "1", ")", "\n", "if", "n", "!=", "c", ".", "n", "{", "a", ".", "Append", "(", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "n", ",", "c", ".", "n", ")", ")", "\n", "}", "\n", "}", "else", "if", "c", ".", "isBitmap", "(", ")", "{", "if", "n", ":=", "c", ".", "bitmapCountRange", "(", "0", ",", "maxContainerVal", "+", "1", ")", ";", "n", "!=", "c", ".", "n", "{", "a", ".", "Append", "(", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "n", ",", "c", ".", "n", ")", ")", "\n", "}", "\n", "}", "else", "{", "a", ".", "Append", "(", "fmt", ".", "Errorf", "(", "\"", "\"", ")", ")", "\n", "if", "c", ".", "n", "!=", "0", "{", "a", ".", "Append", "(", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "c", ".", "n", ")", ")", "\n", "}", "\n", "}", "\n\n", "if", "a", "==", "nil", "{", "return", "nil", "\n", "}", "\n", "return", "a", "\n", "}" ]
// check performs a consistency check on the container.
[ "check", "performs", "a", "consistency", "check", "on", "the", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2218-L2246
train
pilosa/pilosa
roaring/roaring.go
flip
func flip(a *Container) *Container { // nolint: deadcode if a.isArray() { return flipArray(a) } else if a.isRun() { return flipRun(a) } else { return flipBitmap(a) } }
go
func flip(a *Container) *Container { // nolint: deadcode if a.isArray() { return flipArray(a) } else if a.isRun() { return flipRun(a) } else { return flipBitmap(a) } }
[ "func", "flip", "(", "a", "*", "Container", ")", "*", "Container", "{", "// nolint: deadcode", "if", "a", ".", "isArray", "(", ")", "{", "return", "flipArray", "(", "a", ")", "\n", "}", "else", "if", "a", ".", "isRun", "(", ")", "{", "return", "flipRun", "(", "a", ")", "\n", "}", "else", "{", "return", "flipBitmap", "(", "a", ")", "\n", "}", "\n", "}" ]
// flip returns a new container containing the inverse of all // bits in a.
[ "flip", "returns", "a", "new", "container", "containing", "the", "inverse", "of", "all", "bits", "in", "a", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2282-L2290
train
pilosa/pilosa
roaring/roaring.go
intersectRunRun
func intersectRunRun(a, b *Container) *Container { statsHit("intersect/RunRun") output := NewContainerRun(nil) ra, rb := a.runs(), b.runs() na, nb := len(ra), len(rb) for i, j := 0, 0; i < na && j < nb; { va, vb := ra[i], rb[j] if va.last < vb.start { // |--va--| |--vb--| i++ } else if vb.last < va.start { // |--vb--| |--va--| j++ } else if va.last > vb.last && va.start >= vb.start { // |--vb-|-|-va--| output.n += output.runAppendInterval(interval16{start: va.start, last: vb.last}) j++ } else if va.last > vb.last && va.start < vb.start { // |--va|--vb--|--| output.n += output.runAppendInterval(vb) j++ } else if va.last <= vb.last && va.start >= vb.start { // |--vb|--va--|--| output.n += output.runAppendInterval(va) i++ } else if va.last <= vb.last && va.start < vb.start { // |--va-|-|-vb--| output.n += output.runAppendInterval(interval16{start: vb.start, last: va.last}) i++ } } runs := output.runs() if output.n < ArrayMaxSize && int32(len(runs)) > output.n/2 { output.runToArray() } else if len(runs) > runMaxSize { output.runToBitmap() } return output }
go
func intersectRunRun(a, b *Container) *Container { statsHit("intersect/RunRun") output := NewContainerRun(nil) ra, rb := a.runs(), b.runs() na, nb := len(ra), len(rb) for i, j := 0, 0; i < na && j < nb; { va, vb := ra[i], rb[j] if va.last < vb.start { // |--va--| |--vb--| i++ } else if vb.last < va.start { // |--vb--| |--va--| j++ } else if va.last > vb.last && va.start >= vb.start { // |--vb-|-|-va--| output.n += output.runAppendInterval(interval16{start: va.start, last: vb.last}) j++ } else if va.last > vb.last && va.start < vb.start { // |--va|--vb--|--| output.n += output.runAppendInterval(vb) j++ } else if va.last <= vb.last && va.start >= vb.start { // |--vb|--va--|--| output.n += output.runAppendInterval(va) i++ } else if va.last <= vb.last && va.start < vb.start { // |--va-|-|-vb--| output.n += output.runAppendInterval(interval16{start: vb.start, last: va.last}) i++ } } runs := output.runs() if output.n < ArrayMaxSize && int32(len(runs)) > output.n/2 { output.runToArray() } else if len(runs) > runMaxSize { output.runToBitmap() } return output }
[ "func", "intersectRunRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "output", ":=", "NewContainerRun", "(", "nil", ")", "\n", "ra", ",", "rb", ":=", "a", ".", "runs", "(", ")", ",", "b", ".", "runs", "(", ")", "\n", "na", ",", "nb", ":=", "len", "(", "ra", ")", ",", "len", "(", "rb", ")", "\n", "for", "i", ",", "j", ":=", "0", ",", "0", ";", "i", "<", "na", "&&", "j", "<", "nb", ";", "{", "va", ",", "vb", ":=", "ra", "[", "i", "]", ",", "rb", "[", "j", "]", "\n", "if", "va", ".", "last", "<", "vb", ".", "start", "{", "// |--va--| |--vb--|", "i", "++", "\n", "}", "else", "if", "vb", ".", "last", "<", "va", ".", "start", "{", "// |--vb--| |--va--|", "j", "++", "\n", "}", "else", "if", "va", ".", "last", ">", "vb", ".", "last", "&&", "va", ".", "start", ">=", "vb", ".", "start", "{", "// |--vb-|-|-va--|", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "interval16", "{", "start", ":", "va", ".", "start", ",", "last", ":", "vb", ".", "last", "}", ")", "\n", "j", "++", "\n", "}", "else", "if", "va", ".", "last", ">", "vb", ".", "last", "&&", "va", ".", "start", "<", "vb", ".", "start", "{", "// |--va|--vb--|--|", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "vb", ")", "\n", "j", "++", "\n", "}", "else", "if", "va", ".", "last", "<=", "vb", ".", "last", "&&", "va", ".", "start", ">=", "vb", ".", "start", "{", "// |--vb|--va--|--|", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "va", ")", "\n", "i", "++", "\n", "}", "else", "if", "va", ".", "last", "<=", "vb", ".", "last", "&&", "va", ".", "start", "<", "vb", ".", "start", "{", "// |--va-|-|-vb--|", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "interval16", "{", "start", ":", "vb", ".", "start", ",", "last", ":", "va", ".", "last", "}", ")", "\n", "i", "++", "\n", "}", "\n", "}", "\n", "runs", ":=", "output", ".", "runs", "(", ")", "\n", "if", "output", ".", "n", "<", "ArrayMaxSize", "&&", "int32", "(", "len", "(", "runs", ")", ")", ">", "output", ".", "n", "/", "2", "{", "output", ".", "runToArray", "(", ")", "\n", "}", "else", "if", "len", "(", "runs", ")", ">", "runMaxSize", "{", "output", ".", "runToBitmap", "(", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// intersectRunRun computes the intersect of two run containers.
[ "intersectRunRun", "computes", "the", "intersect", "of", "two", "run", "containers", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2524-L2562
train
pilosa/pilosa
roaring/roaring.go
intersectBitmapRun
func intersectBitmapRun(a, b *Container) *Container { statsHit("intersect/BitmapRun") var output *Container runs := b.runs() if b.n <= ArrayMaxSize || a.n <= ArrayMaxSize { // output is array container array := make([]uint16, 0, b.n) for _, iv := range runs { for i := iv.start; i <= iv.last; i++ { if a.bitmapContains(i) { array = append(array, i) } // If the run ends the container, break to avoid an infinite loop. if i == 65535 { break } } } output = NewContainerArray(array) } else { // right now this iterates through the runs and sets integers in the // bitmap that are in the runs. alternately, we could zero out ranges in // the bitmap which are between runs. output = NewContainerBitmap(0, nil) bitmap := output.bitmap() aBitmap := a.bitmap() for j := 0; j < len(runs); j++ { vb := runs[j] i := vb.start >> 6 // index into a vastart := i << 6 valast := vastart + 63 for valast >= vb.start && vastart <= vb.last && i < bitmapN { if vastart >= vb.start && valast <= vb.last { // a within b bitmap[i] = aBitmap[i] output.n += int32(popcount(aBitmap[i])) } else if vb.start >= vastart && vb.last <= valast { // b within a var mask uint64 = ((1 << (vb.last - vb.start + 1)) - 1) << (vb.start - vastart) bits := aBitmap[i] & mask bitmap[i] |= bits output.n += int32(popcount(bits)) } else if vastart < vb.start { // a overlaps front of b offset := 64 - (1 + valast - vb.start) bits := (aBitmap[i] >> offset) << offset bitmap[i] |= bits output.n += int32(popcount(bits)) } else if vb.start < vastart { // b overlaps front of a offset := 64 - (1 + vb.last - vastart) bits := (aBitmap[i] << offset) >> offset bitmap[i] |= bits output.n += int32(popcount(bits)) } // update loop vars i++ vastart = i << 6 valast = vastart + 63 } } } return output }
go
func intersectBitmapRun(a, b *Container) *Container { statsHit("intersect/BitmapRun") var output *Container runs := b.runs() if b.n <= ArrayMaxSize || a.n <= ArrayMaxSize { // output is array container array := make([]uint16, 0, b.n) for _, iv := range runs { for i := iv.start; i <= iv.last; i++ { if a.bitmapContains(i) { array = append(array, i) } // If the run ends the container, break to avoid an infinite loop. if i == 65535 { break } } } output = NewContainerArray(array) } else { // right now this iterates through the runs and sets integers in the // bitmap that are in the runs. alternately, we could zero out ranges in // the bitmap which are between runs. output = NewContainerBitmap(0, nil) bitmap := output.bitmap() aBitmap := a.bitmap() for j := 0; j < len(runs); j++ { vb := runs[j] i := vb.start >> 6 // index into a vastart := i << 6 valast := vastart + 63 for valast >= vb.start && vastart <= vb.last && i < bitmapN { if vastart >= vb.start && valast <= vb.last { // a within b bitmap[i] = aBitmap[i] output.n += int32(popcount(aBitmap[i])) } else if vb.start >= vastart && vb.last <= valast { // b within a var mask uint64 = ((1 << (vb.last - vb.start + 1)) - 1) << (vb.start - vastart) bits := aBitmap[i] & mask bitmap[i] |= bits output.n += int32(popcount(bits)) } else if vastart < vb.start { // a overlaps front of b offset := 64 - (1 + valast - vb.start) bits := (aBitmap[i] >> offset) << offset bitmap[i] |= bits output.n += int32(popcount(bits)) } else if vb.start < vastart { // b overlaps front of a offset := 64 - (1 + vb.last - vastart) bits := (aBitmap[i] << offset) >> offset bitmap[i] |= bits output.n += int32(popcount(bits)) } // update loop vars i++ vastart = i << 6 valast = vastart + 63 } } } return output }
[ "func", "intersectBitmapRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "var", "output", "*", "Container", "\n", "runs", ":=", "b", ".", "runs", "(", ")", "\n", "if", "b", ".", "n", "<=", "ArrayMaxSize", "||", "a", ".", "n", "<=", "ArrayMaxSize", "{", "// output is array container", "array", ":=", "make", "(", "[", "]", "uint16", ",", "0", ",", "b", ".", "n", ")", "\n", "for", "_", ",", "iv", ":=", "range", "runs", "{", "for", "i", ":=", "iv", ".", "start", ";", "i", "<=", "iv", ".", "last", ";", "i", "++", "{", "if", "a", ".", "bitmapContains", "(", "i", ")", "{", "array", "=", "append", "(", "array", ",", "i", ")", "\n", "}", "\n", "// If the run ends the container, break to avoid an infinite loop.", "if", "i", "==", "65535", "{", "break", "\n", "}", "\n", "}", "\n", "}", "\n\n", "output", "=", "NewContainerArray", "(", "array", ")", "\n", "}", "else", "{", "// right now this iterates through the runs and sets integers in the", "// bitmap that are in the runs. alternately, we could zero out ranges in", "// the bitmap which are between runs.", "output", "=", "NewContainerBitmap", "(", "0", ",", "nil", ")", "\n", "bitmap", ":=", "output", ".", "bitmap", "(", ")", "\n", "aBitmap", ":=", "a", ".", "bitmap", "(", ")", "\n", "for", "j", ":=", "0", ";", "j", "<", "len", "(", "runs", ")", ";", "j", "++", "{", "vb", ":=", "runs", "[", "j", "]", "\n", "i", ":=", "vb", ".", "start", ">>", "6", "// index into a", "\n", "vastart", ":=", "i", "<<", "6", "\n", "valast", ":=", "vastart", "+", "63", "\n", "for", "valast", ">=", "vb", ".", "start", "&&", "vastart", "<=", "vb", ".", "last", "&&", "i", "<", "bitmapN", "{", "if", "vastart", ">=", "vb", ".", "start", "&&", "valast", "<=", "vb", ".", "last", "{", "// a within b", "bitmap", "[", "i", "]", "=", "aBitmap", "[", "i", "]", "\n", "output", ".", "n", "+=", "int32", "(", "popcount", "(", "aBitmap", "[", "i", "]", ")", ")", "\n", "}", "else", "if", "vb", ".", "start", ">=", "vastart", "&&", "vb", ".", "last", "<=", "valast", "{", "// b within a", "var", "mask", "uint64", "=", "(", "(", "1", "<<", "(", "vb", ".", "last", "-", "vb", ".", "start", "+", "1", ")", ")", "-", "1", ")", "<<", "(", "vb", ".", "start", "-", "vastart", ")", "\n", "bits", ":=", "aBitmap", "[", "i", "]", "&", "mask", "\n", "bitmap", "[", "i", "]", "|=", "bits", "\n", "output", ".", "n", "+=", "int32", "(", "popcount", "(", "bits", ")", ")", "\n", "}", "else", "if", "vastart", "<", "vb", ".", "start", "{", "// a overlaps front of b", "offset", ":=", "64", "-", "(", "1", "+", "valast", "-", "vb", ".", "start", ")", "\n", "bits", ":=", "(", "aBitmap", "[", "i", "]", ">>", "offset", ")", "<<", "offset", "\n", "bitmap", "[", "i", "]", "|=", "bits", "\n", "output", ".", "n", "+=", "int32", "(", "popcount", "(", "bits", ")", ")", "\n", "}", "else", "if", "vb", ".", "start", "<", "vastart", "{", "// b overlaps front of a", "offset", ":=", "64", "-", "(", "1", "+", "vb", ".", "last", "-", "vastart", ")", "\n", "bits", ":=", "(", "aBitmap", "[", "i", "]", "<<", "offset", ")", ">>", "offset", "\n", "bitmap", "[", "i", "]", "|=", "bits", "\n", "output", ".", "n", "+=", "int32", "(", "popcount", "(", "bits", ")", ")", "\n", "}", "\n", "// update loop vars", "i", "++", "\n", "vastart", "=", "i", "<<", "6", "\n", "valast", "=", "vastart", "+", "63", "\n", "}", "\n", "}", "\n", "}", "\n", "return", "output", "\n", "}" ]
// intersectBitmapRun returns an array container if either container's // cardinality is <= ArrayMaxSize. Otherwise it returns a bitmap container.
[ "intersectBitmapRun", "returns", "an", "array", "container", "if", "either", "container", "s", "cardinality", "is", "<", "=", "ArrayMaxSize", ".", "Otherwise", "it", "returns", "a", "bitmap", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2566-L2626
train
pilosa/pilosa
roaring/roaring.go
unionArrayRun
func unionArrayRun(a, b *Container) *Container { statsHit("union/ArrayRun") if b.n == maxContainerVal+1 { return b.Clone() } output := NewContainerRun(nil) aa, rb := a.array(), b.runs() na, nb := len(aa), len(rb) var vb interval16 var va uint16 for i, j := 0, 0; i < na || j < nb; { if i < na { va = aa[i] } if j < nb { vb = rb[j] } if i < na && (j >= nb || va < vb.start) { output.n += output.runAppendInterval(interval16{start: va, last: va}) i++ } else { output.n += output.runAppendInterval(vb) j++ } } if output.n < ArrayMaxSize { output.runToArray() } else if len(output.runs()) > runMaxSize { output.runToBitmap() } return output }
go
func unionArrayRun(a, b *Container) *Container { statsHit("union/ArrayRun") if b.n == maxContainerVal+1 { return b.Clone() } output := NewContainerRun(nil) aa, rb := a.array(), b.runs() na, nb := len(aa), len(rb) var vb interval16 var va uint16 for i, j := 0, 0; i < na || j < nb; { if i < na { va = aa[i] } if j < nb { vb = rb[j] } if i < na && (j >= nb || va < vb.start) { output.n += output.runAppendInterval(interval16{start: va, last: va}) i++ } else { output.n += output.runAppendInterval(vb) j++ } } if output.n < ArrayMaxSize { output.runToArray() } else if len(output.runs()) > runMaxSize { output.runToBitmap() } return output }
[ "func", "unionArrayRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "if", "b", ".", "n", "==", "maxContainerVal", "+", "1", "{", "return", "b", ".", "Clone", "(", ")", "\n", "}", "\n", "output", ":=", "NewContainerRun", "(", "nil", ")", "\n", "aa", ",", "rb", ":=", "a", ".", "array", "(", ")", ",", "b", ".", "runs", "(", ")", "\n", "na", ",", "nb", ":=", "len", "(", "aa", ")", ",", "len", "(", "rb", ")", "\n", "var", "vb", "interval16", "\n", "var", "va", "uint16", "\n", "for", "i", ",", "j", ":=", "0", ",", "0", ";", "i", "<", "na", "||", "j", "<", "nb", ";", "{", "if", "i", "<", "na", "{", "va", "=", "aa", "[", "i", "]", "\n", "}", "\n", "if", "j", "<", "nb", "{", "vb", "=", "rb", "[", "j", "]", "\n", "}", "\n", "if", "i", "<", "na", "&&", "(", "j", ">=", "nb", "||", "va", "<", "vb", ".", "start", ")", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "interval16", "{", "start", ":", "va", ",", "last", ":", "va", "}", ")", "\n", "i", "++", "\n", "}", "else", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "vb", ")", "\n", "j", "++", "\n", "}", "\n", "}", "\n", "if", "output", ".", "n", "<", "ArrayMaxSize", "{", "output", ".", "runToArray", "(", ")", "\n", "}", "else", "if", "len", "(", "output", ".", "runs", "(", ")", ")", ">", "runMaxSize", "{", "output", ".", "runToBitmap", "(", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// unionArrayRun optimistically assumes that the result will be a run container, // and converts to a bitmap or array container afterwards if necessary.
[ "unionArrayRun", "optimistically", "assumes", "that", "the", "result", "will", "be", "a", "run", "container", "and", "converts", "to", "a", "bitmap", "or", "array", "container", "afterwards", "if", "necessary", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2777-L2808
train
pilosa/pilosa
roaring/roaring.go
runAppendInterval
func (c *Container) runAppendInterval(v interval16) int32 { runs := c.runs() if len(runs) == 0 { runs = append(runs, v) c.setRuns(runs) return int32(v.last-v.start) + 1 } last := runs[len(runs)-1] if last.last == maxContainerVal { //protect against overflow return 0 } if last.last+1 >= v.start && v.last > last.last { runs[len(runs)-1].last = v.last c.setRuns(runs) return int32(v.last - last.last) } else if last.last+1 < v.start { runs = append(runs, v) c.setRuns(runs) return int32(v.last-v.start) + 1 } return 0 }
go
func (c *Container) runAppendInterval(v interval16) int32 { runs := c.runs() if len(runs) == 0 { runs = append(runs, v) c.setRuns(runs) return int32(v.last-v.start) + 1 } last := runs[len(runs)-1] if last.last == maxContainerVal { //protect against overflow return 0 } if last.last+1 >= v.start && v.last > last.last { runs[len(runs)-1].last = v.last c.setRuns(runs) return int32(v.last - last.last) } else if last.last+1 < v.start { runs = append(runs, v) c.setRuns(runs) return int32(v.last-v.start) + 1 } return 0 }
[ "func", "(", "c", "*", "Container", ")", "runAppendInterval", "(", "v", "interval16", ")", "int32", "{", "runs", ":=", "c", ".", "runs", "(", ")", "\n", "if", "len", "(", "runs", ")", "==", "0", "{", "runs", "=", "append", "(", "runs", ",", "v", ")", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "return", "int32", "(", "v", ".", "last", "-", "v", ".", "start", ")", "+", "1", "\n", "}", "\n\n", "last", ":=", "runs", "[", "len", "(", "runs", ")", "-", "1", "]", "\n", "if", "last", ".", "last", "==", "maxContainerVal", "{", "//protect against overflow", "return", "0", "\n", "}", "\n", "if", "last", ".", "last", "+", "1", ">=", "v", ".", "start", "&&", "v", ".", "last", ">", "last", ".", "last", "{", "runs", "[", "len", "(", "runs", ")", "-", "1", "]", ".", "last", "=", "v", ".", "last", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "return", "int32", "(", "v", ".", "last", "-", "last", ".", "last", ")", "\n", "}", "else", "if", "last", ".", "last", "+", "1", "<", "v", ".", "start", "{", "runs", "=", "append", "(", "runs", ",", "v", ")", "\n", "c", ".", "setRuns", "(", "runs", ")", "\n", "return", "int32", "(", "v", ".", "last", "-", "v", ".", "start", ")", "+", "1", "\n", "}", "\n", "return", "0", "\n", "}" ]
// runAppendInterval adds the given interval to the run container. It assumes // that the interval comes at the end of the list of runs, and does not check // that this is the case. It will not behave correctly if the start of the given // interval is earlier than the start of the last interval in the list of runs. // Its return value is the amount by which the cardinality of the container was // increased.
[ "runAppendInterval", "adds", "the", "given", "interval", "to", "the", "run", "container", ".", "It", "assumes", "that", "the", "interval", "comes", "at", "the", "end", "of", "the", "list", "of", "runs", "and", "does", "not", "check", "that", "this", "is", "the", "case", ".", "It", "will", "not", "behave", "correctly", "if", "the", "start", "of", "the", "given", "interval", "is", "earlier", "than", "the", "start", "of", "the", "last", "interval", "in", "the", "list", "of", "runs", ".", "Its", "return", "value", "is", "the", "amount", "by", "which", "the", "cardinality", "of", "the", "container", "was", "increased", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2816-L2838
train
pilosa/pilosa
roaring/roaring.go
unionBitmapRunInPlace
func unionBitmapRunInPlace(a, b *Container) { a.unmapBitmap() bitmap := a.bitmap() statsHit("union/BitmapRun") for _, run := range b.runs() { bitmapSetRangeIgnoreN(bitmap, uint64(run.start), uint64(run.last)+1) } }
go
func unionBitmapRunInPlace(a, b *Container) { a.unmapBitmap() bitmap := a.bitmap() statsHit("union/BitmapRun") for _, run := range b.runs() { bitmapSetRangeIgnoreN(bitmap, uint64(run.start), uint64(run.last)+1) } }
[ "func", "unionBitmapRunInPlace", "(", "a", ",", "b", "*", "Container", ")", "{", "a", ".", "unmapBitmap", "(", ")", "\n", "bitmap", ":=", "a", ".", "bitmap", "(", ")", "\n", "statsHit", "(", "\"", "\"", ")", "\n", "for", "_", ",", "run", ":=", "range", "b", ".", "runs", "(", ")", "{", "bitmapSetRangeIgnoreN", "(", "bitmap", ",", "uint64", "(", "run", ".", "start", ")", ",", "uint64", "(", "run", ".", "last", ")", "+", "1", ")", "\n", "}", "\n", "}" ]
// unions the run b into the bitmap a, mutating a in place. The n value of // a will need to be repaired after the fact.
[ "unions", "the", "run", "b", "into", "the", "bitmap", "a", "mutating", "a", "in", "place", ".", "The", "n", "value", "of", "a", "will", "need", "to", "be", "repaired", "after", "the", "fact", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2891-L2898
train
pilosa/pilosa
roaring/roaring.go
bitmapSetRangeIgnoreN
func bitmapSetRangeIgnoreN(bitmap []uint64, i, j uint64) { x := i >> 6 y := (j - 1) >> 6 var X uint64 = maxBitmap << (i % 64) var Y uint64 = maxBitmap >> (63 - ((j - 1) % 64)) if x == y { bitmap[x] |= (X & Y) } else { bitmap[x] |= X for i := x + 1; i < y; i++ { bitmap[i] = maxBitmap } bitmap[y] |= Y } }
go
func bitmapSetRangeIgnoreN(bitmap []uint64, i, j uint64) { x := i >> 6 y := (j - 1) >> 6 var X uint64 = maxBitmap << (i % 64) var Y uint64 = maxBitmap >> (63 - ((j - 1) % 64)) if x == y { bitmap[x] |= (X & Y) } else { bitmap[x] |= X for i := x + 1; i < y; i++ { bitmap[i] = maxBitmap } bitmap[y] |= Y } }
[ "func", "bitmapSetRangeIgnoreN", "(", "bitmap", "[", "]", "uint64", ",", "i", ",", "j", "uint64", ")", "{", "x", ":=", "i", ">>", "6", "\n", "y", ":=", "(", "j", "-", "1", ")", ">>", "6", "\n", "var", "X", "uint64", "=", "maxBitmap", "<<", "(", "i", "%", "64", ")", "\n", "var", "Y", "uint64", "=", "maxBitmap", ">>", "(", "63", "-", "(", "(", "j", "-", "1", ")", "%", "64", ")", ")", "\n\n", "if", "x", "==", "y", "{", "bitmap", "[", "x", "]", "|=", "(", "X", "&", "Y", ")", "\n", "}", "else", "{", "bitmap", "[", "x", "]", "|=", "X", "\n", "for", "i", ":=", "x", "+", "1", ";", "i", "<", "y", ";", "i", "++", "{", "bitmap", "[", "i", "]", "=", "maxBitmap", "\n", "}", "\n", "bitmap", "[", "y", "]", "|=", "Y", "\n", "}", "\n", "}" ]
// sets all bits in [i, j) without updating any corresponding n value.
[ "sets", "all", "bits", "in", "[", "i", "j", ")", "without", "updating", "any", "corresponding", "n", "value", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L2927-L2942
train
pilosa/pilosa
roaring/roaring.go
unionBitmapArrayInPlace
func unionBitmapArrayInPlace(a, b *Container) { a.unmapBitmap() bitmap := a.bitmap() for _, v := range b.array() { bitmap[v>>6] |= (uint64(1) << (v % 64)) } }
go
func unionBitmapArrayInPlace(a, b *Container) { a.unmapBitmap() bitmap := a.bitmap() for _, v := range b.array() { bitmap[v>>6] |= (uint64(1) << (v % 64)) } }
[ "func", "unionBitmapArrayInPlace", "(", "a", ",", "b", "*", "Container", ")", "{", "a", ".", "unmapBitmap", "(", ")", "\n", "bitmap", ":=", "a", ".", "bitmap", "(", ")", "\n", "for", "_", ",", "v", ":=", "range", "b", ".", "array", "(", ")", "{", "bitmap", "[", "v", ">>", "6", "]", "|=", "(", "uint64", "(", "1", ")", "<<", "(", "v", "%", "64", ")", ")", "\n", "}", "\n", "}" ]
// unions array b into bitmap a, mutating a in place. The n value // of a will need to be repaired after the fact.
[ "unions", "array", "b", "into", "bitmap", "a", "mutating", "a", "in", "place", ".", "The", "n", "value", "of", "a", "will", "need", "to", "be", "repaired", "after", "the", "fact", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3046-L3052
train
pilosa/pilosa
roaring/roaring.go
unionBitmapBitmapInPlace
func unionBitmapBitmapInPlace(a, b *Container) { a.unmapBitmap() // local variables added to prevent BCE checks in loop // see https://go101.org/article/bounds-check-elimination.html var ( ab = a.bitmap()[:bitmapN] bb = b.bitmap()[:bitmapN] ) // Manually unroll loop to make it a little faster. // TODO(rartoul): Can probably make this a few x faster using // SIMD instructions. for i := 0; i < bitmapN; i += 4 { ab[i] |= bb[i] ab[i+1] |= bb[i+1] ab[i+2] |= bb[i+2] ab[i+3] |= bb[i+3] } }
go
func unionBitmapBitmapInPlace(a, b *Container) { a.unmapBitmap() // local variables added to prevent BCE checks in loop // see https://go101.org/article/bounds-check-elimination.html var ( ab = a.bitmap()[:bitmapN] bb = b.bitmap()[:bitmapN] ) // Manually unroll loop to make it a little faster. // TODO(rartoul): Can probably make this a few x faster using // SIMD instructions. for i := 0; i < bitmapN; i += 4 { ab[i] |= bb[i] ab[i+1] |= bb[i+1] ab[i+2] |= bb[i+2] ab[i+3] |= bb[i+3] } }
[ "func", "unionBitmapBitmapInPlace", "(", "a", ",", "b", "*", "Container", ")", "{", "a", ".", "unmapBitmap", "(", ")", "\n\n", "// local variables added to prevent BCE checks in loop", "// see https://go101.org/article/bounds-check-elimination.html", "var", "(", "ab", "=", "a", ".", "bitmap", "(", ")", "[", ":", "bitmapN", "]", "\n", "bb", "=", "b", ".", "bitmap", "(", ")", "[", ":", "bitmapN", "]", "\n", ")", "\n", "// Manually unroll loop to make it a little faster.", "// TODO(rartoul): Can probably make this a few x faster using", "// SIMD instructions.", "for", "i", ":=", "0", ";", "i", "<", "bitmapN", ";", "i", "+=", "4", "{", "ab", "[", "i", "]", "|=", "bb", "[", "i", "]", "\n", "ab", "[", "i", "+", "1", "]", "|=", "bb", "[", "i", "+", "1", "]", "\n", "ab", "[", "i", "+", "2", "]", "|=", "bb", "[", "i", "+", "2", "]", "\n", "ab", "[", "i", "+", "3", "]", "|=", "bb", "[", "i", "+", "3", "]", "\n", "}", "\n", "}" ]
// unions bitmap b into bitmap a, mutating a in place. The n value of // a will need to be repaired after the fact.
[ "unions", "bitmap", "b", "into", "bitmap", "a", "mutating", "a", "in", "place", ".", "The", "n", "value", "of", "a", "will", "need", "to", "be", "repaired", "after", "the", "fact", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3077-L3096
train
pilosa/pilosa
roaring/roaring.go
differenceArrayArray
func differenceArrayArray(a, b *Container) *Container { statsHit("difference/ArrayArray") output := NewContainerArray(nil) aa, ab := a.array(), b.array() na, nb := len(aa), len(ab) for i, j := 0, 0; i < na; { va := aa[i] if j >= nb { output.add(va) i++ continue } vb := ab[j] if va < vb { output.add(va) i++ } else if va > vb { j++ } else { i, j = i+1, j+1 } } return output }
go
func differenceArrayArray(a, b *Container) *Container { statsHit("difference/ArrayArray") output := NewContainerArray(nil) aa, ab := a.array(), b.array() na, nb := len(aa), len(ab) for i, j := 0, 0; i < na; { va := aa[i] if j >= nb { output.add(va) i++ continue } vb := ab[j] if va < vb { output.add(va) i++ } else if va > vb { j++ } else { i, j = i+1, j+1 } } return output }
[ "func", "differenceArrayArray", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "output", ":=", "NewContainerArray", "(", "nil", ")", "\n", "aa", ",", "ab", ":=", "a", ".", "array", "(", ")", ",", "b", ".", "array", "(", ")", "\n", "na", ",", "nb", ":=", "len", "(", "aa", ")", ",", "len", "(", "ab", ")", "\n", "for", "i", ",", "j", ":=", "0", ",", "0", ";", "i", "<", "na", ";", "{", "va", ":=", "aa", "[", "i", "]", "\n", "if", "j", ">=", "nb", "{", "output", ".", "add", "(", "va", ")", "\n", "i", "++", "\n", "continue", "\n", "}", "\n\n", "vb", ":=", "ab", "[", "j", "]", "\n", "if", "va", "<", "vb", "{", "output", ".", "add", "(", "va", ")", "\n", "i", "++", "\n", "}", "else", "if", "va", ">", "vb", "{", "j", "++", "\n", "}", "else", "{", "i", ",", "j", "=", "i", "+", "1", ",", "j", "+", "1", "\n", "}", "\n", "}", "\n", "return", "output", "\n", "}" ]
// differenceArrayArray computes the difference bween two arrays.
[ "differenceArrayArray", "computes", "the", "difference", "bween", "two", "arrays", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3127-L3151
train
pilosa/pilosa
roaring/roaring.go
differenceArrayRun
func differenceArrayRun(a, b *Container) *Container { statsHit("difference/ArrayRun") // func (ac *arrayContainer) iandNotRun16(rc *runContainer16) container { if a.n == 0 || b.n == 0 { return a.Clone() } output := NewContainerArray(make([]uint16, 0, a.n)) // cardinality upper bound: card(A) i := 0 // array index j := 0 // run index aa, rb := a.array(), b.runs() // handle overlap for i < int(a.n) { // keep all array elements before beginning of runs if aa[i] < rb[j].start { output.add(aa[i]) i++ continue } // if array element in run, skip it if aa[i] >= rb[j].start && aa[i] <= rb[j].last { i++ continue } // if array element larger than current run, check next run if aa[i] > rb[j].last { j++ if j == len(rb) { break } } } if i < len(aa) { // keep all array elements after end of runs // It's possible that output was converted from array to bitmap in output.add() // so check container type before proceeding. if output.typ == containerArray { array := output.array() array = append(array, aa[i:]...) output.setArray(array) // TODO: consider handling container.n mutations in one place // like we do with container.add(). output.n += int32(len(aa[i:])) } else { for _, v := range aa[i:] { output.add(v) } } } return output }
go
func differenceArrayRun(a, b *Container) *Container { statsHit("difference/ArrayRun") // func (ac *arrayContainer) iandNotRun16(rc *runContainer16) container { if a.n == 0 || b.n == 0 { return a.Clone() } output := NewContainerArray(make([]uint16, 0, a.n)) // cardinality upper bound: card(A) i := 0 // array index j := 0 // run index aa, rb := a.array(), b.runs() // handle overlap for i < int(a.n) { // keep all array elements before beginning of runs if aa[i] < rb[j].start { output.add(aa[i]) i++ continue } // if array element in run, skip it if aa[i] >= rb[j].start && aa[i] <= rb[j].last { i++ continue } // if array element larger than current run, check next run if aa[i] > rb[j].last { j++ if j == len(rb) { break } } } if i < len(aa) { // keep all array elements after end of runs // It's possible that output was converted from array to bitmap in output.add() // so check container type before proceeding. if output.typ == containerArray { array := output.array() array = append(array, aa[i:]...) output.setArray(array) // TODO: consider handling container.n mutations in one place // like we do with container.add(). output.n += int32(len(aa[i:])) } else { for _, v := range aa[i:] { output.add(v) } } } return output }
[ "func", "differenceArrayRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "// func (ac *arrayContainer) iandNotRun16(rc *runContainer16) container {", "if", "a", ".", "n", "==", "0", "||", "b", ".", "n", "==", "0", "{", "return", "a", ".", "Clone", "(", ")", "\n", "}", "\n\n", "output", ":=", "NewContainerArray", "(", "make", "(", "[", "]", "uint16", ",", "0", ",", "a", ".", "n", ")", ")", "\n", "// cardinality upper bound: card(A)", "i", ":=", "0", "// array index", "\n", "j", ":=", "0", "// run index", "\n", "aa", ",", "rb", ":=", "a", ".", "array", "(", ")", ",", "b", ".", "runs", "(", ")", "\n\n", "// handle overlap", "for", "i", "<", "int", "(", "a", ".", "n", ")", "{", "// keep all array elements before beginning of runs", "if", "aa", "[", "i", "]", "<", "rb", "[", "j", "]", ".", "start", "{", "output", ".", "add", "(", "aa", "[", "i", "]", ")", "\n", "i", "++", "\n", "continue", "\n", "}", "\n\n", "// if array element in run, skip it", "if", "aa", "[", "i", "]", ">=", "rb", "[", "j", "]", ".", "start", "&&", "aa", "[", "i", "]", "<=", "rb", "[", "j", "]", ".", "last", "{", "i", "++", "\n", "continue", "\n", "}", "\n\n", "// if array element larger than current run, check next run", "if", "aa", "[", "i", "]", ">", "rb", "[", "j", "]", ".", "last", "{", "j", "++", "\n", "if", "j", "==", "len", "(", "rb", ")", "{", "break", "\n", "}", "\n", "}", "\n", "}", "\n\n", "if", "i", "<", "len", "(", "aa", ")", "{", "// keep all array elements after end of runs", "// It's possible that output was converted from array to bitmap in output.add()", "// so check container type before proceeding.", "if", "output", ".", "typ", "==", "containerArray", "{", "array", ":=", "output", ".", "array", "(", ")", "\n", "array", "=", "append", "(", "array", ",", "aa", "[", "i", ":", "]", "...", ")", "\n", "output", ".", "setArray", "(", "array", ")", "\n", "// TODO: consider handling container.n mutations in one place", "// like we do with container.add().", "output", ".", "n", "+=", "int32", "(", "len", "(", "aa", "[", "i", ":", "]", ")", ")", "\n", "}", "else", "{", "for", "_", ",", "v", ":=", "range", "aa", "[", "i", ":", "]", "{", "output", ".", "add", "(", "v", ")", "\n", "}", "\n", "}", "\n", "}", "\n", "return", "output", "\n", "}" ]
// differenceArrayRun computes the difference of an array from a run.
[ "differenceArrayRun", "computes", "the", "difference", "of", "an", "array", "from", "a", "run", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3154-L3212
train
pilosa/pilosa
roaring/roaring.go
differenceBitmapRun
func differenceBitmapRun(a, b *Container) *Container { statsHit("difference/BitmapRun") if a.n == 0 || b.n == 0 { return a.Clone() } output := a.Clone() for _, run := range b.runs() { output.bitmapZeroRange(uint64(run.start), uint64(run.last)+1) } return output }
go
func differenceBitmapRun(a, b *Container) *Container { statsHit("difference/BitmapRun") if a.n == 0 || b.n == 0 { return a.Clone() } output := a.Clone() for _, run := range b.runs() { output.bitmapZeroRange(uint64(run.start), uint64(run.last)+1) } return output }
[ "func", "differenceBitmapRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "if", "a", ".", "n", "==", "0", "||", "b", ".", "n", "==", "0", "{", "return", "a", ".", "Clone", "(", ")", "\n", "}", "\n\n", "output", ":=", "a", ".", "Clone", "(", ")", "\n", "for", "_", ",", "run", ":=", "range", "b", ".", "runs", "(", ")", "{", "output", ".", "bitmapZeroRange", "(", "uint64", "(", "run", ".", "start", ")", ",", "uint64", "(", "run", ".", "last", ")", "+", "1", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// differenceBitmapRun computes the difference of an bitmap from a run.
[ "differenceBitmapRun", "computes", "the", "difference", "of", "an", "bitmap", "from", "a", "run", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3215-L3226
train
pilosa/pilosa
roaring/roaring.go
differenceRunArray
func differenceRunArray(a, b *Container) *Container { statsHit("difference/RunArray") if a.n == 0 || b.n == 0 { return a.Clone() } ra, ab := a.runs(), b.array() runs := make([]interval16, 0, len(ra)) bidx := 0 vb := ab[bidx] RUNLOOP: for _, run := range ra { start := run.start for vb < run.start { bidx++ if bidx >= len(ab) { break } vb = ab[bidx] } for vb >= run.start && vb <= run.last { if vb == start { if vb == 65535 { // overflow break RUNLOOP } start++ bidx++ if bidx >= len(ab) { break } vb = ab[bidx] continue } runs = append(runs, interval16{start: start, last: vb - 1}) if vb == 65535 { // overflow break RUNLOOP } start = vb + 1 bidx++ if bidx >= len(ab) { break } vb = ab[bidx] } if start <= run.last { runs = append(runs, interval16{start: start, last: run.last}) } } output := NewContainerRun(runs) output.optimize() return output }
go
func differenceRunArray(a, b *Container) *Container { statsHit("difference/RunArray") if a.n == 0 || b.n == 0 { return a.Clone() } ra, ab := a.runs(), b.array() runs := make([]interval16, 0, len(ra)) bidx := 0 vb := ab[bidx] RUNLOOP: for _, run := range ra { start := run.start for vb < run.start { bidx++ if bidx >= len(ab) { break } vb = ab[bidx] } for vb >= run.start && vb <= run.last { if vb == start { if vb == 65535 { // overflow break RUNLOOP } start++ bidx++ if bidx >= len(ab) { break } vb = ab[bidx] continue } runs = append(runs, interval16{start: start, last: vb - 1}) if vb == 65535 { // overflow break RUNLOOP } start = vb + 1 bidx++ if bidx >= len(ab) { break } vb = ab[bidx] } if start <= run.last { runs = append(runs, interval16{start: start, last: run.last}) } } output := NewContainerRun(runs) output.optimize() return output }
[ "func", "differenceRunArray", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "if", "a", ".", "n", "==", "0", "||", "b", ".", "n", "==", "0", "{", "return", "a", ".", "Clone", "(", ")", "\n", "}", "\n", "ra", ",", "ab", ":=", "a", ".", "runs", "(", ")", ",", "b", ".", "array", "(", ")", "\n", "runs", ":=", "make", "(", "[", "]", "interval16", ",", "0", ",", "len", "(", "ra", ")", ")", "\n\n", "bidx", ":=", "0", "\n", "vb", ":=", "ab", "[", "bidx", "]", "\n\n", "RUNLOOP", ":", "for", "_", ",", "run", ":=", "range", "ra", "{", "start", ":=", "run", ".", "start", "\n", "for", "vb", "<", "run", ".", "start", "{", "bidx", "++", "\n", "if", "bidx", ">=", "len", "(", "ab", ")", "{", "break", "\n", "}", "\n", "vb", "=", "ab", "[", "bidx", "]", "\n", "}", "\n", "for", "vb", ">=", "run", ".", "start", "&&", "vb", "<=", "run", ".", "last", "{", "if", "vb", "==", "start", "{", "if", "vb", "==", "65535", "{", "// overflow", "break", "RUNLOOP", "\n", "}", "\n", "start", "++", "\n", "bidx", "++", "\n", "if", "bidx", ">=", "len", "(", "ab", ")", "{", "break", "\n", "}", "\n", "vb", "=", "ab", "[", "bidx", "]", "\n", "continue", "\n", "}", "\n", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ":", "start", ",", "last", ":", "vb", "-", "1", "}", ")", "\n", "if", "vb", "==", "65535", "{", "// overflow", "break", "RUNLOOP", "\n", "}", "\n", "start", "=", "vb", "+", "1", "\n", "bidx", "++", "\n", "if", "bidx", ">=", "len", "(", "ab", ")", "{", "break", "\n", "}", "\n", "vb", "=", "ab", "[", "bidx", "]", "\n", "}", "\n\n", "if", "start", "<=", "run", ".", "last", "{", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ":", "start", ",", "last", ":", "run", ".", "last", "}", ")", "\n", "}", "\n", "}", "\n", "output", ":=", "NewContainerRun", "(", "runs", ")", "\n", "output", ".", "optimize", "(", ")", "\n", "return", "output", "\n", "}" ]
// differenceRunArray subtracts the bits in an array container from a run // container.
[ "differenceRunArray", "subtracts", "the", "bits", "in", "an", "array", "container", "from", "a", "run", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3230-L3283
train
pilosa/pilosa
roaring/roaring.go
differenceRunBitmap
func differenceRunBitmap(a, b *Container) *Container { statsHit("difference/RunBitmap") ra := a.runs() // If a is full, difference is the flip of b. if len(ra) > 0 && ra[0].start == 0 && ra[0].last == 65535 { return flipBitmap(b) } output := NewContainerRun(nil) runs := output.runs() if len(ra) == 0 { return NewContainerRun(nil) } output.n = a.n for _, inputRun := range ra { run := inputRun add := true for bit := inputRun.start; bit <= inputRun.last; bit++ { if b.bitmapContains(bit) { output.n-- if run.start == bit { if bit == 65535 { //overflow add = false } run.start++ } else if bit == run.last { run.last-- } else { run.last = bit - 1 if run.last >= run.start { runs = append(runs, run) } run.start = bit + 1 run.last = inputRun.last } if run.start > run.last { break } } if bit == 65535 { //overflow break } } if run.start <= run.last { if add { runs = append(runs, run) } } } output.setRuns(runs) if output.n < ArrayMaxSize && int32(len(runs)) > output.n/2 { output.runToArray() } else if len(runs) > runMaxSize { output.runToBitmap() } return output }
go
func differenceRunBitmap(a, b *Container) *Container { statsHit("difference/RunBitmap") ra := a.runs() // If a is full, difference is the flip of b. if len(ra) > 0 && ra[0].start == 0 && ra[0].last == 65535 { return flipBitmap(b) } output := NewContainerRun(nil) runs := output.runs() if len(ra) == 0 { return NewContainerRun(nil) } output.n = a.n for _, inputRun := range ra { run := inputRun add := true for bit := inputRun.start; bit <= inputRun.last; bit++ { if b.bitmapContains(bit) { output.n-- if run.start == bit { if bit == 65535 { //overflow add = false } run.start++ } else if bit == run.last { run.last-- } else { run.last = bit - 1 if run.last >= run.start { runs = append(runs, run) } run.start = bit + 1 run.last = inputRun.last } if run.start > run.last { break } } if bit == 65535 { //overflow break } } if run.start <= run.last { if add { runs = append(runs, run) } } } output.setRuns(runs) if output.n < ArrayMaxSize && int32(len(runs)) > output.n/2 { output.runToArray() } else if len(runs) > runMaxSize { output.runToBitmap() } return output }
[ "func", "differenceRunBitmap", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "ra", ":=", "a", ".", "runs", "(", ")", "\n", "// If a is full, difference is the flip of b.", "if", "len", "(", "ra", ")", ">", "0", "&&", "ra", "[", "0", "]", ".", "start", "==", "0", "&&", "ra", "[", "0", "]", ".", "last", "==", "65535", "{", "return", "flipBitmap", "(", "b", ")", "\n", "}", "\n", "output", ":=", "NewContainerRun", "(", "nil", ")", "\n", "runs", ":=", "output", ".", "runs", "(", ")", "\n", "if", "len", "(", "ra", ")", "==", "0", "{", "return", "NewContainerRun", "(", "nil", ")", "\n", "}", "\n", "output", ".", "n", "=", "a", ".", "n", "\n", "for", "_", ",", "inputRun", ":=", "range", "ra", "{", "run", ":=", "inputRun", "\n", "add", ":=", "true", "\n", "for", "bit", ":=", "inputRun", ".", "start", ";", "bit", "<=", "inputRun", ".", "last", ";", "bit", "++", "{", "if", "b", ".", "bitmapContains", "(", "bit", ")", "{", "output", ".", "n", "--", "\n", "if", "run", ".", "start", "==", "bit", "{", "if", "bit", "==", "65535", "{", "//overflow", "add", "=", "false", "\n", "}", "\n\n", "run", ".", "start", "++", "\n", "}", "else", "if", "bit", "==", "run", ".", "last", "{", "run", ".", "last", "--", "\n", "}", "else", "{", "run", ".", "last", "=", "bit", "-", "1", "\n", "if", "run", ".", "last", ">=", "run", ".", "start", "{", "runs", "=", "append", "(", "runs", ",", "run", ")", "\n", "}", "\n", "run", ".", "start", "=", "bit", "+", "1", "\n", "run", ".", "last", "=", "inputRun", ".", "last", "\n", "}", "\n", "if", "run", ".", "start", ">", "run", ".", "last", "{", "break", "\n", "}", "\n", "}", "\n\n", "if", "bit", "==", "65535", "{", "//overflow", "break", "\n", "}", "\n", "}", "\n", "if", "run", ".", "start", "<=", "run", ".", "last", "{", "if", "add", "{", "runs", "=", "append", "(", "runs", ",", "run", ")", "\n", "}", "\n", "}", "\n", "}", "\n\n", "output", ".", "setRuns", "(", "runs", ")", "\n", "if", "output", ".", "n", "<", "ArrayMaxSize", "&&", "int32", "(", "len", "(", "runs", ")", ")", ">", "output", ".", "n", "/", "2", "{", "output", ".", "runToArray", "(", ")", "\n", "}", "else", "if", "len", "(", "runs", ")", ">", "runMaxSize", "{", "output", ".", "runToBitmap", "(", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// differenceRunBitmap computes the difference of an run from a bitmap.
[ "differenceRunBitmap", "computes", "the", "difference", "of", "an", "run", "from", "a", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3286-L3344
train
pilosa/pilosa
roaring/roaring.go
differenceRunRun
func differenceRunRun(a, b *Container) *Container { statsHit("difference/RunRun") if a.n == 0 || b.n == 0 { return a.Clone() } ra, rb := a.runs(), b.runs() apos := 0 // current a-run index bpos := 0 // current b-run index astart := ra[apos].start alast := ra[apos].last bstart := rb[bpos].start blast := rb[bpos].last alen := len(ra) blen := len(rb) runs := make([]interval16, 0, alen+blen) // TODO allocate max then truncate? or something else // cardinality upper bound: sum of number of runs // each B-run could split an A-run in two, up to len(b.runs) times for apos < alen && bpos < blen { switch { case alast < bstart: // current A-run entirely precedes current B-run: keep full A-run, advance to next A-run runs = append(runs, interval16{start: astart, last: alast}) apos++ if apos < alen { astart = ra[apos].start alast = ra[apos].last } case blast < astart: // current B-run entirely precedes current A-run: advance to next B-run bpos++ if bpos < blen { bstart = rb[bpos].start blast = rb[bpos].last } default: // overlap if astart < bstart { runs = append(runs, interval16{start: astart, last: bstart - 1}) } if alast > blast { astart = blast + 1 } else { apos++ if apos < alen { astart = ra[apos].start alast = ra[apos].last } } } } if apos < alen { runs = append(runs, interval16{start: astart, last: alast}) apos++ if apos < alen { runs = append(runs, ra[apos:]...) } } return NewContainerRun(runs) }
go
func differenceRunRun(a, b *Container) *Container { statsHit("difference/RunRun") if a.n == 0 || b.n == 0 { return a.Clone() } ra, rb := a.runs(), b.runs() apos := 0 // current a-run index bpos := 0 // current b-run index astart := ra[apos].start alast := ra[apos].last bstart := rb[bpos].start blast := rb[bpos].last alen := len(ra) blen := len(rb) runs := make([]interval16, 0, alen+blen) // TODO allocate max then truncate? or something else // cardinality upper bound: sum of number of runs // each B-run could split an A-run in two, up to len(b.runs) times for apos < alen && bpos < blen { switch { case alast < bstart: // current A-run entirely precedes current B-run: keep full A-run, advance to next A-run runs = append(runs, interval16{start: astart, last: alast}) apos++ if apos < alen { astart = ra[apos].start alast = ra[apos].last } case blast < astart: // current B-run entirely precedes current A-run: advance to next B-run bpos++ if bpos < blen { bstart = rb[bpos].start blast = rb[bpos].last } default: // overlap if astart < bstart { runs = append(runs, interval16{start: astart, last: bstart - 1}) } if alast > blast { astart = blast + 1 } else { apos++ if apos < alen { astart = ra[apos].start alast = ra[apos].last } } } } if apos < alen { runs = append(runs, interval16{start: astart, last: alast}) apos++ if apos < alen { runs = append(runs, ra[apos:]...) } } return NewContainerRun(runs) }
[ "func", "differenceRunRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "if", "a", ".", "n", "==", "0", "||", "b", ".", "n", "==", "0", "{", "return", "a", ".", "Clone", "(", ")", "\n", "}", "\n\n", "ra", ",", "rb", ":=", "a", ".", "runs", "(", ")", ",", "b", ".", "runs", "(", ")", "\n", "apos", ":=", "0", "// current a-run index", "\n", "bpos", ":=", "0", "// current b-run index", "\n", "astart", ":=", "ra", "[", "apos", "]", ".", "start", "\n", "alast", ":=", "ra", "[", "apos", "]", ".", "last", "\n", "bstart", ":=", "rb", "[", "bpos", "]", ".", "start", "\n", "blast", ":=", "rb", "[", "bpos", "]", ".", "last", "\n", "alen", ":=", "len", "(", "ra", ")", "\n", "blen", ":=", "len", "(", "rb", ")", "\n\n", "runs", ":=", "make", "(", "[", "]", "interval16", ",", "0", ",", "alen", "+", "blen", ")", "// TODO allocate max then truncate? or something else", "\n", "// cardinality upper bound: sum of number of runs", "// each B-run could split an A-run in two, up to len(b.runs) times", "for", "apos", "<", "alen", "&&", "bpos", "<", "blen", "{", "switch", "{", "case", "alast", "<", "bstart", ":", "// current A-run entirely precedes current B-run: keep full A-run, advance to next A-run", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ":", "astart", ",", "last", ":", "alast", "}", ")", "\n", "apos", "++", "\n", "if", "apos", "<", "alen", "{", "astart", "=", "ra", "[", "apos", "]", ".", "start", "\n", "alast", "=", "ra", "[", "apos", "]", ".", "last", "\n", "}", "\n", "case", "blast", "<", "astart", ":", "// current B-run entirely precedes current A-run: advance to next B-run", "bpos", "++", "\n", "if", "bpos", "<", "blen", "{", "bstart", "=", "rb", "[", "bpos", "]", ".", "start", "\n", "blast", "=", "rb", "[", "bpos", "]", ".", "last", "\n", "}", "\n", "default", ":", "// overlap", "if", "astart", "<", "bstart", "{", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ":", "astart", ",", "last", ":", "bstart", "-", "1", "}", ")", "\n", "}", "\n", "if", "alast", ">", "blast", "{", "astart", "=", "blast", "+", "1", "\n", "}", "else", "{", "apos", "++", "\n", "if", "apos", "<", "alen", "{", "astart", "=", "ra", "[", "apos", "]", ".", "start", "\n", "alast", "=", "ra", "[", "apos", "]", ".", "last", "\n", "}", "\n", "}", "\n", "}", "\n", "}", "\n", "if", "apos", "<", "alen", "{", "runs", "=", "append", "(", "runs", ",", "interval16", "{", "start", ":", "astart", ",", "last", ":", "alast", "}", ")", "\n", "apos", "++", "\n", "if", "apos", "<", "alen", "{", "runs", "=", "append", "(", "runs", ",", "ra", "[", "apos", ":", "]", "...", ")", "\n", "}", "\n", "}", "\n", "return", "NewContainerRun", "(", "runs", ")", "\n", "}" ]
// differenceRunRun computes the difference of two runs.
[ "differenceRunRun", "computes", "the", "difference", "of", "two", "runs", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3347-L3408
train
pilosa/pilosa
roaring/roaring.go
apply
func (op *op) apply(b *Bitmap) (changed bool) { switch op.typ { case opTypeAdd: return b.DirectAdd(op.value) case opTypeRemove: return b.remove(op.value) case opTypeAddBatch: changed = b.DirectAddN(op.values...) > 0 case opTypeRemoveBatch: changed = b.DirectRemoveN(op.values...) > 0 default: panic(fmt.Sprintf("invalid op type: %d", op.typ)) } return changed }
go
func (op *op) apply(b *Bitmap) (changed bool) { switch op.typ { case opTypeAdd: return b.DirectAdd(op.value) case opTypeRemove: return b.remove(op.value) case opTypeAddBatch: changed = b.DirectAddN(op.values...) > 0 case opTypeRemoveBatch: changed = b.DirectRemoveN(op.values...) > 0 default: panic(fmt.Sprintf("invalid op type: %d", op.typ)) } return changed }
[ "func", "(", "op", "*", "op", ")", "apply", "(", "b", "*", "Bitmap", ")", "(", "changed", "bool", ")", "{", "switch", "op", ".", "typ", "{", "case", "opTypeAdd", ":", "return", "b", ".", "DirectAdd", "(", "op", ".", "value", ")", "\n", "case", "opTypeRemove", ":", "return", "b", ".", "remove", "(", "op", ".", "value", ")", "\n", "case", "opTypeAddBatch", ":", "changed", "=", "b", ".", "DirectAddN", "(", "op", ".", "values", "...", ")", ">", "0", "\n", "case", "opTypeRemoveBatch", ":", "changed", "=", "b", ".", "DirectRemoveN", "(", "op", ".", "values", "...", ")", ">", "0", "\n", "default", ":", "panic", "(", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "op", ".", "typ", ")", ")", "\n", "}", "\n", "return", "changed", "\n", "}" ]
// apply executes the operation against a bitmap.
[ "apply", "executes", "the", "operation", "against", "a", "bitmap", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3666-L3680
train
pilosa/pilosa
roaring/roaring.go
WriteTo
func (op *op) WriteTo(w io.Writer) (n int64, err error) { buf := make([]byte, op.size()) // Write type and value. buf[0] = byte(op.typ) if op.typ <= 1 { binary.LittleEndian.PutUint64(buf[1:9], op.value) } else { binary.LittleEndian.PutUint64(buf[1:9], uint64(len(op.values))) p := 13 // start of values (skip 4 for checksum) for _, v := range op.values { binary.LittleEndian.PutUint64(buf[p:p+8], v) p += 8 } } // Add checksum at the end. h := fnv.New32a() _, _ = h.Write(buf[0:9]) _, _ = h.Write(buf[13:]) binary.LittleEndian.PutUint32(buf[9:13], h.Sum32()) // Write to writer. nn, err := w.Write(buf) return int64(nn), err }
go
func (op *op) WriteTo(w io.Writer) (n int64, err error) { buf := make([]byte, op.size()) // Write type and value. buf[0] = byte(op.typ) if op.typ <= 1 { binary.LittleEndian.PutUint64(buf[1:9], op.value) } else { binary.LittleEndian.PutUint64(buf[1:9], uint64(len(op.values))) p := 13 // start of values (skip 4 for checksum) for _, v := range op.values { binary.LittleEndian.PutUint64(buf[p:p+8], v) p += 8 } } // Add checksum at the end. h := fnv.New32a() _, _ = h.Write(buf[0:9]) _, _ = h.Write(buf[13:]) binary.LittleEndian.PutUint32(buf[9:13], h.Sum32()) // Write to writer. nn, err := w.Write(buf) return int64(nn), err }
[ "func", "(", "op", "*", "op", ")", "WriteTo", "(", "w", "io", ".", "Writer", ")", "(", "n", "int64", ",", "err", "error", ")", "{", "buf", ":=", "make", "(", "[", "]", "byte", ",", "op", ".", "size", "(", ")", ")", "\n\n", "// Write type and value.", "buf", "[", "0", "]", "=", "byte", "(", "op", ".", "typ", ")", "\n", "if", "op", ".", "typ", "<=", "1", "{", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "buf", "[", "1", ":", "9", "]", ",", "op", ".", "value", ")", "\n", "}", "else", "{", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "buf", "[", "1", ":", "9", "]", ",", "uint64", "(", "len", "(", "op", ".", "values", ")", ")", ")", "\n", "p", ":=", "13", "// start of values (skip 4 for checksum)", "\n", "for", "_", ",", "v", ":=", "range", "op", ".", "values", "{", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "buf", "[", "p", ":", "p", "+", "8", "]", ",", "v", ")", "\n", "p", "+=", "8", "\n", "}", "\n", "}", "\n\n", "// Add checksum at the end.", "h", ":=", "fnv", ".", "New32a", "(", ")", "\n", "_", ",", "_", "=", "h", ".", "Write", "(", "buf", "[", "0", ":", "9", "]", ")", "\n", "_", ",", "_", "=", "h", ".", "Write", "(", "buf", "[", "13", ":", "]", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "buf", "[", "9", ":", "13", "]", ",", "h", ".", "Sum32", "(", ")", ")", "\n\n", "// Write to writer.", "nn", ",", "err", ":=", "w", ".", "Write", "(", "buf", ")", "\n", "return", "int64", "(", "nn", ")", ",", "err", "\n", "}" ]
// WriteTo writes op to the w.
[ "WriteTo", "writes", "op", "to", "the", "w", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3683-L3708
train
pilosa/pilosa
roaring/roaring.go
UnmarshalBinary
func (op *op) UnmarshalBinary(data []byte) error { if len(data) < minOpSize { return fmt.Errorf("op data out of bounds: len=%d", len(data)) } statsHit("op/UnmarshalBinary") op.typ = opType(data[0]) // op.value will actually contain the length of values for batch ops op.value = binary.LittleEndian.Uint64(data[1:9]) // Verify checksum. h := fnv.New32a() _, _ = h.Write(data[0:9]) if op.typ > 1 { if len(data) < int(13+op.value*8) { return fmt.Errorf("op data truncated - expected %d, got %d", 13+op.value*8, len(data)) } _, _ = h.Write(data[13 : 13+op.value*8]) op.values = make([]uint64, op.value) for i := uint64(0); i < op.value; i++ { start := 13 + i*8 op.values[i] = binary.LittleEndian.Uint64(data[start : start+8]) } op.value = 0 } if chk := binary.LittleEndian.Uint32(data[9:13]); chk != h.Sum32() { return fmt.Errorf("checksum mismatch: exp=%08x, got=%08x", h.Sum32(), chk) } return nil }
go
func (op *op) UnmarshalBinary(data []byte) error { if len(data) < minOpSize { return fmt.Errorf("op data out of bounds: len=%d", len(data)) } statsHit("op/UnmarshalBinary") op.typ = opType(data[0]) // op.value will actually contain the length of values for batch ops op.value = binary.LittleEndian.Uint64(data[1:9]) // Verify checksum. h := fnv.New32a() _, _ = h.Write(data[0:9]) if op.typ > 1 { if len(data) < int(13+op.value*8) { return fmt.Errorf("op data truncated - expected %d, got %d", 13+op.value*8, len(data)) } _, _ = h.Write(data[13 : 13+op.value*8]) op.values = make([]uint64, op.value) for i := uint64(0); i < op.value; i++ { start := 13 + i*8 op.values[i] = binary.LittleEndian.Uint64(data[start : start+8]) } op.value = 0 } if chk := binary.LittleEndian.Uint32(data[9:13]); chk != h.Sum32() { return fmt.Errorf("checksum mismatch: exp=%08x, got=%08x", h.Sum32(), chk) } return nil }
[ "func", "(", "op", "*", "op", ")", "UnmarshalBinary", "(", "data", "[", "]", "byte", ")", "error", "{", "if", "len", "(", "data", ")", "<", "minOpSize", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "len", "(", "data", ")", ")", "\n", "}", "\n", "statsHit", "(", "\"", "\"", ")", "\n\n", "op", ".", "typ", "=", "opType", "(", "data", "[", "0", "]", ")", "\n", "// op.value will actually contain the length of values for batch ops", "op", ".", "value", "=", "binary", ".", "LittleEndian", ".", "Uint64", "(", "data", "[", "1", ":", "9", "]", ")", "\n\n", "// Verify checksum.", "h", ":=", "fnv", ".", "New32a", "(", ")", "\n", "_", ",", "_", "=", "h", ".", "Write", "(", "data", "[", "0", ":", "9", "]", ")", "\n\n", "if", "op", ".", "typ", ">", "1", "{", "if", "len", "(", "data", ")", "<", "int", "(", "13", "+", "op", ".", "value", "*", "8", ")", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "13", "+", "op", ".", "value", "*", "8", ",", "len", "(", "data", ")", ")", "\n", "}", "\n", "_", ",", "_", "=", "h", ".", "Write", "(", "data", "[", "13", ":", "13", "+", "op", ".", "value", "*", "8", "]", ")", "\n", "op", ".", "values", "=", "make", "(", "[", "]", "uint64", ",", "op", ".", "value", ")", "\n", "for", "i", ":=", "uint64", "(", "0", ")", ";", "i", "<", "op", ".", "value", ";", "i", "++", "{", "start", ":=", "13", "+", "i", "*", "8", "\n", "op", ".", "values", "[", "i", "]", "=", "binary", ".", "LittleEndian", ".", "Uint64", "(", "data", "[", "start", ":", "start", "+", "8", "]", ")", "\n", "}", "\n", "op", ".", "value", "=", "0", "\n", "}", "\n", "if", "chk", ":=", "binary", ".", "LittleEndian", ".", "Uint32", "(", "data", "[", "9", ":", "13", "]", ")", ";", "chk", "!=", "h", ".", "Sum32", "(", ")", "{", "return", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "h", ".", "Sum32", "(", ")", ",", "chk", ")", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// UnmarshalBinary decodes data into an op.
[ "UnmarshalBinary", "decodes", "data", "into", "an", "op", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3713-L3744
train
pilosa/pilosa
roaring/roaring.go
size
func (op *op) size() int { if op.typ == opTypeAdd || op.typ == opTypeRemove { return 1 + 8 + 4 } return 1 + 8 + 4 + len(op.values)*8 }
go
func (op *op) size() int { if op.typ == opTypeAdd || op.typ == opTypeRemove { return 1 + 8 + 4 } return 1 + 8 + 4 + len(op.values)*8 }
[ "func", "(", "op", "*", "op", ")", "size", "(", ")", "int", "{", "if", "op", ".", "typ", "==", "opTypeAdd", "||", "op", ".", "typ", "==", "opTypeRemove", "{", "return", "1", "+", "8", "+", "4", "\n", "}", "\n", "return", "1", "+", "8", "+", "4", "+", "len", "(", "op", ".", "values", ")", "*", "8", "\n", "}" ]
// size returns the encoded size of the op, in bytes.
[ "size", "returns", "the", "encoded", "size", "of", "the", "op", "in", "bytes", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3747-L3752
train
pilosa/pilosa
roaring/roaring.go
count
func (op *op) count() int { switch op.typ { case 0, 1: return 1 case 2, 3: return len(op.values) default: panic(fmt.Sprintf("unknown operation type: %d", op.typ)) } }
go
func (op *op) count() int { switch op.typ { case 0, 1: return 1 case 2, 3: return len(op.values) default: panic(fmt.Sprintf("unknown operation type: %d", op.typ)) } }
[ "func", "(", "op", "*", "op", ")", "count", "(", ")", "int", "{", "switch", "op", ".", "typ", "{", "case", "0", ",", "1", ":", "return", "1", "\n", "case", "2", ",", "3", ":", "return", "len", "(", "op", ".", "values", ")", "\n", "default", ":", "panic", "(", "fmt", ".", "Sprintf", "(", "\"", "\"", ",", "op", ".", "typ", ")", ")", "\n", "}", "\n", "}" ]
// count returns the number of bits the operation mutates.
[ "count", "returns", "the", "number", "of", "bits", "the", "operation", "mutates", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3755-L3764
train
pilosa/pilosa
roaring/roaring.go
search32
func search32(a []uint16, value uint16) int32 { statsHit("search32") // Optimize for elements and the last element. n := int32(len(a)) if n == 0 { return -1 } else if a[n-1] == value { return n - 1 } // Otherwise perform binary search for exact match. lo, hi := int32(0), n-1 for lo+16 <= hi { i := int32(uint((lo + hi)) >> 1) v := a[i] if v < value { lo = i + 1 } else if v > value { hi = i - 1 } else { return i } } // If an exact match isn't found then return a negative index. for ; lo <= hi; lo++ { v := a[lo] if v == value { return lo } else if v > value { break } } return -(lo + 1) }
go
func search32(a []uint16, value uint16) int32 { statsHit("search32") // Optimize for elements and the last element. n := int32(len(a)) if n == 0 { return -1 } else if a[n-1] == value { return n - 1 } // Otherwise perform binary search for exact match. lo, hi := int32(0), n-1 for lo+16 <= hi { i := int32(uint((lo + hi)) >> 1) v := a[i] if v < value { lo = i + 1 } else if v > value { hi = i - 1 } else { return i } } // If an exact match isn't found then return a negative index. for ; lo <= hi; lo++ { v := a[lo] if v == value { return lo } else if v > value { break } } return -(lo + 1) }
[ "func", "search32", "(", "a", "[", "]", "uint16", ",", "value", "uint16", ")", "int32", "{", "statsHit", "(", "\"", "\"", ")", "\n", "// Optimize for elements and the last element.", "n", ":=", "int32", "(", "len", "(", "a", ")", ")", "\n", "if", "n", "==", "0", "{", "return", "-", "1", "\n", "}", "else", "if", "a", "[", "n", "-", "1", "]", "==", "value", "{", "return", "n", "-", "1", "\n", "}", "\n\n", "// Otherwise perform binary search for exact match.", "lo", ",", "hi", ":=", "int32", "(", "0", ")", ",", "n", "-", "1", "\n", "for", "lo", "+", "16", "<=", "hi", "{", "i", ":=", "int32", "(", "uint", "(", "(", "lo", "+", "hi", ")", ")", ">>", "1", ")", "\n", "v", ":=", "a", "[", "i", "]", "\n\n", "if", "v", "<", "value", "{", "lo", "=", "i", "+", "1", "\n", "}", "else", "if", "v", ">", "value", "{", "hi", "=", "i", "-", "1", "\n", "}", "else", "{", "return", "i", "\n", "}", "\n", "}", "\n\n", "// If an exact match isn't found then return a negative index.", "for", ";", "lo", "<=", "hi", ";", "lo", "++", "{", "v", ":=", "a", "[", "lo", "]", "\n", "if", "v", "==", "value", "{", "return", "lo", "\n", "}", "else", "if", "v", ">", "value", "{", "break", "\n", "}", "\n", "}", "\n", "return", "-", "(", "lo", "+", "1", ")", "\n", "}" ]
// search32 returns the index of value in a. If value is not found, it works the // same way as search64.
[ "search32", "returns", "the", "index", "of", "value", "in", "a", ".", "If", "value", "is", "not", "found", "it", "works", "the", "same", "way", "as", "search64", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3771-L3806
train
pilosa/pilosa
roaring/roaring.go
Append
func (a *ErrorList) Append(err error) { switch err := err.(type) { case ErrorList: *a = append(*a, err...) default: *a = append(*a, err) } }
go
func (a *ErrorList) Append(err error) { switch err := err.(type) { case ErrorList: *a = append(*a, err...) default: *a = append(*a, err) } }
[ "func", "(", "a", "*", "ErrorList", ")", "Append", "(", "err", "error", ")", "{", "switch", "err", ":=", "err", ".", "(", "type", ")", "{", "case", "ErrorList", ":", "*", "a", "=", "append", "(", "*", "a", ",", "err", "...", ")", "\n", "default", ":", "*", "a", "=", "append", "(", "*", "a", ",", "err", ")", "\n", "}", "\n", "}" ]
// Append appends an error to the list. If err is an ErrorList then all errors are appended.
[ "Append", "appends", "an", "error", "to", "the", "list", ".", "If", "err", "is", "an", "ErrorList", "then", "all", "errors", "are", "appended", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3872-L3879
train
pilosa/pilosa
roaring/roaring.go
AppendWithPrefix
func (a *ErrorList) AppendWithPrefix(err error, prefix string) { switch err := err.(type) { case ErrorList: for i := range err { *a = append(*a, fmt.Errorf("%s%s", prefix, err[i])) } default: *a = append(*a, fmt.Errorf("%s%s", prefix, err)) } }
go
func (a *ErrorList) AppendWithPrefix(err error, prefix string) { switch err := err.(type) { case ErrorList: for i := range err { *a = append(*a, fmt.Errorf("%s%s", prefix, err[i])) } default: *a = append(*a, fmt.Errorf("%s%s", prefix, err)) } }
[ "func", "(", "a", "*", "ErrorList", ")", "AppendWithPrefix", "(", "err", "error", ",", "prefix", "string", ")", "{", "switch", "err", ":=", "err", ".", "(", "type", ")", "{", "case", "ErrorList", ":", "for", "i", ":=", "range", "err", "{", "*", "a", "=", "append", "(", "*", "a", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "prefix", ",", "err", "[", "i", "]", ")", ")", "\n", "}", "\n", "default", ":", "*", "a", "=", "append", "(", "*", "a", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "prefix", ",", "err", ")", ")", "\n", "}", "\n", "}" ]
// AppendWithPrefix appends an error to the list and includes a prefix.
[ "AppendWithPrefix", "appends", "an", "error", "to", "the", "list", "and", "includes", "a", "prefix", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3882-L3891
train
pilosa/pilosa
roaring/roaring.go
xorArrayRun
func xorArrayRun(a, b *Container) *Container { statsHit("xor/ArrayRun") output := NewContainerRun(nil) aa, rb := a.array(), b.runs() na, nb := len(aa), len(rb) var vb interval16 var va uint16 lastI, lastJ := -1, -1 for i, j := 0, 0; i < na || j < nb; { if i < na && i != lastI { va = aa[i] } if j < nb && j != lastJ { vb = rb[j] } lastI = i lastJ = j if i < na && (j >= nb || va < vb.start) { //before output.n += output.runAppendInterval(interval16{start: va, last: va}) i++ } else if j < nb && (i >= na || va > vb.last) { //after output.n += output.runAppendInterval(vb) j++ } else if va > vb.start { if va < vb.last { output.n += output.runAppendInterval(interval16{start: vb.start, last: va - 1}) i++ vb.start = va + 1 if vb.start > vb.last { j++ } } else if va > vb.last { output.n += output.runAppendInterval(vb) j++ } else { // va == vb.last vb.last-- if vb.start <= vb.last { output.n += output.runAppendInterval(vb) } j++ i++ } } else { // we know va == vb.start if vb.start == maxContainerVal { // protect overflow j++ } else { vb.start++ if vb.start > vb.last { j++ } } i++ } } if output.n < ArrayMaxSize { output.runToArray() } else if len(output.runs()) > runMaxSize { output.runToBitmap() } return output }
go
func xorArrayRun(a, b *Container) *Container { statsHit("xor/ArrayRun") output := NewContainerRun(nil) aa, rb := a.array(), b.runs() na, nb := len(aa), len(rb) var vb interval16 var va uint16 lastI, lastJ := -1, -1 for i, j := 0, 0; i < na || j < nb; { if i < na && i != lastI { va = aa[i] } if j < nb && j != lastJ { vb = rb[j] } lastI = i lastJ = j if i < na && (j >= nb || va < vb.start) { //before output.n += output.runAppendInterval(interval16{start: va, last: va}) i++ } else if j < nb && (i >= na || va > vb.last) { //after output.n += output.runAppendInterval(vb) j++ } else if va > vb.start { if va < vb.last { output.n += output.runAppendInterval(interval16{start: vb.start, last: va - 1}) i++ vb.start = va + 1 if vb.start > vb.last { j++ } } else if va > vb.last { output.n += output.runAppendInterval(vb) j++ } else { // va == vb.last vb.last-- if vb.start <= vb.last { output.n += output.runAppendInterval(vb) } j++ i++ } } else { // we know va == vb.start if vb.start == maxContainerVal { // protect overflow j++ } else { vb.start++ if vb.start > vb.last { j++ } } i++ } } if output.n < ArrayMaxSize { output.runToArray() } else if len(output.runs()) > runMaxSize { output.runToBitmap() } return output }
[ "func", "xorArrayRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "output", ":=", "NewContainerRun", "(", "nil", ")", "\n", "aa", ",", "rb", ":=", "a", ".", "array", "(", ")", ",", "b", ".", "runs", "(", ")", "\n", "na", ",", "nb", ":=", "len", "(", "aa", ")", ",", "len", "(", "rb", ")", "\n", "var", "vb", "interval16", "\n", "var", "va", "uint16", "\n", "lastI", ",", "lastJ", ":=", "-", "1", ",", "-", "1", "\n", "for", "i", ",", "j", ":=", "0", ",", "0", ";", "i", "<", "na", "||", "j", "<", "nb", ";", "{", "if", "i", "<", "na", "&&", "i", "!=", "lastI", "{", "va", "=", "aa", "[", "i", "]", "\n", "}", "\n", "if", "j", "<", "nb", "&&", "j", "!=", "lastJ", "{", "vb", "=", "rb", "[", "j", "]", "\n", "}", "\n", "lastI", "=", "i", "\n", "lastJ", "=", "j", "\n\n", "if", "i", "<", "na", "&&", "(", "j", ">=", "nb", "||", "va", "<", "vb", ".", "start", ")", "{", "//before", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "interval16", "{", "start", ":", "va", ",", "last", ":", "va", "}", ")", "\n", "i", "++", "\n", "}", "else", "if", "j", "<", "nb", "&&", "(", "i", ">=", "na", "||", "va", ">", "vb", ".", "last", ")", "{", "//after", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "vb", ")", "\n", "j", "++", "\n", "}", "else", "if", "va", ">", "vb", ".", "start", "{", "if", "va", "<", "vb", ".", "last", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "interval16", "{", "start", ":", "vb", ".", "start", ",", "last", ":", "va", "-", "1", "}", ")", "\n", "i", "++", "\n", "vb", ".", "start", "=", "va", "+", "1", "\n\n", "if", "vb", ".", "start", ">", "vb", ".", "last", "{", "j", "++", "\n", "}", "\n", "}", "else", "if", "va", ">", "vb", ".", "last", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "vb", ")", "\n", "j", "++", "\n", "}", "else", "{", "// va == vb.last", "vb", ".", "last", "--", "\n", "if", "vb", ".", "start", "<=", "vb", ".", "last", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "vb", ")", "\n", "}", "\n", "j", "++", "\n", "i", "++", "\n", "}", "\n\n", "}", "else", "{", "// we know va == vb.start", "if", "vb", ".", "start", "==", "maxContainerVal", "{", "// protect overflow", "j", "++", "\n", "}", "else", "{", "vb", ".", "start", "++", "\n", "if", "vb", ".", "start", ">", "vb", ".", "last", "{", "j", "++", "\n", "}", "\n", "}", "\n", "i", "++", "\n", "}", "\n", "}", "\n", "if", "output", ".", "n", "<", "ArrayMaxSize", "{", "output", ".", "runToArray", "(", ")", "\n", "}", "else", "if", "len", "(", "output", ".", "runs", "(", ")", ")", ">", "runMaxSize", "{", "output", ".", "runToBitmap", "(", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// xorArrayRun computes the exclusive or of an array and a run container.
[ "xorArrayRun", "computes", "the", "exclusive", "or", "of", "an", "array", "and", "a", "run", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L3894-L3957
train
pilosa/pilosa
roaring/roaring.go
xorRunRun
func xorRunRun(a, b *Container) *Container { statsHit("xor/RunRun") ra, rb := a.runs(), b.runs() na, nb := len(ra), len(rb) if na == 0 { return b.Clone() } if nb == 0 { return a.Clone() } output := NewContainerRun(nil) lastI, lastJ := -1, -1 state := &xorstm{} for i, j := 0, 0; i < na || j < nb; { if i < na && lastI != i { state.va = ra[i] state.vaValid = true } if j < nb && lastJ != j { state.vb = rb[j] state.vbValid = true } lastI, lastJ = i, j r1, ok := xorCompare(state) if ok { output.n += output.runAppendInterval(r1) } if !state.vaValid { i++ } if !state.vbValid { j++ } } l := len(output.runs()) if output.n < ArrayMaxSize && int32(l) > output.n/2 { output.runToArray() } else if l > runMaxSize { output.runToBitmap() } return output }
go
func xorRunRun(a, b *Container) *Container { statsHit("xor/RunRun") ra, rb := a.runs(), b.runs() na, nb := len(ra), len(rb) if na == 0 { return b.Clone() } if nb == 0 { return a.Clone() } output := NewContainerRun(nil) lastI, lastJ := -1, -1 state := &xorstm{} for i, j := 0, 0; i < na || j < nb; { if i < na && lastI != i { state.va = ra[i] state.vaValid = true } if j < nb && lastJ != j { state.vb = rb[j] state.vbValid = true } lastI, lastJ = i, j r1, ok := xorCompare(state) if ok { output.n += output.runAppendInterval(r1) } if !state.vaValid { i++ } if !state.vbValid { j++ } } l := len(output.runs()) if output.n < ArrayMaxSize && int32(l) > output.n/2 { output.runToArray() } else if l > runMaxSize { output.runToBitmap() } return output }
[ "func", "xorRunRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "ra", ",", "rb", ":=", "a", ".", "runs", "(", ")", ",", "b", ".", "runs", "(", ")", "\n", "na", ",", "nb", ":=", "len", "(", "ra", ")", ",", "len", "(", "rb", ")", "\n", "if", "na", "==", "0", "{", "return", "b", ".", "Clone", "(", ")", "\n", "}", "\n", "if", "nb", "==", "0", "{", "return", "a", ".", "Clone", "(", ")", "\n", "}", "\n", "output", ":=", "NewContainerRun", "(", "nil", ")", "\n\n", "lastI", ",", "lastJ", ":=", "-", "1", ",", "-", "1", "\n\n", "state", ":=", "&", "xorstm", "{", "}", "\n\n", "for", "i", ",", "j", ":=", "0", ",", "0", ";", "i", "<", "na", "||", "j", "<", "nb", ";", "{", "if", "i", "<", "na", "&&", "lastI", "!=", "i", "{", "state", ".", "va", "=", "ra", "[", "i", "]", "\n", "state", ".", "vaValid", "=", "true", "\n", "}", "\n\n", "if", "j", "<", "nb", "&&", "lastJ", "!=", "j", "{", "state", ".", "vb", "=", "rb", "[", "j", "]", "\n", "state", ".", "vbValid", "=", "true", "\n", "}", "\n", "lastI", ",", "lastJ", "=", "i", ",", "j", "\n\n", "r1", ",", "ok", ":=", "xorCompare", "(", "state", ")", "\n", "if", "ok", "{", "output", ".", "n", "+=", "output", ".", "runAppendInterval", "(", "r1", ")", "\n", "}", "\n", "if", "!", "state", ".", "vaValid", "{", "i", "++", "\n", "}", "\n", "if", "!", "state", ".", "vbValid", "{", "j", "++", "\n", "}", "\n\n", "}", "\n\n", "l", ":=", "len", "(", "output", ".", "runs", "(", ")", ")", "\n", "if", "output", ".", "n", "<", "ArrayMaxSize", "&&", "int32", "(", "l", ")", ">", "output", ".", "n", "/", "2", "{", "output", ".", "runToArray", "(", ")", "\n", "}", "else", "if", "l", ">", "runMaxSize", "{", "output", ".", "runToBitmap", "(", ")", "\n", "}", "\n", "return", "output", "\n", "}" ]
// xorRunRun computes the exclusive or of two run containers.
[ "xorRunRun", "computes", "the", "exclusive", "or", "of", "two", "run", "containers", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L4054-L4102
train
pilosa/pilosa
roaring/roaring.go
xorBitmapRun
func xorBitmapRun(a, b *Container) *Container { statsHit("xor/BitmapRun") output := a.Clone() for _, run := range b.runs() { output.bitmapXorRange(uint64(run.start), uint64(run.last)+1) } return output }
go
func xorBitmapRun(a, b *Container) *Container { statsHit("xor/BitmapRun") output := a.Clone() for _, run := range b.runs() { output.bitmapXorRange(uint64(run.start), uint64(run.last)+1) } return output }
[ "func", "xorBitmapRun", "(", "a", ",", "b", "*", "Container", ")", "*", "Container", "{", "statsHit", "(", "\"", "\"", ")", "\n", "output", ":=", "a", ".", "Clone", "(", ")", "\n\n", "for", "_", ",", "run", ":=", "range", "b", ".", "runs", "(", ")", "{", "output", ".", "bitmapXorRange", "(", "uint64", "(", "run", ".", "start", ")", ",", "uint64", "(", "run", ".", "last", ")", "+", "1", ")", "\n", "}", "\n\n", "return", "output", "\n", "}" ]
// xorRunRun computes the exclusive or of a bitmap and a run container.
[ "xorRunRun", "computes", "the", "exclusive", "or", "of", "a", "bitmap", "and", "a", "run", "container", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L4105-L4114
train
pilosa/pilosa
roaring/roaring.go
UnmarshalBinary
func (b *Bitmap) UnmarshalBinary(data []byte) error { if data == nil { // Nothing to unmarshal return nil } statsHit("Bitmap/UnmarshalBinary") b.opN = 0 // reset opN since we're reading new data. fileMagic := uint32(binary.LittleEndian.Uint16(data[0:2])) if fileMagic == MagicNumber { // if pilosa roaring return errors.Wrap(b.unmarshalPilosaRoaring(data), "unmarshaling as pilosa roaring") } keyN, containerTyper, header, pos, haveRuns, err := readOfficialHeader(data) if err != nil { return errors.Wrap(err, "reading roaring header") } b.Containers.Reset() // Descriptive header section: Read container keys and cardinalities. for i, buf := uint(0), data[header:]; i < uint(keyN); i, buf = i+1, buf[4:] { card := int(binary.LittleEndian.Uint16(buf[2:4])) + 1 b.Containers.PutContainerValues( uint64(binary.LittleEndian.Uint16(buf[0:2])), containerTyper(i, card), /// container type voodo with isRunBitmap card, true) } // Read container offsets and attach data. if haveRuns { readWithRuns(b, data, pos, keyN) } else { err := readOffsets(b, data, pos, keyN) if err != nil { return errors.Wrap(err, "reading offsets from official roaring format") } } return nil }
go
func (b *Bitmap) UnmarshalBinary(data []byte) error { if data == nil { // Nothing to unmarshal return nil } statsHit("Bitmap/UnmarshalBinary") b.opN = 0 // reset opN since we're reading new data. fileMagic := uint32(binary.LittleEndian.Uint16(data[0:2])) if fileMagic == MagicNumber { // if pilosa roaring return errors.Wrap(b.unmarshalPilosaRoaring(data), "unmarshaling as pilosa roaring") } keyN, containerTyper, header, pos, haveRuns, err := readOfficialHeader(data) if err != nil { return errors.Wrap(err, "reading roaring header") } b.Containers.Reset() // Descriptive header section: Read container keys and cardinalities. for i, buf := uint(0), data[header:]; i < uint(keyN); i, buf = i+1, buf[4:] { card := int(binary.LittleEndian.Uint16(buf[2:4])) + 1 b.Containers.PutContainerValues( uint64(binary.LittleEndian.Uint16(buf[0:2])), containerTyper(i, card), /// container type voodo with isRunBitmap card, true) } // Read container offsets and attach data. if haveRuns { readWithRuns(b, data, pos, keyN) } else { err := readOffsets(b, data, pos, keyN) if err != nil { return errors.Wrap(err, "reading offsets from official roaring format") } } return nil }
[ "func", "(", "b", "*", "Bitmap", ")", "UnmarshalBinary", "(", "data", "[", "]", "byte", ")", "error", "{", "if", "data", "==", "nil", "{", "// Nothing to unmarshal", "return", "nil", "\n", "}", "\n", "statsHit", "(", "\"", "\"", ")", "\n", "b", ".", "opN", "=", "0", "// reset opN since we're reading new data.", "\n", "fileMagic", ":=", "uint32", "(", "binary", ".", "LittleEndian", ".", "Uint16", "(", "data", "[", "0", ":", "2", "]", ")", ")", "\n", "if", "fileMagic", "==", "MagicNumber", "{", "// if pilosa roaring", "return", "errors", ".", "Wrap", "(", "b", ".", "unmarshalPilosaRoaring", "(", "data", ")", ",", "\"", "\"", ")", "\n", "}", "\n\n", "keyN", ",", "containerTyper", ",", "header", ",", "pos", ",", "haveRuns", ",", "err", ":=", "readOfficialHeader", "(", "data", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "b", ".", "Containers", ".", "Reset", "(", ")", "\n", "// Descriptive header section: Read container keys and cardinalities.", "for", "i", ",", "buf", ":=", "uint", "(", "0", ")", ",", "data", "[", "header", ":", "]", ";", "i", "<", "uint", "(", "keyN", ")", ";", "i", ",", "buf", "=", "i", "+", "1", ",", "buf", "[", "4", ":", "]", "{", "card", ":=", "int", "(", "binary", ".", "LittleEndian", ".", "Uint16", "(", "buf", "[", "2", ":", "4", "]", ")", ")", "+", "1", "\n", "b", ".", "Containers", ".", "PutContainerValues", "(", "uint64", "(", "binary", ".", "LittleEndian", ".", "Uint16", "(", "buf", "[", "0", ":", "2", "]", ")", ")", ",", "containerTyper", "(", "i", ",", "card", ")", ",", "/// container type voodo with isRunBitmap", "card", ",", "true", ")", "\n", "}", "\n\n", "// Read container offsets and attach data.", "if", "haveRuns", "{", "readWithRuns", "(", "b", ",", "data", ",", "pos", ",", "keyN", ")", "\n", "}", "else", "{", "err", ":=", "readOffsets", "(", "b", ",", "data", ",", "pos", ",", "keyN", ")", "\n", "if", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// UnmarshalBinary decodes b from a binary-encoded byte slice. data can be in // either official roaring format or Pilosa's roaring format.
[ "UnmarshalBinary", "decodes", "b", "from", "a", "binary", "-", "encoded", "byte", "slice", ".", "data", "can", "be", "in", "either", "official", "roaring", "format", "or", "Pilosa", "s", "roaring", "format", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L4231-L4269
train
pilosa/pilosa
roaring/roaring.go
markItersWithKeyAsHandled
func (w handledIters) markItersWithKeyAsHandled(startIdx int, key uint64) { for i := startIdx; i < len(w); i++ { wrapped := w[i] currKey, _ := wrapped.iter.Value() if currKey == key { w[i].handled = true } } }
go
func (w handledIters) markItersWithKeyAsHandled(startIdx int, key uint64) { for i := startIdx; i < len(w); i++ { wrapped := w[i] currKey, _ := wrapped.iter.Value() if currKey == key { w[i].handled = true } } }
[ "func", "(", "w", "handledIters", ")", "markItersWithKeyAsHandled", "(", "startIdx", "int", ",", "key", "uint64", ")", "{", "for", "i", ":=", "startIdx", ";", "i", "<", "len", "(", "w", ")", ";", "i", "++", "{", "wrapped", ":=", "w", "[", "i", "]", "\n", "currKey", ",", "_", ":=", "wrapped", ".", "iter", ".", "Value", "(", ")", "\n", "if", "currKey", "==", "key", "{", "w", "[", "i", "]", ".", "handled", "=", "true", "\n", "}", "\n", "}", "\n", "}" ]
// Check all the iters from startIdx and up to see whether their next // key is the given key; if it is, mark them as handled.
[ "Check", "all", "the", "iters", "from", "startIdx", "and", "up", "to", "see", "whether", "their", "next", "key", "is", "the", "given", "key", ";", "if", "it", "is", "mark", "them", "as", "handled", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/roaring.go#L4349-L4357
train
pilosa/pilosa
roaring/btree.go
Seek
func (t *tree) Seek(k uint64) (e *enumerator, ok bool) { q := t.r if q == nil { e = btEPool.get(nil, false, 0, k, nil, t, t.ver) return } for { var i int if i, ok = t.find(q, k); ok { switch x := q.(type) { case *x: q = x.x[i+1].ch continue case *d: return btEPool.get(nil, ok, i, k, x, t, t.ver), true } } switch x := q.(type) { case *x: q = x.x[i].ch case *d: return btEPool.get(nil, ok, i, k, x, t, t.ver), false } } }
go
func (t *tree) Seek(k uint64) (e *enumerator, ok bool) { q := t.r if q == nil { e = btEPool.get(nil, false, 0, k, nil, t, t.ver) return } for { var i int if i, ok = t.find(q, k); ok { switch x := q.(type) { case *x: q = x.x[i+1].ch continue case *d: return btEPool.get(nil, ok, i, k, x, t, t.ver), true } } switch x := q.(type) { case *x: q = x.x[i].ch case *d: return btEPool.get(nil, ok, i, k, x, t, t.ver), false } } }
[ "func", "(", "t", "*", "tree", ")", "Seek", "(", "k", "uint64", ")", "(", "e", "*", "enumerator", ",", "ok", "bool", ")", "{", "q", ":=", "t", ".", "r", "\n", "if", "q", "==", "nil", "{", "e", "=", "btEPool", ".", "get", "(", "nil", ",", "false", ",", "0", ",", "k", ",", "nil", ",", "t", ",", "t", ".", "ver", ")", "\n", "return", "\n", "}", "\n\n", "for", "{", "var", "i", "int", "\n", "if", "i", ",", "ok", "=", "t", ".", "find", "(", "q", ",", "k", ")", ";", "ok", "{", "switch", "x", ":=", "q", ".", "(", "type", ")", "{", "case", "*", "x", ":", "q", "=", "x", ".", "x", "[", "i", "+", "1", "]", ".", "ch", "\n", "continue", "\n", "case", "*", "d", ":", "return", "btEPool", ".", "get", "(", "nil", ",", "ok", ",", "i", ",", "k", ",", "x", ",", "t", ",", "t", ".", "ver", ")", ",", "true", "\n", "}", "\n", "}", "\n\n", "switch", "x", ":=", "q", ".", "(", "type", ")", "{", "case", "*", "x", ":", "q", "=", "x", ".", "x", "[", "i", "]", ".", "ch", "\n", "case", "*", "d", ":", "return", "btEPool", ".", "get", "(", "nil", ",", "ok", ",", "i", ",", "k", ",", "x", ",", "t", ",", "t", ".", "ver", ")", ",", "false", "\n", "}", "\n", "}", "\n", "}" ]
// Seek returns an Enumerator positioned on an item such that k >= item's key. // ok reports if k == item.key The Enumerator's position is possibly after the // last item in the tree.
[ "Seek", "returns", "an", "Enumerator", "positioned", "on", "an", "item", "such", "that", "k", ">", "=", "item", "s", "key", ".", "ok", "reports", "if", "k", "==", "item", ".", "key", "The", "Enumerator", "s", "position", "is", "possibly", "after", "the", "last", "item", "in", "the", "tree", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/btree.go#L517-L543
train
pilosa/pilosa
roaring/btree.go
Prev
func (e *enumerator) Prev() (k uint64, v *Container, err error) { if err = e.err; err != nil { return 0, nil, err } if e.ver != e.t.ver { f, _ := e.t.Seek(e.k) *e = *f f.Close() } if e.q == nil { e.err, err = io.EOF, io.EOF return 0, nil, err } if !e.hit { // move to previous because Seek overshoots if there's no hit if err = e.prev(); err != nil { return 0, nil, err } } if e.i >= e.q.c { if err = e.prev(); err != nil { return 0, nil, err } } i := e.q.d[e.i] k, v = i.k, i.v e.k, e.hit = k, true // Any error returned would be stashed in e.err, and would come up // on the next call. _ = e.prev() return k, v, err }
go
func (e *enumerator) Prev() (k uint64, v *Container, err error) { if err = e.err; err != nil { return 0, nil, err } if e.ver != e.t.ver { f, _ := e.t.Seek(e.k) *e = *f f.Close() } if e.q == nil { e.err, err = io.EOF, io.EOF return 0, nil, err } if !e.hit { // move to previous because Seek overshoots if there's no hit if err = e.prev(); err != nil { return 0, nil, err } } if e.i >= e.q.c { if err = e.prev(); err != nil { return 0, nil, err } } i := e.q.d[e.i] k, v = i.k, i.v e.k, e.hit = k, true // Any error returned would be stashed in e.err, and would come up // on the next call. _ = e.prev() return k, v, err }
[ "func", "(", "e", "*", "enumerator", ")", "Prev", "(", ")", "(", "k", "uint64", ",", "v", "*", "Container", ",", "err", "error", ")", "{", "if", "err", "=", "e", ".", "err", ";", "err", "!=", "nil", "{", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n\n", "if", "e", ".", "ver", "!=", "e", ".", "t", ".", "ver", "{", "f", ",", "_", ":=", "e", ".", "t", ".", "Seek", "(", "e", ".", "k", ")", "\n", "*", "e", "=", "*", "f", "\n", "f", ".", "Close", "(", ")", "\n", "}", "\n", "if", "e", ".", "q", "==", "nil", "{", "e", ".", "err", ",", "err", "=", "io", ".", "EOF", ",", "io", ".", "EOF", "\n", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n\n", "if", "!", "e", ".", "hit", "{", "// move to previous because Seek overshoots if there's no hit", "if", "err", "=", "e", ".", "prev", "(", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "}", "\n\n", "if", "e", ".", "i", ">=", "e", ".", "q", ".", "c", "{", "if", "err", "=", "e", ".", "prev", "(", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "}", "\n\n", "i", ":=", "e", ".", "q", ".", "d", "[", "e", ".", "i", "]", "\n", "k", ",", "v", "=", "i", ".", "k", ",", "i", ".", "v", "\n", "e", ".", "k", ",", "e", ".", "hit", "=", "k", ",", "true", "\n", "// Any error returned would be stashed in e.err, and would come up", "// on the next call.", "_", "=", "e", ".", "prev", "(", ")", "\n", "return", "k", ",", "v", ",", "err", "\n", "}" ]
// Prev returns the currently enumerated item, if it exists and moves to the // previous item in the key collation order. If there is no item to return, err // == io.EOF is returned.
[ "Prev", "returns", "the", "currently", "enumerated", "item", "if", "it", "exists", "and", "moves", "to", "the", "previous", "item", "in", "the", "key", "collation", "order", ".", "If", "there", "is", "no", "item", "to", "return", "err", "==", "io", ".", "EOF", "is", "returned", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/roaring/btree.go#L899-L934
train
pilosa/pilosa
translate.go
OptTranslateFileMapSize
func OptTranslateFileMapSize(mapSize int) TranslateFileOption { return func(f *TranslateFile) error { f.mapSize = mapSize return nil } }
go
func OptTranslateFileMapSize(mapSize int) TranslateFileOption { return func(f *TranslateFile) error { f.mapSize = mapSize return nil } }
[ "func", "OptTranslateFileMapSize", "(", "mapSize", "int", ")", "TranslateFileOption", "{", "return", "func", "(", "f", "*", "TranslateFile", ")", "error", "{", "f", ".", "mapSize", "=", "mapSize", "\n", "return", "nil", "\n", "}", "\n", "}" ]
// OptTranslateFileMapSize is a functional option on TranslateFile // used to set the map size.
[ "OptTranslateFileMapSize", "is", "a", "functional", "option", "on", "TranslateFile", "used", "to", "set", "the", "map", "size", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L106-L111
train
pilosa/pilosa
translate.go
OptTranslateFileLogger
func OptTranslateFileLogger(l logger.Logger) TranslateFileOption { return func(s *TranslateFile) error { s.logger = l return nil } }
go
func OptTranslateFileLogger(l logger.Logger) TranslateFileOption { return func(s *TranslateFile) error { s.logger = l return nil } }
[ "func", "OptTranslateFileLogger", "(", "l", "logger", ".", "Logger", ")", "TranslateFileOption", "{", "return", "func", "(", "s", "*", "TranslateFile", ")", "error", "{", "s", ".", "logger", "=", "l", "\n", "return", "nil", "\n", "}", "\n", "}" ]
// OptTranslateFileLogger is a functional option on TranslateFile // used to set the file logger.
[ "OptTranslateFileLogger", "is", "a", "functional", "option", "on", "TranslateFile", "used", "to", "set", "the", "file", "logger", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L115-L120
train
pilosa/pilosa
translate.go
NewTranslateFile
func NewTranslateFile(opts ...TranslateFileOption) *TranslateFile { var defaultMapSize64 int64 = 10 * (1 << 30) var defaultMapSize int if ^uint(0)>>32 > 0 { // 10GB default map size defaultMapSize = int(defaultMapSize64) } else { // Use 2GB default map size on 32-bit systems defaultMapSize = (1 << 31) - 1 } f := &TranslateFile{ writeNotify: make(chan struct{}), closing: make(chan struct{}), cols: make(map[string]*index), rows: make(map[fieldKey]*index), mapSize: defaultMapSize, logger: logger.NopLogger, replicationClosing: make(chan struct{}), primaryStoreEvents: make(chan primaryStoreEvent), replicationRetryInterval: defaultReplicationRetryInterval, } for _, opt := range opts { err := opt(f) if err != nil { // TODO (2.0): Change func signature to return error panic(errors.Wrap(err, "applying option")) } } return f }
go
func NewTranslateFile(opts ...TranslateFileOption) *TranslateFile { var defaultMapSize64 int64 = 10 * (1 << 30) var defaultMapSize int if ^uint(0)>>32 > 0 { // 10GB default map size defaultMapSize = int(defaultMapSize64) } else { // Use 2GB default map size on 32-bit systems defaultMapSize = (1 << 31) - 1 } f := &TranslateFile{ writeNotify: make(chan struct{}), closing: make(chan struct{}), cols: make(map[string]*index), rows: make(map[fieldKey]*index), mapSize: defaultMapSize, logger: logger.NopLogger, replicationClosing: make(chan struct{}), primaryStoreEvents: make(chan primaryStoreEvent), replicationRetryInterval: defaultReplicationRetryInterval, } for _, opt := range opts { err := opt(f) if err != nil { // TODO (2.0): Change func signature to return error panic(errors.Wrap(err, "applying option")) } } return f }
[ "func", "NewTranslateFile", "(", "opts", "...", "TranslateFileOption", ")", "*", "TranslateFile", "{", "var", "defaultMapSize64", "int64", "=", "10", "*", "(", "1", "<<", "30", ")", "\n", "var", "defaultMapSize", "int", "\n\n", "if", "^", "uint", "(", "0", ")", ">>", "32", ">", "0", "{", "// 10GB default map size", "defaultMapSize", "=", "int", "(", "defaultMapSize64", ")", "\n", "}", "else", "{", "// Use 2GB default map size on 32-bit systems", "defaultMapSize", "=", "(", "1", "<<", "31", ")", "-", "1", "\n", "}", "\n", "f", ":=", "&", "TranslateFile", "{", "writeNotify", ":", "make", "(", "chan", "struct", "{", "}", ")", ",", "closing", ":", "make", "(", "chan", "struct", "{", "}", ")", ",", "cols", ":", "make", "(", "map", "[", "string", "]", "*", "index", ")", ",", "rows", ":", "make", "(", "map", "[", "fieldKey", "]", "*", "index", ")", ",", "mapSize", ":", "defaultMapSize", ",", "logger", ":", "logger", ".", "NopLogger", ",", "replicationClosing", ":", "make", "(", "chan", "struct", "{", "}", ")", ",", "primaryStoreEvents", ":", "make", "(", "chan", "primaryStoreEvent", ")", ",", "replicationRetryInterval", ":", "defaultReplicationRetryInterval", ",", "}", "\n\n", "for", "_", ",", "opt", ":=", "range", "opts", "{", "err", ":=", "opt", "(", "f", ")", "\n", "if", "err", "!=", "nil", "{", "// TODO (2.0): Change func signature to return error", "panic", "(", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", ")", "\n", "}", "\n", "}", "\n\n", "return", "f", "\n", "}" ]
// NewTranslateFile returns a new instance of TranslateFile.
[ "NewTranslateFile", "returns", "a", "new", "instance", "of", "TranslateFile", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L123-L159
train
pilosa/pilosa
translate.go
Open
func (s *TranslateFile) Open() (err error) { // Open writer & buffered writer. if err := os.MkdirAll(filepath.Dir(s.Path), 0777); err != nil { return errors.Wrapf(err, "mkdir %s", filepath.Dir(s.Path)) } else if s.file, err = os.OpenFile(s.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { return errors.Wrapf(err, "open file %s", s.Path) } s.w = bufio.NewWriter(s.file) // Memory map data file. if s.data, err = syscall.Mmap(int(s.file.Fd()), 0, s.mapSize, syscall.PROT_READ, syscall.MAP_SHARED); err != nil { return errors.Wrapf(err, "creating Mmap (size: %d)", s.mapSize) } // Replay the log. if err := s.replayEntries(); err != nil { return errors.Wrap(err, "replaying log entries") } // Listen to primaryStoreEvents channel. s.wg.Add(1) go func() { defer s.wg.Done(); s.monitorPrimaryStoreEvents() }() return nil }
go
func (s *TranslateFile) Open() (err error) { // Open writer & buffered writer. if err := os.MkdirAll(filepath.Dir(s.Path), 0777); err != nil { return errors.Wrapf(err, "mkdir %s", filepath.Dir(s.Path)) } else if s.file, err = os.OpenFile(s.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil { return errors.Wrapf(err, "open file %s", s.Path) } s.w = bufio.NewWriter(s.file) // Memory map data file. if s.data, err = syscall.Mmap(int(s.file.Fd()), 0, s.mapSize, syscall.PROT_READ, syscall.MAP_SHARED); err != nil { return errors.Wrapf(err, "creating Mmap (size: %d)", s.mapSize) } // Replay the log. if err := s.replayEntries(); err != nil { return errors.Wrap(err, "replaying log entries") } // Listen to primaryStoreEvents channel. s.wg.Add(1) go func() { defer s.wg.Done(); s.monitorPrimaryStoreEvents() }() return nil }
[ "func", "(", "s", "*", "TranslateFile", ")", "Open", "(", ")", "(", "err", "error", ")", "{", "// Open writer & buffered writer.", "if", "err", ":=", "os", ".", "MkdirAll", "(", "filepath", ".", "Dir", "(", "s", ".", "Path", ")", ",", "0777", ")", ";", "err", "!=", "nil", "{", "return", "errors", ".", "Wrapf", "(", "err", ",", "\"", "\"", ",", "filepath", ".", "Dir", "(", "s", ".", "Path", ")", ")", "\n", "}", "else", "if", "s", ".", "file", ",", "err", "=", "os", ".", "OpenFile", "(", "s", ".", "Path", ",", "os", ".", "O_RDWR", "|", "os", ".", "O_CREATE", "|", "os", ".", "O_APPEND", ",", "0666", ")", ";", "err", "!=", "nil", "{", "return", "errors", ".", "Wrapf", "(", "err", ",", "\"", "\"", ",", "s", ".", "Path", ")", "\n", "}", "\n", "s", ".", "w", "=", "bufio", ".", "NewWriter", "(", "s", ".", "file", ")", "\n\n", "// Memory map data file.", "if", "s", ".", "data", ",", "err", "=", "syscall", ".", "Mmap", "(", "int", "(", "s", ".", "file", ".", "Fd", "(", ")", ")", ",", "0", ",", "s", ".", "mapSize", ",", "syscall", ".", "PROT_READ", ",", "syscall", ".", "MAP_SHARED", ")", ";", "err", "!=", "nil", "{", "return", "errors", ".", "Wrapf", "(", "err", ",", "\"", "\"", ",", "s", ".", "mapSize", ")", "\n", "}", "\n\n", "// Replay the log.", "if", "err", ":=", "s", ".", "replayEntries", "(", ")", ";", "err", "!=", "nil", "{", "return", "errors", ".", "Wrap", "(", "err", ",", "\"", "\"", ")", "\n", "}", "\n\n", "// Listen to primaryStoreEvents channel.", "s", ".", "wg", ".", "Add", "(", "1", ")", "\n", "go", "func", "(", ")", "{", "defer", "s", ".", "wg", ".", "Done", "(", ")", ";", "s", ".", "monitorPrimaryStoreEvents", "(", ")", "}", "(", ")", "\n\n", "return", "nil", "\n", "}" ]
// Open opens the translate file.
[ "Open", "opens", "the", "translate", "file", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L162-L186
train
pilosa/pilosa
translate.go
handlePrimaryStoreEvent
func (s *TranslateFile) handlePrimaryStoreEvent(ev primaryStoreEvent) error { s.mu.Lock() defer s.mu.Unlock() if ev.id == s.primaryID { return nil } // Stop translate store replication. close(s.replicationClosing) s.repWG.Wait() // Set the primary node for translate store replication. s.logger.Debugf("set primary translate store to %s", ev.id) s.primaryID = ev.id if ev.id == "" { s.PrimaryTranslateStore = nil } else { s.PrimaryTranslateStore = ev.ts } // Start translate store replication. Stream from primary, if available. if s.PrimaryTranslateStore != nil { s.replicationClosing = make(chan struct{}) s.repWG.Add(1) go func() { defer s.repWG.Done(); s.monitorReplication() }() } return nil }
go
func (s *TranslateFile) handlePrimaryStoreEvent(ev primaryStoreEvent) error { s.mu.Lock() defer s.mu.Unlock() if ev.id == s.primaryID { return nil } // Stop translate store replication. close(s.replicationClosing) s.repWG.Wait() // Set the primary node for translate store replication. s.logger.Debugf("set primary translate store to %s", ev.id) s.primaryID = ev.id if ev.id == "" { s.PrimaryTranslateStore = nil } else { s.PrimaryTranslateStore = ev.ts } // Start translate store replication. Stream from primary, if available. if s.PrimaryTranslateStore != nil { s.replicationClosing = make(chan struct{}) s.repWG.Add(1) go func() { defer s.repWG.Done(); s.monitorReplication() }() } return nil }
[ "func", "(", "s", "*", "TranslateFile", ")", "handlePrimaryStoreEvent", "(", "ev", "primaryStoreEvent", ")", "error", "{", "s", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "s", ".", "mu", ".", "Unlock", "(", ")", "\n\n", "if", "ev", ".", "id", "==", "s", ".", "primaryID", "{", "return", "nil", "\n", "}", "\n\n", "// Stop translate store replication.", "close", "(", "s", ".", "replicationClosing", ")", "\n", "s", ".", "repWG", ".", "Wait", "(", ")", "\n\n", "// Set the primary node for translate store replication.", "s", ".", "logger", ".", "Debugf", "(", "\"", "\"", ",", "ev", ".", "id", ")", "\n", "s", ".", "primaryID", "=", "ev", ".", "id", "\n", "if", "ev", ".", "id", "==", "\"", "\"", "{", "s", ".", "PrimaryTranslateStore", "=", "nil", "\n", "}", "else", "{", "s", ".", "PrimaryTranslateStore", "=", "ev", ".", "ts", "\n", "}", "\n\n", "// Start translate store replication. Stream from primary, if available.", "if", "s", ".", "PrimaryTranslateStore", "!=", "nil", "{", "s", ".", "replicationClosing", "=", "make", "(", "chan", "struct", "{", "}", ")", "\n", "s", ".", "repWG", ".", "Add", "(", "1", ")", "\n", "go", "func", "(", ")", "{", "defer", "s", ".", "repWG", ".", "Done", "(", ")", ";", "s", ".", "monitorReplication", "(", ")", "}", "(", ")", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// handlePrimaryStoreEvent changes the PrimaryTranslateStore // used for replication by TranslateFile.
[ "handlePrimaryStoreEvent", "changes", "the", "PrimaryTranslateStore", "used", "for", "replication", "by", "TranslateFile", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L212-L241
train
pilosa/pilosa
translate.go
Close
func (s *TranslateFile) Close() (err error) { s.once.Do(func() { close(s.closing) if s.file != nil { if e := s.file.Close(); e != nil && err == nil { err = e } } if s.data != nil { if e := syscall.Munmap(s.data); e != nil && err == nil { err = e } } }) s.wg.Wait() return err }
go
func (s *TranslateFile) Close() (err error) { s.once.Do(func() { close(s.closing) if s.file != nil { if e := s.file.Close(); e != nil && err == nil { err = e } } if s.data != nil { if e := syscall.Munmap(s.data); e != nil && err == nil { err = e } } }) s.wg.Wait() return err }
[ "func", "(", "s", "*", "TranslateFile", ")", "Close", "(", ")", "(", "err", "error", ")", "{", "s", ".", "once", ".", "Do", "(", "func", "(", ")", "{", "close", "(", "s", ".", "closing", ")", "\n\n", "if", "s", ".", "file", "!=", "nil", "{", "if", "e", ":=", "s", ".", "file", ".", "Close", "(", ")", ";", "e", "!=", "nil", "&&", "err", "==", "nil", "{", "err", "=", "e", "\n", "}", "\n", "}", "\n", "if", "s", ".", "data", "!=", "nil", "{", "if", "e", ":=", "syscall", ".", "Munmap", "(", "s", ".", "data", ")", ";", "e", "!=", "nil", "&&", "err", "==", "nil", "{", "err", "=", "e", "\n", "}", "\n", "}", "\n", "}", ")", "\n", "s", ".", "wg", ".", "Wait", "(", ")", "\n", "return", "err", "\n", "}" ]
// Close closes the translate file.
[ "Close", "closes", "the", "translate", "file", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L244-L261
train
pilosa/pilosa
translate.go
size
func (s *TranslateFile) size() int64 { s.mu.RLock() n := s.n s.mu.RUnlock() return n }
go
func (s *TranslateFile) size() int64 { s.mu.RLock() n := s.n s.mu.RUnlock() return n }
[ "func", "(", "s", "*", "TranslateFile", ")", "size", "(", ")", "int64", "{", "s", ".", "mu", ".", "RLock", "(", ")", "\n", "n", ":=", "s", ".", "n", "\n", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "n", "\n", "}" ]
// size returns the number of bytes in use in the data file.
[ "size", "returns", "the", "number", "of", "bytes", "in", "use", "in", "the", "data", "file", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L269-L274
train
pilosa/pilosa
translate.go
WriteNotify
func (s *TranslateFile) WriteNotify() <-chan struct{} { s.mu.RLock() ch := s.writeNotify s.mu.RUnlock() return ch }
go
func (s *TranslateFile) WriteNotify() <-chan struct{} { s.mu.RLock() ch := s.writeNotify s.mu.RUnlock() return ch }
[ "func", "(", "s", "*", "TranslateFile", ")", "WriteNotify", "(", ")", "<-", "chan", "struct", "{", "}", "{", "s", ".", "mu", ".", "RLock", "(", ")", "\n", "ch", ":=", "s", ".", "writeNotify", "\n", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "ch", "\n", "}" ]
// WriteNotify returns a channel that is closed when a new entry is written.
[ "WriteNotify", "returns", "a", "channel", "that", "is", "closed", "when", "a", "new", "entry", "is", "written", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L282-L287
train
pilosa/pilosa
translate.go
monitorReplication
func (s *TranslateFile) monitorReplication() { // Create context that will cancel on close. ctx, cancel := context.WithCancel(context.Background()) go func() { select { case <-s.closing: case <-s.replicationClosing: } cancel() }() // Keep attempting to replicate until the store closes. for { if err := s.replicate(ctx); err != nil { s.logger.Printf("pilosa: replication error: %s", err) } select { case <-ctx.Done(): return case <-time.After(s.replicationRetryInterval): s.logger.Printf("pilosa: reconnecting to primary replica") } } }
go
func (s *TranslateFile) monitorReplication() { // Create context that will cancel on close. ctx, cancel := context.WithCancel(context.Background()) go func() { select { case <-s.closing: case <-s.replicationClosing: } cancel() }() // Keep attempting to replicate until the store closes. for { if err := s.replicate(ctx); err != nil { s.logger.Printf("pilosa: replication error: %s", err) } select { case <-ctx.Done(): return case <-time.After(s.replicationRetryInterval): s.logger.Printf("pilosa: reconnecting to primary replica") } } }
[ "func", "(", "s", "*", "TranslateFile", ")", "monitorReplication", "(", ")", "{", "// Create context that will cancel on close.", "ctx", ",", "cancel", ":=", "context", ".", "WithCancel", "(", "context", ".", "Background", "(", ")", ")", "\n", "go", "func", "(", ")", "{", "select", "{", "case", "<-", "s", ".", "closing", ":", "case", "<-", "s", ".", "replicationClosing", ":", "}", "\n", "cancel", "(", ")", "\n", "}", "(", ")", "\n\n", "// Keep attempting to replicate until the store closes.", "for", "{", "if", "err", ":=", "s", ".", "replicate", "(", "ctx", ")", ";", "err", "!=", "nil", "{", "s", ".", "logger", ".", "Printf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n", "select", "{", "case", "<-", "ctx", ".", "Done", "(", ")", ":", "return", "\n", "case", "<-", "time", ".", "After", "(", "s", ".", "replicationRetryInterval", ")", ":", "s", ".", "logger", ".", "Printf", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "}" ]
// monitorReplication is executed in a separate goroutine and continually streams // from the primary store until this store is closed.
[ "monitorReplication", "is", "executed", "in", "a", "separate", "goroutine", "and", "continually", "streams", "from", "the", "primary", "store", "until", "this", "store", "is", "closed", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L382-L405
train
pilosa/pilosa
translate.go
monitorPrimaryStoreEvents
func (s *TranslateFile) monitorPrimaryStoreEvents() { // Keep handling events until the store closes. for { select { case <-s.closing: return case ev := <-s.primaryStoreEvents: if err := s.handlePrimaryStoreEvent(ev); err != nil { s.logger.Printf("handle primary store event") } } } }
go
func (s *TranslateFile) monitorPrimaryStoreEvents() { // Keep handling events until the store closes. for { select { case <-s.closing: return case ev := <-s.primaryStoreEvents: if err := s.handlePrimaryStoreEvent(ev); err != nil { s.logger.Printf("handle primary store event") } } } }
[ "func", "(", "s", "*", "TranslateFile", ")", "monitorPrimaryStoreEvents", "(", ")", "{", "// Keep handling events until the store closes.", "for", "{", "select", "{", "case", "<-", "s", ".", "closing", ":", "return", "\n", "case", "ev", ":=", "<-", "s", ".", "primaryStoreEvents", ":", "if", "err", ":=", "s", ".", "handlePrimaryStoreEvent", "(", "ev", ")", ";", "err", "!=", "nil", "{", "s", ".", "logger", ".", "Printf", "(", "\"", "\"", ")", "\n", "}", "\n", "}", "\n", "}", "\n", "}" ]
// monitorPrimaryStoreEvents is executed in a separate goroutine and listens for changes // to the primary store assignment.
[ "monitorPrimaryStoreEvents", "is", "executed", "in", "a", "separate", "goroutine", "and", "listens", "for", "changes", "to", "the", "primary", "store", "assignment", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L409-L421
train
pilosa/pilosa
translate.go
TranslateColumnsToUint64
func (s *TranslateFile) TranslateColumnsToUint64(index string, values []string) ([]uint64, error) { ret := make([]uint64, len(values)) // Read value under read lock. s.mu.RLock() if idx := s.cols[index]; idx != nil { var writeRequired bool for i := range values { v, ok := idx.idByKey([]byte(values[i])) if !ok { writeRequired = true } ret[i] = v } if !writeRequired { s.mu.RUnlock() return ret, nil } } s.mu.RUnlock() // Return error if not all values could be translated and this store is read-only. if s.isReadOnly() { return ret, ErrTranslateStoreReadOnly } // If any values not found then recheck and then add under a write lock. s.mu.Lock() defer s.mu.Unlock() // Recheck if value was created between the read lock and write lock. idx := s.cols[index] if idx != nil { var writeRequired bool for i := range values { if ret[i] != 0 { continue } v, ok := idx.idByKey([]byte(values[i])) if !ok { writeRequired = true continue } ret[i] = v } if !writeRequired { return ret, nil } } // Create index map if it doesn't exists. if idx == nil { idx = newIndex(s.data) s.cols[index] = idx } // Append new identifiers to log. entry := &LogEntry{ Type: LogEntryTypeInsertColumn, Index: []byte(index), IDs: make([]uint64, 0, len(values)), Keys: make([][]byte, 0, len(values)), } check := make(map[string]uint64) for i := range values { if ret[i] != 0 { continue } v, found := check[values[i]] if !found { idx.seq++ v = idx.seq check[values[i]] = v } ret[i] = v entry.IDs = append(entry.IDs, v) entry.Keys = append(entry.Keys, []byte(values[i])) } // Write entry. if err := s.appendEntry(entry); err != nil { return nil, err } return ret, nil }
go
func (s *TranslateFile) TranslateColumnsToUint64(index string, values []string) ([]uint64, error) { ret := make([]uint64, len(values)) // Read value under read lock. s.mu.RLock() if idx := s.cols[index]; idx != nil { var writeRequired bool for i := range values { v, ok := idx.idByKey([]byte(values[i])) if !ok { writeRequired = true } ret[i] = v } if !writeRequired { s.mu.RUnlock() return ret, nil } } s.mu.RUnlock() // Return error if not all values could be translated and this store is read-only. if s.isReadOnly() { return ret, ErrTranslateStoreReadOnly } // If any values not found then recheck and then add under a write lock. s.mu.Lock() defer s.mu.Unlock() // Recheck if value was created between the read lock and write lock. idx := s.cols[index] if idx != nil { var writeRequired bool for i := range values { if ret[i] != 0 { continue } v, ok := idx.idByKey([]byte(values[i])) if !ok { writeRequired = true continue } ret[i] = v } if !writeRequired { return ret, nil } } // Create index map if it doesn't exists. if idx == nil { idx = newIndex(s.data) s.cols[index] = idx } // Append new identifiers to log. entry := &LogEntry{ Type: LogEntryTypeInsertColumn, Index: []byte(index), IDs: make([]uint64, 0, len(values)), Keys: make([][]byte, 0, len(values)), } check := make(map[string]uint64) for i := range values { if ret[i] != 0 { continue } v, found := check[values[i]] if !found { idx.seq++ v = idx.seq check[values[i]] = v } ret[i] = v entry.IDs = append(entry.IDs, v) entry.Keys = append(entry.Keys, []byte(values[i])) } // Write entry. if err := s.appendEntry(entry); err != nil { return nil, err } return ret, nil }
[ "func", "(", "s", "*", "TranslateFile", ")", "TranslateColumnsToUint64", "(", "index", "string", ",", "values", "[", "]", "string", ")", "(", "[", "]", "uint64", ",", "error", ")", "{", "ret", ":=", "make", "(", "[", "]", "uint64", ",", "len", "(", "values", ")", ")", "\n\n", "// Read value under read lock.", "s", ".", "mu", ".", "RLock", "(", ")", "\n", "if", "idx", ":=", "s", ".", "cols", "[", "index", "]", ";", "idx", "!=", "nil", "{", "var", "writeRequired", "bool", "\n", "for", "i", ":=", "range", "values", "{", "v", ",", "ok", ":=", "idx", ".", "idByKey", "(", "[", "]", "byte", "(", "values", "[", "i", "]", ")", ")", "\n", "if", "!", "ok", "{", "writeRequired", "=", "true", "\n", "}", "\n", "ret", "[", "i", "]", "=", "v", "\n", "}", "\n", "if", "!", "writeRequired", "{", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "ret", ",", "nil", "\n", "}", "\n", "}", "\n", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n\n", "// Return error if not all values could be translated and this store is read-only.", "if", "s", ".", "isReadOnly", "(", ")", "{", "return", "ret", ",", "ErrTranslateStoreReadOnly", "\n", "}", "\n\n", "// If any values not found then recheck and then add under a write lock.", "s", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "s", ".", "mu", ".", "Unlock", "(", ")", "\n\n", "// Recheck if value was created between the read lock and write lock.", "idx", ":=", "s", ".", "cols", "[", "index", "]", "\n", "if", "idx", "!=", "nil", "{", "var", "writeRequired", "bool", "\n", "for", "i", ":=", "range", "values", "{", "if", "ret", "[", "i", "]", "!=", "0", "{", "continue", "\n", "}", "\n", "v", ",", "ok", ":=", "idx", ".", "idByKey", "(", "[", "]", "byte", "(", "values", "[", "i", "]", ")", ")", "\n", "if", "!", "ok", "{", "writeRequired", "=", "true", "\n", "continue", "\n", "}", "\n", "ret", "[", "i", "]", "=", "v", "\n", "}", "\n", "if", "!", "writeRequired", "{", "return", "ret", ",", "nil", "\n", "}", "\n", "}", "\n\n", "// Create index map if it doesn't exists.", "if", "idx", "==", "nil", "{", "idx", "=", "newIndex", "(", "s", ".", "data", ")", "\n", "s", ".", "cols", "[", "index", "]", "=", "idx", "\n", "}", "\n\n", "// Append new identifiers to log.", "entry", ":=", "&", "LogEntry", "{", "Type", ":", "LogEntryTypeInsertColumn", ",", "Index", ":", "[", "]", "byte", "(", "index", ")", ",", "IDs", ":", "make", "(", "[", "]", "uint64", ",", "0", ",", "len", "(", "values", ")", ")", ",", "Keys", ":", "make", "(", "[", "]", "[", "]", "byte", ",", "0", ",", "len", "(", "values", ")", ")", ",", "}", "\n\n", "check", ":=", "make", "(", "map", "[", "string", "]", "uint64", ")", "\n", "for", "i", ":=", "range", "values", "{", "if", "ret", "[", "i", "]", "!=", "0", "{", "continue", "\n", "}", "\n", "v", ",", "found", ":=", "check", "[", "values", "[", "i", "]", "]", "\n", "if", "!", "found", "{", "idx", ".", "seq", "++", "\n", "v", "=", "idx", ".", "seq", "\n", "check", "[", "values", "[", "i", "]", "]", "=", "v", "\n", "}", "\n\n", "ret", "[", "i", "]", "=", "v", "\n\n", "entry", ".", "IDs", "=", "append", "(", "entry", ".", "IDs", ",", "v", ")", "\n", "entry", ".", "Keys", "=", "append", "(", "entry", ".", "Keys", ",", "[", "]", "byte", "(", "values", "[", "i", "]", ")", ")", "\n", "}", "\n\n", "// Write entry.", "if", "err", ":=", "s", ".", "appendEntry", "(", "entry", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "ret", ",", "nil", "\n", "}" ]
// TranslateColumnsToUint64 converts values to a uint64 id. // If value does not have an associated id then one is created.
[ "TranslateColumnsToUint64", "converts", "values", "to", "a", "uint64", "id", ".", "If", "value", "does", "not", "have", "an", "associated", "id", "then", "one", "is", "created", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L496-L584
train
pilosa/pilosa
translate.go
TranslateRowToString
func (s *TranslateFile) TranslateRowToString(index, field string, id uint64) (string, error) { s.mu.RLock() if idx := s.rows[fieldKey{index, field}]; idx != nil { if ret, ok := idx.keyByID(id); ok { s.mu.RUnlock() return string(ret), nil } } s.mu.RUnlock() return "", nil }
go
func (s *TranslateFile) TranslateRowToString(index, field string, id uint64) (string, error) { s.mu.RLock() if idx := s.rows[fieldKey{index, field}]; idx != nil { if ret, ok := idx.keyByID(id); ok { s.mu.RUnlock() return string(ret), nil } } s.mu.RUnlock() return "", nil }
[ "func", "(", "s", "*", "TranslateFile", ")", "TranslateRowToString", "(", "index", ",", "field", "string", ",", "id", "uint64", ")", "(", "string", ",", "error", ")", "{", "s", ".", "mu", ".", "RLock", "(", ")", "\n", "if", "idx", ":=", "s", ".", "rows", "[", "fieldKey", "{", "index", ",", "field", "}", "]", ";", "idx", "!=", "nil", "{", "if", "ret", ",", "ok", ":=", "idx", ".", "keyByID", "(", "id", ")", ";", "ok", "{", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "string", "(", "ret", ")", ",", "nil", "\n", "}", "\n", "}", "\n", "s", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "\"", "\"", ",", "nil", "\n", "}" ]
// TranslateRowToString translates a row ID to a string key.
[ "TranslateRowToString", "translates", "a", "row", "ID", "to", "a", "string", "key", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L693-L703
train
pilosa/pilosa
translate.go
Reader
func (s *TranslateFile) Reader(ctx context.Context, offset int64) (io.ReadCloser, error) { rc := newTranslateFileReader(ctx, s, offset) if err := rc.Open(); err != nil { return nil, err } return rc, nil }
go
func (s *TranslateFile) Reader(ctx context.Context, offset int64) (io.ReadCloser, error) { rc := newTranslateFileReader(ctx, s, offset) if err := rc.Open(); err != nil { return nil, err } return rc, nil }
[ "func", "(", "s", "*", "TranslateFile", ")", "Reader", "(", "ctx", "context", ".", "Context", ",", "offset", "int64", ")", "(", "io", ".", "ReadCloser", ",", "error", ")", "{", "rc", ":=", "newTranslateFileReader", "(", "ctx", ",", "s", ",", "offset", ")", "\n", "if", "err", ":=", "rc", ".", "Open", "(", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "return", "rc", ",", "nil", "\n", "}" ]
// Reader returns a reader that streams the underlying data file.
[ "Reader", "returns", "a", "reader", "that", "streams", "the", "underlying", "data", "file", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L706-L712
train
pilosa/pilosa
translate.go
headerSize
func (e *LogEntry) headerSize() int64 { sz := uVarintSize(e.Length) + // total entry length 1 + // type uVarintSize(uint64(len(e.Index))) + len(e.Index) + // Index length and data uVarintSize(uint64(len(e.Field))) + len(e.Field) + // Field length and data uVarintSize(uint64(len(e.IDs))) // ID/Key pair count return int64(sz) }
go
func (e *LogEntry) headerSize() int64 { sz := uVarintSize(e.Length) + // total entry length 1 + // type uVarintSize(uint64(len(e.Index))) + len(e.Index) + // Index length and data uVarintSize(uint64(len(e.Field))) + len(e.Field) + // Field length and data uVarintSize(uint64(len(e.IDs))) // ID/Key pair count return int64(sz) }
[ "func", "(", "e", "*", "LogEntry", ")", "headerSize", "(", ")", "int64", "{", "sz", ":=", "uVarintSize", "(", "e", ".", "Length", ")", "+", "// total entry length", "1", "+", "// type", "uVarintSize", "(", "uint64", "(", "len", "(", "e", ".", "Index", ")", ")", ")", "+", "len", "(", "e", ".", "Index", ")", "+", "// Index length and data", "uVarintSize", "(", "uint64", "(", "len", "(", "e", ".", "Field", ")", ")", ")", "+", "len", "(", "e", ".", "Field", ")", "+", "// Field length and data", "uVarintSize", "(", "uint64", "(", "len", "(", "e", ".", "IDs", ")", ")", ")", "// ID/Key pair count", "\n", "return", "int64", "(", "sz", ")", "\n", "}" ]
// headerSize returns the number of bytes required for size, type, index, field, & pair count.
[ "headerSize", "returns", "the", "number", "of", "bytes", "required", "for", "size", "type", "index", "field", "&", "pair", "count", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L730-L737
train
pilosa/pilosa
translate.go
WriteTo
func (e *LogEntry) WriteTo(w io.Writer) (_ int64, err error) { var buf bytes.Buffer b := make([]byte, binary.MaxVarintLen64) // Write the entry type. if err := binary.Write(&buf, binary.BigEndian, e.Type); err != nil { return 0, err } // Write the index name. sz := binary.PutUvarint(b, uint64(len(e.Index))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Index); err != nil { return 0, err } // Write field name. sz = binary.PutUvarint(b, uint64(len(e.Field))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Field); err != nil { return 0, err } // Write key count. sz = binary.PutUvarint(b, uint64(len(e.IDs))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } // Write each id/key pairs. for i := range e.Keys { // Write identifier. sz = binary.PutUvarint(b, e.IDs[i]) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } // Write key. sz = binary.PutUvarint(b, uint64(len(e.Keys[i]))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Keys[i]); err != nil { return 0, err } } // Write buffer size. e.Length = uint64(buf.Len()) sz = binary.PutUvarint(b, e.Length) if n, err := w.Write(b[:sz]); err != nil { return int64(n), err } // Write buffer. n, err := buf.WriteTo(w) return int64(sz) + n, err }
go
func (e *LogEntry) WriteTo(w io.Writer) (_ int64, err error) { var buf bytes.Buffer b := make([]byte, binary.MaxVarintLen64) // Write the entry type. if err := binary.Write(&buf, binary.BigEndian, e.Type); err != nil { return 0, err } // Write the index name. sz := binary.PutUvarint(b, uint64(len(e.Index))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Index); err != nil { return 0, err } // Write field name. sz = binary.PutUvarint(b, uint64(len(e.Field))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Field); err != nil { return 0, err } // Write key count. sz = binary.PutUvarint(b, uint64(len(e.IDs))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } // Write each id/key pairs. for i := range e.Keys { // Write identifier. sz = binary.PutUvarint(b, e.IDs[i]) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } // Write key. sz = binary.PutUvarint(b, uint64(len(e.Keys[i]))) if _, err := buf.Write(b[:sz]); err != nil { return 0, err } else if _, err := buf.Write(e.Keys[i]); err != nil { return 0, err } } // Write buffer size. e.Length = uint64(buf.Len()) sz = binary.PutUvarint(b, e.Length) if n, err := w.Write(b[:sz]); err != nil { return int64(n), err } // Write buffer. n, err := buf.WriteTo(w) return int64(sz) + n, err }
[ "func", "(", "e", "*", "LogEntry", ")", "WriteTo", "(", "w", "io", ".", "Writer", ")", "(", "_", "int64", ",", "err", "error", ")", "{", "var", "buf", "bytes", ".", "Buffer", "\n", "b", ":=", "make", "(", "[", "]", "byte", ",", "binary", ".", "MaxVarintLen64", ")", "\n\n", "// Write the entry type.", "if", "err", ":=", "binary", ".", "Write", "(", "&", "buf", ",", "binary", ".", "BigEndian", ",", "e", ".", "Type", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n\n", "// Write the index name.", "sz", ":=", "binary", ".", "PutUvarint", "(", "b", ",", "uint64", "(", "len", "(", "e", ".", "Index", ")", ")", ")", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "else", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "e", ".", "Index", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n\n", "// Write field name.", "sz", "=", "binary", ".", "PutUvarint", "(", "b", ",", "uint64", "(", "len", "(", "e", ".", "Field", ")", ")", ")", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "else", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "e", ".", "Field", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n\n", "// Write key count.", "sz", "=", "binary", ".", "PutUvarint", "(", "b", ",", "uint64", "(", "len", "(", "e", ".", "IDs", ")", ")", ")", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n\n", "// Write each id/key pairs.", "for", "i", ":=", "range", "e", ".", "Keys", "{", "// Write identifier.", "sz", "=", "binary", ".", "PutUvarint", "(", "b", ",", "e", ".", "IDs", "[", "i", "]", ")", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n\n", "// Write key.", "sz", "=", "binary", ".", "PutUvarint", "(", "b", ",", "uint64", "(", "len", "(", "e", ".", "Keys", "[", "i", "]", ")", ")", ")", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "else", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "e", ".", "Keys", "[", "i", "]", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n", "}", "\n\n", "// Write buffer size.", "e", ".", "Length", "=", "uint64", "(", "buf", ".", "Len", "(", ")", ")", "\n", "sz", "=", "binary", ".", "PutUvarint", "(", "b", ",", "e", ".", "Length", ")", "\n", "if", "n", ",", "err", ":=", "w", ".", "Write", "(", "b", "[", ":", "sz", "]", ")", ";", "err", "!=", "nil", "{", "return", "int64", "(", "n", ")", ",", "err", "\n", "}", "\n\n", "// Write buffer.", "n", ",", "err", ":=", "buf", ".", "WriteTo", "(", "w", ")", "\n", "return", "int64", "(", "sz", ")", "+", "n", ",", "err", "\n", "}" ]
// WriteTo serializes a LogEntry to w.
[ "WriteTo", "serializes", "a", "LogEntry", "to", "w", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L817-L875
train
pilosa/pilosa
translate.go
validLogEntriesLen
func validLogEntriesLen(p []byte) (n int) { r := bytes.NewReader(p) for { if sz, err := binary.ReadUvarint(r); err != nil { return n } else if off, err := r.Seek(int64(sz), io.SeekCurrent); err != nil { return n } else if off > int64(len(p)) { return n } else { n = int(off) } } }
go
func validLogEntriesLen(p []byte) (n int) { r := bytes.NewReader(p) for { if sz, err := binary.ReadUvarint(r); err != nil { return n } else if off, err := r.Seek(int64(sz), io.SeekCurrent); err != nil { return n } else if off > int64(len(p)) { return n } else { n = int(off) } } }
[ "func", "validLogEntriesLen", "(", "p", "[", "]", "byte", ")", "(", "n", "int", ")", "{", "r", ":=", "bytes", ".", "NewReader", "(", "p", ")", "\n", "for", "{", "if", "sz", ",", "err", ":=", "binary", ".", "ReadUvarint", "(", "r", ")", ";", "err", "!=", "nil", "{", "return", "n", "\n", "}", "else", "if", "off", ",", "err", ":=", "r", ".", "Seek", "(", "int64", "(", "sz", ")", ",", "io", ".", "SeekCurrent", ")", ";", "err", "!=", "nil", "{", "return", "n", "\n", "}", "else", "if", "off", ">", "int64", "(", "len", "(", "p", ")", ")", "{", "return", "n", "\n", "}", "else", "{", "n", "=", "int", "(", "off", ")", "\n", "}", "\n", "}", "\n", "}" ]
// validLogEntriesLen returns the maximum length of p that contains valid entries.
[ "validLogEntriesLen", "returns", "the", "maximum", "length", "of", "p", "that", "contains", "valid", "entries", "." ]
67d53f6b48a43f7fe090f48e35518ca8d024747c
https://github.com/pilosa/pilosa/blob/67d53f6b48a43f7fe090f48e35518ca8d024747c/translate.go#L878-L891
train