<commit_before>#include <dsnutil/compiler_features.h>
#include <dsnutil/log/sinkmanager.h>
#include <dsnutil/map_sort.h>

using namespace dsn::log;

SinkManager::SinkManager()
{
}

SinkManager::~SinkManager()
{
#if (dsnutil_cpp_COMPILER_CXX_RANGE_FOR && dsnutil_cpp_COMPILER_CXX_AUTO_TYPE)
    for (auto& sink : m_sinks) {
        BOOST_LOG_SEV(log, severity::trace) << "Removing leftover log sink: " << sink.first;
        boost::log::core::get()->remove_sink(sink.second);
    }
#else
    for (sink_storage::iterator it = m_sinks.begin(); it != m_sinks.end(); ++it) {
        BOOST_LOG_SEV(log, severity::trace) << "Removing leftover log sink: " << it->first;
        boost::log::core::get()->remove_sink(it->second);
    }
#endif
    m_sinks.clear();
}

bool SinkManager::exists(const std::string& name) const
{
    sink_storage::const_iterator it = m_sinks.find(name);
    if (it != m_sinks.end())
        return true;

    return false;
}

bool SinkManager::add(const std::string& name, const SinkManager::sink_ptr& sink)
{
    if (exists(name)) {
        if (!remove(name)) {
            BOOST_LOG_SEV(log, severity::error) << "Failed to remove existing '" << name << "' sink!";
            return false;
        }
    }

    boost::log::core::get()->add_sink(sink);
    m_sinks[name] = sink;

    return true;
}

bool SinkManager::remove(const std::string& name)
{
    if (!exists(name)) {
        BOOST_LOG_SEV(log, severity::warning) << "Tried to remove non-existant sink: " << name;
        return false;
    }

    boost::log::core::get()->remove_sink(m_sinks[name]);
    m_sinks.erase(name);

    return true;
}

std::vector<std::string> SinkManager::sinks() const
{
    return dsn::map_keys(m_sinks);
}

SinkManager::sink_ptr SinkManager::sink(const std::string& name)
{
    if (!exists(name)) {
        BOOST_LOG_SEV(log, severity::error) << "Tried access non-existant sink '" << name.c_str() << "'; returning nullptr!";
        return nullptr;
    }

    return m_sinks[name];
}
<commit_msg>Add nullptr fallback to log::SinkManager<commit_after>#include <dsnutil/compiler_features.h>
#include <dsnutil/log/sinkmanager.h>
#include <dsnutil/map_sort.h>

using namespace dsn::log;

SinkManager::SinkManager()
{
}

SinkManager::~SinkManager()
{
#if (dsnutil_cpp_COMPILER_CXX_RANGE_FOR && dsnutil_cpp_COMPILER_CXX_AUTO_TYPE)
    for (auto& sink : m_sinks) {
        BOOST_LOG_SEV(log, severity::trace) << "Removing leftover log sink: " << sink.first;
        boost::log::core::get()->remove_sink(sink.second);
    }
#else
    for (sink_storage::iterator it = m_sinks.begin(); it != m_sinks.end(); ++it) {
        BOOST_LOG_SEV(log, severity::trace) << "Removing leftover log sink: " << it->first;
        boost::log::core::get()->remove_sink(it->second);
    }
#endif
    m_sinks.clear();
}

bool SinkManager::exists(const std::string& name) const
{
    sink_storage::const_iterator it = m_sinks.find(name);
    if (it != m_sinks.end())
        return true;

    return false;
}

bool SinkManager::add(const std::string& name, const SinkManager::sink_ptr& sink)
{
    if (exists(name)) {
        if (!remove(name)) {
            BOOST_LOG_SEV(log, severity::error) << "Failed to remove existing '" << name << "' sink!";
            return false;
        }
    }

    boost::log::core::get()->add_sink(sink);
    m_sinks[name] = sink;

    return true;
}

bool SinkManager::remove(const std::string& name)
{
    if (!exists(name)) {
        BOOST_LOG_SEV(log, severity::warning) << "Tried to remove non-existant sink: " << name;
        return false;
    }

    boost::log::core::get()->remove_sink(m_sinks[name]);
    m_sinks.erase(name);

    return true;
}

std::vector<std::string> SinkManager::sinks() const
{
    return dsn::map_keys(m_sinks);
}

SinkManager::sink_ptr SinkManager::sink(const std::string& name)
{
    if (!exists(name)) {
        BOOST_LOG_SEV(log, severity::error) << "Tried access non-existant sink '" << name.c_str() << "'; returning nullptr!";
        return sink_ptr(dsnutil_cpp_NULLPTR);
    }

    return m_sinks[name];
}
<|endoftext|>


<commit_before><commit_msg>ppl: respect constrained pin groups during random placement<commit_after><|endoftext|>


<commit_before>#pragma once

#include <vector>
#include <deque>
#include <map>
#include <utility>
#include <functional>

#include <iostream>

namespace MotionPlanning
{
    template<class T>
    class TabuSearch
    {
        int short_memory_size;
        std::deque<T> short_memory;
        std::function<float(T)> fitness;
        std::function<T()> random;
        std::function<std::vector<T>(T)> neighbours;
    public:
        TabuSearch(int short_memory_size, std::function<float(T)> fitness, std::function<T()> random, std::function<std::vector<T>(T)> neighbours)
            : short_memory_size(short_memory_size),  short_memory(short_memory_size, random()), fitness(fitness), random(random), neighbours(neighbours) { };

        T explore(int max_attempts)
        {
            T current = short_memory.front();
            T best = current;
            float best_fitness = fitness(best);
            for (int i = 0; i < max_attempts; i++)
            {
                auto possibles = neighbours(current);

                for (auto& sm : short_memory)
                {
                    auto it = std::find(possibles.begin(), possibles.end(), sm);
                    if (it != possibles.end())
                    {
                        possibles.erase(it);
                    }
                }
                if (possibles.size() == 0)
                {
                    break;
                }

                std::vector<std::pair<float, T>> scores;
                std::transform(possibles.begin(), possibles.end(), std::inserter(scores, scores.begin()), [&](T p) { return std::pair<float, T>(fitness(p), p); });
                std::sort(scores.begin(), scores.end(), std::greater<std::pair<float, T>>());

                for (auto& kvp : scores)
                {
                    short_memory.pop_back();
                    short_memory.push_front(kvp.second);
                    if (kvp.first > best_fitness)
                    {
                        best = kvp.second;
                        best_fitness = kvp.first;
                    }
                    current = kvp.second;
                    break;
                }
            }

            return best;
        }
    };
}<commit_msg>remove erroneous break<commit_after>#pragma once

#include <vector>
#include <deque>
#include <map>
#include <utility>
#include <functional>

#include <iostream>

namespace MotionPlanning
{
    template<class T>
    class TabuSearch
    {
        int short_memory_size;
        std::deque<T> short_memory;
        std::function<float(T)> fitness;
        std::function<T()> random;
        std::function<std::vector<T>(T)> neighbours;
    public:
        TabuSearch(int short_memory_size, std::function<float(T)> fitness, std::function<T()> random, std::function<std::vector<T>(T)> neighbours)
            : short_memory_size(short_memory_size),  short_memory(short_memory_size, random()), fitness(fitness), random(random), neighbours(neighbours) { };

        T explore(int max_attempts)
        {
            T current = short_memory.front();
            T best = current;
            float best_fitness = fitness(best);
            for (int i = 0; i < max_attempts; i++)
            {
                auto possibles = neighbours(current);

                for (auto& sm : short_memory)
                {
                    auto it = std::find(possibles.begin(), possibles.end(), sm);
                    if (it != possibles.end())
                    {
                        possibles.erase(it);
                    }
                }
                if (possibles.size() == 0)
                {
                    break;
                }

                std::vector<std::pair<float, T>> scores;
                std::transform(possibles.begin(), possibles.end(), std::inserter(scores, scores.begin()), [&](T p) { return std::pair<float, T>(fitness(p), p); });
                std::sort(scores.begin(), scores.end(), std::greater<std::pair<float, T>>());

                for (auto& kvp : scores)
                {
                    short_memory.pop_back();
                    short_memory.push_front(kvp.second);
                    if (kvp.first > best_fitness)
                    {
                        best = kvp.second;
                        best_fitness = kvp.first;
                    }
                    current = kvp.second;
                }
            }

            return best;
        }
    };
}<|endoftext|>


<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/page_click_tracker.h"

#include "chrome/common/render_messages.h"
#include "chrome/renderer/page_click_listener.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMouseEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"

using WebKit::WebDOMEvent;
using WebKit::WebDOMMouseEvent;
using WebKit::WebElement;
using WebKit::WebFormControlElement;
using WebKit::WebFrame;
using WebKit::WebInputElement;
using WebKit::WebInputEvent;
using WebKit::WebMouseEvent;
using WebKit::WebNode;
using WebKit::WebString;
using WebKit::WebView;

namespace {

// Casts |node| to a WebInputElement.
// Returns an empty (isNull()) WebInputElement if |node| is not a text
// WebInputElement.
const WebInputElement GetTextWebInputElement(const WebNode& node) {
  if (!node.isElementNode())
    return WebInputElement();
  const WebElement element = node.toConst<WebElement>();
  if (!element.isFormControlElement())
    return WebInputElement();
  const WebFormControlElement control =
      element.toConst<WebFormControlElement>();
  if (control.formControlType() != WebString::fromUTF8("text"))
    return WebInputElement();
  return element.toConst<WebInputElement>();
}

// Checks to see if a text field was the previously selected node and is now
// losing its focus.
bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) {
  WebKit::WebNode focused_node = newly_clicked_node.document().focusedNode();

  if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull())
    return false;

  return focused_node != newly_clicked_node;
}

}  // namespace

PageClickTracker::PageClickTracker(content::RenderView* render_view)
    : content::RenderViewObserver(render_view),
      was_focused_(false) {
}

PageClickTracker::~PageClickTracker() {
  // Note that even though RenderView calls FrameDetached when notified that
  // a frame was closed, it might not always get that notification from WebKit
  // for all frames.
  // By the time we get here, the frame could have been destroyed so we cannot
  // unregister listeners in frames remaining in tracked_frames_ as they might
  // be invalid.
}

void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
  if (event.type != WebInputEvent::MouseDown ||
      last_node_clicked_.isNull()) {
    return;
  }

  // We are only interested in text field clicks.
  const WebInputElement input_element =
      GetTextWebInputElement(last_node_clicked_);
  if (input_element.isNull())
    return;

  bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode());
  ObserverListBase<PageClickListener>::Iterator it(listeners_);
  PageClickListener* listener;
  while ((listener = it.GetNext()) != NULL) {
    if (listener->InputElementClicked(input_element, was_focused_, is_focused))
      break;
  }
}

void PageClickTracker::AddListener(PageClickListener* listener) {
  listeners_.AddObserver(listener);
}

void PageClickTracker::RemoveListener(PageClickListener* listener) {
  listeners_.RemoveObserver(listener);
}

void PageClickTracker::DidFinishDocumentLoad(WebKit::WebFrame* frame) {
  tracked_frames_.push_back(frame);
  frame->document().addEventListener("mousedown", this, false);
}

void PageClickTracker::FrameDetached(WebKit::WebFrame* frame) {
  FrameList::iterator iter =
      std::find(tracked_frames_.begin(), tracked_frames_.end(), frame);
  if (iter == tracked_frames_.end()) {
    // Some frames might never load contents so we may not have a listener on
    // them.  Calling removeEventListener() on them would trigger an assert, so
    // we need to keep track of which frames we are listening to.
    return;
  }
  tracked_frames_.erase(iter);
}

void PageClickTracker::handleEvent(const WebDOMEvent& event) {
  last_node_clicked_.reset();

  if (!event.isMouseEvent())
    return;

  const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>();
  DCHECK(mouse_event.buttonDown());
  if (mouse_event.button() != 0)
    return;  // We are only interested in left clicks.

  // Remember which node has focus before the click is processed.
  // We'll get a notification once the mouse event has been processed
  // (DidHandleMouseEvent), we'll notify the listener at that point.
  WebNode node = mouse_event.target();

  HandleTextFieldMaybeLosingFocus(node);

  // We are only interested in text field clicks.
  if (GetTextWebInputElement(node).isNull())
    return;

  last_node_clicked_ = node;
  was_focused_ = (node.document().focusedNode() == last_node_clicked_);
}

void PageClickTracker::HandleTextFieldMaybeLosingFocus(
    const WebNode& newly_clicked_node) {
  if (!DidSelectedTextFieldLoseFocus(newly_clicked_node))
    return;

  ObserverListBase<PageClickListener>::Iterator it(listeners_);
  PageClickListener* listener;
  while ((listener = it.GetNext()) != NULL) {
    if (listener->InputElementLostFocus())
      break;
  }
}
<commit_msg>Open autofill popup when clicking on any text field input element, not just input type=text<commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/page_click_tracker.h"

#include "chrome/common/render_messages.h"
#include "chrome/renderer/autofill/form_autofill_util.h"
#include "chrome/renderer/page_click_listener.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMouseEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"

using WebKit::WebDOMEvent;
using WebKit::WebDOMMouseEvent;
using WebKit::WebElement;
using WebKit::WebFormControlElement;
using WebKit::WebFrame;
using WebKit::WebInputElement;
using WebKit::WebInputEvent;
using WebKit::WebMouseEvent;
using WebKit::WebNode;
using WebKit::WebString;
using WebKit::WebView;

namespace {

// Casts |node| to a WebInputElement.
// Returns an empty (isNull()) WebInputElement if |node| is not a text field.
const WebInputElement GetTextWebInputElement(const WebNode& node) {
  if (!node.isElementNode())
    return WebInputElement();
  const WebElement element = node.toConst<WebElement>();
  if (!element.hasTagName("input"))
    return WebInputElement();
  const WebInputElement* input = WebKit::toWebInputElement(&element);
  if (!autofill::IsTextInput(input))
    return WebInputElement();
  return *input;
}

// Checks to see if a text field was the previously selected node and is now
// losing its focus.
bool DidSelectedTextFieldLoseFocus(const WebNode& newly_clicked_node) {
  WebKit::WebNode focused_node = newly_clicked_node.document().focusedNode();

  if (focused_node.isNull() || GetTextWebInputElement(focused_node).isNull())
    return false;

  return focused_node != newly_clicked_node;
}

}  // namespace

PageClickTracker::PageClickTracker(content::RenderView* render_view)
    : content::RenderViewObserver(render_view),
      was_focused_(false) {
}

PageClickTracker::~PageClickTracker() {
  // Note that even though RenderView calls FrameDetached when notified that
  // a frame was closed, it might not always get that notification from WebKit
  // for all frames.
  // By the time we get here, the frame could have been destroyed so we cannot
  // unregister listeners in frames remaining in tracked_frames_ as they might
  // be invalid.
}

void PageClickTracker::DidHandleMouseEvent(const WebMouseEvent& event) {
  if (event.type != WebInputEvent::MouseDown ||
      last_node_clicked_.isNull()) {
    return;
  }

  // We are only interested in text field clicks.
  const WebInputElement input_element =
      GetTextWebInputElement(last_node_clicked_);
  if (input_element.isNull())
    return;

  bool is_focused = (last_node_clicked_ == render_view()->GetFocusedNode());
  ObserverListBase<PageClickListener>::Iterator it(listeners_);
  PageClickListener* listener;
  while ((listener = it.GetNext()) != NULL) {
    if (listener->InputElementClicked(input_element, was_focused_, is_focused))
      break;
  }
}

void PageClickTracker::AddListener(PageClickListener* listener) {
  listeners_.AddObserver(listener);
}

void PageClickTracker::RemoveListener(PageClickListener* listener) {
  listeners_.RemoveObserver(listener);
}

void PageClickTracker::DidFinishDocumentLoad(WebKit::WebFrame* frame) {
  tracked_frames_.push_back(frame);
  frame->document().addEventListener("mousedown", this, false);
}

void PageClickTracker::FrameDetached(WebKit::WebFrame* frame) {
  FrameList::iterator iter =
      std::find(tracked_frames_.begin(), tracked_frames_.end(), frame);
  if (iter == tracked_frames_.end()) {
    // Some frames might never load contents so we may not have a listener on
    // them.  Calling removeEventListener() on them would trigger an assert, so
    // we need to keep track of which frames we are listening to.
    return;
  }
  tracked_frames_.erase(iter);
}

void PageClickTracker::handleEvent(const WebDOMEvent& event) {
  last_node_clicked_.reset();

  if (!event.isMouseEvent())
    return;

  const WebDOMMouseEvent mouse_event = event.toConst<WebDOMMouseEvent>();
  DCHECK(mouse_event.buttonDown());
  if (mouse_event.button() != 0)
    return;  // We are only interested in left clicks.

  // Remember which node has focus before the click is processed.
  // We'll get a notification once the mouse event has been processed
  // (DidHandleMouseEvent), we'll notify the listener at that point.
  WebNode node = mouse_event.target();

  HandleTextFieldMaybeLosingFocus(node);

  // We are only interested in text field clicks.
  if (GetTextWebInputElement(node).isNull())
    return;

  last_node_clicked_ = node;
  was_focused_ = (node.document().focusedNode() == last_node_clicked_);
}

void PageClickTracker::HandleTextFieldMaybeLosingFocus(
    const WebNode& newly_clicked_node) {
  if (!DidSelectedTextFieldLoseFocus(newly_clicked_node))
    return;

  ObserverListBase<PageClickListener>::Iterator it(listeners_);
  PageClickListener* listener;
  while ((listener = it.GetNext()) != NULL) {
    if (listener->InputElementLostFocus())
      break;
  }
}
<|endoftext|>


<commit_before>/* Copyright Ciprian Ilies */

#pragma once

#include "../internal_header.hpp"

#include <string>
#include <stdio>
#include <iostream>

#include <sqlite3.h>

/*
 * SQLite 3 functions.
 */

namespace mdata
{

namespace sqlite
{

sqlite3* openDatabase(std::string database)
{
	std::string error_message = "";
	int rc;

	sqlite3 *db = nullptr;

	rc = sqlite3_open(database.c_str(), &db);

	if (rc)
	{
		fprintf(stderr, "\n[mdata::sqlite] ERROR: Can't open database: %s.\n", sqlite3_errmsg(db));
		exit(0);
	}
	else
	{
		std::cout << "\n[mdata::sqlite] Opened database successfully.\n";
	}
}

} //namespace sqlite

} //namespace mdata
<commit_msg>Fixed includes.<commit_after>/* Copyright Ciprian Ilies */

#pragma once

#include "../internal_header.hpp"

#include <string>
#include <stdio.h>
#include <iostream>

#include <sqlite3.h>

/*
 * SQLite 3 functions.
 */

namespace mdata
{

namespace sqlite
{

sqlite3* openDatabase(std::string database)
{
	std::string error_message = "";
	int rc;

	sqlite3 *db = nullptr;

	rc = sqlite3_open(database.c_str(), &db);

	if (rc)
	{
		fprintf(stderr, "\n[mdata::sqlite] ERROR: Can't open database: %s.\n", sqlite3_errmsg(db));
		exit(0);
	}
	else
	{
		std::cout << "\n[mdata::sqlite] Opened database successfully.\n";
	}
}

} //namespace sqlite

} //namespace mdata
<|endoftext|>


<commit_before>#include <iostream>

int main()
{
	unsigned scores[11]{33, 44, 55, 66, 77, 88, 99};

	unsigned *b = scores;
	unsigned *e	= scores + 11;
	while (b != e) {
		std::cout << *b++ << " ";
	}
	std::cout << std::endl;

	//遍历数组推荐使用范围for循环
	for (auto i : scores) {
		std::cout << i << " ";
	}
	std::cout << std::endl;

	return 0;
}
<commit_msg>std::begin std::end<commit_after>#include <iostream>
#include <iterator>

int main()
{
	unsigned scores[11]{33, 44, 55, 66, 77, 88, 99};

	//迭代遍历
	auto b = std::begin(scores);
	auto e = std::end(scores);
	while (b != e) {
		std::cout << *b++ << " ";
	}
	std::cout << std::endl;

	//遍历数组推荐使用范围for循环
	for (auto i : scores) {
		std::cout << i << " ";
	}
	std::cout << std::endl;

	return 0;
}
<|endoftext|>


<commit_before>/* Copyright Ciprian Ilies */

#pragma once

#include "../internal_header.hpp"

#include <string>
#include <stdio>
#include <iostream>

#include <sqlite3.h>

/*
 * SQLite 3 functions.
 */

namespace mdata
{

namespace sqlite
{

sqlite3* openDatabase(std::string database)
{
	std::string error_message = "";
	int rc;

	sqlite3 *db = nullptr;

	rc = sqlite3_open(database.c_str(), &db);

	if (rc)
	{
		fprintf(stderr, "\n[mdata::sqlite] ERROR: Can't open database: %s.\n", sqlite3_errmsg(db));
		exit(0);
	}
	else
	{
		cout << "\n[mdata::sqlite] Opened database successfully.\n";
	}
}

} //namespace sqlite

} //namespace mdata
<commit_msg>Fixed namespace stuff.<commit_after>/* Copyright Ciprian Ilies */

#pragma once

#include "../internal_header.hpp"

#include <string>
#include <stdio>
#include <iostream>

#include <sqlite3.h>

/*
 * SQLite 3 functions.
 */

namespace mdata
{

namespace sqlite
{

sqlite3* openDatabase(std::string database)
{
	std::string error_message = "";
	int rc;

	sqlite3 *db = nullptr;

	rc = sqlite3_open(database.c_str(), &db);

	if (rc)
	{
		fprintf(stderr, "\n[mdata::sqlite] ERROR: Can't open database: %s.\n", sqlite3_errmsg(db));
		exit(0);
	}
	else
	{
		std::cout << "\n[mdata::sqlite] Opened database successfully.\n";
	}
}

} //namespace sqlite

} //namespace mdata
<|endoftext|>