Пример #1
0
// Builds a BFS tree (as a directed graph) from the given graph and start node.
func BFSTree(g graph.Graph, start graph.Node) *simple.DirectedGraph {
	if !g.Has(start) {
		panic(fmt.Sprintf("BFSTree: Start node %r not in graph %r", start, g))
	}

	ret := simple.NewDirectedGraph(0.0, math.Inf(1))
	seen := make(map[int]bool)
	q := queue.New()
	q.Add(start)
	ret.AddNode(simple.Node(start.ID()))

	for q.Length() > 0 {
		node := q.Peek().(graph.Node)
		q.Remove()
		for _, neighbor := range g.From(node) {
			if !seen[neighbor.ID()] {
				seen[neighbor.ID()] = true
				ret.AddNode(simple.Node(neighbor.ID()))
				ret.SetEdge(simple.Edge{F: simple.Node(node.ID()), T: simple.Node(neighbor.ID()), W: g.Edge(node, neighbor).Weight()})
				q.Add(neighbor)
			}
		}
	}

	return ret
}
Пример #2
0
//NewChannel spawns and returns a new channel sharing the underlying buffer.
func (buf *SharedBuffer) NewChannel() SimpleChannel {
	ch := &sharedBufferChannel{
		in:  make(chan interface{}),
		out: make(chan interface{}),
		buf: queue.New(),
	}
	buf.in <- ch
	return ch
}
Пример #3
0
func NewInfiniteChannel() *InfiniteChannel {
	ch := &InfiniteChannel{
		input:  make(chan interface{}),
		output: make(chan interface{}),
		length: make(chan int),
		buffer: queue.New(),
	}
	go ch.infiniteBuffer()
	return ch
}
Пример #4
0
func NewRingChannel(size BufferCap) *RingChannel {
	if size < 0 && size != Infinity {
		panic("channels: invalid negative size in NewRingChannel")
	}
	ch := &RingChannel{make(chan interface{}), make(chan interface{}), queue.New(), size}
	if size == None {
		go ch.overflowingDirect()
	} else {
		go ch.ringBuffer()
	}
	return ch
}
Пример #5
0
func NewResizableChannel() *ResizableChannel {
	ch := &ResizableChannel{
		input:    make(chan interface{}),
		output:   make(chan interface{}),
		length:   make(chan int),
		capacity: make(chan BufferCap),
		resize:   make(chan BufferCap),
		size:     1,
		buffer:   queue.New(),
	}
	go ch.magicBuffer()
	return ch
}
Пример #6
0
func NewFeedIndex(id string, uuid1 *uuid.UUID) *FeedIndex {
	iq := queue.New()
	index := &FeedIndex{
		Id:     id,
		Uuid:   uuid1,
		iq:     iq,
		bufq:   make([]string, MinQueue),
		itemCh: make(chan string, 1),
		doneCh: make(chan struct{}, 1),
		dirty:  false,
	}
	go index.Serve()
	return index
}
Пример #7
0
func NewOverflowingChannel(size BufferCap) *OverflowingChannel {
	if size < 0 && size != Infinity {
		panic("channels: invalid negative size in NewOverflowingChannel")
	}
	ch := &OverflowingChannel{
		input:  make(chan interface{}),
		output: make(chan interface{}),
		length: make(chan int),
		size:   size,
	}
	if size == None {
		go ch.overflowingDirect()
	} else {
		ch.buffer = queue.New()
		go ch.overflowingBuffer()
	}
	return ch
}
Пример #8
0
// singleton
// effectively a "bridge" between the flushers and the topicDispatcher in order to avoid deadlock
// based on https://godoc.org/github.com/eapache/channels#InfiniteChannel
func (p *asyncProducer) retryHandler() {
	var msg *ProducerMessage
	buf := queue.New()

	for {
		if buf.Length() == 0 {
			msg = <-p.retries
		} else {
			select {
			case msg = <-p.retries:
			case p.input <- buf.Peek().(*ProducerMessage):
				buf.Remove()
				continue
			}
		}

		if msg == nil {
			return
		}

		buf.Add(msg)
	}
}
Пример #9
0
func NewQHist() *QHist {
	return &QHist{
		hist: queue.New(),
	}
}
Пример #10
0
func NewInfiniteChannel() *InfiniteChannel {
	ch := &InfiniteChannel{make(chan interface{}), make(chan interface{}), queue.New()}
	go ch.infiniteBuffer()
	return ch
}
Пример #11
0
func NewResizableChannel() *ResizableChannel {
	ch := &ResizableChannel{make(chan interface{}), make(chan interface{}), make(chan BufferCap), 1, queue.New()}
	go ch.magicBuffer()
	return ch
}