text:
"""metric contains all the aggregate metrics generated for an experiments."""

import logging
import math

from scapy.all import IP
from scapy.all import IPv6
from scapy.all import TCP
from transperf import CONVERGENCE_BUCKET
from transperf import METRIC_NO_PORT
from transperf import outparser
from transperf import tcp
from transperf import VarBandwidth

LOG = logging.getLogger('transperf/metric')

def parse_float(float_str, default=0):
    """Parses the float_str and returns the value if valid.

    Args:
        float_str: String to parse as float.
        default: Value to return if float_str is not valid.

    Returns:
        Parsed float value if valid or default.
    """
    try:
        return float(float_str)
    except ValueError:
        return default

class Metric(object):
    """Represents port-based metics.

    Arg:
        name: The name of this metric.
        vals: The default values.

    Attributes:
        _name: The name of the metric.
        _values: The dictionary of ports to values.
    """

    def __init__(self, name, vals=None, cc=None):
        self._name = name
        self._values = vals if vals is not None else dict()
        self._cc = cc if cc is not None else dict()

    def name(self):
        """Name of the metric."""
        return self._name

    def has(self, port):
        """Returns whether the metric has a value for the port.

        Args:
            port: the port number.

        Returns:
            whether the metrics has a value for the port.
        """
        return int(port) in self._values.keys()

    def set(self, port, value):
        """Sets the metric value for the port.

        Args:
            port: The port.
            value: The value.
        """
        self._values[int(port)] = value

    def set_cc(self, port, cc):
        """Sets the metric cc for the port.

        Args:
            port: The port.
            cc: The cc algorithm.
        """
        self._cc[int(port)] = cc

    def get_cc(self, port):
        """Gets the metric cc for the port.

        Args:
            port: The port.

        Returns:
            The cc algorithm.
        """
        return self._cc[int(port)]

    def as_array(self):
        """Metric values as an array.

        Returns:
            Metric values sorted by port number.
        """
        return [self._values[port] for port in sorted(self._values.keys())]

    def ports(self):
        """Ports that have a value in this metric.

        Returns:
            The port numbers.
        """
        return sorted(self._values.keys())

    def cc_ports(self, cc):
        """Ports that have a specific cc in this metric.

        Args:
            cc: The cc algorithm.

        Returns:
            The port numbers.
        """
        return sorted([k for k, v in self._cc.items() if v == cc])

    def get(self, port):
        """The value assigned for the port.

        Args:
            port: The port number.

        Returns:
            The value
        """
        port = int(port)
        if port not in self._values:
            return 0
        return self._values[port]

    def __str__(self):
        vals = ','.join([('%s:%s' % (port, val))
                         for port, val in self._values.items()])
        ccs = ','.join([('%s:%s' % (port, cc))
                        for port, cc in self._cc.items()])
        return 'Metric(\'%s\',{%s},{%s})' %  (self._name, vals, ccs)

class MetricPublisher(outparser.Visitor):
    """The abstract base class for metrics.

    MetricPublishers are visitors that can publish metrics in a dictionary.
    """

    def publish_metrics(self):
        """Publishes metrics in the metrics dictionary.

        Returns:
            An array of published metrics.
        """
        pass

class TputMetricPublisher(MetricPublisher):
    """Generates throughput metrics.

    Attributes:
        _tputs: Throughput of each port.
    """

    def __init__(self):
        super(TputMetricPublisher, self).__init__()
        self._tputs = Metric('tputs')
        self._tool_tputs = Metric('tool_tputs')  # tput from netperf or similar
        # We want the throughput for each port. Note that not all the flows
        # start at the same time, and they may be configured to run past the
        # duration of the experiment (ie. per conn dur overrides experiment
        # provided dur; suppose a 10-sec experiment where a conn starts at 9
        # seconds in and has a conn-configured dur of 20 seconds.)
        #
        # All we care is that, for each second with a flow active, where that
        # second falls into the lifespan of the experiment as a *whole* (ie.
        # dur= for the experiment itself in the config), that the tput for all
        # such seconds is above the configured threshold for the test. We ignore
        # seconds where no flow was configured to be active (eg. flow from 0-2
        # seconds, then 1 second with now flows, then a flow from 3-10 seconds
        # would ignore t=2 to t=3 seconds). We also ignore cases where a flow
        # continues past the experiment duration as in the case of the 20 second
        # flow above.
        #
        # As metric, we output a timeline of bandwidths for all flows
        # aggregated.
        self._stats = {}
        self._tputs_timeline = {}
        self._prev_time = 0

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        """Stores connection throughputs.

        See the MetricPublisher interface.
        """
        self._tputs.set(port, parse_float(tput, 0.0))
        self._tputs.set_cc(port, cc)
        self._tool_tputs.set(port, parse_float(tput, 0.0))
        self._tool_tputs.set_cc(port, cc)
        # Prep for when we visit the ss log.
        port = int(port)
        self._stats[port] = (0, 0)
        self._tputs_timeline[port] = []

    def visit_ss_log(self, time, data):
        if 'port' not in data:
            return

        ts = self._prev_time
        while ts + CONVERGENCE_BUCKET <= time:
            ts += CONVERGENCE_BUCKET
            ports = sorted(self._stats.keys())
            for port in ports:
                # The values are cumulative, so use the delta.
                prev, curr = self._stats[port]
                tput_for_port = 8 * (curr - prev) / CONVERGENCE_BUCKET
                # Convert to mbps.
                tput_for_port /= (10**6)
                # Track both the timeline for this port in this bucket and the
                # state for the cumulative delta in the next iteration.
                self._tputs_timeline[port].append((ts, tput_for_port))
                self._stats[port] = (curr, curr)
        self._prev_time = ts

        port = data['port']
        prev, curr = self._stats[port]
        if 'bytes_acked' in data:
            curr = max(curr, data['bytes_acked'])
        self._stats[port] = (prev, curr)

    def publish_metrics(self):
        """Publishes an array of tputs.

        See the MetricPublisher interface.
        """
        for port in self._stats:
            if not self._tputs_timeline[port]:
                # If we did not get data from sslog, we have to rely on what
                # netperf reports - which is definitely wrong if the flows did
                # not coincide in time but it (usually) works for simple tests
                # where they do.
                return [self._tputs, self._tool_tputs]
        # If we do have sslog data: rather than assume all flows have the same
        # lifespan of activity, and setting a single per-flow tput value, we
        # instead will have a list of <timestamp, bandwidth> tuples. It is up to
        # the caller to properly interpret whether the resulting data passes the
        # requirements of whatever test is being run.
        ports = sorted(self._stats.keys())
        for port in ports:
            LOG.debug('port %d: visit_conn tput was %s, sslog is %s\n',
                      port, self._tputs.get(port), self._tputs_timeline[port])
            self._tputs.set(port, self._tputs_timeline[port])
        return [self._tputs, self._tool_tputs]

class KlogMetricsPublisher(MetricPublisher):
    """Generates metrics based on klog entries.

    It publishes lock_on_bw, probing_cnt, compete_cnt,
    policer_cnt, and lock_on_cnt.

    Attributes:
        _port_modes: The count of modes for each port.
        _lock_on_bw: The lock-on BW of each port.
    """

    def __init__(self):
        super(KlogMetricsPublisher, self).__init__()
        self._port_modes = {}
        self._lock_on_bw = Metric('lock_on_bw')

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        """Stores the experiment port numbers.

        See the MetricPublisher interface.
        """
        port = int(port)
        self._port_modes[port] = {}
        self._lock_on_bw.set(port, 0)

    def visit_klog(self, time, line, match):
        """Updates mode counts and set the lock on bandwidth.

        Args:
            time: Time of the log entry relative to the start time of the
                  experiment.
            line: The raw content of the log file.
            match: The dictionary of all grouped regex matches.
        """
        port = int(match['port'])
        if port not in self._port_modes:
            return

        modes = self._port_modes[port]
        mode = match.get('mode', None)
        if mode not in modes:
            modes[mode] = 1
            if mode == 'W':
                self._lock_on_bw.set(port, int(match.get('bw', 0)) / 1000.)
        else:
            modes[mode] += 1

    def publish_metrics(self):
        """Publishes metrics from the klog entries."""
        probing_cnt = Metric('probing_cnt')
        compete_cnt = Metric('compete_cnt')
        policer_cnt = Metric('policer_cnt')
        lock_on_cnt = Metric('lock_on_cnt')
        for port in self._lock_on_bw.ports():
            port_mode_cnt = self._port_modes[port]
            for mode, metric in [('G', probing_cnt), ('C', compete_cnt),
                                 ('P', policer_cnt), ('W', lock_on_cnt)]:
                if mode not in port_mode_cnt:
                    metric.set(port, 0)
                else:
                    metric.set(port, port_mode_cnt[mode])

        return [probing_cnt, compete_cnt, policer_cnt, lock_on_cnt,
                self._lock_on_bw]

class RetxRateMetricPublisher(MetricPublisher):
    """Publishes the retransmittion rate for each flow.

    Attributes:
        _rcv_ip: The receiver IP address.
        _max_seq: The maximum sequence number sent so far for each port.
        _stats: The number of transmistted and retransmitted packets for each
                port.
    """

    def __init__(self):
        super(RetxRateMetricPublisher, self).__init__()
        self._rcv_ip = None
        self._max_seq = {}
        self._stats = {}

    def begin(self, exp, exp_dir, rcv_ip):
        """Stores the receiver ip address.

        See the outparser.Visitor interface.
        """
        self._rcv_ip = rcv_ip

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        """Initializes the state for the given port.

        See the outparser.Visitor interface.
        """
        port = int(port)
        self._stats[port] = (0, 0)
        self._max_seq[port] = -1

    def visit_packet(self, time, packet):
        """Generate statistics for each port.

        See the outparser.Visitor interface.
        """
        if (IP not in packet and IPv6 not in packet) or TCP not in packet:
            return

        iph = packet[IP] if IP in packet else packet[IPv6]
        tcph = packet[TCP]

        if iph.src == self._rcv_ip:
            return

        port = tcph.sport
        if port not in self._stats:
            return

        max_seq = self._max_seq[port]
        if IP in packet:
            data_len = iph.len - 4 * iph.ihl - 4 * tcph.dataofs
        else:
            if iph.nh != 6:
                LOG.info('IPv6 pachet has extension headers, skipping.')
                return
            data_len = iph.plen - 4 * tcph.dataofs
        next_seq = tcp.add_seq(tcph.seq, data_len - 1)
        if max_seq == -1 or tcp.after(tcph.seq, max_seq):
            self._max_seq[port] = next_seq
            is_retx = 0
        else:
            is_retx = 1

        tx, retx = self._stats[port]
        self._stats[port] = (tx + data_len, retx + is_retx * data_len)

    def visit_ss_log(self, time, data):
        if 'port' not in data:
            return
        port = data['port']
        tx, retx = self._stats[port]
        if 'data_segs_out' in data:
            tx = data['data_segs_out']
        if 'retrans' in data:
            retx = data['retrans']
        self._stats[port] = (tx, retx)

    def publish_metrics(self):
        ports = sorted(self._stats.keys())
        retxs = Metric('retx')
        for port in ports:
            tx, retx = self._stats[port]
            retxs.set(port, (float(retx) / tx) if tx else 0)
        return [retxs]

class ConvergenceMetricPublisher(MetricPublisher):
    """Publishes the convergence status.

    Attributes:
        _stats: The bytes_acked of previous and current time bucket.
        _tputs: The list containing the throughput per bucket.
        _fairness: The list containing the fairness score per bucket.
        _prev_time: The timestamp in seconds of previous bucket processed.
    """

    def __init__(self):
        super(ConvergenceMetricPublisher, self).__init__()
        self._stats = {}
        self._tputs = []
        self._fairness = []
        self._prev_time = 0

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        """Initializes the state for the given port.

        See the outparser.Visitor interface.
        """
        port = int(port)
        self._stats[port] = (0, 0)

    def visit_ss_log(self, time, data):
        if 'port' not in data:
            return

        ts = self._prev_time
        while ts + CONVERGENCE_BUCKET <= time:
            ts += CONVERGENCE_BUCKET
            ports = sorted(self._stats.keys())
            tputs = []
            for port in ports:
                # The values are cumulative, so use the delta.
                prev, curr = self._stats[port]
                tputs.append(8*(curr - prev)/CONVERGENCE_BUCKET)
                self._stats[port] = (curr, curr)

            # Compute Jain's fairness index for throughputs for each flow.
            sum_ = sum(tputs)
            self._tputs.append(sum_)
            num_flows = len(ports)
            sum2_ = sum([tput**2 for tput in tputs])
            if sum2_ > 0:
                fairness_score = 100 * sum_ * sum_ / sum2_ / num_flows
            else:
                fairness_score = 0
            self._fairness.append(fairness_score)
        self._prev_time = ts

        port = data['port']
        prev, curr = self._stats[port]
        if 'bytes_acked' in data:
            curr = max(curr, data['bytes_acked'])
        self._stats[port] = (prev, curr)

    def publish_metrics(self):
        tputs_history = Metric('tputs_history')
        tputs_history.set(METRIC_NO_PORT, self._tputs)
        fairness_history = Metric('fairness_history')
        fairness_history.set(METRIC_NO_PORT, self._fairness)
        return [tputs_history, fairness_history]

class RTTMetricPublisher(MetricPublisher):
    """Publishes RTT metrics.

    To be an efficient metric generator with low memory consumption,
    we use the following heuristics to calculate RTT and store the values:

    1) RTT is only calculated when we receive an Ack or SAck after the
       highest previously Ack'ed/SAck'ed data.
    2) We use a multi-resolution histogram to store the measured RTTs.
       In this histogram we store RTTs of less than 10ms in us resolution,
       and RTTs of more than 10ms is ms resolution. Assuming that we almost
       always use a RTT of less 1sec, we will have at most 10,990 buckets.

    Using this histogram we calculate the following metrics:
    1. med_rtt
    2. p95_rtt
    3. p99_rtt

    And we use O(1) memory to calculate the following metrics:
    4. min_rtt
    5. avg_rtt
    6. max_rtt

    Times are kept in pythonic format: a float where its integer part is seconds.

    Attributes:
        _rcv_ip: The receiver IP address.
        _last_acked: The last sequence acked/sacked for each port.
        _unacked: The sequence numbers that higher than the last ack/sack
                  received.
        _samples: Number of RTT samples taken for each port
        _min_rtt: The minimum RTT of each port.
        _max_rtt: The maximum RTT of each port.
        _sum_rtt: The sum of RTTs for each port.
        _hist: The histogram of each port.
    """

    def __init__(self):
        self._rcv_ip = None
        self._last_acked = {}
        self._unacked = {}
        self._samples = {}
        self._min_rtt = {}
        self._max_rtt = {}
        self._sum_rtt = {}
        self._hist = {}

    def begin(self, exp, exp_dir, rcv_ip):
        """Stores the receiver ip address.

        See the outparser.Visitor interface.
        """
        self._rcv_ip = rcv_ip

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        """Initializes the state for the given port.

        See the outparser.Visitor interface.
        """
        port = int(port)
        self._last_acked[port] = -1
        self._unacked[port] = []
        self._samples[port] = 0
        self._min_rtt[port] = -1
        self._max_rtt[port] = 0
        self._sum_rtt[port] = 0
        self._hist[port] = {}

    def visit_ss_log(self, time, data):
        if ('port' not in data) or ('rtt' not in data):
            return
        self._sample_rtt(data['port'], data['rtt'])

    def visit_packet(self, time, packet):
        if (IP not in packet and IPv6 not in packet) or TCP not in packet:
            return

        iph = packet[IP] if IP in packet else packet[IPv6]
        tcph = packet[TCP]
        if iph.src == self._rcv_ip:
            self._handle_rcv(time, tcph)
        else:
            self._handle_snd(time, tcph)

    def _sample_rtt(self, port, rtt):
        self._max_rtt[port] = max(self._max_rtt[port], rtt)

        min_rtt = self._min_rtt[port]
        if min_rtt == -1:
            min_rtt = rtt
        self._min_rtt[port] = min(rtt, min_rtt)

        samples = self._samples[port]
        self._samples[port] = samples + 1

        self._sum_rtt[port] += rtt

        # RTTs more than 10ms are rounded DOWN to ms resolution.
        # All rtt values in the histogram are in units of usec.
        if rtt > .01:
            rtt = int(rtt * 1000) * 1000
        else:
            rtt = int(rtt * 1000 * 1000)

        if rtt in self._hist[port]:
            self._hist[port][rtt] += 1
        else:
            self._hist[port][rtt] = 1

    def _handle_snd(self, time, tcph):
        port = tcph.sport
        if port not in self._unacked:
            return

        seq = tcph.seq
        unacked = self._unacked[port]
        if not unacked:
            unacked.append((seq, time))
            return

        max_seq = unacked[-1][0]
        if tcp.after(seq, max_seq):
            unacked.append((seq, time))

    def _handle_rcv(self, time, tcph):
        port = tcph.dport
        if port not in self._unacked:
            return

        ack = tcph.ack
        # Ignore packets with ack of 0 or with RST.
        if not ack or tcph.flags & 0x4:
            return

        for _, end in tcp.sacks(tcph):
            ack = max(ack, end)

        i = 0
        max_seq_time = -1
        unacked = self._unacked[port]
        for i, (seq, seq_time) in enumerate(unacked):
            if not tcp.after(ack, seq):
                break

            max_seq_time = max(max_seq_time, seq_time)

        if i >= 1:
            self._unacked[port] = unacked[i-1:]

        if max_seq_time == -1:
            return

        rtt = time - max_seq_time
        self._sample_rtt(port, rtt)

    def publish_metrics(self):
        ports = sorted(self._unacked.keys())

        # min/max/avg_rtt are multiplied by 1000 to convert from sec to msec.
        min_rtt = Metric('min_rtt')
        max_rtt = Metric('max_rtt')
        for port in ports:
            min_rtt.set(port, self._min_rtt[port] * 1000)
            max_rtt.set(port, self._max_rtt[port] * 1000)

        avg_rtts = Metric('avg_rtt')
        for port in ports:
            if self._samples[port]:
                avg_rtts.set(port,
                             self._sum_rtt[port] * 1000. / self._samples[port])
            else:
                avg_rtts.set(port, 0)

        med_rtts = Metric('med_rtt')
        p95_rtts = Metric('p95_rtt')
        p99_rtts = Metric('p99_rtt')

        # med/p95/p99_rtt are divided by 1000 to convert from usec to msec.
        for port in ports:
            samples = self._samples[port]
            hist = self._hist[port]

            med_rtt = p95_rtt = p99_rtt = -1
            total = 0
            for rtt, cnt in sorted(hist.items()):
                total += cnt

                if 0.5 * samples <= total and med_rtt == -1:
                    med_rtt = rtt

                if 0.95 * samples <= total and p95_rtt == -1:
                    p95_rtt = rtt

                if 0.99 * samples <= total and p99_rtt == -1:
                    p99_rtt = rtt

            med_rtts.set(port, med_rtt / 1000.0)
            p95_rtts.set(port, p95_rtt / 1000.0)
            p99_rtts.set(port, p99_rtt / 1000.0)

        # return all metrics in units of msec
        return [min_rtt, avg_rtts, med_rtts, p95_rtts, p99_rtts, max_rtt]

class SerialDelayMetricPublisher(MetricPublisher):
    """Calculates the serialization delay.

    Attributes:
        _rcv_ip: The receiver IP address.
        _packet_size: Counts the size of packets for each port.
    """

    def __init__(self):
        self._rcv_ip = None
        self._packet_size = {}
        self._bw = -1

    def begin(self, exp, exp_dir, rcv_ip):
        self._rcv_ip = rcv_ip

        if isinstance(exp.bw, VarBandwidth):
            self._bw = min([bw.downlink for bw in exp.bw.bws])
        else:
            self._bw = exp.bw.downlink

        # If there is no HTB, set the bandwidth to 10Gbps
        if self._bw == 0:
            self._bw = 10000

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        self._packet_size[port] = {}

    def visit_packet(self, time, packet):
        if (IP not in packet and IPv6 not in packet) or TCP not in packet:
            return

        iph = packet[IP] if IP in packet else packet[IPv6]
        tcph = packet[TCP]

        if iph.src == self._rcv_ip:
            return

        port = tcph.sport
        if port not in self._packet_size:
            return

        # TODO(arjunroy) IPv4 = total len, IPv6 = payload len. Is it important?
        packet_len = packet.len if IP in packet else packet.plen
        sizes = self._packet_size[port]
        if packet_len in sizes:
            sizes[packet_len] += 1
        else:
            sizes[packet_len] = 1

    def publish_metrics(self):
        ports = sorted(self._packet_size.keys())

        serial_delays = Metric('serial_delay')
        for port in ports:
            samples = sum([v for _, v in self._packet_size[port].items()])

            med_size = 1514
            total = 0
            for size, cnt in sorted(self._packet_size[port].items()):
                total += cnt
                if 0.5 * samples <= total:
                    med_size = size
                    break

            # 3 * median_skb_size_in_bits / configured_link_bw_in_bits
            delay = float(3 * med_size * 8) / (self._bw * 1000 * 1000)
            serial_delays.set(port, delay * 1000)  # in ms.

        return [serial_delays]

class AppLatencyMetricPublisher(MetricPublisher):
    """Estimates the app latency metric.

    Attributes:
        _exp: The experiment.
        _rcv_ip: The receiver IP address.
        _rtt_of_port: The map of port to min RTT (specified in the experiment).
        _bursts_of_ports: The burst of each port.
        _cnt: The number of samples collected for each port.
        _avg: The avg metrics.
        _max: The max metrics.
        _tops: Are the top latency samples maintained for each port, used for
               the p95 metric.
        _chunk_latency: The latency of sending a full chunk for each port.
        _first_ack: The first ack sequence of each port.
        _last_ack: The last relative ack sequence of each port.
        _ports: The ports of this experiment.
    """

    def __init__(self):
        self._exp = None
        self._rcv_ip = None
        self._rtt_of_port = None
        self._bursts_of_ports = {}
        self._cnt = {}
        self._avg = Metric('packet_transfer_avg')
        self._max = Metric('packet_transfer_max')
        self._tops = {}
        self._chunk_latency = {}
        self._first_ack = {}
        self._last_ack = {}
        self._ports = {}

    def begin(self, exp, exp_dir, rcv_ip):
        self._exp = exp
        self._rcv_ip = rcv_ip

    def visit_conn(self, ip, port, tool, cc, params, start, dur, tput):
        self._ports[port] = True
        self._chunk_latency[port] = []

    def _write_time(self, port, byte):
        """Returns the time the application has written "byte" byte of data."""
        if not self._bursts_of_ports:
            # Fill bursts_of_ports.
            bursts = []
            for s in xrange(self._exp.nsenders()):
                for c in self._exp.conns_of_sender(s):
                    for i in range(c.num):
                        bursts.append(c.burst)

            for i, port in enumerate(sorted(self._ports)):
                self._bursts_of_ports[port] = bursts[i]

        if port not in self._bursts_of_ports:
            return 0
        burst = self._bursts_of_ports[port]
        if not burst:
            return 0
        return int(
            byte / (burst.rounds * burst.repeat * burst.req)) * burst.wait

    def _sample(self, port, ack, dur):
        """Collects one latency sample.

        Args:
            port: the connection port.
            ack: relative acknowledged byte.
            dur: latency duration.
        """
        port = int(port)
        if port not in self._cnt.keys():
            self._cnt[port] = 1
            self._avg.set(port, dur)
            self._max.set(port, dur)
            self._tops[port] = [dur]
            return

        cnt = self._cnt[port] + 1
        self._cnt[port] = cnt

        avg = self._avg.get(port)
        avg = (avg * (cnt - 1) + dur) / cnt
        self._avg.set(port, avg)

        max_m = self._max
        max_m.set(port, max(max_m.get(port), dur))

        cnt = self._top_vals_to_keep(cnt)
        tops = self._tops[port]

        # keep tops sorted and keep cnt samples in it.
        i = len(tops) - 1
        while i >= 0:
            if dur <= tops[i]:
                break
            i -= 1
        i += 1
        tops.insert(i, dur)
        if len(tops) > cnt:
            tops.pop()

        burst = self._burst_size(port)
        if not burst:
            return

        c_lats = self._chunk_latency[port]
        # We ignore the first chunk. Thus, if c_lats contains 0 elements, we are
        # looking to receive the ack of the second chunk, and so on...
        if ack >= (len(c_lats) + 2) * burst - 1:
            c_lats.append(dur)

    def _top_vals_to_keep(self, cnt):
        """How much top values we should keep to have a good accuracy for p95.

        Args:
          cnt: Total number of records.

        Returns:
          Number of records to keep.
        """
        if cnt <= 100:
            return 1000
        # Keep at least 10% of the required value, in power of 10 steps:
        #   100 for     101..1000
        #   1000 for    1001..10000
        #   10000 for   10001..100000
        #   ...
        return int(math.pow(10, math.ceil(math.log(cnt, 10)) - 1))

    def _ack(self, time, tcph):
        """Processes an ack and calculates the latency of the acked packets."""
        port = int(tcph.sport)
        if port not in self._first_ack:
            self._first_ack[port] = tcph.ack
            self._last_ack[port] = -1
            return

        ack = tcph.ack
        if ack == 0:
            return
        if ack < self._first_ack[port]:
            ack += 65536
        ack -= self._first_ack[port]
        if ack <= self._last_ack[port]:
            return

        self._last_ack[port] = ack

        ack -= 1
        if ack <= self._burst_size(port):
            return

        wtime = self._write_time(port, ack)
        self._sample(port, ack, time - wtime)

    def visit_packet(self, time, packet):
        if (IP not in packet and IPv6 not in packet) or TCP not in packet:
            return

        iph = packet[IP] if IP in packet else packet[IPv6]
        tcph = packet[TCP]

        if iph.src == self._rcv_ip:
            self._ack(time, tcph)

    def _min_bw(self):
        """Returns the minimum bottleneck bandwidth of the link."""
        bw = self._exp.bw
        if isinstance(bw, VarBandwidth):
            return min([vbw.downlink for vbw in bw.bws])
        else:
            return bw.downlink

    def _burst_size(self, port):
        """Returns the size of the bursts in bytes.

        If the connection is not a burst connection it returns 0.

        Args:
            port: The port number.
        Returns:
            The size of the first burst if any, otherwise 0.
        """
        burst = self._bursts_of_ports.get(port)
        if not burst:
            return 0
        return burst.rounds * burst.repeat * burst.req

    def publish_metrics(self):
        pkt_p95 = Metric('packet_transfer_p95')
        pkt_p99 = Metric('packet_transfer_p99')
        for port, tops in self._tops.items():
            cnt = self._cnt[port]
            top5cnt = int(.05 * cnt)
            top5cnt = min(top5cnt, len(tops) - 1)
            pkt_p95.set(port, tops[top5cnt])
            top1cnt = int(.01 * cnt)
            top1cnt = min(top1cnt, len(tops) - 1)
            pkt_p99.set(port, tops[top1cnt])

        cl_med = Metric('chunk_latency_med')
        cl_avg = Metric('chunk_latency_avg')
        cl_p90 = Metric('chunk_latency_p90')
        cl_p95 = Metric('chunk_latency_p95')
        cl_p99 = Metric('chunk_latency_p99')
        cl_max = Metric('chunk_latency_max')
        for port, lat in self._chunk_latency.items():
            if not lat:
                cl_med.set(port, -1)
                cl_avg.set(port, -1)
                cl_p90.set(port, -1)
                cl_p95.set(port, -1)
                cl_p99.set(port, -1)
                cl_max.set(port, -1)
                continue
            lat = sorted(lat)
            cl_avg.set(port, float(sum(lat)) / len(lat))
            cl_max.set(port, max(lat))
            cl_med.set(port, lat[max(0, int(len(lat) * .50) - 1)])
            cl_p90.set(port, lat[max(0, int(len(lat) * .90) - 1)])
            cl_p95.set(port, lat[max(0, int(len(lat) * .95) - 1)])
            cl_p99.set(port, lat[max(0, int(len(lat) * .99) - 1)])

        # To avoid publishing rubbish on the metrics, make sure each port has
        # 1) sent at least 25% of its data, if it's a stream.
        # 2) sent at least the first chunk, if it's a burst (write-wait).
        bw = self._min_bw()
        for port in self._ports:
            if port not in self._last_ack:
                continue
            lack = self._last_ack[port]
            burst = self._bursts_of_ports.get(port)
            if not burst or burst.wait <= 0:
                thresh = self._exp.dur * bw / 8. / 4
            else:
                thresh = burst.rounds * burst.repeat * burst.req
            if lack < thresh:
                pkt_p95.set(port, -1)
                pkt_p99.set(port, -1)
                self._max.set(port, -1)
                self._avg.set(port, -1)

        return [self._avg, self._max, pkt_p95, pkt_p99,
                cl_med, cl_avg, cl_p90, cl_p95, cl_p99, cl_max]

# TODO(soheil): Maybe later add the following metrics:
#               bloated_rtt, avg_policer_bw, max_policer_bw, fq_bw.


text:
IntrinsicEdPropertyView::IntrinsicEdPropertyView(QWidget* parent)
    : QDockWidget(parent)
{
  _ui.setupUi(this);

  // Setup UI
  {
    _ui.autoCollapseButton->setCheckable(true);
  }

  QObject::connect(_ui.refreshToolButton, SIGNAL(clicked()), this,
                   SLOT(onRefreshProperties()));
  QObject::connect(_ui.collapseAllButton, SIGNAL(clicked()), this,
                   SLOT(onCollapseAll()));
  QObject::connect(_ui.expandAllButton, SIGNAL(clicked()), this,
                   SLOT(onExpandAll()));
  QObject::connect(_ui.autoCollapseButton, SIGNAL(toggled(bool)), this,
                   SLOT(onAutoCollapsePropertyCategories(bool)));

  // Setup JSON document
  _propertyDocument = rapidjson::Document(rapidjson::kArrayType);
  for (uint32_t i = 0; i < 16u; ++i)
  {
    _propertyDocument.PushBack(rapidjson::Value(rapidjson::kObjectType),
                               _propertyDocument.GetAllocator());
  }
}

IntrinsicEdPropertyView::~IntrinsicEdPropertyView() {}

void IntrinsicEdPropertyView::onRefreshProperties()
{
  clearAndUpdatePropertyView();
}

void IntrinsicEdPropertyView::onCollapseAll()
{
  for (auto it = _categoryCollapsedState.begin();
       it != _categoryCollapsedState.end(); ++it)
  {
    it->second = true;
  }

  clearAndUpdatePropertyView();
}

void IntrinsicEdPropertyView::onExpandAll()
{
  for (auto it = _categoryCollapsedState.begin();
       it != _categoryCollapsedState.end(); ++it)
  {
    it->second = false;
  }

  clearAndUpdatePropertyView();
}

void IntrinsicEdPropertyView::onAutoCollapsePropertyCategories(bool p_Checked)
{
  // Collapse all categories but the one that has been selected
  if (p_Checked)
  {
    for (auto it = _categoryCollapsedState.begin();
         it != _categoryCollapsedState.end(); ++it)
    {
      it->second = it->first != _currentCategory || _currentCategory == "";
    }

    clearAndUpdatePropertyView();
  }
}

void IntrinsicEdPropertyView::clearPropertyView()
{
  QLayout* layout = _ui.scrollAreaWidgetContents->layout();
  _INTR_ASSERT(layout);

  QLayoutItem* item;
  while ((item = layout->takeAt(0)) != nullptr)
  {
    delete item->widget();
    delete item;
  }
}

QFrame* IntrinsicEdPropertyView::createCategoryHeaderWidget(const char* p_Title,
                                                            bool p_Collapsed)
{
  QFrame* frame = new QFrame();
  frame->setLayout(new QHBoxLayout());
  frame->setFrameStyle(QFrame::Shape::StyledPanel | QFrame::Shadow::Raised);
  frame->setMinimumSize(0, 32);
  frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  frame->layout()->setContentsMargins(QMargins(6, 2, 6, 2));

  QLabel* label = new QLabel(p_Title);
  label->setObjectName("title");

  QPushButton* button = new QPushButton();

  if (p_Collapsed)
  {
    button->setIcon(QIcon(":/Icons/icons/arrows/shrink.png"));
  }
  else
  {
    button->setIcon(QIcon(":/Icons/icons/arrows/expand.png"));
  }

  button->setStyleSheet(
      "QPushButton { border-style: outset; border-width: 0px; }");

  frame->layout()->addWidget(button);

  QObject::connect(button, SIGNAL(clicked()), this,
                   SLOT(onCategoryHeaderClicked()));

  // Create icon (if mapping is available)
  {
    QLabel* iconLabel = new QLabel();
    iconLabel->setPixmap(IntrinsicEd::getPixmap(p_Title));
    frame->layout()->addWidget(iconLabel);
  }

  frame->layout()->addWidget(label);
  frame->layout()->addItem(
      new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
  frame->setStyleSheet(
      ".QFrame { border: 1px solid #2d3339; border-radius: 6px;"
      "background-color: qlineargradient(x1: 0 y1: 0, x2: 0 "
      "y2: 1, stop: 0 #22262a, stop: 1 #202428); }");

  return frame;
}

void IntrinsicEdPropertyView::clearAndUpdatePropertyView()
{
  if (_propertyDocument.Empty())
  {
    return;
  }

  clearPropertyView();
  _propertyEntries.clear();

  const char* categories[16] = {};
  uint32_t categoryCount = 0u;

  // Collect categories and compile properties
  for (uint32_t ci = 0u; ci < _currentPropertyCompilerEntries.size(); ++ci)
  {
    rapidjson::Value& currentProperties = _propertyDocument[ci];
    currentProperties.RemoveAllMembers();

    Dod::PropertyCompilerEntry& compilerEntry =
        _currentPropertyCompilerEntries[ci];

    // Compile properties
    {
      compilerEntry.compileFunction(compilerEntry.ref, true, currentProperties,
                                    _propertyDocument);
    }

    // Collect categories
    for (auto it = currentProperties.MemberBegin();
         it != currentProperties.MemberEnd(); ++it)
    {
      const rapidjson::Value& prop = it->value;
      if (!prop.IsObject() || !prop.HasMember("cat") ||
          prop["internal"].GetBool())
      {
        continue;
      }

      const char* cat = prop["cat"].GetString();

      bool found = false;
      for (uint32_t i = 0; i < categoryCount; ++i)
      {
        if (strcmp(categories[i], cat) == 0u)
        {
          found = true;
          break;
        }
      }

      if (!found)
      {
        categories[categoryCount++] = cat;
      }
    }
  }

  // Create category headers and add property editors
  for (uint32_t ci = 0u; ci < categoryCount; ++ci)
  {
    bool collapsedState = false;

    if (!_ui.autoCollapseButton->isChecked())
    {
      auto collapsedStateIt = _categoryCollapsedState.find(categories[ci]);
      if (collapsedStateIt != _categoryCollapsedState.end())
      {
        collapsedState = collapsedStateIt->second;
      }
      else
      {
        _categoryCollapsedState[categories[ci]] = false;
      }
    }
    else
    {
      collapsedState = _currentCategory != categories[ci];
    }

    QFrame* header = createCategoryHeaderWidget(categories[ci], collapsedState);
    _ui.scrollAreaWidgetContents->layout()->addWidget(header);

    if (collapsedState)
    {
      continue;
    }

    for (uint32_t pi = 0u; pi < _currentPropertyCompilerEntries.size(); ++pi)
    {
      rapidjson::Value& currentProperties = _propertyDocument[pi];

      for (auto it = currentProperties.MemberBegin();
           it != currentProperties.MemberEnd(); ++it)
      {
        const rapidjson::Value& prop = it->value;
        if (!prop.IsObject() || !prop.HasMember("cat"))
        {
          continue;
        }

        const char* cat = prop["cat"].GetString();

        if (prop["internal"].GetBool() || strcmp(categories[ci], cat) != 0u)
        {
          continue;
        }

        const char* editor = prop["editor"].GetString();

        IntrinsicEdPropertyEditorBase* propertyEditor = nullptr;
        if (strcmp(editor, "vec2") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorVec2(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "vec3") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorVec3(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "vec4") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorVec4(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "string") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorString(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "enum") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorEnum(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "float") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorFloat(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "uint") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorUInt(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "rotation") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorRotation(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "meshSelector") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorResourceSelector(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString(), "Mesh");
        }
        else if (strcmp(editor, "scriptSelector") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorResourceSelector(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString(), "Script");
        }
        else if (strcmp(editor, "flags") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorFlags(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }
        else if (strcmp(editor, "color") == 0u)
        {
          propertyEditor = new IntrinsicEdPropertyEditorColor(
              &_propertyDocument, &currentProperties, &it->value,
              it->name.GetString());
        }

        if (propertyEditor)
        {
          QObject::connect(propertyEditor,
                           SIGNAL(valueChanged(rapidjson::Value&)), this,
                           SLOT(onValueChanged(rapidjson::Value&)));

          _ui.scrollAreaWidgetContents->layout()->addWidget(propertyEditor);
          _propertyEntries.push_back(
              {propertyEditor, pi, it->name.GetString()});
        }
      }
    }
  }

  QSpacerItem* spacer =
      new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
  _ui.scrollAreaWidgetContents->layout()->addItem(spacer);
}

void IntrinsicEdPropertyView::updatePropertyView()
{
  for (uint32_t ci = 0u; ci < _currentPropertyCompilerEntries.size(); ++ci)
  {
    rapidjson::Value& currentProperties = _propertyDocument[ci];
    currentProperties.RemoveAllMembers();

    Dod::PropertyCompilerEntry& compilerEntry =
        _currentPropertyCompilerEntries[ci];

    // Compile properties
    {
      compilerEntry.compileFunction(compilerEntry.ref, true, currentProperties,
                                    _propertyDocument);
    }
  }

  for (uint32_t i = 0u; i < _propertyEntries.size(); ++i)
  {
    const PropertyEntry& entry = _propertyEntries[i];
    rapidjson::Value& currentProperties =
        _propertyDocument[entry.propertyCompilerEntryIdx];

    entry.propertyEditor->init(&_propertyDocument, &currentProperties,
                               &currentProperties[entry.propertyName.c_str()],
                               entry.propertyName.c_str());
    entry.propertyEditor->updateFromProperty();
  }
}

void IntrinsicEdPropertyView::clearPropertySet()
{
  _currentPropertyCompilerEntries.clear();
  _currentManagerEntries.clear();
}

void IntrinsicEdPropertyView::addPropertySet(
    const Dod::PropertyCompilerEntry& p_Entry,
    const Dod::ManagerEntry& p_ManagerEntry)
{
  _currentPropertyCompilerEntries.push_back(p_Entry);
  _currentManagerEntries.push_back(p_ManagerEntry);
}

void IntrinsicEdPropertyView::onValueChanged(rapidjson::Value& p_Properties)
{
  for (uint32_t i = 0u; i < _propertyDocument.Size(); ++i)
  {
    rapidjson::Value& currentProperties = _propertyDocument[i];
    if (currentProperties == p_Properties)
    {
      Dod::PropertyCompilerEntry& entry = _currentPropertyCompilerEntries[i];
      Dod::ManagerEntry& managerEntry = _currentManagerEntries[i];

      entry.initFunction(entry.ref, true, p_Properties);

      // Recreate resources if available
      if (managerEntry.destroyResourcesFunction &&
          managerEntry.createResourcesFunction)
      {
        Dod::RefArray refs = {entry.ref};
        managerEntry.destroyResourcesFunction(refs);
        managerEntry.createResourcesFunction(refs);
      }

      if (managerEntry.onPropertyUpdateFinishedFunction)
      {
        managerEntry.onPropertyUpdateFinishedFunction(entry.ref);
      }
    }
  }
}

void IntrinsicEdPropertyView::onCategoryHeaderClicked()
{
  QPushButton* categoryHeaderButton = (QPushButton*)QObject::sender();
  const QFrame* categoryHeader = (QFrame*)categoryHeaderButton->parent();

  for (uint32_t i = 0; i < (uint32_t)categoryHeader->layout()->count(); ++i)
  {
    QLayoutItem* layoutItem = categoryHeader->layout()->itemAt(i);

    if (layoutItem->widget()->objectName() == "title")
    {
      QLabel* label = (QLabel*)layoutItem->widget();
      _INTR_STRING cat = _INTR_STRING(label->text().toStdString().c_str());

      auto collapsedStateIt = _categoryCollapsedState.find(cat);
      if (collapsedStateIt != _categoryCollapsedState.end())
      {
        collapsedStateIt->second = !collapsedStateIt->second;
      }
      else
      {
        _categoryCollapsedState[cat] = true;
      }

      if (_categoryCollapsedState[cat])
      {
        categoryHeaderButton->setIcon(QIcon(":/Icons/roundRight"));
      }
      else
      {
        categoryHeaderButton->setIcon(QIcon(":/Icons/roundDown"));
      }

      _currentCategory = cat;
      break;
    }
  }

  clearAndUpdatePropertyView();
}

redpajama_set_name: RedPajamaGithub


text:
package classloading.domain;

import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.MutableEntry;
import java.io.Serializable;

public class PersonEntryProcessor implements EntryProcessor<String, Person, Person>, Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public Person process(MutableEntry<String, Person> entry, Object... arguments) throws EntryProcessorException {
        return new Person();
    }
}


text:
---
title: use-isnan - Rules
layout: doc
edit_link: https://github.com/eslint/eslint/edit/master/docs/rules/use-isnan.md
rule_type: problem
---
<!-- Note: No pull requests accepted for this file. See README.md in the root directory for details. -->

# require calls to `isNaN()` when checking for `NaN` (use-isnan)

(recommended) The `"extends": "eslint:recommended"` property in a configuration file enables this rule.

In JavaScript, `NaN` is a special value of the `Number` type. It's used to represent any of the "not-a-number" values represented by the double-precision 64-bit format as specified by the IEEE Standard for Binary Floating-Point Arithmetic.

Because `NaN` is unique in JavaScript by not being equal to anything, including itself, the results of comparisons to `NaN` are confusing:

* `NaN === NaN` or `NaN == NaN` evaluate to false
* `NaN !== NaN` or `NaN != NaN` evaluate to true

Therefore, use `Number.isNaN()` or global `isNaN()` functions to test whether a value is `NaN`.

## Rule Details

This rule disallows comparisons to 'NaN'.

Examples of **incorrect** code for this rule:

```js
/*eslint use-isnan: "error"*/

if (foo == NaN) {
    // ...
}

if (foo != NaN) {
    // ...
}
```

Examples of **correct** code for this rule:

```js
/*eslint use-isnan: "error"*/

if (isNaN(foo)) {
    // ...
}

if (!isNaN(foo)) {
    // ...
}
```

## Version

This rule was introduced in ESLint 0.0.6.

## Resources

* [Rule source](https://github.com/eslint/eslint/tree/master/lib/rules/use-isnan.js)
* [Documentation source](https://github.com/eslint/eslint/tree/master/docs/rules/use-isnan.md)


text:
onmessage = function(e) {
  postMessage('load');
}


text:
import { NgModule } from '@angular/core';
import { FacetsBoxComponent } from './facets-box/facets-box.component';
import { SearchBoxComponent } from './search-box/search-box.component';
import { SearchResultsComponent } from './search-results/search-results.component';
import { SearchService } from './shared/search.service';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatSortHeaderIntl } from '@angular/material/sort';
import { MaterialConfigModule } from '../routing/material-config.module';
import { SharedModule } from '../shared/shared.module';
import { UserModule } from '../user/user.module';
import { SearchPageComponent } from './search-page/search-page.component';
import { SearchDaterangePickerComponent } from './search-daterange-picker/search-daterange-picker.component';
import { NgxDaterangepickerMd } from 'ngx-daterangepicker-material';
import { DisplaySearchPageComponent } from './display-search-page/display-search-page.component';
import { DocumentDisplayerComponent } from './document-displayer/document-displayer.component';

@NgModule({
  imports: [
    SharedModule,
    FormsModule,
    ReactiveFormsModule,
    MaterialConfigModule,
    NgxDaterangepickerMd.forRoot(),
    UserModule,
  ],
  exports: [SearchPageComponent, DisplaySearchPageComponent, SearchResultsComponent],
  declarations: [
    SearchDaterangePickerComponent,
    SearchPageComponent,
    DisplaySearchPageComponent,
    SearchResultsComponent,
    SearchBoxComponent,
    FacetsBoxComponent,
    DocumentDisplayerComponent,
  ],
  providers: [SearchService, MatSortHeaderIntl],
})
export class SearchModule {}


text:
#include "config.h"

#if HAVE_NL_TYPES_H
#include <nl_types.h>  /* for nl_item */
#endif

#if HAVE_LANGINFO_H
#include <langinfo.h>  /* for nl_langinfo */
#endif

#if HAVE_LOCALE_H
#include <locale.h>    /* for lconv */
#endif

/* Replace the libc localeconv and nl_langinfo with ones we can manipulate.

   This is done in a C file since if it was in a C++ file then we'd have to
   match the "throw" or lack thereof declared for localeconv in <locale.h>.
   g++ 3.2 gives an error about mismatched throws under "-pedantic", other
   C++ compilers may very possibly do so too.  */

extern char point_string[];

#if HAVE_LOCALECONV && ! defined __MINGW32__
struct lconv *
localeconv (void)
#if defined __cplusplus && defined __GLIBC__
  throw()
#endif
{
  static struct lconv  l;
  l.decimal_point = point_string;
  return &l;
}
#endif

#if HAVE_NL_LANGINFO
char *
nl_langinfo (nl_item n)
#if defined __cplusplus && defined __GLIBC__
  throw()
#endif
{
  return point_string;
}
#endif


text:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="CoreNLP-to-HTML.xsl" type="text/xsl"?>
<root>
  <document>
    <sentences>
      <sentence id="1">
        <tokens>
          <token id="1">
            <word>reach</word>
            <lemma>reach</lemma>
            <CharacterOffsetBegin>0</CharacterOffsetBegin>
            <CharacterOffsetEnd>5</CharacterOffsetEnd>
            <POS>VB</POS>
          </token>
          <token id="2">
            <word>for</word>
            <lemma>for</lemma>
            <CharacterOffsetBegin>6</CharacterOffsetBegin>
            <CharacterOffsetEnd>9</CharacterOffsetEnd>
            <POS>IN</POS>
          </token>
          <token id="3">
            <word>the</word>
            <lemma>the</lemma>
            <CharacterOffsetBegin>10</CharacterOffsetBegin>
            <CharacterOffsetEnd>13</CharacterOffsetEnd>
            <POS>DT</POS>
          </token>
          <token id="4">
            <word>moon</word>
            <lemma>moon</lemma>
            <CharacterOffsetBegin>14</CharacterOffsetBegin>
            <CharacterOffsetEnd>18</CharacterOffsetEnd>
            <POS>NN</POS>
          </token>
          <token id="5">
            <word>dreams</word>
            <lemma>dream</lemma>
            <CharacterOffsetBegin>19</CharacterOffsetBegin>
            <CharacterOffsetEnd>25</CharacterOffsetEnd>
            <POS>VBZ</POS>
          </token>
        </tokens>
        <parse>(ROOT (S (VP (VB reach) (SBAR (IN for) (S (NP (DT the) (NN moon)) (VP (VBZ dreams))))))) </parse>
        <dependencies type="basic-dependencies">
          <dep type="root">
            <governor idx="0">ROOT</governor>
            <dependent idx="1">reach</dependent>
          </dep>
          <dep type="mark">
            <governor idx="5">dreams</governor>
            <dependent idx="2">for</dependent>
          </dep>
          <dep type="det">
            <governor idx="4">moon</governor>
            <dependent idx="3">the</dependent>
          </dep>
          <dep type="nsubj">
            <governor idx="5">dreams</governor>
            <dependent idx="4">moon</dependent>
          </dep>
          <dep type="advcl">
            <governor idx="1">reach</governor>
            <dependent idx="5">dreams</dependent>
          </dep>
        </dependencies>
        <dependencies type="collapsed-dependencies">
          <dep type="root">
            <governor idx="0">ROOT</governor>
            <dependent idx="1">reach</dependent>
          </dep>
          <dep type="mark">
            <governor idx="5">dreams</governor>
            <dependent idx="2">for</dependent>
          </dep>
          <dep type="det">
            <governor idx="4">moon</governor>
            <dependent idx="3">the</dependent>
          </dep>
          <dep type="nsubj">
            <governor idx="5">dreams</governor>
            <dependent idx="4">moon</dependent>
          </dep>
          <dep type="advcl">
            <governor idx="1">reach</governor>
            <dependent idx="5">dreams</dependent>
          </dep>
        </dependencies>
        <dependencies type="collapsed-ccprocessed-dependencies">
          <dep type="root">
            <governor idx="0">ROOT</governor>
            <dependent idx="1">reach</dependent>
          </dep>
          <dep type="mark">
            <governor idx="5">dreams</governor>
            <dependent idx="2">for</dependent>
          </dep>
          <dep type="det">
            <governor idx="4">moon</governor>
            <dependent idx="3">the</dependent>
          </dep>
          <dep type="nsubj">
            <governor idx="5">dreams</governor>
            <dependent idx="4">moon</dependent>
          </dep>
          <dep type="advcl">
            <governor idx="1">reach</governor>
            <dependent idx="5">dreams</dependent>
          </dep>
        </dependencies>
      </sentence>
    </sentences>
  </document>
</root>


text:
namespace PhysX
{
	ref class Physics;
	ref class Scene;

	/// <summary>
	/// Collection class for serialization.
	/// A collection is a container for serializable SDK objects.
	/// All serializable SDK objects inherit from PxSerializable. Serialization and deserialization only work through collections.
	/// </summary>
	public ref class Collection : IDisposable
	{
	public:
		virtual event EventHandler^ OnDisposing;
		virtual event EventHandler^ OnDisposed;

	private:
		PxCollection* _collection;

	internal:
		Collection(PxCollection* collection, IDisposable^ owner);
	public:
		~Collection();
	protected:
		!Collection();
	public:
		property bool Disposed
		{
			virtual bool get();
		}

		[Obsolete]
		void Add(IPhysXEntity^ object);
		void AddId(IPhysXEntity^ object, long reference);
		[Obsolete]
		void AddRequired(IPhysXEntity^ object);

		[Obsolete]
		bool Contains(IPhysXEntity^ object);

		IPhysXEntity^ GetObject(int index);

		void Remove(IPhysXEntity^ object);
		void RemoveId(long reference);

		/// <summary>
		/// Gets number of objects in the collection.
		/// </summary>
		property int NumberOfObjects
		{
			int get();
		}

		property int NumberOfIds
		{
			int get();
		}

	internal:
		property PxCollection* UnmanagedPointer
		{
			PxCollection* get();
		}
	};
};


text:
package net.dongliu.apk.parser.utils.xml;

import net.dongliu.apk.parser.utils.xml.CodePointTranslator;

import java.io.IOException;
import java.io.Writer;

/**
 * Helper subclass to CharSequenceTranslator to remove unpaired surrogates.
 */
class UnicodeUnpairedSurrogateRemover extends CodePointTranslator {
    /**
     * Implementation of translate that throws out unpaired surrogates.
     * {@inheritDoc}
     */
    @Override
    public boolean translate(int codepoint, Writer out) throws IOException {
        if (codepoint >= Character.MIN_SURROGATE && codepoint <= Character.MAX_SURROGATE) {
            // It's a surrogate. Write nothing and say we've translated.
            return true;
        } else {
            // It's not a surrogate. Don't translate it.
            return false;
        }
    }
}


text:
go_start
========

Learning Go


text:
=pod

=head1 NAME

RAND_load_file, RAND_write_file, RAND_file_name - PRNG seed file

=head1 SYNOPSIS

 #include <openssl/rand.h>

 const char *RAND_file_name(char *buf, size_t num);

 int RAND_load_file(const char *filename, long max_bytes);

 int RAND_write_file(const char *filename);

=head1 DESCRIPTION

RAND_file_name() generates a default path for the random seed
file. B<buf> points to a buffer of size B<num> in which to store the
filename. The seed file is $RANDFILE if that environment variable is
set, $HOME/.rnd otherwise. If $HOME is not set either, or B<num> is
too small for the path name, an error occurs.

RAND_load_file() reads a number of bytes from file B<filename> and
adds them to the PRNG. If B<max_bytes> is non-negative,
up to to B<max_bytes> are read; starting with OpenSSL 0.9.5,
if B<max_bytes> is -1, the complete file is read.

RAND_write_file() writes a number of random bytes (currently 1024) to
file B<filename> which can be used to initialize the PRNG by calling
RAND_load_file() in a later session.

=head1 RETURN VALUES

RAND_load_file() returns the number of bytes read.

RAND_write_file() returns the number of bytes written, and -1 if the
bytes written were generated without appropriate seed.

RAND_file_name() returns a pointer to B<buf> on success, and NULL on
error.

=head1 SEE ALSO

L<rand(3)|rand(3)>, L<RAND_add(3)|RAND_add(3)>, L<RAND_cleanup(3)|RAND_cleanup(3)>

=head1 HISTORY

RAND_load_file(), RAND_write_file() and RAND_file_name() are available in
all versions of SSLeay and OpenSSL.

=cut


text:
var load = function() {

    console.log("load, biatch!");

}

exports.load = load;


text:
<?php
/**
 * Отображение для blogBackend/_search:
 *
 *   @category YupeView
 *   @package  yupe
 *   @author   Yupe Team <team@yupe.ru>
 *   @license  https://github.com/yupe/yupe/blob/master/LICENSE BSD
 *   @link     http://yupe.ru
 **/
$form = $this->beginWidget(
    'bootstrap.widgets.TbActiveForm', array(
        'action'      => Yii::app()->createUrl($this->route),
        'method'      => 'get',
        'type'        => 'vertical',
        'htmlOptions' => array('class' => 'well'),
    )
);

?>

    <fieldset class="inline">

                <?php echo $form->textFieldRow($model, 'name', array('class' => 'popover-help', 'maxlength' => 300, 'size' => 60, 'data-original-title' => $model->getAttributeLabel('name'), 'data-content' => $model->getAttributeDescription('name'))); ?>

                <?php echo $form->textFieldRow($model, 'slug', array('class' => 'popover-help', 'maxlength' => 150, 'size' => 60, 'data-original-title' => $model->getAttributeLabel('slug'), 'data-content' => $model->getAttributeDescription('slug'))); ?>

                <?php echo $form->dropDownListRow($model, 'type', $model->getTypeList(), array('empty' => '----', 'class' => 'popover-help', 'data-original-title' => $model->getAttributeLabel('type'), 'data-content' => $model->getAttributeDescription('type'))); ?>

                <?php echo $form->dropDownListRow($model, 'status', $model->getStatusList(), array('empty' => '----', 'class' => 'popover-help', 'data-original-title' => $model->getAttributeLabel('status'), 'data-content' => $model->getAttributeDescription('status'))); ?>

            <?php echo $form->textFieldRow($model, 'description', array('class' => 'span7 popover-help', 'maxlength' => 300, 'size' => 60, 'data-original-title' => $model->getAttributeLabel('description'), 'data-content' => $model->getAttributeDescription('description'))); ?>

    </fieldset>

    <?php
    $this->widget(
        'bootstrap.widgets.TbButton', array(
            'type'        => 'primary',
            'encodeLabel' => false,
            'buttonType'  => 'submit',
            'label'       => '  ' . Yii::t('BlogModule.blog', 'Find a blog'),
        )
    ); ?>

<?php $this->endWidget(); ?>


text:
from nose.tools import ok_
from rest_framework.generics import GenericAPIView

from django.contrib.auth.models import AnonymousUser

from amo.tests import TestCase
from mkt.access.middleware import ACLMiddleware
from mkt.feed.authorization import FeedAuthorization
from mkt.site.fixtures import fixture
from test_utils import RequestFactory
from mkt.users.models import UserProfile

class TestFeedAuthorization(TestCase):
    auth_class = FeedAuthorization
    fixtures = fixture('user_2519')

    def setUp(self):
        super(TestFeedAuthorization, self).setUp()
        self.auth = self.auth_class()
        self.user = UserProfile.objects.get(pk=2519)
        self.profile = self.user
        self.view = GenericAPIView()

    def give_permission(self):
        self.grant_permission(self.profile, 'Feed:Curate')

    def request(self, verb, anon=False):
        request = getattr(RequestFactory(), verb.lower())('/')
        request.user = AnonymousUser() if anon else self.user
        ACLMiddleware().process_request(request)
        return request

    def is_authorized(self, request):
        return self.auth.has_permission(request, self.view)

    def test_get(self):
        ok_(self.is_authorized(self.request('GET', anon=True)))
        ok_(self.is_authorized(self.request('GET')))

    def test_get_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('GET')))

    def test_head(self):
        ok_(self.is_authorized(self.request('HEAD', anon=True)))
        ok_(self.is_authorized(self.request('HEAD')))

    def test_head_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('HEAD')))

    def test_options(self):
        ok_(self.is_authorized(self.request('OPTIONS', anon=True)))
        ok_(self.is_authorized(self.request('OPTIONS')))

    def test_options_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('OPTIONS')))

    def test_post(self):
        ok_(not self.is_authorized(self.request('POST', anon=True)))
        ok_(not self.is_authorized(self.request('POST')))

    def test_post_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('POST')))

    def test_patch(self):
        ok_(not self.is_authorized(self.request('PATCH', anon=True)))
        ok_(not self.is_authorized(self.request('PATCH')))

    def test_patch_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('PATCH')))

    def test_put(self):
        ok_(not self.is_authorized(self.request('PUT', anon=True)))
        ok_(not self.is_authorized(self.request('PUT')))

    def test_put_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('PUT')))

    def test_delete(self):
        ok_(not self.is_authorized(self.request('DELETE', anon=True)))
        ok_(not self.is_authorized(self.request('DELETE')))

    def test_delete_permission(self):
        self.give_permission()
        ok_(self.is_authorized(self.request('DELETE')))


text:
using Xunit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.SpanTests
{
    public static partial class MemoryMarshalTests
    {
        [Fact]
        public static void ReadOnlySpan_AsBytesUIntToByte()
        {
            uint[] a = { 0x44332211, 0x88776655 };
            ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
            ReadOnlySpan<byte> asBytes = MemoryMarshal.AsBytes<uint>(span);

            Assert.True(Unsafe.AreSame(ref Unsafe.As<uint, byte>(ref Unsafe.AsRef(in MemoryMarshal.GetReference(span))), ref Unsafe.AsRef(in MemoryMarshal.GetReference(asBytes))));
            asBytes.Validate<byte>(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
        }

        [Fact]
        public static void ReadOnlySpan_AsBytesContainsReferences()
        {
            ReadOnlySpan<TestHelpers.StructWithReferences> span = new ReadOnlySpan<TestHelpers.StructWithReferences>(Array.Empty<TestHelpers.StructWithReferences>());
            TestHelpers.AssertThrows<ArgumentException, TestHelpers.StructWithReferences>(span, (_span) => MemoryMarshal.AsBytes(_span).DontBox());
        }
    }
}


text:
"""
    SAT Competition 2011 results import script
"""
import os, sys, datetime
sys.path.append("..") # append parent directory to python path to be able to import edacc when running from ./scripts
from edacc import models, config

config.DATABASE_HOST = "localhost"
models.add_database("user", "pw", "db", "label")
db = models.get_database("db")

# mapping the "checked answer" column to EDACC's status/resultCode codes
ANSWER_MAP = {
    'UNKNOWN':          (1, 0),
    'UNKNOWN EXCODE':   (1, 0),
    'UNKNOWN TO':       (21, -21),
    'UNKNOWN MO':       (23, -23),
    'UNKNOWN (?)':      (1, 0),
    'SIGNAL':           (21, -309),
    'SAT':              (1, 11),
    'UNSAT':            (1, 10),
    'ERR WRONGCERT':    (1, 0),
    'ERR NOCERT':       (1, 0),
    'ERR UNSAT':        (1, 0),
}

def parse_phase_txt(filepath, phase):
    experiments = {'RANDOM': db.Experiment(), 'CRAFTED': db.Experiment(), 'APPLICATION': db.Experiment(), 'MUS': db.Experiment()}
    for exp in experiments.keys():
        experiments[exp].name = 'phase' + str(phase) + ' ' + exp
        experiments[exp].description = 'imported'
        experiments[exp].date = datetime.datetime.now()
        experiments[exp].active = True
        experiments[exp].priority = 0
        experiments[exp].configurationExp = False
        db.session.add(experiments[exp])

    db.session.commit()

    with open(filepath) as fh:
        num_lines = sum(1 for line in fh)

    print "Parsing results"
    i = 0
    solvers = {}
    solver_configs = {}
    instances = {}
    with open(filepath) as fh:
        fh.readline()
        fh.readline()
        for line in fh:
            i += 1
            if i % 10000 == 0: print i, '/', num_lines-2
            data = map(str.strip, line.split('|'))
            category, instance_path, answer, cputime, walltime, \
                solver_name, solver_version =   data[0], 'SATCompetition2011/' + data[1], data[2], float(data[3]), float(data[4]), \
                                                data[5], data[6]

            if instance_path not in instances:
                # new instance, add to cache
                instance_class_hierarchy = instance_path.split('/')[:-1]
                instance_name = instance_path.split('/')[-1]
                instances[instance_path] = db.session.query(db.Instance).filter_by(name=instance_name).first()

                if instances[instance_path] is None:
                    # instance is not in the database yet, which is bad ...
                    print "Instance " + instance_path + " hasn't been found in the database! aborting ..."
                    inst = db.Instance()
                    inst.name = instance_name
                    inst.instance = "dummy"
                    inst.md5 = instance_path[:58]
                    inst.instance_class = db.session.query(db.InstanceClass).first()
                    db.session.add(inst)

                experiments[category].instances.append(instances[instance_path])

            solver_ident = (solver_name, solver_version)
            if not solver_ident in solvers: # new solver
                ex_solver = db.session.query(db.SolverBinary).filter_by(binaryName=solver_name, version=solver_version).first()
                if ex_solver:
                    solver = ex_solver
                else:
                    solver = db.Solver()
                    solver.name = solver_name
                    solver.authors = "-"
                    solver.description = "-"
                    db.session.add(solver)
                    solver_binary = db.SolverBinary()
                    solver_binary.binaryName = solver_name
                    solver_binary.binaryArchive = "dummy"
                    solver_binary.md5 = "dummy"
                    solver_binary.version = solver_version
                    solver_binary.runCommand = "dummy"
                    solver_binary.runPath = "dummy"
                    solver_binary.solver = solver
                    db.session.add(solver_binary)

                solvers[solver_ident] = solver_binary
                solver = solver_binary
            else:
                solver = solvers[solver_ident]

            solver_config_ident = (category, solver_name, solver_version)
            if not solver_config_ident in solver_configs:
                # create solver config and add it to the experiment
                solver_config = db.SolverConfiguration()
                solver_config.solver_binary = solver
                solver_config.name = solver_name + " " + solver_version
                solver_config.seed_group = 0
                solver_config.experiment = experiments[category]
                db.session.add(solver_config)
                solver_configs[solver_config_ident] = solver_config

            experiment = experiments[category]
            solver_config = solver_configs[(category, solver_name, solver_version)]
            instance = instances[instance_path]

            er = db.ExperimentResult()
            er.instance = instance
            er.experiment = experiment
            er.solver_configuration = solver_config
            er.status = ANSWER_MAP[answer][0]
            er.resultCode = ANSWER_MAP[answer][1]
            er.seed = 0
            er.run = 0
            er.resultTime = cputime
            er.date_modified = datetime.datetime.now()
            er.priority = 0
            er.CPUTimeLimit = 5000
            er.wallClockTimeLimit = er.memoryLimit = er.stackSizeLimit = er.outputSizeLimitFirst = er.outputSizeLimitLast = -1
            ero = db.ExperimentResultOutput()
            ero.result = er
            db.session.add(er)
            db.session.add(ero)

    print "Writing to DB"
    db.session.commit()
    db.session.flush()

if __name__ == '__main__':
    import time
    dir = raw_input("Warning: Existing experiments and solvers will be deleted!! Enter phase files directory: ")
    start_time = time.clock()
    db.session.query(db.ExperimentResult).delete()
    db.session.query(db.Experiment).delete()
    db.session.query(db.Solver).delete()
    db.session.commit()
    #print "Importing phase1.txt"
    #parse_phase_txt(os.path.join(dir, "phase1.txt"), 1)
    print "Importing phase2.txt\n==========================================="
    parse_phase_txt(os.path.join(dir, "phase2.txt"), 2)
    print "Done. Imported data in ", time.clock() - start_time, "s"


text:
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.wiki.webservice.data.config.auto;

import com.yahoo.bard.webservice.data.config.names.TableName;
import com.yahoo.bard.webservice.data.time.TimeGrain;

import org.apache.druid.timeline.DataSegment;

import java.util.List;
import java.util.Set;

/**
 * Holds the minimum necessary configuration to set up fili to
 * make requests to druid. This defines all metrics, dimensions, *one*
 * valid time grain, and data segments of a datasource.
 * Note the restriction to one time grain, a druid datasource could have
 * more than one, but it *should* have just a single grain.
 */
public interface DataSourceConfiguration {
    /**
     * Gets the name of a datasource as would be stored in Druid.
     *
     * @return the name of the datasource.
     */
    String getName();

    /**
     * Gets the name of the datasource to be used as a {@link TableName} in fili.
     *
     * @return the {@link TableName} for this datasource.
     */
    TableName getTableName();

    /**
     * Gets the names of all the metrics for the current datasource.
     *
     * @return a set of names of metrics for the current datasource.
     */
    Set<String> getMetrics();

    /**
     * Gets the names of all the dimensions for the current datasource.
     *
     * @return a set of names of dimensions for the current datasource.
     */
    Set<String> getDimensions();

    /**
     * Gets the {@link TimeGrain} which is valid for use in queries.
     * Note: In theory, a Datasource in Druid could have more than 1 grain at
     * different times, but for now we only want to support one.
     *
     * @return a {@link TimeGrain} for the current table.
     */
    TimeGrain getValidTimeGrain();

    /**
     * Gets a list of {@link org.apache.druidtimeline.DataSegment data segments} for a datasource in Druid.
     *
     * @return the list of data segments reported by druid.
     */
    List<DataSegment> getDataSegments();
}


text:
<title>Project Euler work</title>
This folder contains some of my work on Project Euler. Append "problem.pdf" to the directory, so Problem 1 can be accessed at www.patrickstevens.co.uk/ProjectEuler/1.pdf .


text:
//
// Translated by CS2J (http://www.cs2j.com): 12.11.2016 09:40:33
//

package AutoDiff;

/**
* Represents a constraint utility function.
*/
public class ConstraintUtility  extends Term
{
    /**
    * Constructs a new instance of the
    *  {@link #LTConstraint}
    *  type.
    *
    *  @param constraint The constraint term
    *  @param utility The utility term
    */
    public ConstraintUtility(Term constraint, Term utility) throws Exception {
        setConstraint(constraint);
        setUtility(utility);
    }

    /**
    * Gets the constraint.
    */
    private Term Constraint;
    public Term getConstraint() {
        return Constraint;
    }

    public void setConstraint(Term value) {
        Constraint = value;
    }

    /**
    * Gets the utility.
    */
    private Term Utility;
    public Term getUtility() {
        return Utility;
    }

    public void setUtility(Term value) {
        Utility = value;
    }

//    /**
//    * Accepts a term visitor
//    *
//    *  @param visitor The term visitor to accept
//    */
//    public void accept(ITermVisitor visitor) throws Exception {
//        visitor.visit(this);
//    }

    /**
    * Accepts a term visitor with a generic result
    * The type of the result from the visitor's function
    *  @param visitor The visitor to accept
    *  @return
    * The result from the visitor's visit function.
    */
    public <TResult>TResult accept(ITermVisitor<TResult> visitor) throws Exception {
        return visitor.visit(this);
    }

    public Term negate() throws Exception {
        throw new Exception("Do not negate a Constraint Utility");
    }

    //return this;
    public Term aggregateConstants() throws Exception {
        setConstraint(getConstraint().aggregateConstants());
        setUtility(getUtility().aggregateConstants());
        return this;
    }

    public Term derivative(Variable v) throws Exception {
        throw new Exception("Symbolic Derivation of ConstraintUtility not supported.");
    }

    public String toString() {
        try
        {
            return String.format("[ConstraintUtility: Constraint=%s, Utility=%s]", getConstraint(), getUtility());
        }
        catch (RuntimeException exception)
        {
            throw exception;
        }
        catch (Exception exception)
        {
            throw new RuntimeException(exception);
        }

    }

}


text:
 #define COREBLUETOOTH_IMPEXP __declspec(dllimport)
#endif

#ifndef COREBLUETOOTH_EXPORT
 #ifdef __cplusplus
  #define COREBLUETOOTH_EXPORT COREBLUETOOTH_IMPEXP extern "C"
  #define COREBLUETOOTH_EXPORT_CLASS COREBLUETOOTH_IMPEXP
 #else
  #define COREBLUETOOTH_EXPORT COREBLUETOOTH_IMPEXP extern
  #define COREBLUETOOTH_EXPORT_CLASS COREBLUETOOTH_IMPEXP
 #endif
#endif


text:
> Be able to:
> * add two unsigned binary integers
> * multiply two unsigned binary integers.

## Addition

Adding two binary integers is similar to adding two positive decimal integers.
Each bit is added in turn starting from the right most bit (least significant) and continuing to the most significant bit (MSB).

### Rules for addition

* When two bits are both `0`, the sum is 0.
* When one of the bits is `1` and the other is `0` then the sum is `1`.
* When both bits are `1` then you must carry a `1` into the next bit's sum and then place a `0` in the current bit

**Example**

`1 0 0 0 1 1 0 1` **+** `0 1 1 0 1 0 0 0`

```
	1 0 0 0 1 1 0 1
+	0 1 1 0 1 0 0 0
-----------------------
Sum:	1 1 1 0 0 1 0 1
Carry:        1
Total:  1 1 1 1 0 1 0 1
```

## Multiplication

Binary multiplication is very simple. To multiply two binary integers you sum the values of shifting the first number by every 1 in the second number. It is similar to decimal multiplication.

### Rules for multiplication

* When multiplying by a 0 bit the value is all 0's
* When multiplying by a 1 bit, the value is the original number shifted by the number of bit up to the 1 bit
* Remember to sum all the numbers at the end

**Example**

`1 0 0 0 1 1 0 1` **X** `0 0 0 0 1 0 0 1`

```
           1 0 0 0 1 1 0 1
x          0 0 0 0 1 0 0 1
--------------------------
           1 0 0 0 1 1 0 1
   1 0 0 0 1 1 0 1 0 0 0 0
--------------------------
   1 0 0 1 0 1 0 1 1 1 0 1
```


text:
SYNONYM

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Nouv. Fl. Environs Paris 45 (1821)

#### Original name
Boletus lividus Bull., 1791

### Remarks
null


text:
using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;

namespace XenAPI
{
    public partial class Failure : Exception
    {
        public const string INTERNAL_ERROR = "INTERNAL_ERROR";
        public const string MESSAGE_PARAMETER_COUNT_MISMATCH = "MESSAGE_PARAMETER_COUNT_MISMATCH";

        private static ResourceManager errorDescriptions = XenAPI.FriendlyErrorNames.ResourceManager;

        private readonly List<string> errorDescription;
        private string errorText;
        private string shortError;

        public List<string> ErrorDescription
        {
            get
            {
                return errorDescription;
            }
        }

        public string ShortMessage
        {
            get
            {
                return shortError;
            }
        }

        public override string Message
        {
            get
            {
                return errorText;
            }
        }

        public Failure(params string[] err)
            : this(new List<string>(err))
        {}

        public Failure(List<string> errDescription)
        {
            errorDescription = errDescription;
            Setup();
        }

        public void Setup()
        {
            if (ErrorDescription.Count > 0)
            {
                try
                {
                    string formatString;
                    try
                    {
                        formatString = errorDescriptions.GetString(ErrorDescription[0]);
                    }
                    catch
                    {
                        formatString = null;
                    }

                    if (formatString == null)
                    {
                        // If we don't have a translation, just combine all the error results from the server
                        List<string> cleanBits = new List<string>();
                        foreach (string s in ErrorDescription)
                        {
                            // Only show non-empty bits of ErrorDescription.
                            // Also, trim the bits, since the server occasionally sends spurious newlines.
                            if (s.Trim().Length > 0)
                            {
                                cleanBits.Add(s.Trim());
                            }
                        }

                        this.errorText = string.Join(" - ", cleanBits.ToArray());
                    }
                    else
                    {

                        // We need a string array to pass to String.Format, and it must not contain the 0th element.

                        string[] objects = new string[ErrorDescription.Count - 1];

                        for (int i = 1; i < ErrorDescription.Count; i++)
                            objects[i - 1] = ErrorDescription[i];

                        this.errorText = String.Format(formatString, objects);
                    }
                }
                catch (Exception)
                {
                    this.errorText = ErrorDescription[0];
                }

                try
                {
                    shortError = errorDescriptions.GetString(ErrorDescription[0] + "-SHORT") ?? errorText;
                }
                catch (Exception)
                {
                    shortError = this.errorText;
                }

                // now try and parse CSLG failures (these have embedded xml)
                TryParseCslg();
            }
        }

        /// <summary>
        /// Tries the parse CSLG failures. These have embedded xml. The useful part (from the user's perspective) is copied to errorText.
        /// </summary>
        /// <returns>A value specifying whether a CSLG error was found.</returns>
        private bool TryParseCslg()
        {
            //failure.ErrorDescription[2]:
            //<StorageLinkServiceError>
            //    <Fault>Host ivory has not yet been added to the service. [err=Object was not found]</Fault>
            //    <Detail>
            //        <errorCode>6</errorCode>
            //        <messageId></messageId>
            //        <defaultMessage>Host ivory has not yet been added to the service. [err=Object was not found]</defaultMessage>
            //        <severity>2</severity>
            //        <errorFunction>CXSSHostUtil::getHost</errorFunction>
            //        <errorLine>113</errorLine>
            //        <errorFile>.\\xss_util_host.cpp</errorFile>
            //    </Detail>
            // </StorageLinkServiceError>

            if (ErrorDescription.Count > 2 && ErrorDescription[2] != null && ErrorDescription[0] != null && ErrorDescription[0].StartsWith("SR_BACKEND_FAILURE"))
            {
                Match m = Regex.Match(ErrorDescription[2], @"<StorageLinkServiceError>.*</StorageLinkServiceError>", RegexOptions.Singleline);

                if (m.Success)
                {
                    XmlDocument doc = new XmlDocument();

                    try
                    {
                        doc.LoadXml(m.Value);
                    }
                    catch (XmlException)
                    {
                        return false;
                    }

                    XmlNodeList nodes = doc.SelectNodes("/StorageLinkServiceError/Fault");

                    if (nodes != null && nodes.Count > 0 && !string.IsNullOrEmpty(nodes[0].InnerText))
                    {
                        if (string.IsNullOrEmpty(errorText))
                        {
                            errorText = nodes[0].InnerText;
                        }
                        else
                        {
                            errorText = string.Format("{0} ({1})", errorText, nodes[0].InnerText);
                        }
                        return true;
                    }
                }
            }
            return false;
        }

        public override string ToString()
        {
            return Message;
        }
    }
}


text:
import PropTypes from 'prop-types';
import React, { Fragment } from 'react';

const WidthComponent = props => (<Fragment>
    {props.width}
</Fragment>);

WidthComponent.propTypes = {
    id: PropTypes.string,
    width: PropTypes.number
};

WidthComponent.defaultProps = {
    width: 0
};

export default WidthComponent;


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Edc.Domain.Common;
using Edc.Domain.Models;
using Edc.Domain.Services.Interfaces;
using Edc.Infrastructure.DataAccess.Interfaces;
using MtcModel;

namespace Edc.Domain.Services
{
    public class DocumentService : IDocumentService
    {
        private readonly IDocumentRepository _documentRepository;
        private readonly IStructureContentRepository _structureContentRepository;

        public DocumentService(IDocumentRepository documentRepository, IStructureContentRepository structureContentRepository)
        {
            _documentRepository = documentRepository;
            _structureContentRepository = structureContentRepository;
        }

        public Document GetById(int id)
        {
           return ModelHelper.Mapper(_documentRepository.GetById(id));
        }

        public Document GetByName(string name)
        {
            throw new NotImplementedException();
        }

        public Document Create(Document document)
        {
            document.DocumentState = DocumentState.Started;

            IEnumerable<Section> allSubsections = document.Sections.Where(s => s.Content != null).SelectMany(s => s.Subsections).ToList();
            document.MaxCycle = DeadlineCalculator.CalculateMaxCycles(document.Deadline, allSubsections.Count());

            DOCUMENT documentToInsert = ModelHelper.Mapper(document);

            IList<STRUCTURECONTENT> documentContent = documentToInsert.STRUCTURECONTENTs.ToList();
            documentToInsert.STRUCTURECONTENTs = null;
            documentToInsert = _documentRepository.Insert(documentToInsert);

            foreach (var structurecontent in documentContent)
            {
                structurecontent.DocumentId = documentToInsert.ID;

                var subsection = allSubsections.FirstOrDefault(s => s.Id == structurecontent.StructureElementId);
                if (subsection != null)
                {
                    structurecontent.MinWordCount = subsection.MinWordCount;
                }
                _structureContentRepository.Insert(structurecontent);
            }

            documentToInsert = _documentRepository.GetById(documentToInsert.ID);
            return
                ModelHelper.Mapper(documentToInsert);
        }

        public void Update(Document entity)
        {
            _documentRepository.Update(ModelHelper.Mapper(entity));
        }

        public void Delete(Document entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Document> GetAll()
        {
            return _documentRepository.Get().Select(ModelHelper.Mapper);
        }

        public void UpdateDocumentProgress(int documentId)
        {
            Document document = GetById(documentId);
            document.CurrentProgress = (int)
                Math.Floor((double)document.Sections.SelectMany(s => s.Subsections).Count(ss => ss.Content != null && ss.Content.CurrentProgress == 100) * 100 / document.Sections.SelectMany(s => s.Subsections).Count(ss=>ss.Content != null));
            if (document.Tasks.All(t => t.TaskState == TaskState.Done)) document.CurrentProgress = 100;
            if (document.CurrentProgress >= 100)
            {
                document.CurrentProgress = 100;
                document.DocumentState = DocumentState.Finished;
            }
            else if (document.CurrentProgress < 1)
            {
                document.CurrentProgress = 1;
                document.DocumentState = DocumentState.Started;
            }
            else
            {
                document.DocumentState = DocumentState.InProgress;
            }
            _documentRepository.Update(ModelHelper.Mapper(document));
        }

        public MemoryStream GenerateComplexDocument(int documentId, ExportDocumentType exportDocumentType)
        {
            Document document = GetById(documentId);
            switch (exportDocumentType)
            {
                case ExportDocumentType.Docx:
                    return DocxDocumentGenerator.GenerateComplexDocument(document);
                case ExportDocumentType.Txt:
                    return TxtDocumentGenerator.GenerateComplexDocument(document);
                case ExportDocumentType.Pdf:
                    return PdfDocumentGenerator.GenerateComplexDocument(document);
                case ExportDocumentType.Csv:
                    return CsvDocumentGenerator.GenerateComplexDocument(document);
                default:
                    throw new InvalidOperationException("No such file format found");
            }
        }

        public int GetLastDocumentByUserId(string userName)
        {
            IEnumerable<DOCUMENT> documents = _documentRepository.Get(
                d =>
                    d.USER_UserId.Email == userName
                    && (d.DocumentState == DocumentState.Started ||
                    d.DocumentState == DocumentState.InProgress)).OrderByDescending(d=>d.ID).ToList();
            if (documents.Any())
                return documents.First().ID;
            return -1;
        }

        public IList<Document> GetAllForCheck(int mentorId)
        {
            IEnumerable<DOCUMENT> documents = _documentRepository.Get(
                d => d.MentorId == mentorId).OrderByDescending(d => d.ID).ToList();

            return documents.Select(ModelHelper.Mapper).ToList();
        }
    }
}


text:
#import "SQStatusMenuItemView.h"
#import "NSFileManager+Squash.h"
#import "SQApplication.h"

@implementation SQStatusMenuItemView

- (id)initWithStatusItem:(NSStatusItem *)statusItem
{
    NSRect frame = NSMakeRect(0.0, 0.0, [statusItem length], [[NSStatusBar systemStatusBar] thickness]);
    if ((self = [super initWithFrame:frame])) {
        _statusItem = [statusItem retain];
        _isHighlighted = NO;
        [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
    }
    return self;
}

- (void)dealloc
{
    [_statusItem release];
    [super dealloc];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [_statusItem drawStatusBarBackgroundInRect:rect withHighlight:_isHighlighted];

    NSImage *icon = [(SQApplication *) [NSApp delegate] statusMenuImage];
    NSSize iconSize = [icon size];
    CGFloat x = (rect.size.width - iconSize.width) / 2;
    CGFloat y = (rect.size.height - iconSize.height) / 2;
    NSPoint o = {x, y};
    [icon drawAtPoint:o fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}

- (void)toggleHighlight
{
    _isHighlighted = !_isHighlighted;
    [self setNeedsDisplay:YES];
}

#pragma mark UI Responder

- (void)mouseDown:(NSEvent *)event
{
    [NSApp sendAction:@selector(togglePopover:) to:[NSApp delegate] from:self];
}

#pragma mark Drag Operations

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];
    NSDragOperation op = [sender draggingSourceOperationMask];

    if ([[pboard types] containsObject:NSFilenamesPboardType] && (op & NSDragOperationCopy)) {
        if ((op & NSDragOperationCopy)) {
            return NSDragOperationCopy;
        } else if ((op & NSDragOperationLink)) {
            return NSDragOperationLink;
        }
    }
    return NSDragOperationNone;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];
    NSDragOperation op = [sender draggingSourceOperationMask];
    NSAssert([[pboard types] containsObject:NSFilenamesPboardType], @"Dragged item is not a file");
    NSAssert((op & NSDragOperationCopy) || (op & NSDragOperationLink), @"Drag operation is not copy or link");

    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
    for (NSString *path in files) {
        if (![fm directoryExistsAtPath:path]) {
            NSLog(@"%@ does not exist or is not a directory", path);
            return NO;
        }
    }

    NSLog(@"Watching directories: %@", files);
    return YES;
}

@end


text:
package cz.seznam.euphoria.core.client.dataset.windowing;

import cz.seznam.euphoria.core.annotation.audience.Audience;
import cz.seznam.euphoria.core.client.triggers.Trigger;

import java.io.Serializable;

/**
 * A windowing policy of a dataset.
 */
@Audience(Audience.Type.CLIENT)
public interface Windowing<T, W extends Window> extends Serializable {

  /**
   * Assign a set of windows to a given input element. The input element
   * provides its so-far assigned window, i.e. a window the element was
   * assigned at some point earlier. Note: elements read directly from
   * an input source are assigned the {@link GlobalWindowing.Window}
   * by default.
   *
   * @param el The element to which windows should be assigned.
   *
   * @return collection of windows to be assigned this element into, never {@code null}.
   */
  Iterable<W> assignWindowsToElement(WindowedElement<?, T> el);

  /**
   * @return a {@link Trigger} associated with the current windowing strategy
   */
  Trigger<W> getTrigger();
}


text:
layout: post
title: "Building a Lync bot using C#"
subtitle: "Building a Lync bot using C#"
date: 2015-04-13 06:46
author: "Anuraj"
comments: true
categories: [.Net, .Net 3.0 / 3.5, .Net 4.0, Visual Studio, Windows Forms]
tags: [.Net, .Net 4.0, C#, C#.Net, Lync, Lync Bot, Windows Forms]
header-img: "img/post-bg-01.jpg"
---
Long back I wrote an [article](http://www.anuraj.dev/blog/building-a-language-translation-bot-using-skype-and-c/) on creating a language translation bot using Skype and C#, since Microsoft stopped Skype COM API, and I am using lync more than Skype, I thought of writing the same implementation using lync.

The implementation is pretty same, you have to get the instance of the lync instance first, attach event handlers, translate and return the text back.  To make this post simple, I am not including the translation logic, I am reversing the text and sending back to the sender.

You need to install the Lync SDK to start using the lync APIs, which you can download from [here](http://www.microsoft.com/en-in/download/details.aspx?id=36824). You need to add reference of Microsoft.Lync.Model and Microsoft.Lync.Utilities DLLs.

![Lync References ]({{ site.url }}{{ site.baseurl }}/assets/images/2015/04/lyncAPIRefe.png)

And here is the code snippet.

{% highlight CSharp %}
private LyncClient _lyncClient;
private ConversationManager _conversationManager;

_lyncClient = LyncClient.GetClient();
_conversationManager = _lyncClient.ConversationManager;
_conversationManager.ConversationAdded += ConversationAdded;

private void ConversationAdded(object sender, ConversationManagerEventArgs e)
{
    var conversation = e.Conversation;
    conversation.ParticipantAdded += ParticipantAdded;
}

private void ParticipantAdded(object sender, ParticipantCollectionChangedEventArgs e)
{
    var participant = e.Participant;
    if (participant.IsSelf)
    {
        return;
    }

    var instantMessageModality =
        e.Participant.Modalities[ModalityTypes.InstantMessage] as InstantMessageModality;
    instantMessageModality.InstantMessageReceived += InstantMessageReceived;
}

private void InstantMessageReceived(object sender, MessageSentEventArgs e)
{
    var text = e.Text.Replace(Environment.NewLine, string.Empty);
    (sender as InstantMessageModality).BeginSendMessage(Reverse(text), null, null);
}
{% endhighlight %}

Reverse method reverses the text and send back to the user using BeginSendMessage method. You can extend the bot MEF and custom plugins.

Happy Programming :)