text:
import collections
import functools
import itertools

import gdb
import gdb.printing
import gdb.types
import gdb.unwinder
import gdb.xmethod

#
# Pretty Printers
#

class FiberPrinter:
    """PrettyPrint a folly::fibers::Fiber"""

    def __init__(self, val):
        self.val = val

        state = self.val["state_"]
        d = gdb.types.make_enum_dict(state.type)
        d = dict((v, k) for k, v in d.items())
        self.state = d[int(state)]

    def state_to_string(self):
        if self.state == "folly::fibers::Fiber::INVALID":
            return "Invalid"
        if self.state == "folly::fibers::Fiber::NOT_STARTED":
            return "Not started"
        if self.state == "folly::fibers::Fiber::READY_TO_RUN":
            return "Ready to run"
        if self.state == "folly::fibers::Fiber::RUNNING":
            return "Running"
        if self.state == "folly::fibers::Fiber::AWAITING":
            return "Awaiting"
        if self.state == "folly::fibers::Fiber::AWAITING_IMMEDIATE":
            return "Awaiting immediate"
        if self.state == "folly::fibers::Fiber::YIELDED":
            return "Yielded"
        return "Unknown"

    def backtrace_available(self):
        return (
            self.state != "folly::fibers::Fiber::INVALID"
            and self.state != "folly::fibers::Fiber::NOT_STARTED"
            and self.state != "folly::fibers::Fiber::RUNNING"
        )

    def to_string(self):
        return 'folly::fibers::Fiber(state="{state}", backtrace={bt})'.format(
            state=self.state_to_string(), bt=self.backtrace_available()
        )

class FiberManagerPrinter:
    """PrettyPrint a folly::fibers::FiberManager"""

    def __init__(self, fm):
        self.fm = fm

    def children(self):
        def limit_with_dots(fibers_iterator):
            num_items = 0
            fiber_print_limit = gdb.parameter("fiber manager-print-limit")
            for fiber in fibers_iterator:
                if fiber_print_limit and num_items >= fiber_print_limit:
                    yield ("address", "...")
                    yield ("fiber", "...")
                    return
                yield ("address", str(fiber.address))
                yield ("fiber", fiber)
                num_items += 1

        return limit_with_dots(fiber_manager_active_fibers(self.fm))

    def to_string(self):
        return "folly::fibers::FiberManager"

    def display_hint(self):
        return "map"

#
# XMethods
#

class GetFiberXMethodWorker(gdb.xmethod.XMethodWorker):
    def get_arg_types(self):
        return gdb.lookup_type("int")

    def get_result_type(self):
        return gdb.lookup_type("int")

    def __call__(self, *args):
        fm = args[0]
        index = int(args[1])
        fiber = next(itertools.islice(fiber_manager_active_fibers(fm), index, None))
        if fiber is None:
            raise gdb.GdbError("Index out of range")
        else:
            return fiber

class GetFiberXMethodMatcher(gdb.xmethod.XMethodMatcher):
    def __init__(self):
        super(GetFiberXMethodMatcher, self).__init__("Fiber address method matcher")
        self.worker = GetFiberXMethodWorker()

    def match(self, class_type, method_name):
        if (
            class_type.name == "folly::fibers::FiberManager"
            and method_name == "get_fiber"
        ):
            return self.worker
        return None

class FiberXMethodWorker(gdb.xmethod.XMethodWorker):
    def get_arg_types(self):
        return None

    def get_result_type(self):
        return None

    def __call__(self, *args):
        return fiber_activate(args[0])

class FiberXMethodMatcher(gdb.xmethod.XMethodMatcher):
    def __init__(self):
        super(FiberXMethodMatcher, self).__init__("Fiber method matcher")
        self.worker = FiberXMethodWorker()

    def match(self, class_type, method_name):
        if class_type.name == "folly::fibers::Fiber" and method_name == "activate":
            return self.worker
        return None

#
# Unwinder
#

class FrameId(object):
    def __init__(self, sp, pc):
        self.sp = sp
        self.pc = pc

class FiberUnwinder(gdb.unwinder.Unwinder):
    instance = None

    @classmethod
    def init(cls):
        if cls.instance is None:
            cls.instance = FiberUnwinder()
            gdb.unwinder.register_unwinder(None, cls.instance)

    @classmethod
    def get_fiber(cls):
        cls.init()
        return cls.instance.fiber

    @classmethod
    def set_fiber(cls, fiber):
        cls.init()

        if not fiber:
            if not cls.instance.fiber:
                return "No active fiber."

        # get the relevant fiber's info, new one if specified, old one if not
        info = FiberInfo(fiber or cls.instance.fiber)

        if fiber:
            if not FiberPrinter(fiber).backtrace_available():
                return "Can not activate a non-waiting fiber."
            # set the unwinder to do the right thing
            cls.instance.fiber_context_ptr = fiber["fiberImpl_"]["fiberContext_"]
        else:
            # disable the unwinder
            cls.instance.fiber_context_ptr = None

        # track the active fiber (or lack thereof)
        cls.instance.fiber = fiber

        # Clear frame cache to make sure we actually use the right unwinder
        gdb.invalidate_cached_frames()

        return "[{action} fiber {id} ({info})]".format(
            action="Switching to" if fiber else "Deactivating", id=info.id, info=info
        )

    def __init__(self):
        super(FiberUnwinder, self).__init__("Fiber unwinder")
        self.fiber_context_ptr = None
        self.fiber = None

    def __call__(self, pending_frame):
        # We only unwind the first frame, bail if already done
        if not self.fiber_context_ptr:
            return None

        orig_rsp = pending_frame.read_register("rsp")
        orig_rip = pending_frame.read_register("rip")

        void_star_star = gdb.lookup_type("uint64_t").pointer()
        ptr = self.fiber_context_ptr.cast(void_star_star)

        # This code may need to be adjusted to newer versions of boost::context.
        #
        # The easiest way to get these offsets is to first attach to any
        # program which uses folly::fibers and add a break point in
        # boost::context::jump_fcontext. You then need to save information about
        # frame 1 via 'info frame 1' command.
        #
        # After that you need to resume program until fiber switch is complete
        # and expore the contents of saved fiber context via
        # 'x/16gx {fiber pointer}->fiberImpl_.fiberContext_' command.
        # You then need to match those to the following values you've previously
        # observed in the output of 'info frame 1'  command.
        #
        # Value found at "rbp at X" of 'info frame 1' output:
        rbp = (ptr + 6).dereference()
        # Value found at "rip = X" of 'info frame 1' output:
        rip = (ptr + 7).dereference()
        # Value found at "caller of frame at X" of 'info frame 1' output:
        rsp = rbp - 96

        # we play a horrible trick on gdb here, to make it unwind the fiber rather
        # than the stack of the currently selected frame.  Essentially, we take
        # whatever frame is currently newest, and lie to gdb saying that we are
        # the "next older" frame by creating fake unwind info that points to our
        # fiber's frame.  This means that frame #0 is actually from the currently
        # selected thread, so we filter that out when we inspect it elsewhere.
        frame_id = FrameId(orig_rsp, orig_rip)
        unwind_info = pending_frame.create_unwind_info(frame_id)
        unwind_info.add_saved_register("rbp", rbp)
        unwind_info.add_saved_register("rsp", rsp)
        unwind_info.add_saved_register("rip", rip)

        # "unregister" ourselves from further frame processing; we only need to
        # handle the top-level sentinel frame
        self.fiber_context_ptr = None

        return unwind_info

class FiberFrameFilter(object):
    """Frame filter for fiber stacks

    This class is used to "skip" past the innermost frame when parsing backtraces,
    which is actually from the currently selected thread.
    """

    def __init__(self):
        self.name = "fibers-skip-first-frame"
        self.priority = 100
        self.enabled = True
        gdb.frame_filters[self.name] = self

    def filter(self, frames):
        # skip first frame filter if a fiber is active
        if FiberUnwinder.get_fiber():
            next(frames)
        return frames

#
# Commands
#

# small utility to make sure a method always runs in a particular language mode
# and sets it back when donex
def use_language(lang):
    def wrapper(fn):
        @functools.wraps(fn)
        def wrapped(*args, **kwds):
            orig = gdb.parameter("language")
            if lang == orig:
                return fn(*args, **kwds)
            try:
                gdb.execute("set language " + lang)
                return fn(*args, **kwds)
            finally:
                gdb.execute("set language " + orig)

        return wrapped

    return wrapper

class FiberCommand(gdb.Command):
    """Use this command to switch between fibers.

    Due to gdb limitations, this will *override* the current thread, and you will
    need to run "fiber deactivate" in order to get normal thread processing back.
    """

    def __init__(self):
        super(FiberCommand, self).__init__("fiber", gdb.COMMAND_USER, prefix=True)

    @use_language("c++")
    def invoke(self, arg, from_tty):
        self.dont_repeat()
        argv = gdb.string_to_argv(arg)

        if not argv:
            if not FiberUnwinder.instance or not FiberUnwinder.instance.fiber:
                print("No fiber selected")
            else:
                info = FiberInfo(FiberUnwinder.instance.fiber)
                print("[Current fiber is {id} ({info})]".format(id=info.id, info=info))
            return

        # look up fiber
        fiber_ptr = get_fiber(arg)
        if not fiber_ptr:
            print("Invalid fiber id or address: " + arg)
            return

        # activate
        print(fiber_activate(fiber_ptr))

class FiberNameCommand(gdb.Command):
    """Set the current fiber's name.

    Usage: fiber name [NAME]
    If NAME is not given, then any existing name is removed"""

    def __init__(self):
        super(FiberNameCommand, self).__init__("fiber name", gdb.COMMAND_USER)

    def invoke(self, arg, from_tty):
        fiber = FiberUnwinder.get_fiber()
        if not fiber:
            print("No fiber activated.")
            return
        FiberInfo(fiber).name = arg or None

class FiberInfoCommand(gdb.Command):
    """info on fibers

    Can filter output with "M.F" or "M" format, e.g. "2.3" for manager 2 fiber 3
    or "1" for all fibers from manager 1. Can specify multiple times.

    See `help set fiber` for ways to alter the information printed.
    """

    def __init__(self):
        super(FiberInfoCommand, self).__init__(
            "info fibers", gdb.COMMAND_STATUS, prefix=True
        )

    def trace_info(self, fiber, skip=()):
        # get the fiber symbol info if we can
        if not FiberPrinter(fiber).backtrace_available():
            return "[backtrace unavailable]"

        # save original fiber state
        orig = FiberUnwinder.get_fiber()
        try:
            fiber_activate(fiber)
            # first frame is always bogus
            frame = gdb.selected_frame().older()
            frame_id = 1

            # find last "useful" frame for display
            while any(word in frame.name() for word in skip):
                # check for unwinding failures and if so use selected frame
                if frame.unwind_stop_reason() != gdb.FRAME_UNWIND_NO_REASON:
                    frame = gdb.selected_frame()
                    break
                frame = frame.older()
                frame_id += 1

            if not frame.is_valid():
                return "[invalid frame]"

            sal = frame.find_sal()
            return "#{frame_id} 0x{pc:016x} in {function} at {filename}:{line}".format(
                frame_id=frame_id,
                pc=frame.pc(),
                function=frame.function(),
                filename=sal.symtab.filename,
                line=sal.line,
            )
        finally:
            # restore original fiber state
            FiberUnwinder.set_fiber(orig)

    @use_language("c++")
    def invoke(self, arg, to_tty):
        only = gdb.string_to_argv(arg)
        orig = FiberUnwinder.get_fiber()
        frame_skip_words = gdb.parameter("fiber info-frame-skip-words").split(" ")

        seen = set()
        for (mid, fid), info in get_fiber_info(only, managers=True):
            # track that we've seen this fiber/manager
            if fid:
                seen.add("{}.{}".format(mid, fid))
                seen.add(str(mid))

            # If it's a manager, print the header and continue
            if not fid:
                manager = info
                print(
                    "  {mid:<4} {fmtype} {address}".format(
                        mid=mid, fmtype=manager.type, address=manager.address
                    )
                )
                continue

            # print the fiber info
            print(
                "{active}   {id:<6} {info} {trace_info}".format(
                    active="*" if info.fiber == orig else " ",
                    id=info.id,
                    info=info,
                    trace_info=self.trace_info(info.fiber, frame_skip_words),
                )
            )

        # print warning if there are no fibers
        if not seen:
            print("No fibers.")

        # print warnings for any filters we didn't find
        for match in only or []:
            if match not in seen:
                if "." in match:
                    print("Invalid fiber id: " + match)
                else:
                    print("Invalid fiber manager id: " + match)

class FiberApplyCommand(gdb.Command):
    """Apply a command to a list of fibers.

    Usage: fiber apply ( ( FIBER_ID | MANAGER_ID ) [ ... ] | all ) COMMAND

    This will cause each specified FIBER_ID (or all fibers for a specified MANAGER_ID)
    to be activated in turn, running the specified gdb COMMAND after each activation.
    """

    def __init__(self):
        super(FiberApplyCommand, self).__init__("fiber apply", gdb.COMMAND_USER)

    def usage(self):
        raise gdb.Error("usage: fiber apply [ FIBER_FILTER [ ... ] | apply ] COMMAND")

    @use_language("c++")
    def invoke(self, arg, from_tty):
        self.dont_repeat()
        argv = gdb.string_to_argv(arg)

        if not argv:
            return self.usage()

        # parse fiber ids and command
        targets = []
        if argv[0] == "all":
            argv.pop(0)  # use empty filter for all fibers
        else:
            while argv and argv[0][0].isdigit():
                targets.append(argv.pop(0))
        command = " ".join(argv)
        if not argv:
            return self.usage()

        # save original fiber state
        orig = FiberUnwinder.get_fiber()
        try:
            # loop over, activating and running command in turn
            for _, info in get_fiber_info(targets):
                print("Fiber {id} ({info})".format(id=info.id, info=info))
                fiber_activate(info.fiber)
                gdb.execute(command, from_tty=from_tty)
        finally:
            # restore original fiber state
            FiberUnwinder.set_fiber(orig)

class FiberDeactivateCommand(gdb.Command):
    """Deactivates fiber processing, returning to normal thread processing"""

    def __init__(self):
        super(FiberDeactivateCommand, self).__init__(
            "fiber deactivate", gdb.COMMAND_USER
        )

    def invoke(self, arg, from_tty):
        if arg:
            print("fiber deactivate takes no arguments.")
            return
        print(fiber_deactivate())

#
# Parameters
#

class SetFiberCommand(gdb.Command):
    """Generic command for setting how fibers are handled"""

    def __init__(self):
        super(SetFiberCommand, self).__init__(
            "set fiber", gdb.COMMAND_DATA, prefix=True
        )

    def invoke(self, arg, from_tty):
        print('"set fiber" must be followed by the name of a fiber subcommand')
        gdb.execute("help set fiber")

class ShowFiberCommand(gdb.Command):
    """Generic command for showing fiber settings"""

    REGISTRY = set()

    def __init__(self):
        super(ShowFiberCommand, self).__init__(
            "show fiber", gdb.COMMAND_DATA, prefix=True
        )

    def invoke(self, arg, from_tty):
        for name in sorted(self.REGISTRY):
            print(
                "{name}: {value}".format(
                    name=name, value=gdb.parameter("fiber " + name)
                )
            )

class FiberParameter(gdb.Parameter):
    """track fiber parameters to print all on "show fiber" command"""

    def __init__(self, name, *args, **kwds):
        super(FiberParameter, self).__init__(name, *args, **kwds)
        (category, _, parameter) = name.partition(" ")
        assert category == "fiber"
        ShowFiberCommand.REGISTRY.add(parameter)

class FiberManagerPrintLimitParameter(FiberParameter):
    """Manage the limit of fibers displayed when printing a FiberManager"""

    show_doc = "Show limit of fibers of a fiber manager to print"
    set_doc = "Set limit of fibers of a fiber manager to print"

    def __init__(self):
        super(FiberManagerPrintLimitParameter, self).__init__(
            "fiber manager-print-limit", gdb.COMMAND_DATA, gdb.PARAM_UINTEGER
        )
        self.value = 100

class FiberInfoFrameSkipWordsParameter(FiberParameter):
    """Manage which frames are skipped when showing fiber info.

    Usage: set fiber info-frame-skip-words [ WORD [ ... ] ]

    Space-separated list of strings that, if present in the function name, will
    cause "info fibers" to skip that frame when printing current location. Set
    to no words to disable behavior and always pring the topmost frame.

    Default: folly::fibers wait
    """

    show_doc = "Show words to skip frames of in fiber info printing"
    set_doc = "Set words to skip frames of in fiber info printing"

    def __init__(self):
        super(FiberInfoFrameSkipWordsParameter, self).__init__(
            "fiber info-frame-skip-words", gdb.COMMAND_DATA, gdb.PARAM_STRING
        )
        self.value = "folly::fibers wait"

class Shortcut(gdb.Function):
    def __init__(self, function_name, value_lambda):
        super(Shortcut, self).__init__(function_name)
        self.value_lambda = value_lambda

    def invoke(self, *args):
        return self.value_lambda(*args)

#
# Internal
#

# This class is responsible for maintaining the name/address:fiberinfo mapping.
# Creating a FiberInfo object adds it to the cache, and trying to create one
# for a cached fiber will just return the same cached FiberInfo.
class FiberInfo(object):

    NAMES = {}

    # Lookup of fiber address/name to (mid, fid)
    def __new__(cls, fiber):
        # return cached info object if it exists
        cached = cls.NAMES.get(str(fiber.address), None)
        if cached:
            return cached

        # otherwise create a new one
        obj = super(FiberInfo, cls).__new__(cls)
        obj.fiber = fiber
        obj._name = ""
        obj.mid = None
        obj.fid = None

        # make sure it's in the cache
        obj.NAMES[str(fiber.address)] = obj
        return obj

    @property
    def id(self):
        return "{}.{}".format(self.mid or "?", self.fid or "?")

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        if self._name:
            # clear any existing name in cache
            del self.NAMES[self._name]
        if name:
            # clear any old holder of the name
            if name in self.NAMES:
                self.NAMES[name]._name = ""
            # update cache to point to us
            self.NAMES[name] = self
        # update the name
        self._name = name or ""

    @classmethod
    def by_name(cls, name):
        return cls.NAMES.get(name, None)

    def __repr__(self):
        return 'FiberInfo(address={}, id={}, name="{}")'.format(
            self.fiber.address, self.id, self.name
        )

    def __str__(self):
        return "Fiber {address} ({state}){name}".format(
            address=self.fiber.address,
            state=FiberPrinter(self.fiber).state_to_string(),
            name=' "{}"'.format(self._name) if self._name else "",
        )

    def __eq__(self, other):
        return self.fiber.address == other.fiber.address

def fiber_activate(fiber):
    return FiberUnwinder.set_fiber(fiber)

def fiber_deactivate():
    return FiberUnwinder.set_fiber(None)

def get_fiber(fid):
    """return a fiber for the given id, name, or address, or None"""
    # check if it's a named fiber
    info = FiberInfo.by_name(fid)
    if info:
        return info.fiber

    # next check if it's an assigned id
    if "." in fid:
        try:
            # Just grab the first result and return
            return next(get_fiber_info([fid]))[1].fiber
        except IndexError:
            return None

    # finally assume it's an address to a fiber
    try:
        return (
            gdb.parse_and_eval(fid)
            .cast(gdb.lookup_type("folly::fibers::Fiber").pointer())
            .dereference()
        )
    except gdb.error:
        return None

def get_fiber_info(only=None, managers=False):
    """iterator of tuples of ((mid, fid), info)

    Can filter output with "M.F" or "M" format, e.g. "2.3" for manager 2 fiber 3
    or "1" for all fibers from manager 1.  If "fid" is None, then the value is
    a gdb.Value of a fiber manager, and if it's non-None, then it's of a FiberInfo.
    Guaranteed to return values in ascending order, managers before their fibers.
    Manager output only occurs when `managers` is set to True.

    Implemented as iterators for interactive speed purposes. A binary can have
    very many fibers, so we don't want to process everything before printing
    some data to the user.  Results of full fiber managers are cached.
    """
    for mid, manager in get_fiber_managers(only):
        if managers:
            yield ((mid, None), manager)

        # first check if pre-cached
        if mid in get_fiber_info.cache:
            for (fid, info) in get_fiber_info.cache[mid].items():
                fiber_id = "{}.{}".format(mid, fid)
                if not only or str(mid) in only or fiber_id in only:
                    yield ((mid, fid), info)
            continue

        # list fibers from the manager
        fibers = collections.OrderedDict()
        fid = 1
        for fiber in fiber_manager_active_fibers(manager):
            # look up/create cached info
            info = FiberInfo(fiber)
            # associate mid.fid with info
            info.mid = mid
            info.fid = fid
            fibers[fid] = info
            # output only if matching the filter
            fiber_id = "{}.{}".format(mid, fid)
            if not only or str(mid) in only or fiber_id in only:
                yield ((mid, fid), info)
            fid += 1
        get_fiber_info.cache[mid] = fibers

# Cache of {mid: {fid: FiberInfo}}
# If the mid is there, then all of the fibers for it are there
get_fiber_info.cache = collections.OrderedDict()

def get_fiber_managers(only=None):
    """iterator of (mid, manager) tuples

    Can filter output with "M.*" or "M" format, e.g. "2.3" for manager 2 (fiber is
    ignored) or "1" for manager 1.
    """
    # first check if pre-cached
    if get_fiber_managers.cache:
        for (mid, manager) in get_fiber_managers.cache.items():
            # output only if matching filter
            if not only or str(mid) in only:
                yield (mid, manager)
        return

    # extract the unique managers from the fiber filters
    only = {i.partition(".")[0] for i in only or []}
    managers = collections.OrderedDict()
    mgr_map = None
    for evb_type in ("folly::EventBase", "folly::VirtualEventBase"):
        # this can possibly return an empty map, even if it exists
        try:
            mgr_map = get_fiber_manager_map(evb_type)
        except gdb.GdbError:
            continue
        # the map pretty printer knows how to extract map entries
        map_pp = gdb.default_visualizer(mgr_map)

        # The children are alternating pairs of (_, (evb, int))/(_, uptr<FiberManager>)
        # the first entry is irrelevant, just an internal name for the pretty printer
        mid = 0

        for _, entry in map_pp.children():
            # The "key" in this map is std::pair<EventBaseT, long>, and the long is
            # almost always 0 (used for frozen options).  We'll ignore it and just use
            # our own internal id.  We unwrap the unique_ptr, though.
            if "unique_ptr" not in entry.type.tag:
                mid += 1
                continue

            # we have a value, make sure we have a unique key
            assert mid not in managers
            value = gdb.default_visualizer(entry)

            # Before GCC9, the stl gdb libs don't expose the unique_ptr target
            # address except through the pretty printer, as the last
            # space-delimited word. From GCC9 forward, the unique_ptr visualizer
            # exposes a children iterator with the target address. We extract
            # that address whicever way we can, then create a new gdb.Value of
            # that address cast to the fibermanager type.
            address = None
            if callable(getattr(value, "children", None)):
                for _, pointer in value.children():
                    address = pointer
            else:
                address = int(value.to_string().split(" ")[-1], 16)

            if address is not None:
                manager = (
                    gdb.Value(address)
                    .cast(gdb.lookup_type("folly::fibers::FiberManager").pointer())
                    .dereference()
                )

                # output only if matching filter
                if not only or str(mid) in only:
                    yield (mid, manager)

        # set cache
        get_fiber_managers.cache = managers

# Cache of {mid: manager}
get_fiber_managers.cache = collections.OrderedDict()

def fiber_manager_active_fibers(fm):
    """iterator of Fiber values for a given fiber manager"""
    all_fibers = fm["allFibers_"]["data_"]["root_plus_size_"]["m_header"]
    fiber_hook = all_fibers["next_"]

    while fiber_hook != all_fibers.address:
        fiber = fiber_hook.cast(gdb.lookup_type("int64_t"))
        fiber = fiber - gdb.parse_and_eval(
            "(int64_t)&'folly::fibers::Fiber'::globalListHook_"
        )
        fiber = fiber.cast(
            gdb.lookup_type("folly::fibers::Fiber").pointer()
        ).dereference()

        if FiberPrinter(fiber).state != "folly::fibers::Fiber::INVALID":
            yield fiber

        fiber_hook = fiber_hook.dereference()["next_"]

def get_fiber_manager_map(evb_type):
    # global cache was moved from anonymous namespace to "detail", we want to
    # work in both cases.
    for ns in ("detail", "(anonymous namespace)"):
        try:
            # Exception thrown if unable to find type
            global_cache_type = gdb.lookup_type(
                "folly::fibers::{ns}::GlobalCache<{evb_type}>".format(
                    ns=ns, evb_type=evb_type
                )
            )
            break
        except gdb.error:
            pass
    else:
        raise gdb.GdbError(
            "Unable to find types. "
            "Please make sure debug info is available for this binary.\n"
            "Have you run 'fbload debuginfo_fbpkg'?"
        )

    global_cache_instance_ptr_ptr = gdb.parse_and_eval(
        "&'" + global_cache_type.name + "::instance()::ret'"
    )
    global_cache_instance_ptr = global_cache_instance_ptr_ptr.cast(
        global_cache_type.pointer().pointer()
    ).dereference()
    if global_cache_instance_ptr == 0x0:
        raise gdb.GdbError("FiberManager map is empty.")

    global_cache_instance = global_cache_instance_ptr.dereference()
    return global_cache_instance["map_"]

def get_fiber_manager_map_evb():
    return get_fiber_manager_map("folly::EventBase")

def get_fiber_manager_map_vevb():
    return get_fiber_manager_map("folly::VirtualEventBase")

# reset the caches when we continue; the current fibers will almost certainly change
def clear_fiber_caches(*args):
    # default to only clearing manager and info caches
    caches = {arg.string() for arg in args} or {"managers", "info"}
    cleared = set()
    if "managers" in caches:
        cleared.add("manager")
        get_fiber_managers.cache.clear()
    if "info" in caches:
        cleared.add("info")
        get_fiber_info.cache.clear()
    # This one we may want to keep, so we can reference known fibers by name/address
    # even though their assigned ids will almost certainly change
    if "names" in caches:
        cleared.add("names")
        FiberInfo.NAMES.clear()

    result = "Cleared {}.".format(", ".join(cleared))
    unknown = caches - cleared
    if unknown:
        # print a warning if we asked for any unknown caches to clear
        result += " Skipped unknown " + ", ".join(unknown)
    return result

def build_pretty_printer():
    pp = gdb.printing.RegexpCollectionPrettyPrinter("folly_fibers")
    pp.add_printer("fibers::Fiber", "^folly::fibers::Fiber$", FiberPrinter)
    pp.add_printer(
        "fibers::FiberManager", "^folly::fibers::FiberManager$", FiberManagerPrinter
    )
    return pp

def load():
    gdb.printing.register_pretty_printer(gdb, build_pretty_printer())
    gdb.xmethod.register_xmethod_matcher(gdb, FiberXMethodMatcher())
    gdb.xmethod.register_xmethod_matcher(gdb, GetFiberXMethodMatcher())
    FiberFrameFilter()
    SetFiberCommand()
    ShowFiberCommand()
    FiberManagerPrintLimitParameter()
    FiberInfoFrameSkipWordsParameter()
    FiberCommand()
    FiberDeactivateCommand()
    FiberInfoCommand()
    FiberApplyCommand()
    FiberNameCommand()
    Shortcut("get_fiber_manager_map_evb", get_fiber_manager_map_evb)
    Shortcut("get_fiber_manager_map_vevb", get_fiber_manager_map_vevb)
    Shortcut("clear_fiber_caches", lambda _: clear_fiber_caches())
    gdb.events.cont.connect(lambda _: clear_fiber_caches())

def info():
    return "Pretty printers for folly::fibers"


text:
import Ember from 'ember';
import config from '../../../config/environment';
import request from 'npm:request-promise';

export default Ember.Component.extend({
  formType: 'Login',

  login: true,

  actions: {
    authenticate: function () {
      this.get('session').authenticate('simple-auth-authenticator:jwt', this.getProperties('identification', 'password'));
    },
    register: function () {
      request.post(`${config.API_URL}/user`).form({
        email: this.get('identification'),

        password: this.get('password')
      }).then(() => {
        this.get('session').authenticate('simple-auth-authenticator:jwt', this.getProperties('identification', 'password'));
      });
    },
    switch: function () {
      if (this.get('login')) {
        this.set('formType', 'Register');
        this.set('login', false);
      } else {
        this.set('formType', 'Login');
        this.set('login', true);
      }
    }
  }
});

redpajama_set_name: RedPajamaGithub


text:
package li.people.action;

import li.annotation.At;
import li.annotation.Bean;
import li.annotation.Inject;
import li.dao.Page;
import li.mvc.AbstractAction;
import li.people.Const;
import li.people.record.Bill;

@Bean
public class BillAction extends AbstractAction implements Const {
    @Inject
    Bill billDao;

    @At("bill_list.do")
    public void list(Page page) {
        setRequest(LIST, billDao.list(page));
        setRequest(PAGE, page);
        view("bill/list");
    }

    @At("bill_add.do")
    public void add() {
        view("bill/add");
    }

    @At(value = "bill_save.do", method = POST)
    public void save(Bill bill) {
        write(billDao.saveIgnoreNull(bill) ? "添加账单成功" : "添加账单失败");
    }

    @At("bill_edit.do")
    public void edit(Integer id) {
        setRequest("bill", billDao.find(id));
        view("bill/edit");
    }

    @At(value = "bill_update.do", method = POST)
    public void update(Bill bill) {
        write(billDao.updateIgnoreNull(bill) ? "修改账单成功" : "修改账单失败");
    }

    @At(value = "bill_delete.do", method = POST)
    public void delete(Integer id) {
        write(billDao.delete(id) ? "删除账单成功" : "删除账单失败");
    }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>CacheKey (apache-cassandra API)</title>
<meta name="date" content="2015-06-18">
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method

org.apache.cassandra.cache

Interface CacheKey

All Superinterfaces:

IMeasurableMemory

All Known Implementing Classes:

KeyCacheKey, RowCacheKey

public interface CacheKey
extends IMeasurableMemory

Method Summary

Methods

Modifier and Type
Method and Description

Pair<java.lang.String,java.lang.String>
getPathInfo()

Methods inherited from interface org.apache.cassandra.cache.IMeasurableMemory

memorySize

Method Detail

getPathInfo

Pair<java.lang.String,java.lang.String> getPathInfo()

Returns:

The keyspace and ColumnFamily names to which this key belongs

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method

Copyright © 2015 The Apache Software Foundation


text:
namespace _02.LBS
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public class LBS
    {
        public static void Main()
        {
            var fieldSize = int.Parse(Console.ReadLine());
            var ladyBugsIndexes = Console.ReadLine()
                .Split()
                .Select(int.Parse)
                .Where(f => f >= 0 && f < fieldSize)
                .ToArray();

            var ladyBugs = new int[fieldSize];

            for (int i = 0; i < ladyBugsIndexes.Length; i++)
            {
                var currentLadyBugIndex = ladyBugsIndexes[i];
                ladyBugs[currentLadyBugIndex] = 1;
            }
        }
    }
}


text:
require 'linguist/blob_helper'
require 'linguist/language'
require 'rugged'

module Linguist
  class LazyBlob
    GIT_ATTR = ['linguist-documentation', 'linguist-language', 'linguist-vendored']
    GIT_ATTR_OPTS = { :priority => [:index], :skip_system => true }
    GIT_ATTR_FLAGS = Rugged::Repository::Attributes.parse_opts(GIT_ATTR_OPTS)

    include BlobHelper

    MAX_SIZE = 128 * 1024

    attr_reader :repository
    attr_reader :oid
    attr_reader :name
    attr_reader :mode

    def initialize(repo, oid, name, mode = nil)
      @repository = repo
      @oid = oid
      @name = name
      @mode = mode
    end

    def git_attributes
      @git_attributes ||= repository.fetch_attributes(
        name, GIT_ATTR, GIT_ATTR_FLAGS)
    end

    def vendored?
      if attr = git_attributes['linguist-vendored']
        return boolean_attribute(attr)
      else
        return super
      end
    end

    def documentation?
      if attr = git_attributes['linguist-documentation']
        boolean_attribute(attr)
      else
        super
      end
    end

    def language
      return @language if defined?(@language)

      @language = if lang = git_attributes['linguist-language']
        Language.find_by_name(lang)
      else
        super
      end
    end

    def data
      load_blob!
      @data
    end

    def size
      load_blob!
      @size
    end

    protected

    # Returns true if the attribute is present and not the string "false".
    def boolean_attribute(attr)
      attr != "false"
    end

    def load_blob!
      @data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil?
    end
  end
end


text:
from typing import List, Union, Dict, Tuple
from collections import OrderedDict, namedtuple, Counter
from datetime import datetime
from odk_aggregation_tool.aggregation import readers
import xmltodict
from copy import copy
import logging
import os
import re
from datetime import datetime

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
ListODict = List[OrderedDict]
DictODict = Dict[str, OrderedDict]

type_map = namedtuple('TypeMap', ['xlsform_type', 'stata_type', 'stata_fmt'])
# TODO: consider inspecting data to get a more accurate type for str and int.
# This would benefit the .dta file size, as well as remove the need for
# current assumptions around select items being always coded with integers.
type_mappings = [
    type_map('start',        'str26',    '%26s'),
    type_map('end',          'str26',    '%26s'),
    type_map('deviceid',     'str17',    '%17s'),
    type_map('date',         'int',      '%td'),
    type_map('text',         'str2045',  '%30s'),
    type_map('integer',      'int',      '%10.0g')
]
STATA_ZERO_DATE = datetime.strptime("1960-01-01", "%Y-%m-%d")

def variable_type(var_name: str, stata_type: str) -> OrderedDict:
    """Prepare a Stata XML variable type (type) element."""
    return OrderedDict([
        ('@varname', var_name),
        ('#text', stata_type)
    ])

def variable_name(var_name: str) -> OrderedDict:
    """Prepare a Stata XML variable name (variable) element."""
    return OrderedDict([
        ('@varname', var_name)
    ])

def variable_format(var_name: str, stata_fmt: str) -> OrderedDict:
    """Prepare a Stata XML variable format (fmt) element."""
    return OrderedDict([
        ('@varname', var_name),
        ('#text', stata_fmt)
    ])

def value_label_map(var_name: str, choices_name: str) -> OrderedDict:
    """Prepare a Stata XML value label map (lblname) element."""
    return OrderedDict([
        ('@varname', var_name),
        ('#text', choices_name)
    ])

def variable_label(var_name: str, description: str) -> OrderedDict:
    """Prepare a Stata XML variable label (vlabel) element."""
    return OrderedDict([
        ('@varname', var_name),
        ('#text', description)
    ])

def value_label(label_value: str, label_text: str) -> OrderedDict:
    """Prepare a Stata XML value label (label) element."""
    return OrderedDict([
        ('@value', str(int(label_value))),
        ('#text', label_text)
    ])

def value_label_collection(val_lab_name: str, choices: ListODict,
                           label_column: str) -> OrderedDict:
    """Prepare a Stata XML value label (vallab) collection of labels (label)."""
    choice_list = [value_label(x['name'], x[label_column]) for x in choices]
    return OrderedDict([
        ('@name', val_lab_name),
        ('label', choice_list)
    ])

def observation_value(var_name: str, var_value: str) -> OrderedDict:
    """"Prepare a Stata XML observation value (v) element."""
    return OrderedDict([
        ('@varname', var_name.replace('@', '')),
        ('#text', var_value)
    ])

def compose_xml(var_types: ListODict, var_names: ListODict,
                var_formats: ListODict, var_labels: ListODict,
                var_vallabel_map: ListODict, value_labels: ListODict,
                observations: ListODict, nvar: str, nobs: str) -> OrderedDict:
    """Prepare a final Stata XML document."""
    return OrderedDict([
        ('dta', OrderedDict([
            ('header', OrderedDict([
                ('ds_format', '113'),
                ('byteorder', 'LOHI'),
                ('filetype', '1'),
                ('nvar', nvar),
                ('nobs', nobs),
                ('data_label', 'ODK data from Python'),
                ('time_stamp', datetime.now().strftime("%d %b %Y %H:%M"))
            ])),
            ('descriptors', OrderedDict([
                ('typelist', OrderedDict([
                    ('type', var_types)
                ])),
                ('varlist', OrderedDict([
                    ('variable', var_names)
                ])),
                ('srtlist', None),
                ('fmtlist', OrderedDict([
                    ('fmt', var_formats)
                ])),
                ('lbllist', OrderedDict([
                    ('lblname', var_vallabel_map)
                ]))
            ])),
            ('variable_labels', [
                OrderedDict([
                    ('vlabel', var_labels)
                ]),
            ]),
            ('expansion', None),
            ('data', [
                OrderedDict([
                    ('o', observations)
                ])
            ]),
            ('value_labels', OrderedDict([
                ('vallab', value_labels)
            ]))
        ]))
    ])

def to_stata_xml(xlsform_path: str, instances_path: str) -> Dict[str, str]:
    """Return Stata XML documents for all discovered XLSForms and XML data."""
    form_defs = collate_xlsforms_by_form_id(xlsform_path=xlsform_path)
    raw_data = collate_xform_instances(instances_path=instances_path)
    instances = remove_duplicate_instances(instances=raw_data)

    stata_docs = dict()
    for form_id, form_def in form_defs.items():
        xform_instances = [x for x in instances if x["@id"] == form_id]
        logger.info("Collecting data for form_id: {0}".format(form_id))
        xform_data, unknown_vars = prepare_xform_data(
            xform_instances=xform_instances, form_def=form_def)
        form_def = tidy_form_def(
            form_id=form_id, form_def=form_def, unknown_vars=unknown_vars)
        observations = prepare_observations(
            xform_data=xform_data, form_def=form_def)
        stata_metadata = prepare_xlsform_metadata(
            form_id=form_id, form_def=form_def)
        nvar = str(len([x["@varname"] for x in stata_metadata["var_names"]]))
        nobs = str(len(observations))
        logger.info("Collected data for {0} "
                    "observations for form_id: {1}".format(nobs, form_id))
        stata_doc = compose_xml(
            observations=observations, nvar=nvar, nobs=nobs, **stata_metadata)
        stata_docs[form_id] = xmltodict.unparse(stata_doc)

    return stata_docs

def collate_xlsforms_by_form_id(xlsform_path: str) -> DictODict:
    """Return discovered form def metadata, from last of sorted versions."""
    logger.info("Looking for XLSForms to read.")
    read_xlsforms = list(
        readers.read_xlsform_definitions(root_dir=xlsform_path))
    unique_form_ids = sorted(
        set(x["@settings"]["form_id"] for x in read_xlsforms))
    if len(unique_form_ids) == 0:
        logger.warning("No XLSForms were read from the specified path. "
                       "Please check it and try again.")
    else:
        logger.info("The following XLSForm form_ids were read: "
                    "{0}.".format(unique_form_ids))

    form_dict = OrderedDict()
    for form_id in unique_form_ids:
        form_defs = [x for x in read_xlsforms
                     if x["@settings"]["form_id"] == form_id]
        versions = sorted(set(x["@settings"]["version"] for x in form_defs),
                          reverse=True)
        logger.info("Reading XLSForms for form_id: {0}, sorted by version "
                    "in order of: {1}".format(form_id, versions))
        sorted_form_defs = sorted(
            form_defs, key=lambda x: x["@settings"]["version"], reverse=True)
        master_form_def = OrderedDict()

        for xlsform in sorted_form_defs:
            if master_form_def.get("@settings") is None:
                master_form_def["@settings"] = xlsform["@settings"]
            for k, v in xlsform.items():
                ignoreable = v.get("type") in ["begin group", "end group", None]
                if not ignoreable and k not in master_form_def:
                    master_form_def[k] = v
        form_dict[form_id] = master_form_def
    return form_dict

def prepare_xlsform_metadata(form_id: str, form_def: OrderedDict) -> DictODict:
    """Return Stata metadata for the default language for each form item."""
    metadata = dict(
        var_types=list(), var_names=list(), var_formats=list(),
        var_labels=list(), var_vallabel_map=list(), value_labels=list())
    form_def_items = copy(form_def)
    form_def_items.pop("@settings")
    form_default_lang = form_def["@settings"].get("default_language")
    if form_default_lang is not None:
        logger.info(
            "Using default language: {0}, for metadata "
            "with form_id: {1}".format(form_default_lang, form_id))
    choices_added = list()
    for k, v in form_def_items.items():
        var_type = v.get('type', '')
        label_column = maybe_language_column(
            metadata_name='label', language_name=form_default_lang)
        if var_type.startswith('select'):
            choice_list = v.get('choices', [])
            if choice_data_type_is_integer(choice_list=choice_list):
                choices_name = var_type.split(' ')[1]
                metadata["var_vallabel_map"].append(value_label_map(
                    var_name=k, choices_name=choices_name))
                if choices_name not in choices_added:
                    metadata["value_labels"].append(value_label_collection(
                        val_lab_name=choices_name, choices=choice_list,
                        label_column=label_column))
                    choices_added.append(choices_name)
                var_type = 'integer'
        type_mapping = next(
            (x for x in type_mappings if x.xlsform_type == var_type), '')
        metadata["var_names"].append(variable_name(var_name=k))
        metadata["var_types"].append(variable_type(
            var_name=k, stata_type=type_mapping.stata_type))
        metadata["var_formats"].append(variable_format(
            var_name=k, stata_fmt=type_mapping.stata_fmt))
        label_column_value = v.get(label_column)
        metadata["var_labels"].append(variable_label(
            var_name=k, description=v.get(
                'name_description', label_column_value)))
    return metadata

def maybe_language_column(metadata_name: str,
                          language_name: Union[str, None]) -> str:
    """Return a language-specific column name if the language isn't None."""
    maybe_lang = ''
    if language_name is not None:
        maybe_lang = '::{0}'.format(language_name)
    return '{0}{1}'.format(metadata_name, maybe_lang)

def collate_xform_instances(instances_path: str) -> ListODict:
    """Return collated (parsed and flattened) XForm data."""
    read_instances = list(readers.read_xml_files(root_dir=instances_path))
    parsed = []
    for xml_data, file_path in read_instances:
        parsed_data = xmltodict.parse(xml_input=xml_data)
        parsed_data["_source_file"] = os.path.normpath(file_path)
        parsed_data["_source_xml"] = xml_data
        parsed.append(parsed_data)
    flattened = list()
    remove_keys = list()
    for instance in parsed:
        flat = readers.flatten_dict_leaf_nodes(instance)
        for k in flat.keys():
            if k.startswith("@") and k not in ["@id", "@version"]:
                if k not in remove_keys:
                    remove_keys.append(k)
        for k in remove_keys:
            if k in flat.keys():
                del flat[k]
        flattened.append(flat)
    if len(remove_keys) > 0:
        logger.info(
            "Removed XML attributes from parsed data, for the following "
            "keys that were neither '@id' (form id) or '@version' "
            "(form version):\n{0}".format(", ".join(remove_keys)))
    return flattened

def prepare_xform_data(
        xform_instances: ListODict,
        form_def: OrderedDict) -> Tuple[ListODict, List[str]]:
    """Return a list of observation values. Convert dates to Stata format."""
    exclude_variables = ["_source_xml"]
    unknown_vars = list()
    prepared_instances = list()
    for instance in xform_instances:
        for k, v in form_def.items():
            if k == "@settings":
                continue
            elif k not in instance.keys():
                # Expand all missing items so Stata considers them missing.
                instance[k] = None
            elif v.get("type") is not None:
                # Convert dates to string integers using Stata's SIF.
                if v["type"] == "date" and instance[k] is not None:
                    date_parse = datetime.strptime(instance[k], "%Y-%m-%d")
                    instance[k] = str((date_parse - STATA_ZERO_DATE).days)
        for k, v in instance.items():
            key_ok = k not in form_def.keys() and k not in exclude_variables
            if key_ok and v is not None:
                unknown_vars.append(re.sub("[^A-z0-9_]", "", k))
        new_instance = OrderedDict(
            (re.sub("[^A-z0-9_]", "", k), v) for k, v in instance.items())
        prepared_instances.append(new_instance)
    return prepared_instances, unknown_vars

def tidy_form_def(form_id: str, form_def: OrderedDict, unknown_vars: List[str]
                  ) -> OrderedDict:
    """Remove label variables and add unknown variables to form definition."""
    if len(unknown_vars) > 0:
        unknown_vars = sorted(set(unknown_vars))
        logger.info(
            "Data was read for the following variables, but there was no "
            "metadata for them in the XLSForms that were read. Now attempting "
            "to add these variables to the form metadata for output. "
            "Form_id: {0}, variable names:\n{1}".format(
             form_id, ", ".join(unknown_vars)))
        added_vars = list()
        for var in unknown_vars:
            if var not in form_def:
                form_def[var] = OrderedDict([
                    ("name", var), ("type", "text"),
                    ("name_description", "{0} (Unknown variable)".format(var))])
                added_vars.append(var)
        logger.info(
            "Metadata added to form_id {0} for the following unknown "
            "variables:\n{1}".format(form_id, ", ".join(added_vars)))
    remove_keys = list()
    for k, v in form_def.items():
        is_label_var = v.get("read_only") == "yes" and v.get("type") == "text"
        if is_label_var:
            remove_keys.append(k)
    if len(remove_keys) > 0:
        for k in remove_keys:
            if k in form_def:
                del form_def[k]
        logger.info(
            "Removed variable metadata for the following fields assumed to "
            "be empty labelling variables (type=text and read_only=yes). "
            "Form_id: {0}, variable names:\n{1}".format(
             form_id, ", ".join(remove_keys)))
    return form_def

def prepare_observations(
        xform_data: ListODict, form_def: OrderedDict) -> ListODict:
    observations = list()
    for instance in xform_data:
        var_values = list()
        for k, v in instance.items():
            if k in form_def.keys():
                var_values.append(observation_value(var_name=k, var_value=v))
        observations.append(OrderedDict([('v', var_values)]))
    return observations

def write_stata_docs(stata_docs: Dict[str, str], output_path: str) -> None:
    """Assuming the form_id is a valid basename, write the Stata docs out."""
    for form_id, document in stata_docs.items():
        write_path = os.path.join(output_path, '{0}.xml'.format(form_id))
        with open(write_path, mode='w', encoding="UTF-8") as out_doc:
            out_doc.write(document)
        logger.info("Wrote form data for form_id: {0}, to a file at: "
                    " {1}.".format(form_id, write_path))

def choice_data_type_is_integer(choice_list: List[Dict]) -> bool:
    """Inspect choice list values to select an appropriate data type."""
    for choice in choice_list:
        try:
            choice["name"] = int(choice["name"])
        except ValueError:
            logger.warning(
                "Found non-integer value in choice list: {0}, value: {1}. "
                "Variables using this choice list will not have labelling "
                "applied as Stata only allows labelling integer "
                "values.".format(choice["list_name"], choice["name"]))
            return False
    return True

def remove_duplicate_instances(instances: ListODict) -> ListODict:
    """Remove duplicate instances, logging a warning if so."""
    counts = Counter(x.get("_source_xml") for x in instances)
    dupes = {k: v for k, v in counts.items() if v != 1}
    for k, v in dupes.items():
        dupe_values = [x for x in instances if x.get("_source_xml") == k]
        dupe_paths = list()
        for i, dupe in enumerate(dupe_values):
            dupe_paths.append(dupe.get("_source_file"))
            if i > 0:
                instances.remove(dupe)
        logger.warning(
            "Found duplicate XML files. Only data from the first file listed "
            "below will be included in the output. Duplicates found: {0},\n"
            "Source files:\n{1}".format(v, '\n'.join(dupe_paths)))
    return instances


text:
package com.twitter.finagle.exp

import com.twitter.conversions.time._
import com.twitter.util.{Duration, Time, Stopwatch}
import org.junit.runner.RunWith
import org.scalatest.{Matchers, FunSuite}
import org.scalatest.junit.JUnitRunner
import scala.util.Random

@RunWith(classOf[JUnitRunner])
class LatencyHistogramTest extends FunSuite
  with Matchers
{
  val range = 10 * 1000 // 10 seconds

  def testRandom(rng: Random, N: Int, err: Double): Unit = {
    val histo = new LatencyHistogram(
      range, err, Duration.Top.inMilliseconds, LatencyHistogram.DefaultSlices, Stopwatch.timeMillis)
    val input = Array.fill(N) {
      (rng.nextDouble() * range).toLong
    }
    for (d <- input)
      histo.add(d)

    val epsilon = if (err == 0.0) 0 else range * err / 2
    val sorted = input.sorted
    for (q <- 0 until 100) {
      withClue(s"quantile $q: ") {
        val actual = histo.quantile(q)
        val ideal = sorted(q * N / 100)
        if (epsilon == 0)
          assert(actual === ideal)
        else
          actual.toDouble should be(ideal.toDouble +- epsilon)
      }
    }
  }

  val tests = Seq(
    (130827L, 300),
    (130655L, 200),
    (127290L, 800),
    (128163L, 123)
  )

  Seq(0.0, 0.1, 0.01).foreach { err =>
    for ((seed, n) <- tests)
      test(s"random: seed=$seed num=$n error=$err") {
        testRandom(new Random(seed), n, err)
      }
  }

  test("constructor checks inputs") {
    intercept[IllegalArgumentException] {
      new LatencyHistogram(-1L, 0.0, 1L, LatencyHistogram.DefaultSlices, Stopwatch.systemMillis)
    }
    intercept[IllegalArgumentException] {
      new LatencyHistogram(1L, -0.1, 1L, LatencyHistogram.DefaultSlices, Stopwatch.systemMillis)
    }
    intercept[IllegalArgumentException] {
      new LatencyHistogram(1L, 1.1, 1L, LatencyHistogram.DefaultSlices, Stopwatch.systemMillis)
    }
  }

  // Since the sliding window internally is sliced, we shouldn't make
  // too many assumptions about which updates are dropped, only that
  // they are dropped by the windowed time.
  //
  // We can also assume that all updates inside of a window should be
  // reflected, but no others.
  //
  // So we test these two properties instead of a sliding window
  // directly.
  test("maintains sliding window by time") {
    Time.withCurrentTimeFrozen { tc =>
      val histo = new LatencyHistogram(
        clipDuration = 40,
        error = 0.0,
        history = 4000,
        slices = LatencyHistogram.DefaultSlices,
        now = Stopwatch.timeMillis)
      for (_ <- 0 until 100) histo.add(30)
      tc.advance(1.second)

      for (_ <- 0 until 100) histo.add(10)
      assert(histo.quantile(99) == 30)

      tc.advance(1.second)
      assert(histo.quantile(99) == 30)
      tc.advance(1.second)
      assert(histo.quantile(99) == 30)
      tc.advance(2.seconds)
      assert(histo.quantile(99) == 10)
    }
  }

  test("handles durations longer than range by not exploding") {
    val histo = new LatencyHistogram(
      clipDuration = 40,
      error = 0.0,
      history = 4 * 1000,
      slices = LatencyHistogram.DefaultSlices,
      now = Stopwatch.timeMillis)
    histo.add(40)
  }
}


text:
var alertModule = angular.module('AlertModule', []);

alertModule.controller('AlertController', function ($scope, $modalInstance, data) {

  $scope.title = data.title;
  $scope.message = data.message;
  $scope.okButton = data.okButton;
  $scope.cancelButton = data.cancelButton;

  $scope.ok = function () {
    $modalInstance.close();
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
});


text:
/* $Id: UIKeyboardHandlerSeamless.h $ */
/** @file
 * VBox Qt GUI - UIKeyboardHandlerSeamless class declaration.
 */

/*
 * Copyright (C) 2010-2014 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef ___UIKeyboardHandlerSeamless_h___
#define ___UIKeyboardHandlerSeamless_h___

/* GUI includes: */
#include "UIKeyboardHandler.h"

/** UIKeyboardHandler reimplementation
  * providing machine-logic with PopupMenu keyboard handler. */
class UIKeyboardHandlerSeamless : public UIKeyboardHandler
{
    Q_OBJECT;

protected:

    /** Seamless keyboard-handler constructor. */
    UIKeyboardHandlerSeamless(UIMachineLogic *pMachineLogic);
    /** Seamless keyboard-handler destructor. */
    virtual ~UIKeyboardHandlerSeamless();

private:

#ifndef Q_WS_MAC
    /** General event-filter. */
    bool eventFilter(QObject *pWatched, QEvent *pEvent);
#endif /* !Q_WS_MAC */

    /* Friend class: */
    friend class UIKeyboardHandler;
};

#endif /* !___UIKeyboardHandlerSeamless_h___ */


text:
export module a {
    export function foo(x: number) {
        return x;
    }
}

export module c {
    export import b = a.foo;
    export var bVal = b(10);
    export var bVal2 = b;
}

//// [internalAliasFunctionInsideLocalModuleWithExport.js]
(function (a) {
    function foo(x) {
        return x;
    }
    a.foo = foo;
})(exports.a || (exports.a = {}));
var a = exports.a;
(function (c) {
    c.b = a.foo;
    c.bVal = c.b(10);
    c.bVal2 = c.b;
})(exports.c || (exports.c = {}));
var c = exports.c;

//// [internalAliasFunctionInsideLocalModuleWithExport.d.ts]
export declare module a {
    function foo(x: number): number;
}
export declare module c {
    export import b = a.foo;
    var bVal: number;
    var bVal2: typeof b;
}


text:
package org.wso2.carbon.ui.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.Header;

public class CarbonUIAuthenticationUtil {

    /**
     * Sets the cookie information, i.e. whether remember me cookie is enabled of disabled. If enabled
     * we will send that information in a HTTP header.
     * @param cookie  The remember me cookie.
     * @param serviceClient The service client used in communication.
     */
    public static void setCookieHeaders(Cookie cookie, ServiceClient serviceClient) {

        List<Header> headers = new ArrayList<Header>();
        Header rememberMeHeader = new Header("RememberMeCookieData", cookie.getValue());
        headers.add(rememberMeHeader);

        serviceClient.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headers);
    }

}


text:
function [direction] = log_map_so3(start_point, end_point)

    axis_angle = vrrotmat2vec(start_point'*end_point);

    direction = axis_angle(1:3)*axis_angle(4);
end


text:
// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.sampling.experiential.client;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.sampling.experiential.shared.InputDAO;

/**
 * A collection of all the ListChoicePanels to define the choices for a given
 * Input object whose responsetype is List.
 *
 * @author Bob Evans
 *
 */
public class ListChoicesPanel extends Composite {

  private InputDAO input;
  private VerticalPanel mainPanel;
  private LinkedList<ListChoicePanel> choicePanelsList;

  /**
   * @param input
   */
  public ListChoicesPanel(final InputDAO input) {
    this.input = input;
    mainPanel = new VerticalPanel();
    mainPanel.setSpacing(2);
    initWidget(mainPanel);

    final CheckBox multiselect = new CheckBox("Multiple selections");
    multiselect.setValue(input.getMultiselect());
    multiselect.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        input.setMultiselect(multiselect.getValue());
      }

    });
    mainPanel.add(multiselect);

    Label lblSignalTimes = new Label("List Choice (s)");
    lblSignalTimes.setStyleName("gwt-Label-Header");
    mainPanel.add(lblSignalTimes);

    choicePanelsList = new LinkedList<ListChoicePanel>();
    String[] choices = input.getListChoices();
    if (choices == null || choices.length == 0) {
      ListChoicePanel choicePanel = new ListChoicePanel(this);
      String choice = choicePanel.getChoice();
      choices = new String[] {choice};
      mainPanel.add(choicePanel);
      choicePanelsList.add(choicePanel);
      input.setListChoices(choices);
    } else {
      for (int i = 0; i < choices.length; i++) {
        ListChoicePanel choicePanel = new ListChoicePanel(this);
        choicePanel.setChoice(choices[i]);
        mainPanel.add(choicePanel);
        choicePanelsList.add(choicePanel);
      }
    }
  }

  public void deleteChoice(ListChoicePanel choicePanel) {
    if (choicePanelsList.size() == 1) {
      return;
    }
    choicePanelsList.remove(choicePanel);
    mainPanel.remove(choicePanel);
    updateChoices();
  }

  public void addChoice(ListChoicePanel choicePanel) {
    int index = choicePanelsList.indexOf(choicePanel);

    ListChoicePanel choicePanel2 = new ListChoicePanel(this);
    choicePanelsList.add(index + 1, choicePanel2);

    int widgetIndex = mainPanel.getWidgetIndex(choicePanel);
    mainPanel.insert(choicePanel2, widgetIndex + 1);

    updateChoices();

  }

  // TODO this is not very efficient.
  private void updateChoices() {
    String[] newTimes = new String[choicePanelsList.size()];
    for (int i = 0; i < choicePanelsList.size(); i++) {
      newTimes[i] = choicePanelsList.get(i).getChoice();
    }
    input.setListChoices(newTimes);
  }

  public void updateChoice(ListChoicePanel choicePanel) {
    int index = choicePanelsList.indexOf(choicePanel);
    input.getListChoices()[index] = choicePanel.getChoice();
  }

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>nodeAttributes</key>
	<dict>
		<key>category</key>
		<string>Kineme GL</string>
		<key>categories</key>
		<array>
			<string>Kineme GL</string>
		</array>
		<key>copyright</key>
		<string>Copyright 2008 Kosada Incorporated. All rights reserved.</string>
		<key>description</key>
		<string>This patch provides camera-style inverse transformation.

http://kineme.net/</string>
		<key>name</key>
		<string>GL Camera</string>
	</dict>
	<key>inputAttributes</key>
	<dict>
		<key>inputXPosition</key>
		<dict>
			<key>description</key>
			<string>X Position</string>
			<key>name</key>
			<string>X Position</string>
		</dict>
		<key>inputYPosition</key>
		<dict>
			<key>description</key>
			<string>Y Position</string>
			<key>name</key>
			<string>Y Position</string>
		</dict>
		<key>inputZPosition</key>
		<dict>
			<key>description</key>
			<string>Z Position</string>
			<key>name</key>
			<string>Z Position</string>
		</dict>
		<key>inputYaw</key>
		<dict>
			<key>description</key>
			<string>Yaw</string>
			<key>name</key>
			<string>Yaw</string>
		</dict>
		<key>inputPitch</key>
		<dict>
			<key>description</key>
			<string>Pitch</string>
			<key>name</key>
			<string>Pitch</string>
		</dict>
		<key>inputRoll</key>
		<dict>
			<key>description</key>
			<string>Roll</string>
			<key>name</key>
			<string>Roll</string>
		</dict>
	</dict>
</dict>
</plist>


text:
using System;
using System.Threading.Tasks;
using Orleans.Runtime;

namespace Orleans
{
    using System.Runtime.ExceptionServices;

    /// <summary>
    /// This class a convinent utiliity class to execute a certain asyncronous function with retires,
    /// allowing to specify custom retry filters and policies.
    /// </summary>
    internal static class AsyncExecutorWithRetries
    {
        public static readonly int INFINITE_RETRIES = -1;
        private static readonly Func<Exception, int, bool> retryAllExceptionsFilter = (Exception exc, int i) => true;

        /// <summary>
        /// Execute a given function a number of times, based on retry configuration parameters.
        /// </summary>
        public static Task ExecuteWithRetries(
            Func<int, Task> action,
            int maxNumErrorTries,
            Func<Exception, int, bool> retryExceptionFilter,
            TimeSpan maxExecutionTime,
            IBackoffProvider onErrorBackOff)
        {
            Func<int, Task<bool>> function = async (int i) => { await action(i); return true; };
            return ExecuteWithRetriesHelper<bool>(
                function,
                0,
                0,
                maxNumErrorTries,
                maxExecutionTime,
                DateTime.UtcNow,
                null,
                retryExceptionFilter,
                null,
                onErrorBackOff);
        }

        /// <summary>
        /// Execute a given function a number of times, based on retry configuration parameters.
        /// </summary>
        public static Task<T> ExecuteWithRetries<T>(
            Func<int, Task<T>> function,
            int maxNumErrorTries,
            Func<Exception, int, bool> retryExceptionFilter,
            TimeSpan maxExecutionTime,
            IBackoffProvider onErrorBackOff)
        {
            return ExecuteWithRetries<T>(
                function,
                0,
                maxNumErrorTries,
                null,
                retryExceptionFilter,
                maxExecutionTime,
                null,
                onErrorBackOff);
        }

        /// <summary>
        /// Execute a given function a number of times, based on retry configuration parameters.
        /// </summary>
        /// <param name="function">Function to execute</param>
        /// <param name="maxNumSuccessTries">Maximal number of successful execution attempts.
        /// ExecuteWithRetries will try to re-execute the given function again if directed so by retryValueFilter.
        /// Set to -1 for unlimited number of success retries, until retryValueFilter is satisfied.
        /// Set to 0 for only one success attempt, which will cause retryValueFilter to be ignored and the given function executed only once until first success.</param>
        /// <param name="maxNumErrorTries">Maximal number of execution attempts due to errors.
        /// Set to -1 for unlimited number of error retries, until retryExceptionFilter is satisfied.</param>
        /// <param name="retryValueFilter">Filter function to indicate if successful execution should be retied.
        /// Set to null to disable successful retries.</param>
        /// <param name="retryExceptionFilter">Filter function to indicate if error execution should be retied.
        /// Set to null to disable error retries.</param>
        /// <param name="maxExecutionTime">The maximal execution time of the ExecuteWithRetries function.</param>
        /// <param name="onSuccessBackOff">The backoff provider object, which determines how much to wait between success retries.</param>
        /// <param name="onErrorBackOff">The backoff provider object, which determines how much to wait between error retries</param>
        /// <returns></returns>
        public static Task<T> ExecuteWithRetries<T>(
            Func<int, Task<T>> function,
            int maxNumSuccessTries,
            int maxNumErrorTries,
            Func<T, int, bool> retryValueFilter,
            Func<Exception, int, bool> retryExceptionFilter,
            TimeSpan maxExecutionTime = default(TimeSpan),
            IBackoffProvider onSuccessBackOff = null,
            IBackoffProvider onErrorBackOff = null)
        {
            return ExecuteWithRetriesHelper<T>(
                function,
                0,
                maxNumSuccessTries,
                maxNumErrorTries,
                maxExecutionTime,
                DateTime.UtcNow,
                retryValueFilter,
                retryExceptionFilter,
                onSuccessBackOff,
                onErrorBackOff);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        private static async Task<T> ExecuteWithRetriesHelper<T>(
            Func<int, Task<T>> function,
            int callCounter,
            int maxNumSuccessTries,
            int maxNumErrorTries,
            TimeSpan maxExecutionTime,
            DateTime startExecutionTime,
            Func<T, int, bool> retryValueFilter = null,
            Func<Exception, int, bool> retryExceptionFilter = null,
            IBackoffProvider onSuccessBackOff = null,
            IBackoffProvider onErrorBackOff = null)
        {
            if (maxExecutionTime != Constants.INFINITE_TIMESPAN && maxExecutionTime != default(TimeSpan))
            {
                DateTime now = DateTime.UtcNow;
                if (now - startExecutionTime > maxExecutionTime)
                {
                    Exception timeoutException = new TimeoutException(String.Format("ExecuteWithRetries has exceeded its max execution time of {0}. Now is {1}, started at {2}, passed {3}",
                            maxExecutionTime, LogFormatter.PrintDate(now), LogFormatter.PrintDate(startExecutionTime), now - startExecutionTime));
                    throw timeoutException;
                }
            }
            T result = default(T);
            int counter = callCounter;
            Exception exception = null;
            try
            {
                callCounter++;
                result = await function(counter);

                bool retry = false;
                if (callCounter < maxNumSuccessTries || maxNumSuccessTries == INFINITE_RETRIES) // -1 for infinite retries
                {
                    if (retryValueFilter != null)
                        retry = retryValueFilter(result, counter);
                }
                if (retry)
                {
                    if (onSuccessBackOff == null)
                    {
                        return await ExecuteWithRetriesHelper(function, callCounter, maxNumSuccessTries, maxNumErrorTries, maxExecutionTime, startExecutionTime, retryValueFilter, retryExceptionFilter, onSuccessBackOff, onErrorBackOff);
                    }
                    else
                    {
                        TimeSpan delay = onSuccessBackOff.Next(counter);
                        await Task.Delay(delay);
                        return await ExecuteWithRetriesHelper(function, callCounter, maxNumSuccessTries, maxNumErrorTries, maxExecutionTime, startExecutionTime, retryValueFilter, retryExceptionFilter, onSuccessBackOff, onErrorBackOff);
                    }
                }
                return result;
            }
            catch (Exception exc)
            {
                exception = exc;
            }

            if (exception != null)
            {
                bool retry = false;
                if (callCounter < maxNumErrorTries || maxNumErrorTries == INFINITE_RETRIES)
                {
                    if (retryExceptionFilter != null)
                        retry = retryExceptionFilter(exception, counter);
                }
                if (retry)
                {
                    if (onErrorBackOff == null)
                    {
                        return await ExecuteWithRetriesHelper(function, callCounter, maxNumSuccessTries, maxNumErrorTries, maxExecutionTime, startExecutionTime, retryValueFilter, retryExceptionFilter, onSuccessBackOff, onErrorBackOff);
                    }
                    else
                    {
                        TimeSpan delay = onErrorBackOff.Next(counter);
                        await Task.Delay(delay);
                        return await ExecuteWithRetriesHelper(function, callCounter, maxNumSuccessTries, maxNumErrorTries, maxExecutionTime, startExecutionTime, retryValueFilter, retryExceptionFilter, onSuccessBackOff, onErrorBackOff);
                    }
                }

                ExceptionDispatchInfo.Capture(exception).Throw();
            }
            return result; // this return value is just for the compiler to supress "not all control paths return a value".
        }
    }

    // Allow multiple implementations of the backoff algorithm.
    // For instance, ConstantBackoff variation that always waits for a fixed timespan,
    // or a RateLimitingBackoff that keeps makes sure that some minimum time period occurs between calls to some API
    // (especially useful if you use the same instance for multiple potentially simultaneous calls to ExecuteWithRetries).
    // Implementations should be imutable.
    // If mutable state is needed, extend the next function to pass the state from the caller.
    // example: TimeSpan Next(int attempt, object state, out object newState);
    internal interface IBackoffProvider
    {
        TimeSpan Next(int attempt);
    }

    internal class FixedBackoff : IBackoffProvider
    {
        private readonly TimeSpan fixedDelay;

        public FixedBackoff(TimeSpan delay)
        {
            fixedDelay = delay;
        }

        public TimeSpan Next(int attempt)
        {
            return fixedDelay;
        }
    }

    internal class ExponentialBackoff : IBackoffProvider
    {
        private readonly TimeSpan minDelay;
        private readonly TimeSpan maxDelay;
        private readonly TimeSpan step;
        private readonly SafeRandom random;

        public ExponentialBackoff(TimeSpan minDelay, TimeSpan maxDelay, TimeSpan step)
        {
            if (minDelay <= TimeSpan.Zero) throw new ArgumentOutOfRangeException("minDelay", minDelay, "ExponentialBackoff min delay must be a positive number.");
            if (maxDelay <= TimeSpan.Zero) throw new ArgumentOutOfRangeException("maxDelay", maxDelay, "ExponentialBackoff max delay must be a positive number.");
            if (step <= TimeSpan.Zero) throw new ArgumentOutOfRangeException("step", step, "ExponentialBackoff step must be a positive number.");
            if (minDelay >= maxDelay) throw new ArgumentOutOfRangeException("minDelay", minDelay, "ExponentialBackoff min delay must be greater than max delay.");

            this.minDelay = minDelay;
            this.maxDelay = maxDelay;
            this.step = step;
            this.random = new SafeRandom();
        }

        public TimeSpan Next(int attempt)
        {
            TimeSpan currMax;
            try
            {
                long multiple = checked(1 << attempt);
                currMax = minDelay + step.Multiply(multiple); // may throw OverflowException
                if (currMax <= TimeSpan.Zero)
                    throw new OverflowException();
            }
            catch (OverflowException)
            {
                currMax = maxDelay;
            }
            currMax = StandardExtensions.Min(currMax, maxDelay);

            if (minDelay >= currMax) throw new ArgumentOutOfRangeException(String.Format("minDelay {0}, currMax = {1}", minDelay, currMax));
            return random.NextTimeSpan(minDelay, currMax);
        }
    }
}


text:
<!DOCTYPE html>

<html>
  <head>

    <title>Value Copies</title>

  <meta charset="utf-8"/>
  <link rel=stylesheet href="../../codemirror/docs.css">
  <link rel="stylesheet" href="../../codemirror/codemirror.css">
  <script src="../../codemirror/codemirror.js"></script>
  <script src="../../codemirror/javascript.js"></script>

  <style>
    body {background-color:transparent;}
    .CodeMirror { font-size:1em;}
    .CodeMirror { height: auto; border: 1px solid #ddd; }
    .CodeMirror-scroll { max-height: auto}
    .CodeMirror pre { padding-left: 7px; line-height: 1.25; }
  </style>

  </head>

			<textarea id="code" name="code">
/* --------------------------------------------------
	Answer the questions in line 19, 27, and 31
   -------------------------------------------------- */
var school = {name:"MIT",
		  	school:"cambridge",
		  	state:"masschusetts",
		  	river:{name:"charles",
		  		   sailing:true,
		  		   rowing:true}};

// get rowing value
var rowing = school.river.rowing;

// assign a new value to rowing
rowing = false;

// has the value to "school.river.rowing" changed?
// --- output value of "school.river.rowing"
// --- your code ---

// get river and assign a new value to "name"
var river = school.river;
river.name = "mishaum";

// has the value to "school.river.name" changed?
// --- output value of "school.river.name"
// --- your code ---

// can you explain the behavior?</textarea>

<script type="text/javascript">

  var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    mode: "javascript"
  });

</script>

</html>


text:
import os
import random
import numpy as np
from tqdm import tqdm
from dataset import GeometryDataset

import torch
from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence

from transformers import BartTokenizerFast, BartForConditionalGeneration, get_linear_schedule_with_warmup

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True

setup_seed(0)

def collate_fn(batch):
    input, output = zip(*batch)

    input = [torch.LongTensor(i) for i in input]
    output = [torch.LongTensor(i) for i in output]
    input = pad_sequence(input, batch_first=True, padding_value=1)
    output = pad_sequence(output, batch_first=True, padding_value=1)
    return input, output

if __name__ == '__main__':

    MAX_EPOCH = 20

    diagram_logic_file = '../data/geometry3k/logic_forms/diagram_logic_forms_annot.json'
    text_logic_file = '../data/geometry3k/logic_forms/text_logic_forms_annot_dissolved.json'
    seq_file = 'results/train/pred_seqs_train_merged_correct.json'

    output_path = "models/"
    os.makedirs(output_path, exist_ok=True)

    device = torch.device('cuda:0')

    tokenizer = BartTokenizerFast.from_pretrained('facebook/bart-base')

    train_set = GeometryDataset('train', diagram_logic_file, text_logic_file, seq_file, tokenizer)
    val_set = GeometryDataset('val', diagram_logic_file, text_logic_file, seq_file, tokenizer)

    train_loader = DataLoader(train_set, batch_size=16, shuffle=True, collate_fn=collate_fn)
    val_loader = DataLoader(val_set, batch_size=16, shuffle=True, collate_fn=collate_fn)

    model = BartForConditionalGeneration.from_pretrained('facebook/bart-base').to(device)

    optimizer = torch.optim.AdamW(model.parameters(), 3e-5)
    scheduler = get_linear_schedule_with_warmup(optimizer, 100, len(train_loader) * 20)
    best_loss = 1e6

    for epoch in tqdm(range(MAX_EPOCH)):
        total_loss = 0
        model.train()
        for idx, (input, output) in enumerate(train_loader):
            optimizer.zero_grad()
            res = model(input.to(device), labels=output[:, 1:].contiguous().to(device),
                        decoder_input_ids=output[:, :-1].contiguous().to(device))
            loss = res.loss
            loss.backward()
            optimizer.step()
            scheduler.step()
            total_loss += loss.item()
        print('\nepoch: ', epoch, " train_loss ", total_loss)
        torch.save(model.state_dict(), output_path+"/tp_model_" + str(epoch) + ".pt")

        total_loss = 0
        model.eval()
        for idx, (input, output) in enumerate(val_loader):
            res = model(input.to(device), labels=output[:, 1:].contiguous().to(device),
                        decoder_input_ids=output[:, :-1].contiguous().to(device))
            loss = res.loss
            total_loss += loss.item()
        if total_loss < best_loss:
            best_loss = total_loss
            torch.save(model.state_dict(), output_path+"/tp_model_best.pt")
        print('epoch: ', epoch, " val_loss ", total_loss)


text:
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/ocmb/explorer/procedures/hwp/memory/exp_mss_memdiag.C $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2022                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file exp_mss_memdiag.C
/// @brief HW Procedure pattern testing
///
// *HWP HWP Owner: Sneha Kadam <sneha.kadam1@ibm.com>
// *HWP HWP Backup: Louis Stermole <stermole@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 3
// *HWP Consumed by: FSP:HB

#include <fapi2.H>
#include <exp_mss_memdiag.H>
#include <lib/dimm/exp_rank.H>
#include <lib/mcbist/exp_mcbist_traits.H>
#include <lib/exp_attribute_accessors_manual.H>
#include <mss_generic_attribute_getters.H>
#include <generic/memory/lib/utils/conversions.H>
#include <generic/memory/lib/utils/find.H>
#include <mss_explorer_attribute_getters.H>
#include <lib/mc/exp_port_traits.H>
#include <lib/shared/exp_consts.H>
#include <generic/memory/lib/utils/mcbist/gen_mss_memdiags.H>
#include <generic/memory/lib/utils/mc/gen_mss_port.H>
#include <generic/memory/lib/utils/mc/gen_mss_restore_repairs.H>
#include <generic/memory/lib/utils/shared/mss_generic_consts.H>

extern "C"
{
    ///
    /// @brief Initializes memory and sets firs
    /// @param[in] i_target OCMB Chip
    /// @return FAPI2_RC_SUCCESS iff ok
    /// @note This is a cronus only procedure. PRD handles memdiags in hostboot
    ///
    fapi2::ReturnCode exp_mss_memdiag( const fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP>& i_target )
    {
        uint8_t l_post_memdiags_subtest = 0;

        FAPI_INF("Start exp_mss_memdiag on: %s", mss::c_str( i_target ));
        FAPI_TRY(mss::memdiags::mss_initialize_memory<mss::mc_type::EXPLORER>(i_target));

        FAPI_TRY(mss::attr::get_post_memdiags_read_subtest(i_target, l_post_memdiags_subtest));

        // Perform subtest if attribute is set to
        if (l_post_memdiags_subtest == fapi2::ENUM_ATTR_MSS_POST_MEMDIAGS_READ_SUBTEST_ENABLE)
        {
            FAPI_TRY(mss::memdiags::perform_read_only_subtest<mss::mc_type::EXPLORER>(i_target));

            // Polls for completion here to avoid issues in future isteps
            FAPI_TRY(mss::memdiags::mss_async_polling_loop<mss::mc_type::EXPLORER>(i_target));

            // Turn off FIFO mode again
            // Note this is normally done in mss_initialize_memory but
            // the read-only subtest above switches back to FIFO mode
            FAPI_TRY(mss::reset_reorder_queue_settings<mss::mc_type::EXPLORER>(i_target) );
        }

    fapi_try_exit:
        FAPI_INF("End exp_mss_memdiag on %s", mss::c_str( i_target ));
        return fapi2::current_err;
    }
}


text: default_app_config = 'rdmo.accounts.apps.AccountsConfig'


text:
void game_loop( void );

#endif


text:
require 'set'

class Subgraphs

  # Constructor.
  def initialize(project)

    # Store the provided project.
    @project = Project.find(project)

    # Find all subgraphs.
    @subgraphs = object_subgraphs
  end

  # Create a string that represents this object.
  def to_s

    # Initialise string.
    s = ""

    # Print header.
    s << "Subgraph test on #{@project.id} - #{@project.name}\n"

    # Print data.
    s << "There are #{object_subgraph_count} subgraphs in this project.\n"

    # Print summary.
    s << "#{object_subgraph_summary}\n"

    # Print individual details
    object_subgraph_details.each_with_index do |detail, index|
      s << "Subgraph #{index}: #{detail} objects.\n"
    end

    # Return string.
    return s
  end

  # Find the number of subgraphs in this project.
  def object_subgraph_count

    # Return the number of subgraphs.
    return @subgraphs.count
  end

  # Display details about each subgraph.
  def object_subgraph_details

    # Return the counts per subgraph.
    return @subgraphs.map { |s| s.count }
  end

  # Find the number of objects in each subgraph.
  def object_subgraph_summary

    # Return counts summary.
    return Measurement.new("Subgraph Object Counts", object_subgraph_details)
  end

  # Find the object subgraphs in this project.
  def object_subgraphs

    # Array of subgraphs.
    subgraphs = []

    # Set of unvisited objects.
    unvisited = Set.new(
      @project.digital_objects.select { |o| o.concepts.count > 0 }
    )

    # While there are still unvisited objects:
    while !unvisited.empty?

        # Find the next set of objects.
        set = find_set(unvisited.first)

        # Remove items in this set from unvisited.
        unvisited.subtract(set)

        # Store set in subgraphs.
        subgraphs << set
    end

    # Return the subgraphs found.
    return subgraphs
  end

  # Find the set of objects associated to the given object via concepts.
  def find_set(object)

    # Track the last and current sets of found objects.
    last = Set.new
    current = Set.new([object])

    # While the last set is different to the current set:
    while last != current

      # Update last.
      last = current

      # Get the set of concepts linked to objects in the last set.
      concepts = Set.new(last.reduce([]) { |all, o| all + o.concepts })

      # Get the set of objects linked to the concept set.
      current = Set.new(concepts.reduce([]) {|all, c| all + c.digital_objects })
    end

    # Return the current set.
    return current
  end
end


text:
package com.cloudera.oryx.app.batch.mllib.kmeans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.typesafe.config.Config;
import org.apache.hadoop.fs.Path;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.mllib.clustering.KMeans;
import org.apache.spark.mllib.clustering.KMeansModel;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import org.dmg.pmml.ComparisonMeasure;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.MiningFunction;
import org.dmg.pmml.PMML;
import org.dmg.pmml.SquaredEuclidean;
import org.dmg.pmml.clustering.Cluster;
import org.dmg.pmml.clustering.ClusteringField;
import org.dmg.pmml.clustering.ClusteringModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudera.oryx.app.common.fn.MLFunctions;
import com.cloudera.oryx.app.kmeans.ClusterInfo;
import com.cloudera.oryx.app.kmeans.KMeansPMMLUtils;
import com.cloudera.oryx.app.kmeans.KMeansUtils;
import com.cloudera.oryx.app.pmml.AppPMMLUtils;
import com.cloudera.oryx.app.schema.InputSchema;
import com.cloudera.oryx.common.pmml.PMMLUtils;
import com.cloudera.oryx.ml.MLUpdate;
import com.cloudera.oryx.ml.param.HyperParamValues;
import com.cloudera.oryx.ml.param.HyperParams;

/**
 * Update function that builds and evaluates k-means models in the Batch Layer.
 */
public final class KMeansUpdate extends MLUpdate<String> {

  private static final Logger log = LoggerFactory.getLogger(KMeansUpdate.class);

  private final String initializationStrategy;
  private final int maxIterations;
  private final List<HyperParamValues<?>> hyperParamValues;
  private final InputSchema inputSchema;
  private final KMeansEvalStrategy evaluationStrategy;

  public KMeansUpdate(Config config) {
    super(config);
    initializationStrategy = config.getString("oryx.kmeans.initialization-strategy");
    evaluationStrategy = Enum.valueOf(KMeansEvalStrategy.class, config.getString("oryx.kmeans.evaluation-strategy"));
    maxIterations = config.getInt("oryx.kmeans.iterations");
    hyperParamValues = new ArrayList<>();
    hyperParamValues.add(HyperParams.fromConfig(config, "oryx.kmeans.hyperparams.k"));
    inputSchema = new InputSchema(config);
    Preconditions.checkArgument(maxIterations > 0);
    Preconditions.checkArgument(
        initializationStrategy.equals(KMeans.K_MEANS_PARALLEL()) ||
            initializationStrategy.equals(KMeans.RANDOM()));
    // Should be an unsupervised problem. This impl only supports numeric features.
    Preconditions.checkArgument(!inputSchema.hasTarget());
    for (int i = 0; i < inputSchema.getNumFeatures(); i++) {
      Preconditions.checkArgument(!inputSchema.isCategorical(i));
    }
  }

  /**
   * @return a list of hyperparameter value ranges to try, one {@link HyperParamValues} per
   *  hyperparameter. Different combinations of the values derived from the list will be
   *  passed back into {@link #buildModel(JavaSparkContext,JavaRDD,List,Path)}
   */
  @Override
  public List<HyperParamValues<?>> getHyperParameterValues() {
    return hyperParamValues;
  }

  /**
   * @param sparkContext    active Spark Context
   * @param trainData       training data on which to build a model
   * @param hyperParameters ordered list of hyper parameter values to use in building model
   * @param candidatePath   directory where additional model files can be written
   * @return a {@link PMML} representation of a model trained on the given data
   */
  @Override
  public PMML buildModel(JavaSparkContext sparkContext,
                         JavaRDD<String> trainData,
                         List<?> hyperParameters,
                         Path candidatePath) {
    int numClusters = (Integer) hyperParameters.get(0);
    Preconditions.checkArgument(numClusters > 1);
    log.info("Building KMeans Model with {} clusters", numClusters);

    JavaRDD<Vector> trainingData = parsedToVectorRDD(trainData.map(MLFunctions.PARSE_FN));
    KMeansModel kMeansModel = KMeans.train(trainingData.rdd(), numClusters, maxIterations, initializationStrategy);

    return kMeansModelToPMML(kMeansModel, fetchClusterCountsFromModel(trainingData, kMeansModel));
  }

  /**
   * @param trainPointData data to cluster
   * @param model trained KMeans Model
   * @return map of ClusterId, count of points associated with the clusterId
   */
  private static Map<Integer,Long> fetchClusterCountsFromModel(JavaRDD<? extends Vector> trainPointData,
                                                               KMeansModel model) {
     return trainPointData.map(model::predict).countByValue();
  }

  /**
   * @param sparkContext    active Spark Context
   * @param model           model to evaluate
   * @param modelParentPath directory containing model files, if applicable
   * @param testData        data on which to test the model performance
   * @return an evaluation of the model on the test data. Higher should mean "better"
   */
  @Override
  public double evaluate(JavaSparkContext sparkContext,
                         PMML model,
                         Path modelParentPath,
                         JavaRDD<String> testData,
                         JavaRDD<String> trainData) {
    KMeansPMMLUtils.validatePMMLVsSchema(model, inputSchema);
    JavaRDD<Vector> evalData =
        parsedToVectorRDD(trainData.union(testData).map(MLFunctions.PARSE_FN));
    List<ClusterInfo> clusterInfoList = KMeansPMMLUtils.read(model);

    log.info("Evaluation Strategy is {}", evaluationStrategy);
    double eval;
    switch (evaluationStrategy) {
      case DAVIES_BOULDIN:
        double dbIndex = new DaviesBouldinIndex(clusterInfoList).evaluate(evalData);
        log.info("Davies-Bouldin index: {}", dbIndex);
        eval = -dbIndex;
        break;
      case DUNN:
        double dunnIndex = new DunnIndex(clusterInfoList).evaluate(evalData);
        log.info("Dunn index: {}", dunnIndex);
        eval = dunnIndex;
        break;
      case SILHOUETTE:
        double silhouette = new SilhouetteCoefficient(clusterInfoList).evaluate(evalData);
        log.info("Silhouette Coefficient: {}", silhouette);
        eval = silhouette;
        break;
      case SSE :
        double sse = new SumSquaredError(clusterInfoList).evaluate(evalData);
        log.info("Sum squared error: {}", sse);
        eval = -sse;
        break;
      default:
        throw new IllegalArgumentException("Unknown evaluation strategy " + evaluationStrategy);
    }

    return eval;
  }

  /**
   * @param model {@link KMeansModel} to translate to PMML
   * @return PMML representation of a KMeans cluster model
   */
  private PMML kMeansModelToPMML(KMeansModel model, Map<Integer,Long> clusterSizesMap) {
    ClusteringModel clusteringModel = pmmlClusteringModel(model, clusterSizesMap);
    PMML pmml = PMMLUtils.buildSkeletonPMML();
    pmml.setDataDictionary(AppPMMLUtils.buildDataDictionary(inputSchema, null));
    pmml.addModels(clusteringModel);
    return pmml;
  }

  private ClusteringModel pmmlClusteringModel(KMeansModel model,
                                              Map<Integer,Long> clusterSizesMap) {
    Vector[] clusterCenters = model.clusterCenters();

    List<ClusteringField> clusteringFields = new ArrayList<>();
    for (int i = 0; i < inputSchema.getNumFeatures(); i++) {
      if (inputSchema.isActive(i)) {
        FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(i));
        ClusteringField clusteringField =
            new ClusteringField(fieldName).setCenterField(ClusteringField.CenterField.TRUE);
        clusteringFields.add(clusteringField);
      }
    }

    List<Cluster> clusters = new ArrayList<>(clusterCenters.length);
    for (int i = 0; i < clusterCenters.length; i++) {
      clusters.add(new Cluster().setId(Integer.toString(i))
                       .setSize(clusterSizesMap.get(i).intValue())
                       .setArray(AppPMMLUtils.toArray(clusterCenters[i].toArray())));
    }

    return new ClusteringModel(
        MiningFunction.CLUSTERING,
        ClusteringModel.ModelClass.CENTER_BASED,
        clusters.size(),
        AppPMMLUtils.buildMiningSchema(inputSchema),
        new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean()),
        clusteringFields,
        clusters);
  }

  private JavaRDD<Vector> parsedToVectorRDD(JavaRDD<String[]> parsedRDD) {
    return parsedRDD.map(data -> {
      try {
        return Vectors.dense(KMeansUtils.featuresFromTokens(data, inputSchema));
      } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
        log.warn("Bad input: {}", Arrays.toString(data));
        throw e;
      }
    });
  }

}


text:
<?php
/**
 * Created by IntelliJ IDEA.
 * User: gerk
 * Date: 27.12.16
 * Time: 12:50
 */
declare(strict_types=1);

namespace PeekAndPoke\Aviator\ServiceContainer\Builder;

use PeekAndPoke\Aviator\ServiceContainer\ServiceDefinitionProvider;
use PeekAndPoke\Aviator\ServiceContainer\ServiceDefinitionProvider\Frame;

/**
 * @author Karsten J. Gerber <kontakt@karsten-gerber.de>
 */
class Capabilities
{
    /** @var ServiceDefinitionProvider[] */
    private $serviceDefinitionProviders = [];

    /**
     * @param ServiceDefinitionProvider $provider
     *
     * @return Capabilities
     */
    public function addServiceDefinitionProvider(ServiceDefinitionProvider $provider) : Capabilities
    {
        if (! isset($this->serviceDefinitionProviders[$provider->handles()])) {

            $this->serviceDefinitionProviders[$provider->handles()] = $provider;
        }

        return $this;
    }

    /**
     * @param Frame $frame
     *
     * @return null|ServiceDefinitionProvider
     */
    public function getServiceDefinitionProvider(Frame $frame)
    {
        return $this->serviceDefinitionProviders[get_class($frame->getServiceAnnotation())] ?? null;
    }
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Bull. Soc. mycol. Fr. 13: 125 (1897)

#### Original name
Cucurbitaria corylicola Delacr.

### Remarks
null


text:
CREATE TABLE `products` (
  `prod_id` int(11) NOT NULL AUTO_INCREMENT,
  `prod_name` varchar(150) NOT NULL,
  `prod_type` varchar(45) NOT NULL,
  `prod_cat` varchar(45) NOT NULL,
  `quantity` int(11) NOT NULL,
  `picture` text NOT NULL,
  `comp_id` int(11) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`prod_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1