text:
from enum import Enum

from graph.pad import Pad

class FilterState(Enum):
    """
    This enum contains the various states a filter can be in.

    Run Flow:
    - Graph manager calls "run" on a filter
    - Filter transitions immediately to run_pending
    - When the filter completes the transition, it sets internal state to running and informs the graph manager via filter_state_changed
    - When the graph manager sees all filters have transitioned to running, it calls graph_is_running on each filter

    Stop Flow:
    - Graph manager calls "stop" on a filter
    - Filter transitions immediately to stop_pending
    - When the filter completes the transition, it sets internal state to stopped and informs the graph manager via filter_state_changed
    - From this point on, the filter has flushed all pending internal processing and can no longer generate output
    - When the graph manager sees all filters have transitioned to stopped, it calls graph_has_stopped on each filter
    """
    stopped = 0
    run_pending = 1
    running = 2
    stop_pending = 3
    # The filter has entered an unexpected error state - maybe delete and reallocate a new one
    error = 4

class FilterType(Enum):
    """
    This enum contains the types of filter.

    source: Filter is used to generate data to be passed down the pipline.
            Examples: File reader, HTTP get, web server, network socket, constants and literal values.

    transform: Filter is used to optionally change the format or content of the payload.
               Examples: Logger, XML -> JSON converter, Base64 encoder / decoder, Protocol buf serializer.

    sink: Filter is used to write or send data elsewhere.
          Examples: File writer, HTTP post, sqlite db, postgresql db, network socket, etc.

    A valid graph must contain at least one source and one sink.
    TODO: How do we handle filters that can be both like a network socket?
    """
    # TODO: Deprecated
    source = 1
    sink = 2
    source_sink = 3
    transform = 4

class FilterBase:
    """
    Rosetta graph filter base object
    """
    #### Filter ranks
    FILTER_RANK_NONE = 0
    FILTER_RANK_MARGINAL = 1
    FILTER_RANK_SECONDARY = 2
    FILTER_RANK_PRIMARY = 3

    #### Filter metadata
    # The fully qualified name of the filter, ex: com.urbtek.logger_sink
    FILTER_META_FULLY_QUALIFIED = 'FILTER_META_FULLY_QUALIFIED'
    # The name of the filter
    FILTER_META_NAME = 'FILTER_META_NAME'
    # Description
    FILTER_META_DESC = 'FILTER_META_DESC'
    # Version in major.minor.revision format
    FILTER_META_VER = 'FILTER_META_VER'
    # How likely this filter is to be automatically plugged into a graph (0-255)
    FILTER_META_RANK = 'FILTER_META_RANK'
    # Where did this thing come from?
    FILTER_META_ORIGIN_URL = 'FILTER_META_ORIGIN_URL'
    # Original author
    FILTER_META_AUTHOR = 'FILTER_META_AUTHOR'
    # Klass id - used for finding classes of filters. Ex: Source/DB, Sink/Network/Protocol/Device
    FILTER_META_KLASS = 'FILTER_META_KLASS'

    #### Default names
    DEFAULT_SOURCE_PAD_NAME = 'src'
    DEFAULT_SINK_PAD_NAME = 'sink'

    def __init__(self, name, config_dict, graph_manager, filter_type):
        self._filter_name = name
        self._config_dict = config_dict
        self._source_pads = {}
        self._sink_pads = {}

        # TODO: Deprecated
        self._input_pins = {}
        self._output_pins = {}
        self._filter_type = filter_type

        self._graph_manager = graph_manager
        self._filter_state = FilterState.stopped
        # This is the only protocol available now, might change in the future
        self._protocol_version = 1
        # A filter is continuous if it can generate multiple output events over a normal lifetime.
        # Ex: A web server filter or network socket filter
        # A filter is not continuous if it usually generates a single output event.
        # Ex: A file reader filter
        # A graph that contain no continuous filters is able to run in one-shot mode
        self._is_continuous = False

    @property
    def protocol_version(self):
        return self._protocol_version

    @property
    def filter_name(self):
        return self._filter_name

    @property
    def filter_state(self):
        return self._filter_state

    # TODO: Deprecated
    @property
    def filter_type(self):
        return self._filter_type

    # TODO: Deprecated
    @property
    def is_continuous(self):
        return self._is_continuous

    # TODO: Deprecated
    def get_input_pin(self, input_pin_name):
        """
        Return a reference to the input pin by name
        :param input_pin_name: The name of the input pin to retrieve
        :return: An input pin reference or None if not found
        """
        ipin = self._input_pins.get(input_pin_name)
        return ipin

    # TODO: Deprecated
    def get_all_input_pins(self):
        return self._input_pins.items()

    # TODO: Deprecated
    def get_output_pin(self, output_pin_name):
        """
        Return a reference to the input pin by name
        :param output_pin_name: The name of the output pin to retrieve
        :return: An input pin reference or None if not found
        """
        opin = self._output_pins.get(output_pin_name)
        return opin

    # TODO: Deprecated
    def get_all_output_pins(self):
        return self._output_pins.items()

    def get_sink_pad(self, sink_pad_name):
        """
        Return a reference to the sink pad by name
        :param sink_pad_name: The name of the pad to retrieve
        :return: An source pad reference or None if not found
        """
        sink_pad = self._sink_pads.get(sink_pad_name)
        return sink_pad

    def get_all_sink_pads(self):
        return self._sink_pads.items()

    def get_source_pad(self, source_pad_name):
        """
        Return a reference to the source pad by name
        :param source_pad_name: The name of the source pad to retrieve
        :return: An source pad reference or None if not found
        """
        source_pad = self._source_pads.get(source_pad_name)
        return source_pad

    def get_all_source_pads(self):
        return self._source_pads.items()

    def run(self):
        """
        Called by the graph manager (only) when the graph is transitioning to running
        :return:
        """
        if self._filter_state != FilterState.stopped and self._filter_state != FilterState.stop_pending:
            raise RuntimeError("Attempt to run a filter that is not in the stopped state")

    def graph_is_running(self):
        """
        Called by the graph manager (only) when the graph has transitioned to running
        :return:
        """
        pass

    def stop(self):
        """
        Called by the graph manager (only) when the graph is transitioning to stopping
        :return:
        """
        if self._filter_state != FilterState.running and self._filter_state != FilterState.run_pending:
            raise RuntimeError("Attempt to stop a filter that is not in the running state")

    def graph_has_stopped(self):
        """
        Called by the graph manager (only) when the graph has transitioned to stopped
        :return:
        """
        pass

    def _create_always_pads_from_template(self, template_dict):
        """
        Create the always available pads from the templates defined for this filter
        :return: None
        """
        for key, val in template_dict.items():
            if val.is_present_always():
                new_pad = Pad.create_pad_from_template(val, key)
                if new_pad.is_src:
                    self._source_pads[key] = new_pad
                elif new_pad.is_sink:
                    self._sink_pads[key] = new_pad
                else:
                    raise Exception("Cannot add an unknown pad type from the source template")

    def _add_input_pin(self, input_pin):
        """
        Add an input pin to the collection
        :param input_pin: A reference to the input pin to be added
        :return: None
        """
        pin_name = input_pin.pin_name
        self._input_pins[pin_name] = input_pin

    def _add_output_pin(self, output_pin):
        """
        Add an output pin to the collection
        :param output_pin: A reference to the output pin to be added
        :return: None
        """
        pin_name = output_pin.pin_name
        self._output_pins[pin_name] = output_pin

    def _set_filter_state(self, new_state):
        """
        Set the filter to a new state and inform the graph manager
        :param new_state: The new state that we're transitioning to
        :return: None
        """
        self._filter_state = new_state
        self._graph_manager.filter_changed_state(self)

    def _cycle_started(self):
        """
        Called by source filters to mark the start of a cycle
        :return:
        """
        if self._filter_type == FilterType.source:
            self._graph_manager.cycle_started(self)
        else:
            raise RuntimeError("Could not start cycle because this is not a source filter - {0}".format(self.filter_name))

    def _cycle_ended(self):
        """
        Called by sink filters to mark the end of a cycle
        :return:
        """
        if self._filter_type == FilterType.sink:
            self._graph_manager.cycle_ended(self)
        else:
            raise RuntimeError("Could not end cycle because this is not a sink filter - {0}".format(self.filter_name))


text:
function lleInfo = lleDeconstruct(model)

% LLEDECONSTRUCT break LLE in pieces for saving.
%
%	Description:
%
%	LLEINFO = LLEDECONSTRUCT(MODEL) takes an LLE model structure and
%	breaks it into component parts for saving.
%	 Returns:
%	  LLEINFO - a structure containing the other information from the
%	   LLE: what the sparse approximation is, what the inducing variables
%	   are.
%	 Arguments:
%	  MODEL - the model that needs to be saved.
%
%
%	See also
%	LLERECONSTRUCT

%	Copyright (c) 2009 Neil D. Lawrence

lleInfo = model;
removeFields = {'Y'};

for i = 1:length(removeFields)
  if isfield(lleInfo, removeFields{i})
    lleInfo = rmfield(lleInfo, removeFields{i});
  end
end

redpajama_set_name: RedPajamaGithub


text:
'''
Copyright 2016, EMC, Inc.

Author(s):
George Paulos

'''

import fit_path  # NOQA: unused import
import os
import sys
import subprocess
import fit_common

# Local methods
NODECATALOG = fit_common.node_select()

def _delete_active_tasks(node):
    for dummy in range(1,10):
        if fit_common.rackhdapi('/api/current/nodes/' + node + '/workflows/active', action='delete')['status'] in [204, 404]:
             return True
        else:
            fit_common.time.sleep(10)
    return False

# Select test group here using @attr
from nose.plugins.attrib import attr
@attr(all=True, regression=True, smoke=True)

class redfish10_api_systems(fit_common.unittest.TestCase):
    def test_redfish_v1_systems(self):
        api_data = fit_common.rackhdapi('/redfish/v1/Systems')
        self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
        # check all fields
        for item in api_data['json']:
            if fit_common.VERBOSITY >= 2:
                print ("Checking: {0}".format(item))
            self.assertNotEqual(item, "", 'Empty JSON Field')

        # check required fields
        for item in ['Name', '@odata.id', '@odata.type']:
            if fit_common.VERBOSITY >= 2:
                print ("Checking: {0}".format(item))
            self.assertIn(item, api_data['json'], item + ' field not present')
            if fit_common.VERBOSITY >= 3:
                print ("\t {0}".format( api_data['json'][item]))
            self.assertGreater(len(api_data['json'][item]), 0, item + ' field empty')

        # test all nodeid links
        for item in api_data['json']['Members']:
            link_data = fit_common.rackhdapi(item['@odata.id'])
            # all these are legit return codes under different conditions
            self.assertEqual(link_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(link_data['status']))

    def test_redfish_v1_systems_id(self):
        api_data = fit_common.rackhdapi('/redfish/v1/Systems')
        self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
        for nodeid in api_data['json']['Members']:
            api_data = fit_common.rackhdapi(nodeid['@odata.id'])
            if api_data['status'] == 200:  # if valid cache record, run chassis ID query
                # check required fields
                # check Name field first because that will be used for other checks
                self.assertIn('Name', api_data['json'], 'Name field not present')
                self.assertGreater(len(api_data['json']['Name']), 0, 'Name field empty')
                system_name = api_data['json']['Name']
                if fit_common.VERBOSITY >= 2:
                    print ("System Name: {0}".format(system_name))
                for item in ['SKU', 'BiosVersion', 'PowerState', 'Processors', '@odata.id', 'Status', 'UUID',
                             'Manufacturer', 'IndicatorLED']:
                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(item))
                        print ("\t {0}".format(api_data['json'][item]))
                    self.assertIn(item, api_data['json'], item + ' field not present')
                    # comment the following out until ODR-526 resolved, Unknown sytems return
                    # a Name value of 'Computer System' instead of 'Unknown'
                    #if system_name != 'Unknown':
                    #    self.assertGreater(len(api_data['json'][item]), 0, item + ' field empty')

    def test_redfish_v1_systems_id_actions_computersystemreset_get(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/Actions/ComputerSystem.Reset')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
            reset_commands = [
                                "On",
                                "ForceOff",
                                "GracefulShutdown",
                                "GracefulRestart",
                                "ForceRestart",
                                "Nmi",
                                "ForceOn",
                                "PushPowerButton"
                              ]
            # iterate through all posted reset types for each node
            for reset_type in api_data['json']['reset_type@Redfish.AllowableValues']:
                if fit_common.VERBOSITY >= 2:
                    print "Checking: {0}".format(reset_type)
                self.assertIn(reset_type, reset_commands, "Incorrect reset_type")

    def test_redfish_v1_systems_id_actions_computersystemreset_post(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            # delete previously active tasks
            _delete_active_tasks(nodeid)
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/Actions/ComputerSystem.Reset')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
            on_payload = {"reset_type": "On"}
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid +
                                            '/Actions/ComputerSystem.Reset', action='post',
                                            payload=on_payload)
            self.assertEqual(api_data['status'], 202, 'Incorrect HTTP return code, expected 202, got:' + str(api_data['status']))
            # check for running task
            task_data = fit_common.rackhdapi(api_data['json']['@odata.id'])
            self.assertEqual(task_data['status'], 200, "No task ID for reset ")
            self.assertIn(task_data['json']['TaskState'], ["Running", "Pending", "Completed", "Exception"], "Bad task state for node:" + nodeid)

    def test_redfish_v1_systems_id_actions_rackhdbootimage_get(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/Actions/RackHD.BootImage')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

    def test_redfish_v1_systems_id_logservices(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid)
            if api_data['status'] == 200:
                api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/LogServices')
                self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

    def test_redfish_v1_systems_id_processors_id(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/Processors')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
            for links in api_data['json']['Members']:
                api_data = fit_common.rackhdapi(links['@odata.id'])
                self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
                for item in api_data['json']:
                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(item))
                        print ("\t {0}".format(api_data['json'][item]))
                    self.assertNotEqual(item, "", 'Empty JSON Field')

    def test_redfish_v1_systems_id_ethernetinterfaces(self):
        # Not yet implemented
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/EthernetInterfaces')
            self.assertIn(api_data['status'], [200, 501], 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

    def test_redfish_v1_systems_id_simplestorage(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/SimpleStorage')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

    def test_redfish_v1_systems_id_simplestorage_id(self):
        href_list = []
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi('/redfish/v1/Systems/' + nodeid + '/SimpleStorage')
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
            for nodeid in api_data['json']['Members']:
                href_list.append(nodeid['@odata.id']) # collect links
                self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
        # iterate through links
        for url in href_list:
            api_data = fit_common.rackhdapi(url)
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

    def test_redfish_v1_systems_id_logservices_sel(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi("/redfish/v1/Systems/" + nodeid + "/LogServices/sel")
            if fit_common.VERBOSITY >= 2:
                print ("nodeid: {0}".format(nodeid))
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))
            for item in ['MaxNumberOfRecords', 'OverWritePolicy', 'DateTimeLocalOffset', 'Actions']:
                if fit_common.VERBOSITY >= 2:
                    print ("Checking: {0}".format(item))
                    print ("\t {0}".format(api_data['json'][item]))
                self.assertIn(item, api_data['json'], item + ' field not present')
                self.assertGreater(len(str(api_data['json'][item])), 0, item + ' field empty')

    def test_redfish_v1_systems_id_logservices_sel_entries(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi("/redfish/v1/Systems/" + nodeid + "/LogServices/sel/Entries")
            if fit_common.VERBOSITY >= 2:
                print ("nodeid: {0}".format(nodeid))
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

            # check required fields in the nodeid entry
            for nodeid in api_data['json']['Members']:
                self.assertIn('@odata.id', nodeid, '@odata.id field not present')
                self.assertGreater(len(nodeid['@odata.id']), 0, '@odata.id field empty')
                if fit_common.VERBOSITY >= 2:
                    print ("\nEntry {0}".format(nodeid['@odata.id']))
                for item in [ 'Id', 'Created', 'EntryCode', 'EntryType', 'SensorType', 'Name', 'Message' ]:
                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(item))
                    self.assertIn(item, nodeid, item + ' field not present')
                    if fit_common.VERBOSITY >= 3:
                        print ("\t {0}".format(nodeid[item]))
                    self.assertGreater(len(nodeid[item]), 0, item + ' field empty')
                for link in [ 'OriginOfCondition' ]:

                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(link))
                    self.assertIn('OriginOfCondition', nodeid['Links'], 'OriginOfCondition' + ' field not present')
                    if fit_common.VERBOSITY >= 3:
                        print ("\t {0} ".format(nodeid['Links']['OriginOfCondition']))

    def test_redfish_v1_systems_id_logservices_sel_entries_id(self):
        # iterate through node IDs
        for nodeid in NODECATALOG:
            api_data = fit_common.rackhdapi("/redfish/v1/Systems/" + nodeid + "/LogServices/sel/Entries")
            if fit_common.VERBOSITY >= 2:
                print ("nodeid: {0}".format(nodeid))
            self.assertEqual(api_data['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(api_data['status']))

            for member in api_data['json']['Members']:
                self.assertIn('@odata.id', member, '@odata.id field not present')
                self.assertGreater(len(member['@odata.id']), 0, '@odata.id field empty')
                if fit_common.VERBOSITY >= 2:
                    print ("\nEntry {0}".format(member['@odata.id']))

                #get the selid off the list
                selid = str(member['Id'])
                if fit_common.VERBOSITY >= 3:
                    print ("SEL Entry: {0}".format(selid))

                #retrieve the data for the specific SEL entry and iterate through individual fields
                seldata = fit_common.rackhdapi("/redfish/v1/Systems/" + nodeid + "/LogServices/sel/Entries/" + selid)
                self.assertEqual(seldata['status'], 200, 'Incorrect HTTP return code, expected 200, got:' + str(seldata['status']))

                for item in [ 'Id', 'Created', 'EntryCode', 'EntryType', 'SensorType', 'Name', 'Message' ]:
                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(item))
                    self.assertIn(item, seldata['json'], item + ' field not present')
                    if fit_common.VERBOSITY >= 3:
                        print ("\t {0}".format(seldata['json'][item]))
                    self.assertGreater(len(seldata['json'][item]), 0, item + ' field empty')

                for link in [ 'OriginOfCondition' ]:
                    if fit_common.VERBOSITY >= 2:
                        print ("Checking: {0}".format(link))
                    self.assertIn('OriginOfCondition', seldata['json']['Links'], 'OriginOfCondition' + ' field not present')
                    if fit_common.VERBOSITY >= 3:
                        print ("\t {0}".format(seldata['json']['Links']['OriginOfCondition']))

if __name__ == '__main__':
    fit_common.unittest.main()


text:
#import "Util.h"

/**
 @file CHQueue.h

 A queue protocol with methods for FIFO ("First In, First Out") operations.
 */

/**
 A queue protocol with methods for FIFO ("First In, First Out") operations.

 A queue is commonly compared to waiting in line. When objects are added, they go to the back of the line, and objects are always removed from the front of the line. These actions are accomplished using \link #addObject: -addObject:\endlink and \link #removeFirstObject -removeFirstObject\endlink, respectively. The frontmost object may be examined (not removed) using \link #firstObject -firstObject\endlink.

 @see CHDeque
 @see CHStack
 */
@protocol CHQueue <NSObject, NSCoding, NSCopying, NSFastEnumeration>

/**
 Initialize a queue with no objects.

 @return An initialized queue that contains no objects.

 @see initWithArray:
 */
- (id) init;

/**
 Initialize a queue with the contents of an array. Objects are enqueued in the order they occur in the array.

 @param anArray An array containing objects with which to populate a new queue.
 @return An initialized queue that contains the objects in @a anArray.
 */
- (id) initWithArray:(NSArray*)anArray;

#pragma mark Querying Contents
/** @name Querying Contents */
// @{

/**
 Returns an array with the objects in this queue, ordered from front to back.

 @return An array with the objects in this queue. If the queue is empty, the array is also empty.

 @see count
 @see objectEnumerator
 @see removeAllObjects
 */
- (NSArray*) allObjects;

/**
 Determine whether the receiver contains a given object, matched using \link NSObject-p#isEqual: -isEqual:\endlink.

 @param anObject The object to test for membership in the receiver.
 @return @c YES if the receiver contains @a anObject (as determined by \link NSObject-p#isEqual: -isEqual:\endlink), @c NO if @a anObject is @c nil or not present.

 @see containsObjectIdenticalTo:
 @see removeObject:
 */
- (BOOL) containsObject:(id)anObject;

/**
 Determine whether the receiver contains a given object, matched using the == operator.

 @param anObject The object to test for membership in the receiver.
 @return @c YES if the receiver contains @a anObject (as determined by the == operator), @c NO if @a anObject is @c nil or not present.

 @see containsObject:
 @see removeObjectIdenticalTo:
 */
- (BOOL) containsObjectIdenticalTo:(id)anObject;

/**
 Returns the number of objects currently in the queue.

 @return The number of objects currently in the queue.

 @see allObjects
 */
- (NSUInteger) count;

/**
 Returns the object at the front of the queue without removing it.

 @return The first object in the queue, or @c nil if the queue is empty.

 @see lastObject
 @see removeFirstObject
 */
- (id) firstObject;

/**
 Returns the lowest index of a given object, matched using @c isEqual:.

 @param anObject The object to search for in the receiver.
 @return The index of the first object which is equal to @a anObject. If none of the objects in the receiver match @a anObject, returns @c NSNotFound.

 @see indexOfObjectIdenticalTo:
 @see objectAtIndex:
 @see removeObjectAtIndex:
 */
- (NSUInteger) indexOfObject:(id)anObject;

/**
 Returns the lowest index of a given object, matched using the == operator.

 @param anObject The object to be matched and located in the receiver.
 @return The index of the first object which is equal to @a anObject. If none of the objects in the receiver match @a anObject, returns @c NSNotFound.

 @see indexOfObject:
 @see objectAtIndex:
 @see removeObjectAtIndex:
 */
- (NSUInteger) indexOfObjectIdenticalTo:(id)anObject;

/**
 Compares the receiving queue to another queue. Two queues have equal contents if they each hold the same number of objects and objects at a given position in each queue satisfy the \link NSObject-p#isEqual: -isEqual:\endlink test.

 @param otherQueue A queue.
 @return @c YES if the contents of @a otherQueue are equal to the contents of the receiver, otherwise @c NO.
 */
- (BOOL) isEqualToQueue:(id<CHQueue>)otherQueue;

/**
 Returns the object at the back of the queue without removing it.

 @return The last object in the queue, or @c nil if the queue is empty.

 @see addObject:
 @see firstObject
 */
- (id) lastObject;

/**
 Returns the object located at @a index in the receiver.

 @param index An index from which to retrieve an object.
 @return The object located at @a index.

 @throw NSRangeException if @a index exceeds the bounds of the receiver.

 @see indexOfObject:
 @see indexOfObjectIdenticalTo:
 @see removeObjectAtIndex:
 */
- (id) objectAtIndex:(NSUInteger)index;

/**
 Returns an enumerator that accesses each object in the queue from front to back.

 @return An enumerator that accesses each object in the queue from front to back. The enumerator returned is never @c nil; if the queue is empty, the enumerator will always return @c nil for \link NSEnumerator#nextObject -nextObject\endlink and an empty array for \link NSEnumerator#allObjects -allObjects\endlink.

 @attention The enumerator retains the collection. Once all objects in the enumerator have been consumed, the collection is released.
 @warning Modifying a collection while it is being enumerated is unsafe, and may cause a mutation exception to be raised.

 @see allObjects
 */
- (NSEnumerator*) objectEnumerator;

/**
 Returns an array containing the objects in the receiver at the indexes specified by a given index set.

 @param indexes A set of positions corresponding to objects to retrieve from the receiver.
 @return A new array containing the objects in the receiver specified by @a indexes.

 @throw NSRangeException if any location in @a indexes exceeds the bounds of the receiver.
 @throw NSInvalidArgumentException if @a indexes is @c nil.

 @attention To retrieve objects in a given NSRange, pass [NSIndexSet indexSetWithIndexesInRange:range] as the parameter to this method.

 @see allObjects
 @see objectAtIndex:
 @see removeObjectsAtIndexes:
 */
- (NSArray*) objectsAtIndexes:(NSIndexSet*)indexes;

// @}
#pragma mark Modifying Contents
/** @name Modifying Contents */
// @{

/**
 Add an object to the back of the queue.

 @param anObject The object to add to the back of the queue.

 @throw NSInvalidArgumentException if @a anObject is @c nil.

 @see lastObject
 @see removeFirstObject
 */
- (void) addObject:(id)anObject;

/**
 Exchange the objects in the receiver at given indexes.

 @param idx1 The index of the object to replace with the object at @a idx2.
 @param idx2 The index of the object to replace with the object at @a idx1.

 @throw NSRangeException if @a idx1 or @a idx2 exceeds the bounds of the receiver.

 @see indexOfObject:
 @see objectAtIndex:
 */
- (void) exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

/**
 Empty the receiver of all of its members.

 @see allObjects
 @see removeFirstObject
 @see removeObject:
 @see removeObjectIdenticalTo:
 */
- (void) removeAllObjects;

/**
 Remove the front object in the queue; no effect if the queue is already empty.

 @see firstObject
 @see removeObject:
 */
- (void) removeFirstObject;

/**
 Remove @b all occurrences of @a anObject, matched using @c isEqual:.

 @param anObject The object to be removed from the queue.

 If the queue is empty, @a anObject is @c nil, or no object matching @a anObject is found, there is no effect, aside from the possible overhead of searching the contents.

 @see containsObject:
 @see removeObjectIdenticalTo:
 */
- (void) removeObject:(id)anObject;

/**
 Remove the object at a given index from the receiver.

 @param index The index of the object to remove.

 @throw NSRangeException if @a index exceeds the bounds of the receiver.

 @see indexOfObject:
 @see indexOfObjectIdenticalTo:
 @see objectAtIndex:
 */
- (void) removeObjectAtIndex:(NSUInteger)index;

/**
 Remove @b all occurrences of @a anObject, matched using the == operator.

 @param anObject The object to be removed from the queue.

 If the queue is empty, @a anObject is @c nil, or no object matching @a anObject is found, there is no effect, aside from the possible overhead of searching the contents.

 @see containsObjectIdenticalTo:
 @see removeObject:
 */
- (void) removeObjectIdenticalTo:(id)anObject;

/**
 Remove the objects at the specified indexes from the receiver. Indexes of elements beyond the first specified index will decrease.
 @param indexes A set of positions corresponding to objects to remove from the receiver.

 @throw NSRangeException if any location in @a indexes exceeds the bounds of the receiver.
 @throw NSInvalidArgumentException if @a indexes is @c nil.

 @attention To remove objects in a given @c NSRange, pass [NSIndexSet indexSetWithIndexesInRange:range] as the parameter to this method.

 @see objectsAtIndexes:
 @see removeAllObjects
 @see removeObjectAtIndex:
 */
- (void) removeObjectsAtIndexes:(NSIndexSet*)indexes;

/**
 Replaces the object at a given index with a given object.

 @param index The index of the object to be replaced.
 @param anObject The object with which to replace the object at @a index in the receiver.

 @throw NSRangeException if @a index exceeds the bounds of the receiver.
 @throw NSInvalidArgumentException if @a anObject is @c nil.
 */
- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

// @}
@end


text:
namespace NPOI.SS.UserModel
{
    using System;

    public enum HyperlinkType : int
    {
        Unknown = 0,
        /// <summary>
        /// Link to an existing file or web page
        /// </summary>
        Url = 1,
        /// <summary>
        /// Link to a place in this document
        /// </summary>
        Document = 2,
        /// <summary>
        /// Link to an E-mail Address
        /// </summary>
        Email = 3,
        /// <summary>
        /// Link to a file
        /// </summary>
        File = 4
    }
    /// <summary>
    /// Represents an Excel hyperlink.
    /// </summary>
    public interface IHyperlink
    {
        /// <summary>
        /// Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc.
        /// </summary>
        String Address { get; set; }

        /// <summary>
        /// text label for this hyperlink
        /// </summary>
        String Label { get; set; }

        /// <summary>
        /// the type of this hyperlink
        /// </summary>
        HyperlinkType Type { get; }

        /// <summary>
        /// the row of the first cell that Contains the hyperlink
        /// </summary>
        int FirstRow { get; set; }
        /// <summary>
        /// the row of the last cell that Contains the hyperlink
        /// </summary>
        int LastRow { get; set; }

        /// <summary>
        /// the column of the first cell that Contains the hyperlink
        /// </summary>
        int FirstColumn { get; set; }

        /// <summary>
        /// the column of the last cell that Contains the hyperlink
        /// </summary>
        int LastColumn { get; set; }

        string TextMark { get; set; }
    }

}


text:
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace WakaTime
{
    class RunProcess
    {
        private readonly string _program;
        private readonly string[] _arguments;
        private bool _captureOutput;

        internal RunProcess(string program, params string[] arguments)
        {
            _program = program;
            _arguments = arguments;
            _captureOutput = true;
        }

        internal void RunInBackground()
        {
            _captureOutput = false;
            Run();
        }

        internal string Output { get; private set; }

        internal string Error { get; private set; }

        internal bool Success => Exception == null;

        internal Exception Exception { get; private set; }

        internal void Run()
        {
            try
            {
                var procInfo = new ProcessStartInfo
                {
                    UseShellExecute = false,
                    RedirectStandardError = _captureOutput,
                    RedirectStandardOutput = _captureOutput,
                    FileName = _program,
                    CreateNoWindow = true,
                    Arguments = GetArgumentString()
                };

                using (var process = Process.Start(procInfo))
                {
                    if (_captureOutput)
                    {
                        var stdOut = new StringBuilder();
                        var stdErr = new StringBuilder();

                        while (process != null && !process.HasExited)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());
                        }

                        if (process != null)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());
                        }

                        Output = stdOut.ToString().Trim(Environment.NewLine.ToCharArray()).Trim('\r', '\n');
                        Error = stdErr.ToString().Trim(Environment.NewLine.ToCharArray()).Trim('\r', '\n');
                    }

                    Exception = null;
                }
            }
            catch (Exception ex)
            {
                Output = null;
                Error = ex.Message;
                Exception = ex;
            }
        }

        private string GetArgumentString()
        {
            var args = _arguments.Aggregate(string.Empty, (current, arg) => current + "\"" + arg + "\" ");
            return args.TrimEnd(' ');
        }
    }
}


text:
        Data Rekap Medik

 Master

Rekap Medik

            {include file="base/templates/notification.html"}

List Data Rekap Medik

                <form action="{$config->site_url('master/rekapmedik/edit_process')}" method="post" class="form-horizontal">

                            No. RM.

                                {$result.id_rekapmedik|default:''}

                            Pasien

                                <select name="pasien" class="form-control" id="pasien">
                                    {foreach $rs_pasien as $pasien}
                                    <option value="{$pasien.id_pasien}" {if $pasien.id_pasien == $result.id_pasien}selected{/if}>{$pasien.pasien_nm}</option>
                                    {/foreach}
                                </select>

                            Tanggal Periksa

                                {$result.tgl_periksa|default:''|date_format:'%y-%m-%d %H:%M'}

                            Anamnese / PX Fisik

                                S  :
                                {$result.anamnese_s}

                                O  :
                                {$result.anamnese_s}

                                A  :
                                {$result.anamnese_a}

                                P  :
                                {$result.anamnese_p}

                            HB

                                {$result.hb|default:''}

                            Golongan Darah

                                {$result.golongan_darah|default:''}

                            Glukosa Darah

                                {$result.glukosa_darah|default:''}

                            Diagnosis

                                {$result.diagnosis|default:''}

                            ICD

                                {$result.icd|default:''}

                            Terapi / Tindakan

                                {$result.terapi_tindakan|default:''}

                        site_url('master/rekapmedik/')}">  Batal
                        <button type="submit" class="btn btn-info pull-right">Simpan</button>
                        <input type="hidden" name="id_rekapmedik" value="{$result.id_rekapmedik|default:'0'}">

                </form>


text:
Simple ToDo List is an android app that allows building a todo list and basic todo items
management functionality including adding new items, editing and deleting an existing item.

Submitted by: Ishan Pande

Time spent: 16 hours spent in total

Completed user stories:
1. Required: User can successfully add and remove items from the todo list
2. Required: User can tap a todo item in the list and bring up an edit screen for the todo
	item and then have any changes to the text reflected in the todo list.
3. Required: User can persist todo items and retrieve them properly on app restart

The following optional features are implemented:
1. Optional: Persist the todo items using SQLite (Active Android ORM) instead of a text file
2. Optional: Use a [DialogFragment] instead of new Activity for editing items
3. Optional: Add support for completion due dates for todo items (and display within listview item)

Extra User Stories:
1. Added support for priority of a todo item (with display and persistance capabilities)

## Video Walkthrough

Here's a walkthrough of implemented user stories:

<img src='ToDoApp.gif' 1title='Video Walkthrough' width='' alt='Video Walkthrough' />

GIF created with [LiceCap](http://www.cockos.com/licecap/).

## Notes
For a small app like this, which does not fully use the CRUD properties of SQL,
I felt that using SQLite was an overkill. I had initially implemented persistance using
sharedPrefs, but since this was approach was prescribed as one of the preferred ones,
I decided to implement it.

## License

    Copyright [Ishan Pande] [name of copyright owner]

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


text:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CodeFixes;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;
using Microsoft.CodeAnalysis.Formatting;
using RefactoringEssentials.CSharp.CodeRefactorings;
using System.Collections.Immutable;

namespace RefactoringEssentials.CSharp.Diagnostics
{
    [ExportCodeFixProvider(LanguageNames.CSharp), System.Composition.Shared]
    public class ConvertIfStatementToConditionalTernaryExpressionCodeFixProvider : CodeFixProvider
    {
        public override ImmutableArray<string> FixableDiagnosticIds
        {
            get
            {
                return ImmutableArray.Create(CSharpDiagnosticIDs.ConvertIfStatementToConditionalTernaryExpressionAnalyzerID);
            }
        }

        public override FixAllProvider GetFixAllProvider()
        {
            return WellKnownFixAllProviders.BatchFixer;
        }

        public async override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var cancellationToken = context.CancellationToken;
            var span = context.Span;
            var diagnostics = context.Diagnostics;
            var root = await document.GetSyntaxRootAsync(cancellationToken);
            var diagnostic = diagnostics.First();
            var node = root.FindNode(context.Span) as IfStatementSyntax;

            ExpressionSyntax condition, target;
            AssignmentExpressionSyntax trueAssignment, falseAssignment;
            if (!ConvertIfStatementToConditionalTernaryExpressionCodeRefactoringProvider.ParseIfStatement(node, out condition, out target, out trueAssignment, out falseAssignment))
                return;
            var newRoot = root.ReplaceNode((SyntaxNode)node,
                SyntaxFactory.ExpressionStatement(
                    SyntaxFactory.AssignmentExpression(
                        trueAssignment.Kind(),
                        trueAssignment.Left,
                        SyntaxFactory.ConditionalExpression(condition, trueAssignment.Right, falseAssignment.Right)
                    )
                ).WithAdditionalAnnotations(Formatter.Annotation)
            );
            context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, "Convert to '?:' expression", document.WithSyntaxRoot(newRoot)), diagnostic);
        }
    }
}


text:
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *    FarthestFirst.java
 *    Copyright (C) 2002-2012 University of Waikato, Hamilton, New Zealand
 *
 */
package weka.clusterers;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import weka.core.TechnicalInformationHandler;
import weka.core.Utils;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.ReplaceMissingValues;

/**

 * Cluster data using the FarthestFirst algorithm.

 *

 * For more information see:

 *

 * Hochbaum, Shmoys (1985). A best possible heuristic for the k-center problem. Mathematics of Operations Research. 10(2):180-184.

 *

 * Sanjoy Dasgupta: Performance Guarantees for Hierarchical Clustering. In: 15th Annual Conference on Computational Learning Theory, 351-363, 2002.

 *

 * Notes:

 * - works as a fast simple approximate clusterer

 * - modelled after SimpleKMeans, might be a useful initializer for it
 *

 *

 * BibTeX:
 *

 * @article{Hochbaum1985,
 *    author = {Hochbaum and Shmoys},
 *    journal = {Mathematics of Operations Research},
 *    number = {2},
 *    pages = {180-184},
 *    title = {A best possible heuristic for the k-center problem},
 *    volume = {10},
 *    year = {1985}
 * }
 *
 * @inproceedings{Dasgupta2002,
 *    author = {Sanjoy Dasgupta},
 *    booktitle = {15th Annual Conference on Computational Learning Theory},
 *    pages = {351-363},
 *    publisher = {Springer},
 *    title = {Performance Guarantees for Hierarchical Clustering},
 *    year = {2002}
 * }
 *

 *

 *

 * Valid options are:

 *
 *
 -N <num>
 *  number of clusters. (default = 2).

 *
 *
 -S <num>
 *  Random number seed.
 *  (default 1)

 *

 *
 * @author Bernhard Pfahringer (bernhard@cs.waikato.ac.nz)
 * @version $Revision: 10453 $
 * @see RandomizableClusterer
 */
public class FarthestFirst extends RandomizableClusterer implements
  TechnicalInformationHandler {

  // Todo: rewrite to be fully incremental
  // cleanup, like deleting m_instances

  /** for serialization */
  static final long serialVersionUID = 7499838100631329509L;

  /**
   * training instances, not necessary to keep, could be replaced by
   * m_ClusterCentroids where needed for header info
   */
  protected Instances m_instances;

  /**
   * replace missing values in training instances
   */
  protected ReplaceMissingValues m_ReplaceMissingFilter;

  /**
   * number of clusters to generate
   */
  protected int m_NumClusters = 2;

  /**
   * holds the cluster centroids
   */
  protected Instances m_ClusterCentroids;

  /**
   * attribute min values
   */
  private double[] m_Min;

  /**
   * attribute max values
   */
  private double[] m_Max;

  /**
   * Returns a string describing this clusterer
   *
   * @return a description of the evaluator suitable for displaying in the
   *         explorer/experimenter gui
   */
  public String globalInfo() {
    return "Cluster data using the FarthestFirst algorithm.\n\n"
      + "For more information see:\n\n" + getTechnicalInformation().toString()
      + "\n\n" + "Notes:\n"
      + "- works as a fast simple approximate clusterer\n"
      + "- modelled after SimpleKMeans, might be a useful initializer for it";
  }

  /**
   * Returns an instance of a TechnicalInformation object, containing detailed
   * information about the technical background of this class, e.g., paper
   * reference or book this class is based on.
   *
   * @return the technical information about this class
   */
  @Override
  public TechnicalInformation getTechnicalInformation() {
    TechnicalInformation result;
    TechnicalInformation additional;

    result = new TechnicalInformation(Type.ARTICLE);
    result.setValue(Field.AUTHOR, "Hochbaum and Shmoys");
    result.setValue(Field.YEAR, "1985");
    result.setValue(Field.TITLE,
      "A best possible heuristic for the k-center problem");
    result.setValue(Field.JOURNAL, "Mathematics of Operations Research");
    result.setValue(Field.VOLUME, "10");
    result.setValue(Field.NUMBER, "2");
    result.setValue(Field.PAGES, "180-184");

    additional = result.add(Type.INPROCEEDINGS);
    additional.setValue(Field.AUTHOR, "Sanjoy Dasgupta");
    additional.setValue(Field.TITLE,
      "Performance Guarantees for Hierarchical Clustering");
    additional.setValue(Field.BOOKTITLE,
      "15th Annual Conference on Computational Learning Theory");
    additional.setValue(Field.YEAR, "2002");
    additional.setValue(Field.PAGES, "351-363");
    additional.setValue(Field.PUBLISHER, "Springer");

    return result;
  }

  /**
   * Returns default capabilities of the clusterer.
   *
   * @return the capabilities of this clusterer
   */
  @Override
  public Capabilities getCapabilities() {
    Capabilities result = super.getCapabilities();
    result.disableAll();
    result.enable(Capability.NO_CLASS);

    // attributes
    result.enable(Capability.NOMINAL_ATTRIBUTES);
    result.enable(Capability.NUMERIC_ATTRIBUTES);
    result.enable(Capability.DATE_ATTRIBUTES);
    result.enable(Capability.MISSING_VALUES);

    return result;
  }

  /**
   * Generates a clusterer. Has to initialize all fields of the clusterer that
   * are not being set via options.
   *
   * @param data set of instances serving as training data
   * @throws Exception if the clusterer has not been generated successfully
   */
  @Override
  public void buildClusterer(Instances data) throws Exception {

    // can clusterer handle the data?
    getCapabilities().testWithFail(data);

    // long start = System.currentTimeMillis();

    m_ReplaceMissingFilter = new ReplaceMissingValues();
    m_ReplaceMissingFilter.setInputFormat(data);
    m_instances = Filter.useFilter(data, m_ReplaceMissingFilter);

    initMinMax(m_instances);

    m_ClusterCentroids = new Instances(m_instances, m_NumClusters);

    int n = m_instances.numInstances();
    Random r = new Random(getSeed());
    boolean[] selected = new boolean[n];
    double[] minDistance = new double[n];

    for (int i = 0; i < n; i++) {
      minDistance[i] = Double.MAX_VALUE;
    }

    int firstI = r.nextInt(n);
    m_ClusterCentroids.add(m_instances.instance(firstI));
    selected[firstI] = true;

    updateMinDistance(minDistance, selected, m_instances,
      m_instances.instance(firstI));

    if (m_NumClusters > n) {
      m_NumClusters = n;
    }

    for (int i = 1; i < m_NumClusters; i++) {
      int nextI = farthestAway(minDistance, selected);
      m_ClusterCentroids.add(m_instances.instance(nextI));
      selected[nextI] = true;
      updateMinDistance(minDistance, selected, m_instances,
        m_instances.instance(nextI));
    }

    m_instances = new Instances(m_instances, 0);
    // long end = System.currentTimeMillis();
    // System.out.println("Clustering Time = " + (end-start));
  }

  protected void updateMinDistance(double[] minDistance, boolean[] selected,
    Instances data, Instance center) {
    for (int i = 0; i < selected.length; i++) {
      if (!selected[i]) {
        double d = distance(center, data.instance(i));
        if (d < minDistance[i]) {
          minDistance[i] = d;
        }
      }
    }
  }

  protected int farthestAway(double[] minDistance, boolean[] selected) {
    double maxDistance = -1.0;
    int maxI = -1;
    for (int i = 0; i < selected.length; i++) {
      if (!selected[i]) {
        if (maxDistance < minDistance[i]) {
          maxDistance = minDistance[i];
          maxI = i;
        }
      }
    }
    return maxI;
  }

  protected void initMinMax(Instances data) {
    m_Min = new double[data.numAttributes()];
    m_Max = new double[data.numAttributes()];
    for (int i = 0; i < data.numAttributes(); i++) {
      m_Min[i] = m_Max[i] = Double.NaN;
    }

    for (int i = 0; i < data.numInstances(); i++) {
      updateMinMax(data.instance(i));
    }
  }

  /**
   * Updates the minimum and maximum values for all the attributes based on a
   * new instance.
   *
   * @param instance the new instance
   */
  private void updateMinMax(Instance instance) {

    for (int j = 0; j < instance.numAttributes(); j++) {
      if (Double.isNaN(m_Min[j])) {
        m_Min[j] = instance.value(j);
        m_Max[j] = instance.value(j);
      } else {
        if (instance.value(j) < m_Min[j]) {
          m_Min[j] = instance.value(j);
        } else {
          if (instance.value(j) > m_Max[j]) {
            m_Max[j] = instance.value(j);
          }
        }
      }
    }
  }

  /**
   * clusters an instance that has been through the filters
   *
   * @param instance the instance to assign a cluster to
   * @return a cluster number
   */
  protected int clusterProcessedInstance(Instance instance) {
    double minDist = Double.MAX_VALUE;
    int bestCluster = 0;
    for (int i = 0; i < m_NumClusters; i++) {
      double dist = distance(instance, m_ClusterCentroids.instance(i));
      if (dist < minDist) {
        minDist = dist;
        bestCluster = i;
      }
    }
    return bestCluster;
  }

  /**
   * Classifies a given instance.
   *
   * @param instance the instance to be assigned to a cluster
   * @return the number of the assigned cluster as an integer if the class is
   *         enumerated, otherwise the predicted value
   * @throws Exception if instance could not be classified successfully
   */
  @Override
  public int clusterInstance(Instance instance) throws Exception {
    m_ReplaceMissingFilter.input(instance);
    m_ReplaceMissingFilter.batchFinished();
    Instance inst = m_ReplaceMissingFilter.output();

    return clusterProcessedInstance(inst);
  }

  /**
   * Calculates the distance between two instances
   *
   * @param first the first instance
   * @param second the second instance
   * @return the distance between the two given instances, between 0 and 1
   */
  protected double distance(Instance first, Instance second) {

    double distance = 0;
    int firstI, secondI;

    for (int p1 = 0, p2 = 0; p1 < first.numValues() || p2 < second.numValues();) {
      if (p1 >= first.numValues()) {
        firstI = m_instances.numAttributes();
      } else {
        firstI = first.index(p1);
      }
      if (p2 >= second.numValues()) {
        secondI = m_instances.numAttributes();
      } else {
        secondI = second.index(p2);
      }
      if (firstI == m_instances.classIndex()) {
        p1++;
        continue;
      }
      if (secondI == m_instances.classIndex()) {
        p2++;
        continue;
      }
      double diff;
      if (firstI == secondI) {
        diff = difference(firstI, first.valueSparse(p1), second.valueSparse(p2));
        p1++;
        p2++;
      } else if (firstI > secondI) {
        diff = difference(secondI, 0, second.valueSparse(p2));
        p2++;
      } else {
        diff = difference(firstI, first.valueSparse(p1), 0);
        p1++;
      }
      distance += diff * diff;
    }

    return Math.sqrt(distance / m_instances.numAttributes());
  }

  /**
   * Computes the difference between two given attribute values.
   */
  protected double difference(int index, double val1, double val2) {

    switch (m_instances.attribute(index).type()) {
    case Attribute.NOMINAL:

      // If attribute is nominal
      if (Utils.isMissingValue(val1) || Utils.isMissingValue(val2)
        || ((int) val1 != (int) val2)) {
        return 1;
      } else {
        return 0;
      }
    case Attribute.NUMERIC:

      // If attribute is numeric
      if (Utils.isMissingValue(val1) || Utils.isMissingValue(val2)) {
        if (Utils.isMissingValue(val1) && Utils.isMissingValue(val2)) {
          return 1;
        } else {
          double diff;
          if (Utils.isMissingValue(val2)) {
            diff = norm(val1, index);
          } else {
            diff = norm(val2, index);
          }
          if (diff < 0.5) {
            diff = 1.0 - diff;
          }
          return diff;
        }
      } else {
        return norm(val1, index) - norm(val2, index);
      }
    default:
      return 0;
    }
  }

  /**
   * Normalizes a given value of a numeric attribute.
   *
   * @param x the value to be normalized
   * @param i the attribute's index
   * @return the normalized value
   */
  protected double norm(double x, int i) {

    if (Double.isNaN(m_Min[i]) || Utils.eq(m_Max[i], m_Min[i])) {
      return 0;
    } else {
      return (x - m_Min[i]) / (m_Max[i] - m_Min[i]);
    }
  }

  /**
   * Returns the number of clusters.
   *
   * @return the number of clusters generated for a training dataset.
   * @throws Exception if number of clusters could not be returned successfully
   */
  @Override
  public int numberOfClusters() throws Exception {
    return m_NumClusters;
  }

  /**
   * Get the centroids found by FarthestFirst
   *
   * @return the centroids found by FarthestFirst
   */
  public Instances getClusterCentroids() {
    return m_ClusterCentroids;
  }

  /**
   * Returns an enumeration describing the available options.
   *
   * @return an enumeration of all the available options.
   */
  @Override
  public Enumeration<Option> listOptions() {
    Vector<Option> result = new Vector<Option>();

    result.addElement(new Option("\tnumber of clusters. (default = 2).", "N",
      1, "-N <num>"));

    result.addAll(Collections.list(super.listOptions()));

    return result.elements();
  }

  /**
   * Returns the tip text for this property
   *
   * @return tip text for this property suitable for displaying in the
   *         explorer/experimenter gui
   */
  public String numClustersTipText() {
    return "set number of clusters";
  }

  /**
   * set the number of clusters to generate
   *
   * @param n the number of clusters to generate
   * @throws Exception if number of clusters is negative
   */
  public void setNumClusters(int n) throws Exception {
    if (n < 0) {
      throw new Exception("Number of clusters must be > 0");
    }
    m_NumClusters = n;
  }

  /**
   * gets the number of clusters to generate
   *
   * @return the number of clusters to generate
   */
  public int getNumClusters() {
    return m_NumClusters;
  }

  /**
   * Parses a given list of options.
   *

   *

   * Valid options are:

   *
   *
 -N <num>
   *  number of clusters. (default = 2).

   *
   *
 -S <num>
   *  Random number seed.
   *  (default 1)

   *

   *
   * @param options the list of options as an array of strings
   * @throws Exception if an option is not supported
   */
  @Override
  public void setOptions(String[] options) throws Exception {

    String optionString = Utils.getOption('N', options);

    if (optionString.length() != 0) {
      setNumClusters(Integer.parseInt(optionString));
    }

    super.setOptions(options);

    Utils.checkForRemainingOptions(options);
  }

  /**
   * Gets the current settings of FarthestFirst
   *
   * @return an array of strings suitable for passing to setOptions()
   */
  @Override
  public String[] getOptions() {

    Vector<String> result = new Vector<String>();

    result.add("-N");
    result.add("" + getNumClusters());

    Collections.addAll(result, super.getOptions());

    return result.toArray(new String[result.size()]);
  }

  /**
   * return a string describing this clusterer
   *
   * @return a description of the clusterer as a string
   */
  @Override
  public String toString() {
    StringBuffer temp = new StringBuffer();

    temp.append("\nFarthestFirst\n==============\n");

    temp.append("\nCluster centroids:\n");
    for (int i = 0; i < m_NumClusters; i++) {
      temp.append("\nCluster " + i + "\n\t");
      for (int j = 0; j < m_ClusterCentroids.numAttributes(); j++) {
        if (m_ClusterCentroids.attribute(j).isNominal()) {
          temp.append(" "
            + m_ClusterCentroids.attribute(j).value(
              (int) m_ClusterCentroids.instance(i).value(j)));
        } else {
          temp.append(" " + m_ClusterCentroids.instance(i).value(j));
        }
      }
    }
    temp.append("\n\n");
    return temp.toString();
  }

  /**
   * Returns the revision string.
   *
   * @return the revision
   */
  @Override
  public String getRevision() {
    return RevisionUtils.extract("$Revision: 10453 $");
  }

  /**
   * Main method for testing this class.
   *
   * @param argv should contain the following arguments:
   *

   *          -t training file [-N number of clusters]
   */
  public static void main(String[] argv) {
    runClusterer(new FarthestFirst(), argv);
  }
}


text:
/**
 * @file   es2812_rainbow.c
 * @author Ondřej Hruška, 2016
 *
 * @brief  Example of a rainbow effect with
 *         WS2812 connected to GPIO2.
 *
 * This demo is in the public domain.
 */

#include "espressif/esp_common.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp/uart.h" // uart_set_baud
#include <stdio.h> // printf
#include <stdint.h>

#include "ws2812.h"

#define delay_ms(ms) vTaskDelay((ms) / portTICK_RATE_MS)

/** GPIO number used to control the RGBs */
static const uint8_t pin = 2;

/**
 * @brief "rainbow" animation with a single RGB led.
 */
void demo_single(void)
{
    // duration between color changes
    const uint8_t delay = 25;

    ws2812_rgb_t x = {.num = 0xFF0000}; // RED color

    while (1) {
        // iterate through the spectrum

        // note: This would be _WAY_ easier with HSL

        while(x.g < 0xFF) { x.g++; ws2812_set(pin, x.num); delay_ms(delay); } // R->RG
        while(x.r > 0x00) { x.r--; ws2812_set(pin, x.num); delay_ms(delay); } // RG->G
        while(x.b < 0xFF) { x.b++; ws2812_set(pin, x.num); delay_ms(delay); } // G->GB
        while(x.g > 0x00) { x.g--; ws2812_set(pin, x.num); delay_ms(delay); } // GB->B
        while(x.r < 0xFF) { x.r++; ws2812_set(pin, x.num); delay_ms(delay); } // B->BR
        while(x.b > 0x00) { x.b--; ws2812_set(pin, x.num); delay_ms(delay); } // BR->R
    }
}

/**
 * @brief "rainbow" effect on a RGB strip (30 pixels - can be adjusted)
 *
 * This example shows how to use the "procedural generation" of colors.
 *
 * The pixel colors are calculated on the fly, which saves RAM
 * (especially with large displays).
 */
void demo_strip(void *pvParameters)
{
    const uint8_t anim_step = 10;
    const uint8_t anim_max = 250;

    // Number of your "pixels"
    const uint8_t pixel_count = 30;

    // duration between color changes
    const uint8_t delay = 25;

    ws2812_rgb_t color = WS2812_RGB(anim_max, 0, 0);
    uint8_t step = 0;

    ws2812_rgb_t color2 = WS2812_RGB(anim_max, 0, 0);
    uint8_t step2 = 0;

    while (1) {

        color = color2;
        step = step2;

        // Start a data sequence (disables interrupts)
        ws2812_seq_start();

        for (uint8_t i = 0; i < pixel_count; i++) {

            // send a color
            ws2812_seq_rgb(pin, color.num);

            // now we have a few hundred nanoseconds
            // to calculate the next color

            if (i == 1) {
                color2 = color;
                step2 = step;
            }

            switch (step) {
                case 0: color.g += anim_step; if (color.g >= anim_max) step++;  break;
                case 1: color.r -= anim_step; if (color.r == 0) step++; break;
                case 2: color.b += anim_step; if (color.b >= anim_max) step++; break;
                case 3: color.g -= anim_step; if (color.g == 0) step++; break;
                case 4: color.r += anim_step; if (color.r >= anim_max) step++; break;
                case 5: color.b -= anim_step; if (color.b == 0) step = 0; break;
            }
        }

        // End the data sequence, display colors (interrupts are restored)
        ws2812_seq_end();

        // wait a bit
        delay_ms(delay);
    }
}

void user_init(void)
{
    uart_set_baud(0, 115200);
    printf("--- RGB Rainbow demo ---");

    // Configure the GPIO
    gpio_enable(pin, GPIO_OUTPUT);

    // Select a demo function:

#if true
# define demo demo_strip
#else
# define demo demo_single
#endif

    // Choose how to run it:

#if true

    // Blocking function - works OK, because WiFi isn't
    // initialized yet & we're hogging the CPU.

    printf("Starting a blocking function.\r\n");
    demo(NULL);

#else

    // Start a task. This is a real-life example,
    // notice the glitches due to NMI.

    printf("Starting a task. There may be glitches!\r\n");
    xTaskCreate(&demo, (signed char *)"strip demo", 256, NULL, 10, NULL);
#endif
}


text:
namespace boost { namespace geometry
{

#ifndef DOXYGEN_NO_DETAIL

namespace detail { namespace envelope
{

template<typename MultiLinestring, typename Box>
struct envelope_multi_linestring
{
    static inline void apply(MultiLinestring const& mp, Box& mbr)
    {
        assign_inverse(mbr);
        for (typename boost::range_iterator<MultiLinestring const>::type
                    it = mp.begin();
            it != mp.end();
            ++it)
        {
            envelope_range_additional(*it, mbr);
        }
    }
};

// version for multi_polygon: outer ring's of all polygons
template<typename MultiPolygon, typename Box>
struct envelope_multi_polygon
{
    static inline void apply(MultiPolygon const& mp, Box& mbr)
    {
        assign_inverse(mbr);
        for (typename boost::range_const_iterator<MultiPolygon>::type
                    it = mp.begin();
            it != mp.end();
            ++it)
        {
            envelope_range_additional(exterior_ring(*it), mbr);
        }
    }
};

}} // namespace detail::envelope

#endif

#ifndef DOXYGEN_NO_DISPATCH
namespace dispatch
{

template
<
    typename Multi, typename Box,
    typename StrategyLess, typename StrategyGreater
>
struct envelope<multi_point_tag, box_tag, Multi, Box, StrategyLess, StrategyGreater>
    : detail::envelope::envelope_range<Multi, Box>
{};

template
<
    typename Multi, typename Box,
    typename StrategyLess, typename StrategyGreater
>
struct envelope<multi_linestring_tag, box_tag, Multi, Box, StrategyLess, StrategyGreater>
    : detail::envelope::envelope_multi_linestring<Multi, Box>
{};

template
<
    typename Multi, typename Box,
    typename StrategyLess, typename StrategyGreater
>
struct envelope<multi_polygon_tag, box_tag, Multi, Box, StrategyLess, StrategyGreater>
    : detail::envelope::envelope_multi_polygon<Multi, Box>
{};

} // namespace dispatch
#endif

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_MULTI_ALGORITHMS_ENVELOPE_HPP


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Anal. Mus. nac. B. Aires, Ser. 3 20(13): 379 (1910)

#### Original name
Septoria andropogonicola Speg.

### Remarks
null


text:
using leveldb_proto::test::FakeDB;
using testing::_;
using testing::Eq;
using testing::Pointee;

namespace media {

class VideoDecodeStatsDBImplTest : public ::testing::Test {
 public:
  using VideoDescKey = VideoDecodeStatsDB::VideoDescKey;
  using DecodeStatsEntry = VideoDecodeStatsDB::DecodeStatsEntry;

  VideoDecodeStatsDBImplTest()
      : kStatsKeyVp9(VideoDescKey::MakeBucketedKey(VP9PROFILE_PROFILE3,
                                                   gfx::Size(1024, 768),
                                                   60,
                                                   "com.widevine.alpha",
                                                   true)),
        kStatsKeyAvc(VideoDescKey::MakeBucketedKey(H264PROFILE_MIN,
                                                   gfx::Size(1024, 768),
                                                   60,
                                                   "",
                                                   false)) {
    bool parsed_time = base::Time::FromString(
        VideoDecodeStatsDBImpl::kDefaultWriteTime, &kDefaultWriteTime);
    DCHECK(parsed_time);

    // Fake DB simply wraps a std::map with the LevelDB interface. We own the
    // map and will delete it in TearDown().
    fake_db_map_ = std::make_unique<FakeDB<DecodeStatsProto>::EntryMap>();
    // |stats_db_| will own this pointer, but we hold a reference to control
    // its behavior.
    fake_db_ = new FakeDB<DecodeStatsProto>(fake_db_map_.get());

    // Wrap the fake proto DB with our interface.
    stats_db_ = base::WrapUnique(new VideoDecodeStatsDBImpl(
        std::unique_ptr<FakeDB<DecodeStatsProto>>(fake_db_)));
  }

  VideoDecodeStatsDBImplTest(const VideoDecodeStatsDBImplTest&) = delete;
  VideoDecodeStatsDBImplTest& operator=(const VideoDecodeStatsDBImplTest&) =
      delete;

  ~VideoDecodeStatsDBImplTest() override {
    // Tests should always complete any pending operations
    VerifyNoPendingOps();
  }

  void VerifyOnePendingOp(std::string op_name) {
    EXPECT_EQ(stats_db_->pending_ops_.size(), 1u);
    VideoDecodeStatsDBImpl::PendingOperation* pending_op =
        stats_db_->pending_ops_.begin()->second.get();
    EXPECT_EQ(pending_op->uma_str_, op_name);
  }

  void VerifyNoPendingOps() { EXPECT_TRUE(stats_db_->pending_ops_.empty()); }

  int GetMaxFramesPerBuffer() {
    return VideoDecodeStatsDBImpl::GetMaxFramesPerBuffer();
  }

  int GetMaxDaysToKeepStats() {
    return VideoDecodeStatsDBImpl::GetMaxDaysToKeepStats();
  }

  bool GetEnableUnweightedEntries() {
    return VideoDecodeStatsDBImpl::GetEnableUnweightedEntries();
  }

  static base::FieldTrialParams GetFieldTrialParams() {
    return VideoDecodeStatsDBImpl::GetFieldTrialParams();
  }

  void SetDBClock(base::Clock* clock) {
    stats_db_->set_wall_clock_for_test(clock);
  }

  void InitializeDB() {
    stats_db_->Initialize(base::BindOnce(
        &VideoDecodeStatsDBImplTest::OnInitialize, base::Unretained(this)));
    EXPECT_CALL(*this, OnInitialize(true));
    fake_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
    testing::Mock::VerifyAndClearExpectations(this);
  }

  void AppendStats(const VideoDescKey& key, const DecodeStatsEntry& entry) {
    EXPECT_CALL(*this, MockAppendDecodeStatsCb(true));
    stats_db_->AppendDecodeStats(
        key, entry,
        base::BindOnce(&VideoDecodeStatsDBImplTest::MockAppendDecodeStatsCb,
                       base::Unretained(this)));
    VerifyOnePendingOp("Read");
    fake_db_->GetCallback(true);
    VerifyOnePendingOp("Write");
    fake_db_->UpdateCallback(true);
    testing::Mock::VerifyAndClearExpectations(this);
  }

  void VerifyReadStats(const VideoDescKey& key,
                       const DecodeStatsEntry& expected) {
    EXPECT_CALL(*this, MockGetDecodeStatsCb(true, Pointee(Eq(expected))));
    stats_db_->GetDecodeStats(
        key, base::BindOnce(&VideoDecodeStatsDBImplTest::GetDecodeStatsCb,
                            base::Unretained(this)));
    VerifyOnePendingOp("Read");
    fake_db_->GetCallback(true);
    testing::Mock::VerifyAndClearExpectations(this);
  }

  void VerifyEmptyStats(const VideoDescKey& key) {
    EXPECT_CALL(*this, MockGetDecodeStatsCb(true, nullptr));
    stats_db_->GetDecodeStats(
        key, base::BindOnce(&VideoDecodeStatsDBImplTest::GetDecodeStatsCb,
                            base::Unretained(this)));
    VerifyOnePendingOp("Read");
    fake_db_->GetCallback(true);
    testing::Mock::VerifyAndClearExpectations(this);
  }

  // Unwraps move-only parameters to pass to the mock function.
  void GetDecodeStatsCb(bool success, std::unique_ptr<DecodeStatsEntry> entry) {
    MockGetDecodeStatsCb(success, entry.get());
  }

  void AppendToProtoDB(const VideoDescKey& key,
                       const DecodeStatsProto* const proto) {
    base::RunLoop run_loop;
    base::OnceCallback<void(bool)> update_done_cb = base::BindOnce(
        [](base::RunLoop* run_loop, bool success) {
          ASSERT_TRUE(success);
          run_loop->Quit();
        },
        Unretained(&run_loop));

    using DBType = leveldb_proto::ProtoDatabase<DecodeStatsProto>;
    std::unique_ptr<DBType::KeyEntryVector> entries =
        std::make_unique<DBType::KeyEntryVector>();
    entries->emplace_back(key.Serialize(), *proto);

    fake_db_->UpdateEntries(std::move(entries),
                            std::make_unique<leveldb_proto::KeyVector>(),
                            std::move(update_done_cb));

    fake_db_->UpdateCallback(true);
    run_loop.Run();
  }

  MOCK_METHOD1(OnInitialize, void(bool success));

  MOCK_METHOD2(MockGetDecodeStatsCb,
               void(bool success, DecodeStatsEntry* entry));

  MOCK_METHOD1(MockAppendDecodeStatsCb, void(bool success));

  MOCK_METHOD0(MockClearStatsCb, void());

 protected:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  const VideoDescKey kStatsKeyVp9;
  const VideoDescKey kStatsKeyAvc;

  // Const in practice, but not marked const for compatibility with
  // base::Time::FromString.
  base::Time kDefaultWriteTime;

  // See documentation in SetUp()
  std::unique_ptr<FakeDB<DecodeStatsProto>::EntryMap> fake_db_map_;
  FakeDB<DecodeStatsProto>* fake_db_;
  std::unique_ptr<VideoDecodeStatsDBImpl> stats_db_;
};

TEST_F(VideoDecodeStatsDBImplTest, InitializeFailed) {
  stats_db_->Initialize(base::BindOnce(
      &VideoDecodeStatsDBImplTest::OnInitialize, base::Unretained(this)));
  EXPECT_CALL(*this, OnInitialize(false));
  fake_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kError);
}

TEST_F(VideoDecodeStatsDBImplTest, InitializeTimedOut) {
  // Queue up an Initialize.
  stats_db_->Initialize(base::BindOnce(
      &VideoDecodeStatsDBImplTest::OnInitialize, base::Unretained(this)));
  VerifyOnePendingOp("Initialize");

  // Move time forward enough to trigger timeout.
  EXPECT_CALL(*this, OnInitialize(_)).Times(0);
  task_environment_.FastForwardBy(base::Seconds(100));
  task_environment_.RunUntilIdle();

  // Verify we didn't get an init callback and task is no longer considered
  // pending (because it timed out).
  testing::Mock::VerifyAndClearExpectations(this);
  VerifyNoPendingOps();

  // Verify callback still works if init completes very late.
  EXPECT_CALL(*this, OnInitialize(false));
  fake_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kError);
}

TEST_F(VideoDecodeStatsDBImplTest, ReadExpectingNothing) {
  InitializeDB();
  VerifyEmptyStats(kStatsKeyVp9);
}

TEST_F(VideoDecodeStatsDBImplTest, WriteReadAndClear) {
  InitializeDB();

  // Append and read back some VP9 stats.
  DecodeStatsEntry entry(1000, 2, 10);
  AppendStats(kStatsKeyVp9, entry);
  VerifyReadStats(kStatsKeyVp9, entry);

  // Reading with the wrong key (different codec) should still return nothing.
  VerifyEmptyStats(kStatsKeyAvc);

  // Appending same VP9 stats should read back as 2x the initial entry.
  AppendStats(kStatsKeyVp9, entry);
  DecodeStatsEntry aggregate_entry(2000, 4, 20);
  VerifyReadStats(kStatsKeyVp9, aggregate_entry);

  // Clear all stats from the DB.
  EXPECT_CALL(*this, MockClearStatsCb);
  stats_db_->ClearStats(base::BindOnce(
      &VideoDecodeStatsDBImplTest::MockClearStatsCb, base::Unretained(this)));
  VerifyOnePendingOp("Clear");
  fake_db_->UpdateCallback(true);

  // Database is now empty. Expect null entry.
  VerifyEmptyStats(kStatsKeyVp9);
}

TEST_F(VideoDecodeStatsDBImplTest, ConfigureMaxFramesPerBuffer) {
  // Setup field trial to use a tiny window of 1 decoded frame.

  base::test::ScopedFeatureList scoped_feature_list;
  std::unique_ptr<base::FieldTrialList> field_trial_list;

  int previous_max_frames_per_buffer = GetMaxFramesPerBuffer();
  int new_max_frames_per_buffer = 1;

  ASSERT_LT(new_max_frames_per_buffer, previous_max_frames_per_buffer);

  // Override field trial.
  base::FieldTrialParams params;
  params[VideoDecodeStatsDBImpl::kMaxFramesPerBufferParamName] =
      base::NumberToString(new_max_frames_per_buffer);

  scoped_feature_list.InitAndEnableFeatureWithParameters(
      media::kMediaCapabilitiesWithParameters, params);

  EXPECT_EQ(GetFieldTrialParams(), params);

  EXPECT_EQ(new_max_frames_per_buffer, GetMaxFramesPerBuffer());

  // Now verify the configured window is used by writing a frame and then
  // writing another.

  InitializeDB();

  // Append single frame which was, sadly, dropped and not efficient.
  DecodeStatsEntry entry(1, 1, 0);
  AppendStats(kStatsKeyVp9, entry);
  VerifyReadStats(kStatsKeyVp9, entry);

  // Appending another frame which is *not* dropped and *is* efficient.
  // Verify that only this last entry is still in the buffer (no aggregation).
  DecodeStatsEntry second_entry(1, 0, 1);
  AppendStats(kStatsKeyVp9, second_entry);
  VerifyReadStats(kStatsKeyVp9, second_entry);
}

TEST_F(VideoDecodeStatsDBImplTest, ConfigureExpireDays) {
  base::test::ScopedFeatureList scoped_feature_list;
  std::unique_ptr<base::FieldTrialList> field_trial_list;

  int previous_max_days_to_keep_stats = GetMaxDaysToKeepStats();
  int new_max_days_to_keep_stats = 4;

  ASSERT_LT(new_max_days_to_keep_stats, previous_max_days_to_keep_stats);

  // Override field trial.
  base::FieldTrialParams params;
  params[VideoDecodeStatsDBImpl::kMaxDaysToKeepStatsParamName] =
      base::NumberToString(new_max_days_to_keep_stats);

  scoped_feature_list.InitAndEnableFeatureWithParameters(
      media::kMediaCapabilitiesWithParameters, params);

  EXPECT_EQ(GetFieldTrialParams(), params);

  EXPECT_EQ(new_max_days_to_keep_stats, GetMaxDaysToKeepStats());

  InitializeDB();

  // Inject a test clock and initialize with the current time.
  base::SimpleTestClock clock;
  SetDBClock(&clock);
  clock.SetNow(base::Time::Now());

  // Append and verify read-back.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));

  // Some simple math to avoid troubles of integer division.
  int half_days_to_keep_stats = new_max_days_to_keep_stats / 2;
  int remaining_days_to_keep_stats =
      new_max_days_to_keep_stats - half_days_to_keep_stats;

  // Advance time half way through grace period. Verify stats not expired.
  clock.Advance(base::Days(half_days_to_keep_stats));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));

  // Advance time 1 day beyond grace period, verify stats are expired.
  clock.Advance(base::Days((remaining_days_to_keep_stats) + 1));
  VerifyEmptyStats(kStatsKeyVp9);

  // Advance the clock 100 extra days. Verify stats still expired.
  clock.Advance(base::Days(100));
  VerifyEmptyStats(kStatsKeyVp9);
}

TEST_F(VideoDecodeStatsDBImplTest, FailedWrite) {
  InitializeDB();

  // Expect the callback to indicate success = false when the write fails.
  EXPECT_CALL(*this, MockAppendDecodeStatsCb(false));

  // Append stats, but fail the internal DB update.
  stats_db_->AppendDecodeStats(
      kStatsKeyVp9, DecodeStatsEntry(1000, 2, 10),
      base::BindOnce(&VideoDecodeStatsDBImplTest::MockAppendDecodeStatsCb,
                     base::Unretained(this)));
  fake_db_->GetCallback(true);
  fake_db_->UpdateCallback(false);
}

TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
  InitializeDB();

  // Setup DB entry that half fills the buffer with 10% of frames dropped and
  // 50% of frames power efficient.
  const int kNumFramesEntryA = GetMaxFramesPerBuffer() / 2;
  DecodeStatsEntry entryA(kNumFramesEntryA, std::round(0.1 * kNumFramesEntryA),
                          std::round(0.5 * kNumFramesEntryA));
  const double kDropRateA =
      static_cast<double>(entryA.frames_dropped) / entryA.frames_decoded;
  const double kEfficientRateA =
      static_cast<double>(entryA.frames_power_efficient) /
      entryA.frames_decoded;

  // Append entryA to half fill the buffer and verify read. Verify read.
  AppendStats(kStatsKeyVp9, entryA);
  VerifyReadStats(kStatsKeyVp9, entryA);

  // Append same entryA again to completely fill the buffer. Verify read gives
  // out aggregated stats (2x the initial entryA)
  AppendStats(kStatsKeyVp9, entryA);
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(GetMaxFramesPerBuffer(),
                       std::round(kDropRateA * GetMaxFramesPerBuffer()),
                       std::round(kEfficientRateA * GetMaxFramesPerBuffer())));

  // This row in the DB is now "full" (appended frames >= kMaxFramesPerBuffer)!
  //
  // Future appends will not increase the total count of decoded frames. The
  // ratios of dropped and power efficient frames will be a weighted average.
  // The weight of new appends is determined by how much of the buffer they
  // fill, i.e.
  //
  //   new_append_weight = min(1, new_append_frame_count / kMaxFramesPerBuffer);
  //   old_data_weight = 1 - new_append_weight;
  //
  // The calculation for dropped ratios (same for power efficient) then becomes:
  //
  //   aggregate_drop_ratio = old_drop_ratio * old_drop_weight +
  //                          new_drop_ratio * new_data_weight;
  //
  // See implementation for more details.

  // Append same entryA a 3rd time. Verify we still only get the aggregated
  // stats from above (2x entryA) because the buffer is full and the ratio of
  // dropped and efficient frames is the same.
  AppendStats(kStatsKeyVp9, entryA);
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(GetMaxFramesPerBuffer(),
                       std::round(kDropRateA * GetMaxFramesPerBuffer()),
                       std::round(kEfficientRateA * GetMaxFramesPerBuffer())));

  // Append entryB that will fill just 10% of the buffer. The new entry has
  // different rates of dropped and power efficient frames to help verify that
  // it is given proper weight as it mixes with existing data in the buffer.
  const int kNumFramesEntryB = std::round(.1 * GetMaxFramesPerBuffer());
  DecodeStatsEntry entryB(kNumFramesEntryB, std::round(0.25 * kNumFramesEntryB),
                          std::round(1 * kNumFramesEntryB));
  const double kDropRateB =
      static_cast<double>(entryB.frames_dropped) / entryB.frames_decoded;
  const double kEfficientRateB =
      static_cast<double>(entryB.frames_power_efficient) /
      entryB.frames_decoded;
  AppendStats(kStatsKeyVp9, entryB);
  // Verify that buffer is still full, but dropped and power efficient frame
  // rates are now higher according to entryB's impact (10%) on the full buffer.
  double mixed_drop_rate = .1 * kDropRateB + .9 * kDropRateA;
  double mixed_effiency_rate = .1 * kEfficientRateB + .9 * kEfficientRateA;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(
          GetMaxFramesPerBuffer(),
          std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
          std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));

  // After appending entryB again, verify aggregate ratios behave according to
  // the formula above (appending repeated entryB brings ratios closer to those
  // in entryB, further from entryA).
  AppendStats(kStatsKeyVp9, entryB);
  mixed_drop_rate = .1 * kDropRateB + .9 * mixed_drop_rate;
  mixed_effiency_rate = .1 * kEfficientRateB + .9 * mixed_effiency_rate;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(
          GetMaxFramesPerBuffer(),
          std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
          std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));

  // Appending entry*A* again, verify aggregate ratios behave according to
  // the formula above (ratio's move back in direction of those in entryA).
  // Since entryA fills half the buffer it gets a higher weight than entryB did
  // above.
  AppendStats(kStatsKeyVp9, entryA);
  mixed_drop_rate = .5 * kDropRateA + .5 * mixed_drop_rate;
  mixed_effiency_rate = .5 * kEfficientRateA + .5 * mixed_effiency_rate;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(
          GetMaxFramesPerBuffer(),
          std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
          std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));

  // Now append entryC with a frame count of 2x the buffer max. Verify entryC
  // gets 100% of the weight, erasing the mixed stats from earlier appends.
  const int kNumFramesEntryC = 2 * GetMaxFramesPerBuffer();
  DecodeStatsEntry entryC(kNumFramesEntryC, std::round(0.3 * kNumFramesEntryC),
                          std::round(0.25 * kNumFramesEntryC));
  const double kDropRateC =
      static_cast<double>(entryC.frames_dropped) / entryC.frames_decoded;
  const double kEfficientRateC =
      static_cast<double>(entryC.frames_power_efficient) /
      entryC.frames_decoded;
  AppendStats(kStatsKeyVp9, entryC);
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(GetMaxFramesPerBuffer(),
                       std::round(GetMaxFramesPerBuffer() * kDropRateC),
                       std::round(GetMaxFramesPerBuffer() * kEfficientRateC)));
}

// Overfilling an empty buffer triggers the codepath to compute weighted dropped
// and power efficient ratios under a circumstance where the existing counts are
// all zero. This test ensures that we don't do any dividing by zero with that
// empty data.
TEST_F(VideoDecodeStatsDBImplTest, OverfillEmptyBuffer) {
  InitializeDB();

  // Setup DB entry that overflows the buffer max (by 1) with 10% of frames
  // dropped and 50% of frames power efficient.
  const int kNumFramesOverfill = GetMaxFramesPerBuffer() + 1;
  DecodeStatsEntry entryA(kNumFramesOverfill,
                          std::round(0.1 * kNumFramesOverfill),
                          std::round(0.5 * kNumFramesOverfill));

  // Append entry to completely fill the buffer and verify read.
  AppendStats(kStatsKeyVp9, entryA);
  // Read-back stats should have same ratios, but scaled such that
  // frames_decoded = GetMaxFramesPerBuffer().
  DecodeStatsEntry readBackEntryA(GetMaxFramesPerBuffer(),
                                  std::round(0.1 * GetMaxFramesPerBuffer()),
                                  std::round(0.5 * GetMaxFramesPerBuffer()));
  VerifyReadStats(kStatsKeyVp9, readBackEntryA);

  // Append another entry that again overfills with different dropped and power
  // efficient ratios. Verify that read-back only reflects latest entry.
  DecodeStatsEntry entryB(kNumFramesOverfill,
                          std::round(0.2 * kNumFramesOverfill),
                          std::round(0.6 * kNumFramesOverfill));
  AppendStats(kStatsKeyVp9, entryB);
  // Read-back stats should have same ratios, but scaled such that
  // frames_decoded = GetMaxFramesPerBuffer().
  DecodeStatsEntry readBackEntryB(GetMaxFramesPerBuffer(),
                                  std::round(0.2 * GetMaxFramesPerBuffer()),
                                  std::round(0.6 * GetMaxFramesPerBuffer()));
  VerifyReadStats(kStatsKeyVp9, readBackEntryB);
}

TEST_F(VideoDecodeStatsDBImplTest, NoWriteDateReadAndExpire) {
  InitializeDB();

  // Seed the fake proto DB with an old-style entry lacking a write date. This
  // will cause the DB to use kDefaultWriteTime.
  DecodeStatsProto proto_lacking_date;
  proto_lacking_date.set_frames_decoded(100);
  proto_lacking_date.set_frames_dropped(10);
  proto_lacking_date.set_frames_power_efficient(1);
  fake_db_map_->emplace(kStatsKeyVp9.Serialize(), proto_lacking_date);

  // Set "now" to be *before* the default write date. This will be the common
  // case when the proto update (adding last_write_date) first ships (i.e. we
  // don't want to immediately expire all the existing data).
  base::SimpleTestClock clock;
  SetDBClock(&clock);
  clock.SetNow(kDefaultWriteTime - base::Days(10));
  // Verify the stats are readable (not expired).
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(100, 10, 1));

  // Set "now" to be in the middle of the grace period. Verify stats are still
  // readable (not expired).
  clock.SetNow(kDefaultWriteTime + base::Days(GetMaxDaysToKeepStats() / 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(100, 10, 1));

  // Set the clock 1 day beyond the expiry date. Verify stats are no longer
  // readable due to expiration.
  clock.SetNow(kDefaultWriteTime + base::Days(GetMaxDaysToKeepStats() + 1));
  VerifyEmptyStats(kStatsKeyVp9);

  // Write some stats to the entry. Verify we get back exactly what's written
  // without summing with the expired stats.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(50, 5, 0));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(50, 5, 0));
}

TEST_F(VideoDecodeStatsDBImplTest, NoWriteDateAppendReadAndExpire) {
  InitializeDB();

  // Seed the fake proto DB with an old-style entry lacking a write date. This
  // will cause the DB to use kDefaultWriteTime.
  DecodeStatsProto proto_lacking_date;
  proto_lacking_date.set_frames_decoded(100);
  proto_lacking_date.set_frames_dropped(10);
  proto_lacking_date.set_frames_power_efficient(1);
  fake_db_map_->emplace(kStatsKeyVp9.Serialize(), proto_lacking_date);

  // Set "now" to be *before* the default write date. This will be the common
  // case when the proto update (adding last_write_date) first ships (i.e. we
  // don't want to immediately expire all the existing data).
  base::SimpleTestClock clock;
  SetDBClock(&clock);
  clock.SetNow(kDefaultWriteTime - base::Days(10));
  // Verify the stats are readable (not expired).
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(100, 10, 1));

  // Append some stats and verify the aggregate math is correct. This will
  // update the last_write_date to the current clock time.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(300, 30, 3));

  // Set "now" to be in the middle of the grace period. Verify stats are still
  // readable (not expired).
  clock.SetNow(kDefaultWriteTime + base::Days(GetMaxDaysToKeepStats() / 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(300, 30, 3));

  // Set the clock 1 day beyond the expiry date. Verify stats are no longer
  // readable due to expiration.
  clock.SetNow(kDefaultWriteTime + base::Days(GetMaxDaysToKeepStats() + 1));
  VerifyEmptyStats(kStatsKeyVp9);
}

TEST_F(VideoDecodeStatsDBImplTest, AppendAndExpire) {
  InitializeDB();

  // Inject a test clock and initialize with the current time.
  base::SimpleTestClock clock;
  SetDBClock(&clock);
  clock.SetNow(base::Time::Now());

  // Append and verify read-back.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));

  // Advance time half way through grace period. Verify stats not expired.
  clock.Advance(base::Days(GetMaxDaysToKeepStats() / 2));
  VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));

  // Advance time 1 day beyond grace period, verify stats are expired.
  clock.Advance(base::Days((GetMaxDaysToKeepStats() / 2) + 1));
  VerifyEmptyStats(kStatsKeyVp9);

  // Advance the clock 100 days. Verify stats still expired.
  clock.Advance(base::Days(100));
  VerifyEmptyStats(kStatsKeyVp9);
}

TEST_F(VideoDecodeStatsDBImplTest, EnableUnweightedEntries) {
  base::test::ScopedFeatureList scoped_feature_list;
  std::unique_ptr<base::FieldTrialList> field_trial_list;

  // Default is false.
  EXPECT_FALSE(GetEnableUnweightedEntries());

  // Override field trial.
  base::FieldTrialParams params;
  params[VideoDecodeStatsDBImpl::kEnableUnweightedEntriesParamName] = "true";
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      media::kMediaCapabilitiesWithParameters, params);

  EXPECT_EQ(GetFieldTrialParams(), params);

  // Confirm field trial overridden.
  EXPECT_TRUE(GetMaxDaysToKeepStats());

  InitializeDB();

  // Append 200 frames with 10% dropped, 1% efficient.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(200, 0.10 * 200, 0.01 * 200));
  // Use real doubles to keep track of these things to make sure the precision
  // math for repeating decimals works out with whats done internally.
  int num_appends = 1;
  double unweighted_smoothness_avg = 0.10;
  double unweighted_efficiency_avg = 0.01;

  // NOTE, the members of DecodeStatsEntry have a different meaning when using
  // unweighted DB entries. The denominator is 100,000 * the number of appends
  // and the numerator is whatever value achieves the correct unweighted ratio
  // for those appends. See detailed comment in
  // VideoDecodeStatsDBImpl::OnGotDecodeStats();
  const int kNumAppendScale = 100000;
  int expected_denominator = kNumAppendScale * num_appends;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(expected_denominator,
                       unweighted_smoothness_avg * expected_denominator,
                       unweighted_efficiency_avg * expected_denominator));

  // Append 20K frames with 5% dropped and 10% efficient.
  AppendStats(kStatsKeyVp9,
              DecodeStatsEntry(20000, 0.05 * 20000, 0.10 * 20000));
  num_appends++;
  unweighted_smoothness_avg = (0.10 + 0.05) / num_appends;
  unweighted_efficiency_avg = (0.01 + 0.10) / num_appends;

  // While new record had 100x more frames than the previous append, the ratios
  // should be an unweighted average of the two records (7.5% dropped and
  // 5.5% efficient).
  expected_denominator = kNumAppendScale * num_appends;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(expected_denominator,
                       unweighted_smoothness_avg * expected_denominator,
                       unweighted_efficiency_avg * expected_denominator));

  // Append 1M frames with 3.4567% dropped and 3.4567% efficient.
  AppendStats(kStatsKeyVp9, DecodeStatsEntry(1000000, 0.012345 * 1000000,
                                             0.034567 * 1000000));
  num_appends++;
  unweighted_smoothness_avg = (0.10 + 0.05 + 0.012345) / num_appends;
  unweighted_efficiency_avg = (0.01 + 0.10 + 0.034567) / num_appends;

  // Here, the ratios should still be averaged in the unweighted fashion, but
  // truncated after the 3rd decimal place of the percentage (e.g. 1.234%
  // or the 5th decimal place when represented as a fraction of 1 (0.01234)).
  expected_denominator = kNumAppendScale * num_appends;
  VerifyReadStats(
      kStatsKeyVp9,
      DecodeStatsEntry(expected_denominator,
                       unweighted_smoothness_avg * expected_denominator,
                       unweighted_efficiency_avg * expected_denominator));
}

TEST_F(VideoDecodeStatsDBImplTest, DiscardCorruptedDBData) {
  InitializeDB();

  // Inject a test clock and initialize with the current time.
  base::SimpleTestClock clock;
  SetDBClock(&clock);
  clock.SetNow(base::Time::Now());

  // Construct several distinct key values for storing/retrieving the corrupted
  // data. The details of the keys are not important.
  const auto keyA = VideoDescKey::MakeBucketedKey(
      VP9PROFILE_PROFILE0, gfx::Size(1024, 768), 60, "", false);
  const auto keyB = VideoDescKey::MakeBucketedKey(
      VP9PROFILE_PROFILE1, gfx::Size(1024, 768), 60, "", false);
  const auto keyC = VideoDescKey::MakeBucketedKey(
      VP9PROFILE_PROFILE2, gfx::Size(1024, 768), 60, "", false);
  const auto keyD = VideoDescKey::MakeBucketedKey(
      VP9PROFILE_PROFILE3, gfx::Size(1024, 768), 60, "", false);
  const auto keyE = VideoDescKey::MakeBucketedKey(
      H264PROFILE_BASELINE, gfx::Size(1024, 768), 60, "", false);
  const auto keyF = VideoDescKey::MakeBucketedKey(
      H264PROFILE_MAIN, gfx::Size(1024, 768), 60, "", false);
  const auto keyG = VideoDescKey::MakeBucketedKey(
      H264PROFILE_EXTENDED, gfx::Size(1024, 768), 60, "", false);

  // Start with a proto that represents a valid uncorrupted and unexpired entry.
  DecodeStatsProto protoA;
  protoA.set_frames_decoded(100);
  protoA.set_frames_dropped(15);
  protoA.set_frames_power_efficient(50);
  protoA.set_last_write_date(clock.Now().ToJsTime());
  protoA.set_unweighted_average_frames_dropped(15.0 / 100);
  protoA.set_unweighted_average_frames_efficient(50.0 / 100);
  protoA.set_num_unweighted_playbacks(1);

  // Append it and read it back without issue.
  AppendToProtoDB(keyA, &protoA);
  VerifyReadStats(keyA, DecodeStatsEntry(100, 15, 50));

  // Make the valid proto invalid with more dropped frames than decoded. Verify
  // you can't read it back (filtered for corruption).
  DecodeStatsProto protoB(protoA);
  protoB.set_frames_dropped(150);
  AppendToProtoDB(keyB, &protoB);
  VerifyEmptyStats(keyB);

  // Make an invalid proto with more power efficient frames than decoded. Verify
  // you can't read it back (filtered for corruption).
  DecodeStatsProto protoC(protoA);
  protoC.set_frames_power_efficient(150);
  AppendToProtoDB(keyC, &protoC);
  VerifyEmptyStats(keyC);

  // Make an invalid proto with an unweighted average dropped ratio > 1.
  DecodeStatsProto protoD(protoA);
  protoD.set_unweighted_average_frames_dropped(2.0);
  AppendToProtoDB(keyD, &protoD);
  VerifyEmptyStats(keyD);

  // Make an invalid proto with an unweighted average efficient ratio > 1.
  DecodeStatsProto protoE(protoA);
  protoE.set_unweighted_average_frames_efficient(2.0);
  AppendToProtoDB(keyE, &protoE);
  VerifyEmptyStats(keyE);

  // Make an invalid proto with a negative last write date.
  DecodeStatsProto protoF(protoA);
  protoF.set_last_write_date(-1.0);
  AppendToProtoDB(keyF, &protoF);
  VerifyEmptyStats(keyF);

  // Make an invalid  proto with a last write date in the future.
  DecodeStatsProto protoG(protoA);
  protoG.set_last_write_date((clock.Now() + base::Days(1)).ToJsTime());
  AppendToProtoDB(keyG, &protoG);
  VerifyEmptyStats(keyG);
}

}  // namespace media