# Path: twimp/vecbuf.py
# class VecBufEOB(Exception):
# class VecBuf(object):
#     def __init__(self, vecdata=None):
#     def __len__(self):
#     def write(self, data):
#     def write_seq(self, vecdata):
#     def left(self):
#     def _get(self, bytes):
#     def read(self, bytes):
#     def peek(self, bytes):
#     def read_seq(self, bytes):
#     def peek_seq(self, bytes):
#     def read_clone(self, bytes):
#     def _debug(self):
# def semiflatten(bufvec):
# def flatten(bufvec):


from collections import deque
from operator import itemgetter
from twisted.internet import protocol
from twisted.python import failure
from twimp import vecbuf
import logging
import twimp.log


class FrameSorter(object):
    def __init__(self, callback, keys):
        self._max = len(keys)
        assert self._max > 1, 'need at least two sources'
        self._queues = dict((k, [None, deque(), k]) for k in keys)
        self._active = 0

        self._callback = callback

        self._key_getter = itemgetter(0)
        self._grpos_getter = itemgetter(0)

    def add(self, key, grpos, data):
        sq = self._queues[key]
        if not sq[1]:
            self._active += 1
            sq[0] = grpos
        sq[1].append((grpos, data))
        self._check_send()

    def _check_send(self):
        if self._active < self._max:
            return

        while self._active >= self._max:
            first_q = min(self._queues.itervalues(), key=self._key_getter)
            key = first_q[2]
            second_q = min((q for (k, q) in self._queues.iteritems()

# Path: twimp/vecbuf.py
# class VecBufEOB(Exception):
# class VecBuf(object):
#     def __init__(self, vecdata=None):
#     def __len__(self):
#     def write(self, data):
#     def write_seq(self, vecdata):
#     def left(self):
#     def _get(self, bytes):
#     def read(self, bytes):
#     def peek(self, bytes):
#     def read_seq(self, bytes):
#     def peek_seq(self, bytes):
#     def read_clone(self, bytes):
#     def _debug(self):
# def semiflatten(bufvec):
# def flatten(bufvec):


# Path: twimp/utils.py
from collections import deque
from operator import itemgetter
from twisted.internet import protocol
from twisted.python import failure
from twimp import vecbuf
import logging
import twimp.log



class FrameSorter(object):
    def __init__(self, callback, keys):
        self._max = len(keys)
        assert self._max > 1, 'need at least two sources'
        self._queues = dict((k, [None, deque(), k]) for k in keys)
        self._active = 0

        self._callback = callback

        self._key_getter = itemgetter(0)
        self._grpos_getter = itemgetter(0)

    def add(self, key, grpos, data):
        sq = self._queues[key]
        if not sq[1]:
            self._active += 1
            sq[0] = grpos
        sq[1].append((grpos, data))
        self._check_send()

    def _check_send(self):
        if self._active < self._max:
            return

        while self._active >= self._max:
            first_q = min(self._queues.itervalues(), key=self._key_getter)
            key = first_q[2]
            second_q = min((q for (k, q) in self._queues.iteritems()

                            if k != key), key=self._key_getter)