<commit_before>#include <nan.h>
#ifndef _WIN32
# include <unistd.h>
#endif

using namespace v8;

class MyWorker : public NanAsyncWorker {
 public:
  MyWorker(NanCallback *callback, int delay)
    : NanAsyncWorker(callback), delay(delay) {}
  ~MyWorker() {}

  // Executed inside the worker-thread.
  // It is not safe to access V8, or V8 data structures
  // here, so everything we need for input and output
  // should go on `this`.
  void Execute () {
    printf("FAUX 3\n");
    fflush(stdout);

    #ifdef _WIN32
     Sleep(delay);
    #else
     usleep(delay * 1000);
    #endif

    printf("FAUX 4\n");
    fflush(stdout);
  }

  // Executed when the async work is complete
  // this function will be run inside the main event loop
  // so it is safe to use V8 again
  void HandleOKCallback () {
    NanScope();

    callback->Call(0, NULL);
  }

 private:
  int delay;
};

NAN_METHOD(Delay) {
  NanEscapableScope();

  int delay = args[0].As<Number>()->IntegerValue();
  Local<Function> callback = args[1].As<Function>();

  printf("FAUX 1\n");
  fflush(stdout);

  NanCallback* nanCallback = new NanCallback(callback);
  MyWorker* worker = new MyWorker(nanCallback, delay);
  NanAsyncQueueWorker(worker);

  printf("FAUX 2\n");
  fflush(stdout);

  NanReturnUndefined();
}

void Init(Handle<Object> exports) {
  exports->Set(NanNew("delay"), NanNew<FunctionTemplate>(Delay)->GetFunction());
}

NODE_MODULE(myaddon, Init)
<commit_msg>occasional timing race condition<commit_after>#include <nan.h>
#ifndef _WIN32
# include <unistd.h>
#endif

using namespace v8;

class MyWorker : public NanAsyncWorker {
 public:
  MyWorker(NanCallback *callback, int delay)
    : NanAsyncWorker(callback), delay(delay) {}
  ~MyWorker() {}

  // Executed inside the worker-thread.
  // It is not safe to access V8, or V8 data structures
  // here, so everything we need for input and output
  // should go on `this`.
  void Execute () {
    // tiny sleep prior to ensure proper print order of 2 & 3
    #ifdef _WIN32
     Sleep(10);
    #else
     usleep(10000);
    #endif

    printf("FAUX 3\n");
    fflush(stdout);

    #ifdef _WIN32
     Sleep(delay);
    #else
     usleep(delay * 1000);
    #endif

    printf("FAUX 4\n");
    fflush(stdout);
  }

  // Executed when the async work is complete
  // this function will be run inside the main event loop
  // so it is safe to use V8 again
  void HandleOKCallback () {
    NanScope();

    callback->Call(0, NULL);
  }

 private:
  int delay;
};

NAN_METHOD(Delay) {
  NanEscapableScope();

  int delay = args[0].As<Number>()->IntegerValue();
  Local<Function> callback = args[1].As<Function>();

  printf("FAUX 1\n");
  fflush(stdout);

  NanCallback* nanCallback = new NanCallback(callback);
  MyWorker* worker = new MyWorker(nanCallback, delay);
  NanAsyncQueueWorker(worker);

  printf("FAUX 2\n");
  fflush(stdout);

  NanReturnUndefined();
}

void Init(Handle<Object> exports) {
  exports->Set(NanNew("delay"), NanNew<FunctionTemplate>(Delay)->GetFunction());
}

NODE_MODULE(myaddon, Init)
<|endoftext|>


<commit_before>#include "JobScheduler.h"

namespace sf1r
{

JobScheduler::JobScheduler()
{
    asynchronousWorker_ = boost::thread(
        &JobScheduler::runAsynchronousTasks_, this
    );
}

JobScheduler::~JobScheduler()
{
    close();
}

void JobScheduler::close()
{
    asynchronousWorker_.interrupt();
    asynchronousWorker_.join();
}

void JobScheduler::addTask(task_type task)
{
    asynchronousTasks_.push(task);
}

void JobScheduler::runAsynchronousTasks_()
{
    try
    {
        task_type task;
        while (true)
        {
            asynchronousTasks_.pop(task);
            task();
        }
    }
    catch (boost::thread_interrupted&)
    {
        return;
    }
}

}
<commit_msg>when "kill" command is issued, if the queue in JobScheduler still contains some unprocessed tasks, to enable exit instantly, we need to add an interruption point when each task is done.<commit_after>#include "JobScheduler.h"

namespace sf1r
{

JobScheduler::JobScheduler()
{
    asynchronousWorker_ = boost::thread(
        &JobScheduler::runAsynchronousTasks_, this
    );
}

JobScheduler::~JobScheduler()
{
    close();
}

void JobScheduler::close()
{
    asynchronousWorker_.interrupt();
    asynchronousWorker_.join();
}

void JobScheduler::addTask(task_type task)
{
    asynchronousTasks_.push(task);
}

void JobScheduler::runAsynchronousTasks_()
{
    try
    {
        task_type task;
        while (true)
        {
            asynchronousTasks_.pop(task);
            task();

            // terminate execution if interrupted
            boost::this_thread::interruption_point();
        }
    }
    catch (boost::thread_interrupted&)
    {
        return;
    }
}

}
<|endoftext|>


<commit_before>/* Copyright 2019 Axel Huebl, Benjamin Worpitz
 *
 * This file is part of Alpaka.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <alpaka/mem/alloc/Traits.hpp>

#include <alpaka/core/Common.hpp>
#include <alpaka/core/Unused.hpp>

#include <boost/align.hpp>

namespace alpaka
{
    namespace mem
    {
        //-----------------------------------------------------------------------------
        //! The allocator specifics.
        namespace alloc
        {
            //#############################################################################
            //! The CPU boost aligned allocator.
            //!
            //! \tparam TAlignment An integral constant containing the alignment.
            template<
                typename TAlignment>
            class AllocCpuBoostAligned : public concepts::Implements<ConceptMemAlloc, AllocCpuBoostAligned<TAlignment>>
            {
            };

            namespace traits
            {
                //#############################################################################
                //! The CPU boost aligned allocator memory allocation trait specialization.
                template<
                    typename T,
                    typename TAlignment>
                struct Alloc<
                    T,
                    AllocCpuBoostAligned<TAlignment>>
                {
                    //-----------------------------------------------------------------------------
                    ALPAKA_FN_HOST static auto alloc(
                        AllocCpuBoostAligned<TAlignment> const & alloc,
                        std::size_t const & sizeElems)
                    -> T *
                    {
                        alpaka::ignore_unused(alloc);
                        return
                            reinterpret_cast<T *>(
                                boost::alignment::aligned_alloc(TAlignment::value, sizeElems * sizeof(T)));
                    }
                };

                //#############################################################################
                //! The CPU boost aligned allocator memory free trait specialization.
                template<
                    typename T,
                    typename TAlignment>
                struct Free<
                    T,
                    AllocCpuBoostAligned<TAlignment>>
                {
                    //-----------------------------------------------------------------------------
                    ALPAKA_FN_HOST static auto free(
                        AllocCpuBoostAligned<TAlignment> const & alloc,
                        T const * const ptr)
                    -> void
                    {
                        alpaka::ignore_unused(alloc);
                            boost::alignment::aligned_free(
                                const_cast<void *>(
                                    reinterpret_cast<void const *>(ptr)));
                    }
                };
            }
        }
    }
}
<commit_msg>fix memory alignment<commit_after>/* Copyright 2019 Axel Huebl, Benjamin Worpitz
 *
 * This file is part of Alpaka.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <alpaka/mem/alloc/Traits.hpp>

#include <alpaka/core/Common.hpp>
#include <alpaka/core/Unused.hpp>

#include <boost/align.hpp>

#include <algorithm>

namespace alpaka
{
    namespace mem
    {
        //-----------------------------------------------------------------------------
        //! The allocator specifics.
        namespace alloc
        {
            //#############################################################################
            //! The CPU boost aligned allocator.
            //!
            //! \tparam TAlignment An integral constant containing the alignment.
            template<
                typename TAlignment>
            class AllocCpuBoostAligned : public concepts::Implements<ConceptMemAlloc, AllocCpuBoostAligned<TAlignment>>
            {
            };

            namespace traits
            {
                //#############################################################################
                //! The CPU boost aligned allocator memory allocation trait specialization.
                template<
                    typename T,
                    typename TAlignment>
                struct Alloc<
                    T,
                    AllocCpuBoostAligned<TAlignment>>
                {
                    //-----------------------------------------------------------------------------
                    ALPAKA_FN_HOST static auto alloc(
                        AllocCpuBoostAligned<TAlignment> const & alloc,
                        std::size_t const & sizeElems)
                    -> T *
                    {
#if (defined(ALPAKA_ACC_GPU_CUDA_ENABLED) && BOOST_LANG_CUDA) || (defined(ALPAKA_ACC_GPU_HIP_ENABLED) && BOOST_LANG_HIP)
                        // For CUDA host memory must be aligned to 4 kib to pin it with `cudaHostRegister`,
                        // this was described in older programming guides but was removed later.
                        // From testing with PIConGPU and cuda-memcheck we found out that the alignment is still required.
                        //
                        // For HIP the required alignment is the size of a cache line.
                        // https://rocm-developer-tools.github.io/HIP/group__Memory.html#gab8258f051e1a1f7385f794a15300e674
                        // To avoid issues with HIP(cuda) the alignment will be set also for HIP(clang/hcc)
                        // to 4kib.
                        // @todo evaluate requirements when the HIP ecosystem is more stable
                        constexpr size_t minAlignement = 4096;
#else
                        constexpr size_t minAlignement = TAlignment::value;
#endif
                        alpaka::ignore_unused(alloc);
                        return
                            reinterpret_cast<T *>(
                                boost::alignment::aligned_alloc(std::max(TAlignment::value, minAlignement), sizeElems * sizeof(T)));
                    }
                };

                //#############################################################################
                //! The CPU boost aligned allocator memory free trait specialization.
                template<
                    typename T,
                    typename TAlignment>
                struct Free<
                    T,
                    AllocCpuBoostAligned<TAlignment>>
                {
                    //-----------------------------------------------------------------------------
                    ALPAKA_FN_HOST static auto free(
                        AllocCpuBoostAligned<TAlignment> const & alloc,
                        T const * const ptr)
                    -> void
                    {
                        alpaka::ignore_unused(alloc);
                            boost::alignment::aligned_free(
                                const_cast<void *>(
                                    reinterpret_cast<void const *>(ptr)));
                    }
                };
            }
        }
    }
}
<|endoftext|>


<commit_before>
#pragma once

#include <proxc/config.hpp>

#include <proxc/context.hpp>
#include <proxc/work_steal_deque.hpp>
#include <proxc/scheduling_policy/policy_base.hpp>

#include <chrono>
#include <condition_variable>
#include <mutex>
#include <vector>

PROXC_NAMESPACE_BEGIN

namespace scheduling_policy {
namespace detail {

template<typename T>
class WorkStealingPolicy : public PolicyBase<T>
{
private:
    static std::size_t                          num_workers_;
    static std::vector<WorkStealingPolicy *>    work_stealers_;

    std::size_t                 id_;
    proxc::WorkStealDeque<T>    deque_{};
    std::mutex                  mtx_;
    std::condition_variable     cnd_;
    bool                        flag_{ false };

    static void init_(std::size_t num_workers);

public:
    using TimePointType = typename PolicyBase<T>::TimePointType;

    // 0 == num_cores
    WorkStealingPolicy(std::size_t num_workers = 0);

    WorkStealingPolicy(WorkStealingPolicy const &) = delete;
    WorkStealingPolicy(WorkStealingPolicy &&)      = delete;

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

    // Added work stealing methods
    void reserve(std::size_t capacity) noexcept;

    // Policy base interface methods
    void enqueue(T *) noexcept;

    T * pick_next() noexcept;

    bool is_ready() const noexcept;

    void suspend_until(TimePointType const &) noexcept;

    void notify() noexcept;

private:
    T * steal() noexcept;
};

} // namespace detail

using WorkStealing = detail::WorkStealingPolicy<Context>;

} // namespace scheduling_policy

PROXC_NAMESPACE_END

<commit_msg>Some minor refactoring.<commit_after>
#pragma once

#include <proxc/config.hpp>

#include <proxc/context.hpp>
#include <proxc/work_steal_deque.hpp>
#include <proxc/scheduling_policy/policy_base.hpp>

#include <chrono>
#include <condition_variable>
#include <mutex>
#include <vector>

PROXC_NAMESPACE_BEGIN

namespace scheduling_policy {
namespace detail {

template<typename T>
class WorkStealingPolicy : public PolicyBase<T>
{
private:
    static std::size_t                            num_workers_;
    static std::vector< WorkStealingPolicy * >    work_stealers_;

    std::size_t                   id_;
    proxc::WorkStealDeque< T >    deque_{};
    std::mutex                    mtx_;
    std::condition_variable       cnd_;
    bool                          flag_{ false };

    static void init_(std::size_t num_workers);

public:
    using TimePointType = typename PolicyBase<T>::TimePointType;

    // 0 == num_cores
    WorkStealingPolicy(std::size_t num_workers = 0);

    WorkStealingPolicy(WorkStealingPolicy const &) = delete;
    WorkStealingPolicy(WorkStealingPolicy &&)      = delete;

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

    // Added work stealing methods
    void reserve(std::size_t capacity) noexcept;

    // Policy base interface methods
    void enqueue(T *) noexcept;

    T * pick_next() noexcept;

    bool is_ready() const noexcept;

    void suspend_until(TimePointType const &) noexcept;

    void notify() noexcept;

private:
    T * steal() noexcept;
};

} // namespace detail

using WorkStealing = detail::WorkStealingPolicy<Context>;

} // namespace scheduling_policy

PROXC_NAMESPACE_END

<|endoftext|>


<commit_before>#pragma once

#include <algorithm>
#include <vector>
#include <tudocomp/def.hpp>

namespace tdc {
namespace lzss {

class Factor {
public:
    len_t pos, src, len;

    inline Factor(len_t fpos, len_t fsrc, len_t flen)
        : pos(fpos), src(fsrc), len(flen) {
    }
}  __attribute__((__packed__));

class FactorBuffer {
private:
    std::vector<Factor> m_factors;
    bool m_sorted; //! factors need to be sorted before they are output

    len_t m_shortest_factor;
    len_t m_longest_factor;

public:
    inline FactorBuffer()
        : m_sorted(true)
        , m_shortest_factor(LEN_MAX)
        , m_longest_factor(0)
    {
    }

    inline void emplace_back(len_t fpos, len_t fsrc, len_t flen) {
        m_sorted = m_sorted && (m_factors.empty() || fpos >= m_factors.back().pos);
        m_factors.emplace_back(fpos, fsrc, flen);

        m_shortest_factor = std::min(m_shortest_factor, flen);
        m_longest_factor = std::max(m_longest_factor, flen);
    }
    inline std::vector<Factor>::const_iterator begin() const {
        return m_factors.cbegin();
    }
    inline std::vector<Factor>::const_iterator end() const {
        return m_factors.cend();
    }

    // inline const Factor& operator[](size_t i) const {
    //     return m_factors[i];
    // }

    inline bool empty() const {
        return m_factors.empty();
    }

    inline size_t size() const {
        return m_factors.size();
    }

    inline bool is_sorted() const {
        return m_sorted;
    }

    inline void sort() { //TODO: use radix sort
        if(!m_sorted) {
            std::sort(m_factors.begin(), m_factors.end(),
                [](const Factor& a, const Factor& b) -> bool { return a.pos < b.pos; });

            m_sorted = true;
        }
    }

    inline size_t shortest_factor() const {
        return m_shortest_factor;
    }

    inline size_t longest_factor() const {
        return m_longest_factor;
    }

};

}} //ns

<commit_msg>make operator[] in FactorBuffer deprecated<commit_after>#pragma once

#include <algorithm>
#include <vector>
#include <tudocomp/def.hpp>

namespace tdc {
namespace lzss {

class Factor {
public:
    len_t pos, src, len;

    inline Factor(len_t fpos, len_t fsrc, len_t flen)
        : pos(fpos), src(fsrc), len(flen) {
    }
}  __attribute__((__packed__));

class FactorBuffer {
private:
    std::vector<Factor> m_factors;
    bool m_sorted; //! factors need to be sorted before they are output

    len_t m_shortest_factor;
    len_t m_longest_factor;

public:
    inline FactorBuffer()
        : m_sorted(true)
        , m_shortest_factor(LEN_MAX)
        , m_longest_factor(0)
    {
    }

    inline void emplace_back(len_t fpos, len_t fsrc, len_t flen) {
        m_sorted = m_sorted && (m_factors.empty() || fpos >= m_factors.back().pos);
        m_factors.emplace_back(fpos, fsrc, flen);

        m_shortest_factor = std::min(m_shortest_factor, flen);
        m_longest_factor = std::max(m_longest_factor, flen);
    }
    inline std::vector<Factor>::const_iterator begin() const {
        return m_factors.cbegin();
    }
    inline std::vector<Factor>::const_iterator end() const {
        return m_factors.cend();
    }

    [[deprecated("use interators")]] inline const Factor& operator[](size_t i) const {
        return m_factors[i];
    }

    inline bool empty() const {
        return m_factors.empty();
    }

    inline size_t size() const {
        return m_factors.size();
    }

    inline bool is_sorted() const {
        return m_sorted;
    }

    inline void sort() { //TODO: use radix sort
        if(!m_sorted) {
            std::sort(m_factors.begin(), m_factors.end(),
                [](const Factor& a, const Factor& b) -> bool { return a.pos < b.pos; });

            m_sorted = true;
        }
    }

    inline size_t shortest_factor() const {
        return m_shortest_factor;
    }

    inline size_t longest_factor() const {
        return m_longest_factor;
    }

};

}} //ns

<|endoftext|>


<commit_before>/*
 * database_impl.cpp
 *
 *  Created on: Jul 13, 2015
 *      Author: zmij
 */

#include <tip/db/pg/detail/database_impl.hpp>
#include <tip/db/pg/detail/connection_pool.hpp>
#include <tip/db/pg/error.hpp>
#include <stdexcept>

#include <tip/db/pg/version.hpp>
#include <tip/db/pg/log.hpp>

namespace tip {
namespace db {
namespace pg {
namespace detail {

LOCAL_LOGGING_FACILITY_CFG(PGDB, config::SERVICE_LOG);

database_impl::database_impl(size_t pool_size, client_options_type const& defaults)
    : service_( std::make_shared<asio_config::io_service>() ),
      pool_size_(pool_size), defaults_(defaults),
      state_(running)
{
    local_log() << "Initializing postgre db service";
}

database_impl::~database_impl()
{
    stop();
}

void
database_impl::set_defaults(size_t pool_size, client_options_type const& defaults)
{
    pool_size_ = pool_size;
    defaults_ = defaults;
}

void
database_impl::add_connection(std::string const& connection_string,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");
    connection_options co = connection_options::parse(connection_string);
    add_connection(co, pool_size, params);
}

void
database_impl::add_connection(connection_options co,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");

    if (co.uri.empty())
        throw error::connection_error("No URI in database connection string");

    if (co.database.empty())
        throw error::connection_error("No database name in database connection string");

    if (co.user.empty())
        throw error::connection_error("No user name in database connection string");

    if (co.alias.empty()) {
        co.generate_alias();
    }

    add_pool(co, pool_size, params);
}

database_impl::connection_pool_ptr
database_impl::add_pool(connection_options const& co,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (!connections_.count(co.alias)) {
        if (!pool_size.is_initialized()) {
            pool_size = pool_size_;
        }
        local_log(logger::INFO) << "Create a new connection pool " << co.alias;
        client_options_type parms(params);
        for (auto p : defaults_) {
            if (!parms.count(p.first)) {
                parms.insert(p);
            }
        }
        local_log(logger::INFO) << "Register new connection " << co.uri
                << "[" << co.database << "]" << " with alias " << co.alias;
        connections_.insert(std::make_pair(co.alias,
                connection_pool_ptr(connection_pool::create(service_,
                        *pool_size, co, parms) )));
    }
    return connections_[co.alias];
}

void
database_impl::get_connection(dbalias const& alias,
        transaction_callback const& cb,
        error_callback const& err,
        transaction_mode const& mode)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");

    if (!connections_.count(alias)) {
        throw error::connection_error("Database alias is not registered");
    }
    connection_pool_ptr pool = connections_[alias];
    pool->get_connection(cb, err, mode);
}

void
database_impl::run()
{
    service_->run();
}

void
database_impl::stop()
{
    if (state_ == running) {
        state_ = closing;
        std::shared_ptr< size_t > pool_count =
                std::make_shared< size_t >(connections_.size());
        asio_config::io_service_ptr svc = service_;

        for (auto c: connections_) {
            // FIXME Pass a close callback. Call stop
            // only when all connections are closed, may be with some timeout
            c.second->close(
            [pool_count, svc](){
                --(*pool_count);
                if (*pool_count == 0) {
                    svc->stop();
                }
            });
        }
        connections_.clear();
    }
}

} /* namespace detail */
} /* namespace pg */
} /* namespace db */
} /* namespace tip */
<commit_msg>Be more specific about database alias that is not registered<commit_after>/*
 * database_impl.cpp
 *
 *  Created on: Jul 13, 2015
 *      Author: zmij
 */

#include <tip/db/pg/detail/database_impl.hpp>
#include <tip/db/pg/detail/connection_pool.hpp>
#include <tip/db/pg/error.hpp>
#include <stdexcept>

#include <tip/db/pg/version.hpp>
#include <tip/db/pg/log.hpp>

namespace tip {
namespace db {
namespace pg {
namespace detail {

LOCAL_LOGGING_FACILITY_CFG(PGDB, config::SERVICE_LOG);

database_impl::database_impl(size_t pool_size, client_options_type const& defaults)
    : service_( std::make_shared<asio_config::io_service>() ),
      pool_size_(pool_size), defaults_(defaults),
      state_(running)
{
    local_log() << "Initializing postgre db service";
}

database_impl::~database_impl()
{
    stop();
}

void
database_impl::set_defaults(size_t pool_size, client_options_type const& defaults)
{
    pool_size_ = pool_size;
    defaults_ = defaults;
}

void
database_impl::add_connection(std::string const& connection_string,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");
    connection_options co = connection_options::parse(connection_string);
    add_connection(co, pool_size, params);
}

void
database_impl::add_connection(connection_options co,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");

    if (co.uri.empty())
        throw error::connection_error("No URI in database connection string");

    if (co.database.empty())
        throw error::connection_error("No database name in database connection string");

    if (co.user.empty())
        throw error::connection_error("No user name in database connection string");

    if (co.alias.empty()) {
        co.generate_alias();
    }

    add_pool(co, pool_size, params);
}

database_impl::connection_pool_ptr
database_impl::add_pool(connection_options const& co,
        db_service::optional_size pool_size,
        client_options_type const& params)
{
    if (!connections_.count(co.alias)) {
        if (!pool_size.is_initialized()) {
            pool_size = pool_size_;
        }
        local_log(logger::INFO) << "Create a new connection pool " << co.alias;
        client_options_type parms(params);
        for (auto p : defaults_) {
            if (!parms.count(p.first)) {
                parms.insert(p);
            }
        }
        local_log(logger::INFO) << "Register new connection " << co.uri
                << "[" << co.database << "]" << " with alias " << co.alias;
        connections_.insert(std::make_pair(co.alias,
                connection_pool_ptr(connection_pool::create(service_,
                        *pool_size, co, parms) )));
    }
    return connections_[co.alias];
}

void
database_impl::get_connection(dbalias const& alias,
        transaction_callback const& cb,
        error_callback const& err,
        transaction_mode const& mode)
{
    if (state_ != running)
        throw error::connection_error("Database service is not running");

    if (!connections_.count(alias)) {
        throw error::connection_error("Database alias '" + alias + "' is not registered");
    }
    connection_pool_ptr pool = connections_[alias];
    pool->get_connection(cb, err, mode);
}

void
database_impl::run()
{
    service_->run();
}

void
database_impl::stop()
{
    if (state_ == running) {
        state_ = closing;
        std::shared_ptr< size_t > pool_count =
                std::make_shared< size_t >(connections_.size());
        asio_config::io_service_ptr svc = service_;

        for (auto c: connections_) {
            // FIXME Pass a close callback. Call stop
            // only when all connections are closed, may be with some timeout
            c.second->close(
            [pool_count, svc](){
                --(*pool_count);
                if (*pool_count == 0) {
                    svc->stop();
                }
            });
        }
        connections_.clear();
    }
}

} /* namespace detail */
} /* namespace pg */
} /* namespace db */
} /* namespace tip */
<|endoftext|>


<commit_before><commit_msg>coverity#708359 Uninitialized scalar field<commit_after><|endoftext|>


<commit_before>#include "circuit.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <cstddef>
#include <iterator>
using namespace std;

Circuit::Circuit(vector<bool> inputs, int o): output_no(o), input_no(inputs.size()) {
    for(bool input : inputs){
        Gate* wire = new Wire(input);
        gates.push_back(wire);
    }
}
Circuit::Circuit(int inputs, int o): output_no(o), input_no(inputs) {
    for(int i =0; i < input_no; ++i){
        Gate* wire = new Wire(false);
        gates.push_back(wire);
    }
}

int Circuit::addGate(GateType gate_type, int index_1){
    Gate* built_gate = nullptr;
    Gate* input_1 = gates[index_1];
    if(gate_type == NOT){
        built_gate = new Not(input_1);
    }
    else if(gate_type == WIRE){
        built_gate = new Wire(input_1);
    }
    assert(built_gate != nullptr);
    gates.push_back(built_gate);
    return gates.size();
}

int Circuit::addGate(GateType gate_type, int index_1, int index_2){
    Gate* built_gate = nullptr;
    Gate* input_1 = gates[index_1];
    Gate* input_2 = gates[index_2];
    if(gate_type == AND){
        built_gate = new And(input_1, input_2);
    }
    else if(gate_type == OR){
        built_gate = new Or(input_1, input_2);
    }
    assert(built_gate != nullptr);
    gates.push_back(built_gate);
    return gates.size();
}

vector<vector<bool> > Circuit::evaluateAllInputs(){
    vector<vector<bool> > inputs = getPossibleInputs();
    vector<vector<bool> > outputs;
    for(vector<bool> input : inputs ){
        outputs.push_back(evaluateInputSet(input));
    }
    assert(!outputs.empty());
    return outputs;
}

vector<bool> Circuit::evaluateInputSet(vector<bool> input_set){
    vector<bool> result;
    for(int i = 0; i<input_no; ++i){
        ((Wire*)(gates[i]))->setInput(input_set[i]);
    }
    reverse_iterator<vector<Gate*>::iterator > r = gates.rbegin();
    for(int i = 0; i<output_no; ++i){
        bool output = r[i]->evaluate();
        result.insert(result.begin(), output);
    }
    return result;
}

vector<vector<bool> > Circuit::getPossibleInputs(){
    vector<vector<bool> > possibilities;
    for(int i =0; i < pow(2, input_no); ++i) {
        cout << "Input set: " << i << ":" ;
        vector<bool> row;
        for(int j = input_no-1; j >= 0; --j){
            int current_bit = (i >> j) & 1;
            row.push_back(current_bit);
        }
        assert(row == input_no);
        possibilities.push_back(row);
    }
    return possibilities;
}
<commit_msg>fix assert<commit_after>#include "circuit.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <cstddef>
#include <iterator>
using namespace std;

Circuit::Circuit(vector<bool> inputs, int o): output_no(o), input_no(inputs.size()) {
    for(bool input : inputs){
        Gate* wire = new Wire(input);
        gates.push_back(wire);
    }
}
Circuit::Circuit(int inputs, int o): output_no(o), input_no(inputs) {
    for(int i =0; i < input_no; ++i){
        Gate* wire = new Wire(false);
        gates.push_back(wire);
    }
}

int Circuit::addGate(GateType gate_type, int index_1){
    Gate* built_gate = nullptr;
    Gate* input_1 = gates[index_1];
    if(gate_type == NOT){
        built_gate = new Not(input_1);
    }
    else if(gate_type == WIRE){
        built_gate = new Wire(input_1);
    }
    assert(built_gate != nullptr);
    gates.push_back(built_gate);
    return gates.size();
}

int Circuit::addGate(GateType gate_type, int index_1, int index_2){
    Gate* built_gate = nullptr;
    Gate* input_1 = gates[index_1];
    Gate* input_2 = gates[index_2];
    if(gate_type == AND){
        built_gate = new And(input_1, input_2);
    }
    else if(gate_type == OR){
        built_gate = new Or(input_1, input_2);
    }
    assert(built_gate != nullptr);
    gates.push_back(built_gate);
    return gates.size();
}

vector<vector<bool> > Circuit::evaluateAllInputs(){
    vector<vector<bool> > inputs = getPossibleInputs();
    vector<vector<bool> > outputs;
    for(vector<bool> input : inputs ){
        outputs.push_back(evaluateInputSet(input));
    }
    assert(!outputs.empty());
    return outputs;
}

vector<bool> Circuit::evaluateInputSet(vector<bool> input_set){
    vector<bool> result;
    for(int i = 0; i<input_no; ++i){
        ((Wire*)(gates[i]))->setInput(input_set[i]);
    }
    reverse_iterator<vector<Gate*>::iterator > r = gates.rbegin();
    for(int i = 0; i<output_no; ++i){
        bool output = r[i]->evaluate();
        result.insert(result.begin(), output);
    }
    return result;
}

vector<vector<bool> > Circuit::getPossibleInputs(){
    vector<vector<bool> > possibilities;
    for(int i =0; i < pow(2, input_no); ++i) {
        cout << "Input set: " << i << ":" ;
        vector<bool> row;
        for(int j = input_no-1; j >= 0; --j){
            int current_bit = (i >> j) & 1;
            row.push_back(current_bit);
        }
        assert(row.size() == input_no);
        possibilities.push_back(row);
    }
    return possibilities;
}
<|endoftext|>


<commit_before>#pragma once

#include <bitset>
#include <cstring>
#include <string>
#include <iostream>

namespace {
	namespace binaryNS {
		using std::cout;
		using std::ostream;

		template <typename A>
		class binary final {
			template<typename B>
			using conref = const B &;

			using bitA = std::bitset<sizeof(A) << 3>;
			using byte = unsigned char;
			using bit = bool;

			constexpr static const size_t sz{sizeof(A)};
			constexpr static const size_t szB{sz << 3};
			constexpr static const size_t szOneLess{sz - 1};

			static inline void print(std::ostream &os, conref<binary<A>> a) noexcept {
				for (size_t i = 0; i < sz; ++i) {
					for (size_t j = 0; j < 8; ++j) {
						static bit bitIdx;
						bitIdx = (bit) a.bits[getBitIdx(i, j)];
						os << bitIdx;
					}

					if (szOneLess != i) {
						os << ' ';
					}
				}
			}

			static inline bit getBitFromByte(conref<byte> data, conref<byte> bitIdx) noexcept {
				return data & (1 << bitIdx);
			}

			static inline size_t getBitIdx(conref<size_t> byteIdx, conref<size_t> bitIdx) noexcept {
				return (byteIdx << 3) + bitIdx;
			}

			static inline bit getBit(conref<binary<A>> bits, conref<size_t> byteIdx, conref<size_t> bitIdx) noexcept {
				return bits[getBitIdx(byteIdx, bitIdx)];
			}

		public:
			explicit binary(void) = delete;

			inline explicit binary(conref<A> a) noexcept {
				byte *ptr = new byte[sz];
				memcpy((void *) ptr, (const void *const) &a, sz);

				for (size_t i = 0; i < sz; ++i) {
					static byte ch;
					ch = ptr[i];
					for (size_t j = 0; j < 8; ++j) {
						static bool bitVal;
						bitVal = binary<A>::getBitFromByte(ch, j);
						this->bits[binary<A>::getBitIdx(i, j)] = bitVal;
					}
				}

				delete[] ptr;
			}

			inline explicit binary(A &&a) noexcept : binary(a) {}

			template <typename... B>
			explicit binary(B... b) = delete;

			inline ~binary(void) noexcept {
				bits.~bitA();
			}

			inline explicit operator bool (void) const noexcept {
				return this->bits.all();
			}

			template <typename B>
			inline explicit operator B (void) const noexcept = delete;

			inline bool operator [](size_t idx) const noexcept {
				return this->getBit(idx);
			}

			inline void print(void) const noexcept {
				binary<A>::print(std::cout, *this);
			}

			inline bool getBit(conref<size_t> idx) const noexcept {
				return bits[idx];
			}

			inline bool getBit(size_t&& idx) const noexcept {
				return this->getBit(idx);
			}

			inline bool getBit(conref<size_t> a, conref<size_t> b) const noexcept {
				return this->getBit(binary<A>::getBitIdx((byte) a, (byte) b));
			}

			inline bool getBit(size_t &&a, size_t &&b) const noexcept {
				return this->getBit(a, b);
			}

			inline bool getBit(conref<int> idx) const noexcept {
				return this->getBit((size_t) idx);
			}

			inline bool getBit(int&& idx) const noexcept {
				return this->getBit((size_t) idx);
			}

			inline bool getBit(conref<byte> byteIdx, conref<byte> bitIdx) const noexcept {
				return binary<A>::getBit(*this, byteIdx, bitIdx);
			}

			inline bool getBit(byte&& byteIdx, byte&& bitIdx) const noexcept {
				return binary<A>::getBit(*this, byteIdx, bitIdx);
			}

			inline bool getBit(conref<int> byteIdx, conref<int> bitIdx) const noexcept {
				return binary<A>::getBit((size_t) byteIdx, (size_t) bitIdx);
			}

			inline bool getBit(int&& byteIdx, int&& bitIdx) const noexcept {
				return binary<A>::getBit(byteIdx, bitIdx);
			}

			template <typename... B>
			inline bool getBit(B... b) const noexcept = delete;

			inline unsigned long long getULLong(void) const noexcept {
				return this->bits.to_ullong();
			}

			inline std::string getString(void) const noexcept {
				return this->bits.to_string();
			}

			template <typename B>
			friend inline ostream &operator<<(ostream &os, conref<binary<B>> a) noexcept {
				a.print(os, a);

				return os;
			}

			template <typename B>
			friend inline ostream &operator<<(ostream &os, binary<B> &&a) noexcept {
				return operator<<(os, a);
			}

			explicit operator bool(void) const noexcept {
				return this->bits.all();
			}

			constexpr static inline size_t getSize(void) noexcept {
				return sz;
			}

		private:
			bitA bits;
		};
	}
}
<commit_msg>house keeping i guess...<commit_after>#pragma once

#include <bitset>
#include <cstring>
#include <string>
#include <iostream>

namespace {
	namespace binaryNS {
		using std::cout;
		using std::ostream;

		template <typename A>
		class binary final {
			template<typename B>
			using conref = const B &;

			using bitA = std::bitset<sizeof(A) << 3>;
			using byte = unsigned char;
			using bit = bool;

			constexpr static const size_t sz{sizeof(A)};
			constexpr static const size_t szB{sz << 3};
			constexpr static const size_t szOneLess{sz - 1};

			static inline void print(std::ostream &os, conref<binary<A>> a) noexcept {
				for (size_t i = 0; i < sz; ++i) {
					for (size_t j = 0; j < 8; ++j) {
						static bit bitIdx;
						bitIdx = (bit) a.bits[getBitIdx(i, j)];
						os << bitIdx;
					}

					if (szOneLess != i) {
						os << ' ';
					}
				}
			}

			static inline bit getBitFromByte(conref<byte> data, conref<byte> bitIdx) noexcept {
				return data & (1 << bitIdx);
			}

			static inline size_t getBitIdx(conref<size_t> byteIdx, conref<size_t> bitIdx) noexcept {
				return (byteIdx << 3) + bitIdx;
			}

			static inline bit getBit(conref<binary<A>> bits, conref<size_t> byteIdx, conref<size_t> bitIdx) noexcept {
				return bits[getBitIdx(byteIdx, bitIdx)];
			}

		public:
			explicit binary(void) = delete;

			inline explicit binary(conref<A> a) noexcept {
				byte *ptr = new byte[sz];
				memcpy((void *) ptr, (const void *const) &a, sz);

				for (size_t i = 0; i < sz; ++i) {
					static byte ch;
					ch = ptr[i];
					for (size_t j = 0; j < 8; ++j) {
						static bool bitVal;
						bitVal = binary<A>::getBitFromByte(ch, j);
						this->bits[binary<A>::getBitIdx(i, j)] = bitVal;
					}
				}

				delete[] ptr;
			}

			inline explicit binary(A &&a) noexcept : binary(a) {}

			template <typename... B>
			explicit binary(B... b) = delete;

			inline ~binary(void) noexcept {
				bits.~bitA();
			}

			inline explicit operator bool (void) const noexcept {
				return this->bits.all();
			}

			template <typename B>
			inline explicit operator B (void) const noexcept = delete;

			inline bool operator [](size_t idx) const noexcept {
				return this->getBit(idx);
			}

			inline void print(void) const noexcept {
				binary<A>::print(std::cout, *this);
			}

			inline bool getBit(conref<size_t> idx) const noexcept {
				return bits[idx];
			}

			inline bool getBit(size_t&& idx) const noexcept {
				return this->getBit(idx);
			}

			inline bool getBit(conref<size_t> a, conref<size_t> b) const noexcept {
				return this->getBit(binary<A>::getBitIdx((byte) a, (byte) b));
			}

			inline bool getBit(size_t &&a, size_t &&b) const noexcept {
				return this->getBit(a, b);
			}

			inline bool getBit(conref<int> idx) const noexcept {
				return this->getBit((size_t) idx);
			}

			inline bool getBit(int&& idx) const noexcept {
				return this->getBit((size_t) idx);
			}

			inline bool getBit(conref<byte> byteIdx, conref<byte> bitIdx) const noexcept {
				return binary<A>::getBit(*this, byteIdx, bitIdx);
			}

			inline bool getBit(byte&& byteIdx, byte&& bitIdx) const noexcept {
				return binary<A>::getBit(*this, byteIdx, bitIdx);
			}

			inline bool getBit(conref<int> byteIdx, conref<int> bitIdx) const noexcept {
				return binary<A>::getBit((size_t) byteIdx, (size_t) bitIdx);
			}

			inline bool getBit(int&& byteIdx, int&& bitIdx) const noexcept {
				return binary<A>::getBit(byteIdx, bitIdx);
			}

			template <typename... B>
			inline bool getBit(B... b) const noexcept = delete;

			inline unsigned long long getULLong(void) const noexcept {
				return this->bits.to_ullong();
			}

			inline std::string getString(void) const noexcept {
				return this->bits.to_string();
			}

			template <typename B>
			friend inline ostream &operator<<(ostream &os, conref<binary<B>> a) noexcept {
				a.print(os, a);

				return os;
			}

			template <typename B>
			friend inline ostream &operator<<(ostream &os, binary<B> &&a) noexcept {
				return operator<<(os, a);
			}

			constexpr static inline size_t getSize(void) noexcept {
				return sz;
			}

		private:
			bitA bits;
		};
	}
}
<|endoftext|>


<commit_before>#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>

using namespace std;

class Palindrome {

private:
	string text;	// String variable to hold the user inputted string of text to test.

public:
	bool Check(string text, unsigned int place = 0) {
		/* ************************************
		This function checks if the text string
		is a Palindrome or not.
		Returns true if it is, false if not.
		************************************ */
		if (place == 0) {
			/*
			If it is the first depth of recursion, filter all
			non-alphabet characters out of the string.
			- Alex's idea -
			*/
			text.erase(remove_if(text.begin(), text.end(), isspace), text.end());
			text.erase(remove_if(text.begin(), text.end(), ispunct), text.end());
		}
		// Set len to the length of the new string.
		unsigned int len = text.length();
		if (place >= len / 2) {
			/*
			If place is already reached an integer that is greater than
			or equal to half the string length, return true since it is
			a Palindrome.
			*/
			return true;
		}
		else {
			char a = text.at(place);			// Set a to the current string place from the beginning of the string.
			char b = text.at(len - 1 - place);	// Set b to the current string place from the end of the string.
			if (tolower(a) == tolower(b)) {		// Compare a and b.
				if (!Check(text, place + 1)) {	// If a == b, continue recursion.
					return false;				// If any recursion returns false, the rest of the stack will return false.
				}
			}
			else {
				// If a ever does not equal b, this will return false and cause the rest of the stack to return false.
				return false;
			}
		}
	}

	void AskUser() {
		/* ************************************
		This function asks the user to input
		a string to be checked.
		It will continually ask for new strings
		until the user inputs nothing and
		presses enter.
		************************************ */
		do {
			cout << "Please input a string: ";
			getline(cin, this->text);
			if (this->Check(this->text) && this->text != "") {
				cout << "The string entered is a Palindrome." << endl;
			}
			else if (!this->Check(this->text)) {
				cout << "The string entered is not a Palindrome" << endl;
			}
			else {
				return;
			}
		} while (this->text != "");
	}

};

int main() {

	Palindrome test;	// Initialize the class object.

	test.AskUser();		// Run the AskUser() function to begin the program.

	return 0;
}
<commit_msg>Update Algorithm-Function-Version.cpp<commit_after>#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>

using namespace std;

class Palindrome {

private:
	string text;	// String variable to hold the user inputted string of text to test.

public:
	bool Check(string text, unsigned int place = 0) {
		/* ************************************
		This function checks if the text string
		is a Palindrome or not.
		Returns true if it is, false if not.
		************************************ */
		if (place == 0) {
			/*
			- Alex's idea -
			If it is the first depth of recursion, filter all
			non-alphabet characters out of the string.
			*/
			text.erase(remove_if(text.begin(), text.end(), isspace), text.end());
			text.erase(remove_if(text.begin(), text.end(), ispunct), text.end());
		}
		// Set len to the length of the new string.
		unsigned int len = text.length();
		if (place >= len / 2) {
			/*
			If place is already reached an integer that is greater than
			or equal to half the string length, return true since it is
			a Palindrome.
			*/
			return true;
		}
		else {
			char a = text.at(place);			// Set a to the current string place from the beginning of the string.
			char b = text.at(len - 1 - place);	// Set b to the current string place from the end of the string.
			if (tolower(a) == tolower(b)) {		// Compare a and b.
				if (!Check(text, place + 1)) {	// If a == b, continue recursion.
					return false;				// If any recursion returns false, the rest of the stack will return false.
				}
			}
			else {
				// If a ever does not equal b, this will return false and cause the rest of the stack to return false.
				return false;
			}
		}
	}

	void AskUser() {
		/* ************************************
		This function asks the user to input
		a string to be checked.
		It will continually ask for new strings
		until the user inputs nothing and
		presses enter.
		************************************ */
		do {
			cout << "Please input a string: ";
			getline(cin, this->text);
			if (this->Check(this->text) && this->text != "") {
				cout << "The string entered is a Palindrome." << endl;
			}
			else if (!this->Check(this->text)) {
				cout << "The string entered is not a Palindrome" << endl;
			}
			else {
				return;
			}
		} while (this->text != "");
	}

};

int main() {

	Palindrome test;	// Initialize the class object.

	test.AskUser();		// Run the AskUser() function to begin the program.

	return 0;
}
<|endoftext|>


<commit_before>// Scintilla source code edit control
/** @file LexSpice.cxx
 ** Lexer for Spice
 **/
// Copyright 2006 by Fabien Proriol
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>

#include <string>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"

#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif

/*
 * Interface
 */

static void ColouriseDocument(
    unsigned int startPos,
    int length,
    int initStyle,
    WordList *keywordlists[],
    Accessor &styler);

static const char * const spiceWordListDesc[] = {
    "Keywords",        // SPICE command
    "Keywords2",    // SPICE functions
    "Keywords3",    // SPICE params
    0
};

LexerModule lmSpice(SCLEX_SPICE, ColouriseDocument, "spice", NULL, spiceWordListDesc);

/*
 * Implementation
 */

static void ColouriseComment(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseDelimiter(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseNumber(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseWhiteSpace(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseWord(StyleContext& sc, WordList& keywords, WordList& keywords2, WordList& keywords3, bool& apostropheStartsAttribute);

static inline bool IsDelimiterCharacter(int ch);
static inline bool IsNumberStartCharacter(int ch);
static inline bool IsNumberCharacter(int ch);
static inline bool IsSeparatorOrDelimiterCharacter(int ch);
static inline bool IsWordStartCharacter(int ch);
static inline bool IsWordCharacter(int ch);

static void ColouriseComment(StyleContext& sc, bool&) {
    sc.SetState(SCE_SPICE_COMMENTLINE);
    while (!sc.atLineEnd) {
        sc.Forward();
    }
}

static void ColouriseDelimiter(StyleContext& sc, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = sc.Match (')');
    sc.SetState(SCE_SPICE_DELIMITER);
    sc.ForwardSetState(SCE_SPICE_DEFAULT);
}

static void ColouriseNumber(StyleContext& sc, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = true;
    std::string number;
    sc.SetState(SCE_SPICE_NUMBER);
    // Get all characters up to a delimiter or a separator, including points, but excluding
    // double points (ranges).
    while (!IsSeparatorOrDelimiterCharacter(sc.ch) || (sc.ch == '.' && sc.chNext != '.')) {
        number += static_cast<char>(sc.ch);
        sc.Forward();
    }
    // Special case: exponent with sign
    if ((sc.chPrev == 'e' || sc.chPrev == 'E') &&
            (sc.ch == '+' || sc.ch == '-')) {
        number += static_cast<char>(sc.ch);
        sc.Forward ();
        while (!IsSeparatorOrDelimiterCharacter(sc.ch)) {
            number += static_cast<char>(sc.ch);
            sc.Forward();
        }
    }
    sc.SetState(SCE_SPICE_DEFAULT);
}

static void ColouriseWhiteSpace(StyleContext& sc, bool& ) {
    sc.SetState(SCE_SPICE_DEFAULT);
    sc.ForwardSetState(SCE_SPICE_DEFAULT);
}

static void ColouriseWord(StyleContext& sc, WordList& keywords, WordList& keywords2, WordList& keywords3, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = true;
    sc.SetState(SCE_SPICE_IDENTIFIER);
    std::string word;
    while (!sc.atLineEnd && !IsSeparatorOrDelimiterCharacter(sc.ch)) {
        word += static_cast<char>(tolower(sc.ch));
        sc.Forward();
    }
    if (keywords.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    else if (keywords2.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD2);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    else if (keywords3.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD3);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    sc.SetState(SCE_SPICE_DEFAULT);
}

//
// ColouriseDocument
//
static void ColouriseDocument(
    unsigned int startPos,
    int length,
    int initStyle,
    WordList *keywordlists[],
    Accessor &styler) {
    WordList &keywords = *keywordlists[0];
    WordList &keywords2 = *keywordlists[1];
    WordList &keywords3 = *keywordlists[2];
    StyleContext sc(startPos, length, initStyle, styler);
    int lineCurrent = styler.GetLine(startPos);
    bool apostropheStartsAttribute = (styler.GetLineState(lineCurrent) & 1) != 0;
    while (sc.More()) {
        if (sc.atLineEnd) {
            // Go to the next line
            sc.Forward();
            lineCurrent++;
            // Remember the line state for future incremental lexing
            styler.SetLineState(lineCurrent, apostropheStartsAttribute);
            // Don't continue any styles on the next line
            sc.SetState(SCE_SPICE_DEFAULT);
        }
        // Comments
        if ((sc.Match('*') && sc.atLineStart) || sc.Match('*','~')) {
            ColouriseComment(sc, apostropheStartsAttribute);
        // Whitespace
        } else if (IsASpace(sc.ch)) {
            ColouriseWhiteSpace(sc, apostropheStartsAttribute);
        // Delimiters
        } else if (IsDelimiterCharacter(sc.ch)) {
            ColouriseDelimiter(sc, apostropheStartsAttribute);
        // Numbers
        } else if (IsADigit(sc.ch) || sc.ch == '#') {
            ColouriseNumber(sc, apostropheStartsAttribute);
        // Keywords or identifiers
        } else {
            ColouriseWord(sc, keywords, keywords2, keywords3, apostropheStartsAttribute);
        }
    }
    sc.Complete();
}

static inline bool IsDelimiterCharacter(int ch) {
    switch (ch) {
    case '&':
    case '\'':
    case '(':
    case ')':
    case '*':
    case '+':
    case ',':
    case '-':
    case '.':
    case '/':
    case ':':
    case ';':
    case '<':
    case '=':
    case '>':
    case '|':
        return true;
    default:
        return false;
    }
}

static inline bool IsNumberCharacter(int ch) {
    return IsNumberStartCharacter(ch) ||
           ch == '_' ||
           ch == '.' ||
           ch == '#' ||
           (ch >= 'a' && ch <= 'f') ||
           (ch >= 'A' && ch <= 'F');
}

static inline bool IsNumberStartCharacter(int ch) {
    return IsADigit(ch);
}

static inline bool IsSeparatorOrDelimiterCharacter(int ch) {
    return IsASpace(ch) || IsDelimiterCharacter(ch);
}

static inline bool IsWordCharacter(int ch) {
    return IsWordStartCharacter(ch) || IsADigit(ch);
}

static inline bool IsWordStartCharacter(int ch) {
    return (IsASCII(ch) && isalpha(ch)) || ch == '_';
}
<commit_msg>Removed functions that had never been used - looks like they had been copied from LexAda.<commit_after>// Scintilla source code edit control
/** @file LexSpice.cxx
 ** Lexer for Spice
 **/
// Copyright 2006 by Fabien Proriol
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>

#include <string>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"

#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif

/*
 * Interface
 */

static void ColouriseDocument(
    unsigned int startPos,
    int length,
    int initStyle,
    WordList *keywordlists[],
    Accessor &styler);

static const char * const spiceWordListDesc[] = {
    "Keywords",        // SPICE command
    "Keywords2",    // SPICE functions
    "Keywords3",    // SPICE params
    0
};

LexerModule lmSpice(SCLEX_SPICE, ColouriseDocument, "spice", NULL, spiceWordListDesc);

/*
 * Implementation
 */

static void ColouriseComment(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseDelimiter(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseNumber(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseWhiteSpace(StyleContext& sc, bool& apostropheStartsAttribute);
static void ColouriseWord(StyleContext& sc, WordList& keywords, WordList& keywords2, WordList& keywords3, bool& apostropheStartsAttribute);

static inline bool IsDelimiterCharacter(int ch);
static inline bool IsSeparatorOrDelimiterCharacter(int ch);

static void ColouriseComment(StyleContext& sc, bool&) {
    sc.SetState(SCE_SPICE_COMMENTLINE);
    while (!sc.atLineEnd) {
        sc.Forward();
    }
}

static void ColouriseDelimiter(StyleContext& sc, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = sc.Match (')');
    sc.SetState(SCE_SPICE_DELIMITER);
    sc.ForwardSetState(SCE_SPICE_DEFAULT);
}

static void ColouriseNumber(StyleContext& sc, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = true;
    std::string number;
    sc.SetState(SCE_SPICE_NUMBER);
    // Get all characters up to a delimiter or a separator, including points, but excluding
    // double points (ranges).
    while (!IsSeparatorOrDelimiterCharacter(sc.ch) || (sc.ch == '.' && sc.chNext != '.')) {
        number += static_cast<char>(sc.ch);
        sc.Forward();
    }
    // Special case: exponent with sign
    if ((sc.chPrev == 'e' || sc.chPrev == 'E') &&
            (sc.ch == '+' || sc.ch == '-')) {
        number += static_cast<char>(sc.ch);
        sc.Forward ();
        while (!IsSeparatorOrDelimiterCharacter(sc.ch)) {
            number += static_cast<char>(sc.ch);
            sc.Forward();
        }
    }
    sc.SetState(SCE_SPICE_DEFAULT);
}

static void ColouriseWhiteSpace(StyleContext& sc, bool& ) {
    sc.SetState(SCE_SPICE_DEFAULT);
    sc.ForwardSetState(SCE_SPICE_DEFAULT);
}

static void ColouriseWord(StyleContext& sc, WordList& keywords, WordList& keywords2, WordList& keywords3, bool& apostropheStartsAttribute) {
    apostropheStartsAttribute = true;
    sc.SetState(SCE_SPICE_IDENTIFIER);
    std::string word;
    while (!sc.atLineEnd && !IsSeparatorOrDelimiterCharacter(sc.ch)) {
        word += static_cast<char>(tolower(sc.ch));
        sc.Forward();
    }
    if (keywords.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    else if (keywords2.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD2);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    else if (keywords3.InList(word.c_str())) {
        sc.ChangeState(SCE_SPICE_KEYWORD3);
        if (word != "all") {
            apostropheStartsAttribute = false;
        }
    }
    sc.SetState(SCE_SPICE_DEFAULT);
}

//
// ColouriseDocument
//
static void ColouriseDocument(
    unsigned int startPos,
    int length,
    int initStyle,
    WordList *keywordlists[],
    Accessor &styler) {
    WordList &keywords = *keywordlists[0];
    WordList &keywords2 = *keywordlists[1];
    WordList &keywords3 = *keywordlists[2];
    StyleContext sc(startPos, length, initStyle, styler);
    int lineCurrent = styler.GetLine(startPos);
    bool apostropheStartsAttribute = (styler.GetLineState(lineCurrent) & 1) != 0;
    while (sc.More()) {
        if (sc.atLineEnd) {
            // Go to the next line
            sc.Forward();
            lineCurrent++;
            // Remember the line state for future incremental lexing
            styler.SetLineState(lineCurrent, apostropheStartsAttribute);
            // Don't continue any styles on the next line
            sc.SetState(SCE_SPICE_DEFAULT);
        }
        // Comments
        if ((sc.Match('*') && sc.atLineStart) || sc.Match('*','~')) {
            ColouriseComment(sc, apostropheStartsAttribute);
        // Whitespace
        } else if (IsASpace(sc.ch)) {
            ColouriseWhiteSpace(sc, apostropheStartsAttribute);
        // Delimiters
        } else if (IsDelimiterCharacter(sc.ch)) {
            ColouriseDelimiter(sc, apostropheStartsAttribute);
        // Numbers
        } else if (IsADigit(sc.ch) || sc.ch == '#') {
            ColouriseNumber(sc, apostropheStartsAttribute);
        // Keywords or identifiers
        } else {
            ColouriseWord(sc, keywords, keywords2, keywords3, apostropheStartsAttribute);
        }
    }
    sc.Complete();
}

static inline bool IsDelimiterCharacter(int ch) {
    switch (ch) {
    case '&':
    case '\'':
    case '(':
    case ')':
    case '*':
    case '+':
    case ',':
    case '-':
    case '.':
    case '/':
    case ':':
    case ';':
    case '<':
    case '=':
    case '>':
    case '|':
        return true;
    default:
        return false;
    }
}

static inline bool IsSeparatorOrDelimiterCharacter(int ch) {
    return IsASpace(ch) || IsDelimiterCharacter(ch);
}
<|endoftext|>


<commit_before>#include <iostream>
#include "litesql.hpp"
#include "main.hpp""

// no name collisions expected
using namespace litesql;
using namespace SkypeDB;

int main(int argc, char **argv) {
	try {
		SkypeDB::main db("sqlite3", "database=/home/ilia/.Skype/sc.ryabokon.ilia/main.db");
		// create tables, sequences and indexes
		db.verbose = true;
		auto ds = select<Messages>(db, Messages::Id > 100 && Messages::Id < 200);
		std::cout << "All messages count " << ds.count() << std::endl;
	} catch (Except e) {
		std::cerr << "Error: " << e << std::endl;
	}

	return 0;
}

<commit_msg>Skype test changes<commit_after>#include <iostream>
#include "litesql.hpp"
#include "main.hpp""

// no name collisions expected
using namespace litesql;
using namespace SkypeDB;

int main(int argc, char **argv) {
	try {
		SkypeDB::main db("sqlite3", "database=/home/ilia/.Skype/sc.ryabokon.ilia/main.db");
		// create tables, sequences and indexes
		db.verbose = true;
		auto ds = select<Messages>(db, Messages::Id > 100 && Messages::Id < 200);
		SelectQuery q;
		q.result("max(id)");
		q.source(SkypeDB::Messages::table__);
		int r1 = atoi(db.query(q)[0][0]);

	} catch (Except e) {
		std::cerr << "Error: " << e << std::endl;
	}

	return 0;
}

<|endoftext|>


<commit_before>/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/video_render//video_render_frames.h"

#include <cassert>

#include "modules/interface/module_common_types.h"
#include "system_wrappers/interface/tick_util.h"
#include "system_wrappers/interface/trace.h"

namespace webrtc {

const int32_t KEventMaxWaitTimeMs = 200;
const uint32_t kMinRenderDelayMs = 10;
const uint32_t kMaxRenderDelayMs= 500;

VideoRenderFrames::VideoRenderFrames()
    : incoming_frames_(),
      render_delay_ms_(10) {
}

VideoRenderFrames::~VideoRenderFrames() {
  ReleaseAllFrames();
}

int32_t VideoRenderFrames::AddFrame(I420VideoFrame* new_frame) {
  const int64_t time_now = TickTime::MillisecondTimestamp();

  if (new_frame->render_time_ms() + KOldRenderTimestampMS < time_now) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1,
                 "%s: too old frame.", __FUNCTION__);
    return -1;
  }
  if (new_frame->render_time_ms() > time_now + KFutureRenderTimestampMS) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1,
                 "%s: frame too long into the future.", __FUNCTION__);
    return -1;
  }

  // Get an empty frame
  I420VideoFrame* frame_to_add = NULL;
  if (!empty_frames_.Empty()) {
    ListItem* item = empty_frames_.First();
    if (item) {
      frame_to_add = static_cast<I420VideoFrame*>(item->GetItem());
      empty_frames_.Erase(item);
    }
  }
  if (!frame_to_add) {
    if (empty_frames_.GetSize() + incoming_frames_.GetSize() >
        KMaxNumberOfFrames) {
      // Already allocated too many frames.
      WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer,
                   -1, "%s: too many frames, limit: %d", __FUNCTION__,
                   KMaxNumberOfFrames);
      return -1;
    }

    // Allocate new memory.
    WEBRTC_TRACE(kTraceMemory, kTraceVideoRenderer, -1,
                 "%s: allocating buffer %d", __FUNCTION__,
                 empty_frames_.GetSize() + incoming_frames_.GetSize());

    frame_to_add = new I420VideoFrame();
    if (!frame_to_add) {
      WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, -1,
                   "%s: could not create new frame for", __FUNCTION__);
      return -1;
    }
  }

  frame_to_add->CreateEmptyFrame(new_frame->width(), new_frame->height(),
                                 new_frame->stride(kYPlane),
                                 new_frame->stride(kUPlane),
                                 new_frame->stride(kVPlane));
  // TODO(mflodman) Change this!
  // Remove const ness. Copying will be costly.
  frame_to_add->SwapFrame(new_frame);
  incoming_frames_.PushBack(frame_to_add);

  return incoming_frames_.GetSize();
}

I420VideoFrame* VideoRenderFrames::FrameToRender() {
  I420VideoFrame* render_frame = NULL;
  while (!incoming_frames_.Empty()) {
    ListItem* item = incoming_frames_.First();
    if (item) {
      I420VideoFrame* oldest_frame_in_list =
          static_cast<I420VideoFrame*>(item->GetItem());
      if (oldest_frame_in_list->render_time_ms() <=
          TickTime::MillisecondTimestamp() + render_delay_ms_) {
        // This is the oldest one so far and it's OK to render.
        if (render_frame) {
          // This one is older than the newly found frame, remove this one.
          render_frame->ResetSize();
          render_frame->set_timestamp(0);
          render_frame->set_render_time_ms(0);
          empty_frames_.PushFront(render_frame);
        }
        render_frame = oldest_frame_in_list;
        incoming_frames_.Erase(item);
      } else {
        // We can't release this one yet, we're done here.
        break;
      }
    } else {
      assert(false);
    }
  }
  return render_frame;
}

int32_t VideoRenderFrames::ReturnFrame(I420VideoFrame* old_frame) {
  old_frame->ResetSize();
  old_frame->set_timestamp(0);
  old_frame->set_render_time_ms(0);
  empty_frames_.PushBack(old_frame);
  return 0;
}

int32_t VideoRenderFrames::ReleaseAllFrames() {
  while (!incoming_frames_.Empty()) {
    ListItem* item = incoming_frames_.First();
    if (item) {
      I420VideoFrame* frame = static_cast<I420VideoFrame*>(item->GetItem());
      assert(frame != NULL);
      delete frame;
    }
    incoming_frames_.Erase(item);
  }
  while (!empty_frames_.Empty()) {
    ListItem* item = empty_frames_.First();
    if (item) {
      I420VideoFrame* frame = static_cast<I420VideoFrame*>(item->GetItem());
      assert(frame != NULL);
      delete frame;
    }
    empty_frames_.Erase(item);
  }
  return 0;
}

uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
  int64_t time_to_release = 0;
  ListItem* item = incoming_frames_.First();
  if (item) {
    I420VideoFrame* oldest_frame =
        static_cast<I420VideoFrame*>(item->GetItem());
    time_to_release = oldest_frame->render_time_ms() - render_delay_ms_
                      - TickTime::MillisecondTimestamp();
    if (time_to_release < 0) {
      time_to_release = 0;
    }
  } else {
    time_to_release = KEventMaxWaitTimeMs;
  }
  return static_cast<uint32_t>(time_to_release);
}

int32_t VideoRenderFrames::SetRenderDelay(
    const uint32_t render_delay) {
  if (render_delay < kMinRenderDelayMs ||
      render_delay > kMaxRenderDelayMs) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer,
                 -1, "%s(%d): Invalid argument.", __FUNCTION__,
                 render_delay);
    return -1;
  }

  render_delay_ms_ = render_delay;
  return 0;
}

}  // namespace webrtc
<commit_msg>Log timestamp of the frame when it's dropped from the render module<commit_after>/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/video_render//video_render_frames.h"

#include <cassert>

#include "modules/interface/module_common_types.h"
#include "system_wrappers/interface/tick_util.h"
#include "system_wrappers/interface/trace.h"

namespace webrtc {

const int32_t KEventMaxWaitTimeMs = 200;
const uint32_t kMinRenderDelayMs = 10;
const uint32_t kMaxRenderDelayMs= 500;

VideoRenderFrames::VideoRenderFrames()
    : incoming_frames_(),
      render_delay_ms_(10) {
}

VideoRenderFrames::~VideoRenderFrames() {
  ReleaseAllFrames();
}

int32_t VideoRenderFrames::AddFrame(I420VideoFrame* new_frame) {
  const int64_t time_now = TickTime::MillisecondTimestamp();

  if (new_frame->render_time_ms() + KOldRenderTimestampMS < time_now) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1,
                 "%s: too old frame, timestamp=%u.",
                 __FUNCTION__, new_frame->timestamp());
    return -1;
  }
  if (new_frame->render_time_ms() > time_now + KFutureRenderTimestampMS) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1,
                 "%s: frame too long into the future, timestamp=%u.",
                 __FUNCTION__, new_frame->timestamp());
    return -1;
  }

  // Get an empty frame
  I420VideoFrame* frame_to_add = NULL;
  if (!empty_frames_.Empty()) {
    ListItem* item = empty_frames_.First();
    if (item) {
      frame_to_add = static_cast<I420VideoFrame*>(item->GetItem());
      empty_frames_.Erase(item);
    }
  }
  if (!frame_to_add) {
    if (empty_frames_.GetSize() + incoming_frames_.GetSize() >
        KMaxNumberOfFrames) {
      // Already allocated too many frames.
      WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer,
                   -1, "%s: too many frames, timestamp=%u, limit=%d",
                   __FUNCTION__, new_frame->timestamp(), KMaxNumberOfFrames);
      return -1;
    }

    // Allocate new memory.
    WEBRTC_TRACE(kTraceMemory, kTraceVideoRenderer, -1,
                 "%s: allocating buffer %d", __FUNCTION__,
                 empty_frames_.GetSize() + incoming_frames_.GetSize());

    frame_to_add = new I420VideoFrame();
    if (!frame_to_add) {
      WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, -1,
                   "%s: could not create new frame for", __FUNCTION__);
      return -1;
    }
  }

  frame_to_add->CreateEmptyFrame(new_frame->width(), new_frame->height(),
                                 new_frame->stride(kYPlane),
                                 new_frame->stride(kUPlane),
                                 new_frame->stride(kVPlane));
  // TODO(mflodman) Change this!
  // Remove const ness. Copying will be costly.
  frame_to_add->SwapFrame(new_frame);
  incoming_frames_.PushBack(frame_to_add);

  return incoming_frames_.GetSize();
}

I420VideoFrame* VideoRenderFrames::FrameToRender() {
  I420VideoFrame* render_frame = NULL;
  while (!incoming_frames_.Empty()) {
    ListItem* item = incoming_frames_.First();
    if (item) {
      I420VideoFrame* oldest_frame_in_list =
          static_cast<I420VideoFrame*>(item->GetItem());
      if (oldest_frame_in_list->render_time_ms() <=
          TickTime::MillisecondTimestamp() + render_delay_ms_) {
        // This is the oldest one so far and it's OK to render.
        if (render_frame) {
          // This one is older than the newly found frame, remove this one.
          render_frame->ResetSize();
          render_frame->set_timestamp(0);
          render_frame->set_render_time_ms(0);
          empty_frames_.PushFront(render_frame);
        }
        render_frame = oldest_frame_in_list;
        incoming_frames_.Erase(item);
      } else {
        // We can't release this one yet, we're done here.
        break;
      }
    } else {
      assert(false);
    }
  }
  return render_frame;
}

int32_t VideoRenderFrames::ReturnFrame(I420VideoFrame* old_frame) {
  old_frame->ResetSize();
  old_frame->set_timestamp(0);
  old_frame->set_render_time_ms(0);
  empty_frames_.PushBack(old_frame);
  return 0;
}

int32_t VideoRenderFrames::ReleaseAllFrames() {
  while (!incoming_frames_.Empty()) {
    ListItem* item = incoming_frames_.First();
    if (item) {
      I420VideoFrame* frame = static_cast<I420VideoFrame*>(item->GetItem());
      assert(frame != NULL);
      delete frame;
    }
    incoming_frames_.Erase(item);
  }
  while (!empty_frames_.Empty()) {
    ListItem* item = empty_frames_.First();
    if (item) {
      I420VideoFrame* frame = static_cast<I420VideoFrame*>(item->GetItem());
      assert(frame != NULL);
      delete frame;
    }
    empty_frames_.Erase(item);
  }
  return 0;
}

uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
  int64_t time_to_release = 0;
  ListItem* item = incoming_frames_.First();
  if (item) {
    I420VideoFrame* oldest_frame =
        static_cast<I420VideoFrame*>(item->GetItem());
    time_to_release = oldest_frame->render_time_ms() - render_delay_ms_
                      - TickTime::MillisecondTimestamp();
    if (time_to_release < 0) {
      time_to_release = 0;
    }
  } else {
    time_to_release = KEventMaxWaitTimeMs;
  }
  return static_cast<uint32_t>(time_to_release);
}

int32_t VideoRenderFrames::SetRenderDelay(
    const uint32_t render_delay) {
  if (render_delay < kMinRenderDelayMs ||
      render_delay > kMaxRenderDelayMs) {
    WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer,
                 -1, "%s(%d): Invalid argument.", __FUNCTION__,
                 render_delay);
    return -1;
  }

  render_delay_ms_ = render_delay;
  return 0;
}

}  // namespace webrtc
<|endoftext|>


<commit_before>/*
 * Copyright (C) 2006-2021  Music Technology Group - Universitat Pompeu Fabra
 *
 * This file is part of Essentia
 *
 * Essentia is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation (FSF), 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 Affero GNU General Public License
 * version 3 along with this program.  If not, see http://www.gnu.org/licenses/
 */

#include "tensorflowpredicttempocnn.h"

using namespace std;

namespace essentia {
namespace streaming {

const char* TensorflowPredictTempoCNN::name = essentia::standard::TensorflowPredictTempoCNN::name;
const char* TensorflowPredictTempoCNN::category = essentia::standard::TensorflowPredictTempoCNN::category;
const char* TensorflowPredictTempoCNN::description = essentia::standard::TensorflowPredictTempoCNN::description;

TensorflowPredictTempoCNN::TensorflowPredictTempoCNN() : AlgorithmComposite(),
    _frameCutter(0), _tensorflowInputTempoCNN(0), _vectorRealToTensor(0), _tensorNormalize(0),
    _tensorTranspose(0), _tensorToPool(0), _tensorflowPredict(0), _poolToTensor(0),
    _tensorToVectorReal(0), _configured(false) {

  declareInput(_signal, 4096, "signal", "the input audio signal sampled at 11025 Hz");
  declareOutput(_predictions, 0, "predictions", "the output values from the model node named after `output`");
}

void TensorflowPredictTempoCNN::createInnerNetwork() {
  AlgorithmFactory& factory = AlgorithmFactory::instance();

  _frameCutter             = factory.create("FrameCutter");
  _tensorflowInputTempoCNN = factory.create("TensorflowInputTempoCNN");
  _vectorRealToTensor      = factory.create("VectorRealToTensor");
  _tensorNormalize         = factory.create("TensorNormalize");
  _tensorTranspose         = factory.create("TensorTranspose");
  _tensorToPool            = factory.create("TensorToPool");
  _tensorflowPredict       = factory.create("TensorflowPredict");
  _poolToTensor            = factory.create("PoolToTensor");
  _tensorToVectorReal      = factory.create("TensorToVectorReal");

  _tensorflowInputTempoCNN->output("bands").setBufferType(BufferUsage::forMultipleFrames);

  _signal                                   >> _frameCutter->input("signal");
  _frameCutter->output("frame")             >> _tensorflowInputTempoCNN->input("frame");
  _tensorflowInputTempoCNN->output("bands") >> _vectorRealToTensor->input("frame");
  _vectorRealToTensor->output("tensor")     >> _tensorNormalize->input("tensor");
  _tensorNormalize->output("tensor")        >> _tensorTranspose->input("tensor");
  _tensorTranspose->output("tensor")        >> _tensorToPool->input("tensor");
  _tensorToPool->output("pool")             >> _tensorflowPredict->input("poolIn");
  _tensorflowPredict->output("poolOut")     >> _poolToTensor->input("pool");
  _poolToTensor->output("tensor")           >> _tensorToVectorReal->input("tensor");

  attach(_tensorToVectorReal->output("frame"), _predictions);

  _network = new scheduler::Network(_frameCutter);
}

void TensorflowPredictTempoCNN::clearAlgos() {
  if (!_configured) return;
  delete _network;
}

TensorflowPredictTempoCNN::~TensorflowPredictTempoCNN() {
  clearAlgos();
}

void TensorflowPredictTempoCNN::reset() {
  AlgorithmComposite::reset();
}

void TensorflowPredictTempoCNN::configure() {
  if (_configured) {
    clearAlgos();
  }

  createInnerNetwork();

  int patchHopSize = parameter("patchHopSize").toInt();
  string lastPatchMode = parameter("lastPatchMode").toString();
  int batchSize = parameter("batchSize").toInt();

  if (batchSize == 0) {
    throw EssentiaException("TensorflowPredictTempoCNN: 0 is not a valid `batchSize` value.");
  }

  // Hardcoded parameters matching the training setup:
  // https://github.com/hendriks73/tempo-cnn/blob/master/tempocnn/feature.py
  int frameSize = 1024;
  int hopSize = 512;
  int patchSize = 256;
  int numberBands = 40;
  vector<int> inputShape({batchSize, 1, patchSize, numberBands});
  string scaler = "standard";

  // Hendrik's models expect data shaped as {Batch, Mels, Time, Channel}.
  vector<int> permutation({0, 3, 2, 1});

  _frameCutter->configure("frameSize", frameSize, "hopSize", hopSize);

  _vectorRealToTensor->configure("shape", inputShape,
                                 "lastPatchMode", lastPatchMode,
                                 "patchHopSize", patchHopSize);

  _tensorNormalize->configure("scaler", scaler);

  _tensorTranspose->configure("permutation", permutation);

  _configured = true;

  string input = parameter("input").toString();
  string output = parameter("output").toString();

  _tensorToPool->configure("namespace", input);

  _poolToTensor->configure("namespace", output);

  string graphFilename = parameter("graphFilename").toString();
  string savedModel = parameter("savedModel").toString();

  _tensorflowPredict->configure("graphFilename", graphFilename,
                                "savedModel", savedModel,
                                "squeeze", false,
                                "inputs", vector<string>({input}),
                                "outputs", vector<string>({output}));
}

} // namespace streaming
} // namespace essentia

namespace essentia {
namespace standard {

const char* TensorflowPredictTempoCNN::name = "TensorflowPredictTempoCNN";
const char* TensorflowPredictTempoCNN::category = "Machine Learning";
const char* TensorflowPredictTempoCNN::description = DOC(
  "This algorithm makes predictions using TempoCNN-based models.\n"
  "\n"
  "Internally, it uses TensorflowInputTempoCNN for the input feature "
  "extraction (mel bands). It feeds the model with patches of 256 mel bands "
  "frames and jumps a constant amount of frames determined by `patchHopSize`.\n"
  "\n"
  "With the `batchSize` parameter set to -1 or 0 the patches are stored to run a "
  "single TensorFlow session at the end of the stream. This allows to take "
  "advantage of parallelization when GPUs are available, but at the same time "
  "it can be memory exhausting for long files.\n"
  "\n"
  "The recommended pipeline is as follows::\n"
  "\n"
  "  MonoLoader(sampleRate=11025) >> TensorflowPredictTempoCNN\n"
  "\n"
  "Note: This algorithm does not make any check on the input model so it is "
  "the user's responsibility to make sure it is a valid one.\n"
  "\n"
  "References:\n"
  "\n"
  "1. Hendrik Schreiber, Meinard Müller, A Single-Step Approach to Musical "
  "Tempo Estimation Using a Convolutional Neural Network Proceedings of the "
  "19th International Society for Music Information Retrieval Conference "
  "(ISMIR), Paris, France, Sept. 2018.\n\n"
  "2. Hendrik Schreiber, Meinard Müller, Musical Tempo and Key Estimation "
  "using Convolutional Neural Networks with Directional Filters Proceedings of "
  "the Sound and Music Computing Conference (SMC), Málaga, Spain, 2019.\n\n"
  "3. Original models and code at https://github.com/hendriks73/tempo-cnn\n\n"
  "4. Supported models at https://essentia.upf.edu/models/\n\n");

TensorflowPredictTempoCNN::TensorflowPredictTempoCNN() {
    declareInput(_signal, "signal", "the input audio signal sampled at 11025 Hz");
    declareOutput(_predictions, "predictions", "the output values from the model node named after `output`");

    createInnerNetwork();
  }

TensorflowPredictTempoCNN::~TensorflowPredictTempoCNN() {
  delete _network;
}

void TensorflowPredictTempoCNN::createInnerNetwork() {
  _tensorflowPredictTempoCNN = streaming::AlgorithmFactory::create("TensorflowPredictTempoCNN");
  _vectorInput = new streaming::VectorInput<Real>();

  *_vectorInput  >> _tensorflowPredictTempoCNN->input("signal");
  _tensorflowPredictTempoCNN->output("predictions") >>  PC(_pool, "predictions");

  _network = new scheduler::Network(_vectorInput);
}

void TensorflowPredictTempoCNN::configure() {
  _tensorflowPredictTempoCNN->configure(INHERIT("graphFilename"),
                                        INHERIT("savedModel"),
                                        INHERIT("input"),
                                        INHERIT("output"),
                                        INHERIT("patchHopSize"),
                                        INHERIT("batchSize"),
                                        INHERIT("lastPatchMode"));
}

void TensorflowPredictTempoCNN::compute() {
  const vector<Real>& signal = _signal.get();
  vector<vector<Real> >& predictions = _predictions.get();

  if (!signal.size()) {
    throw EssentiaException("TensorflowPredictTempoCNN: empty input signal");
  }

  _vectorInput->setVector(&signal);

  _network->run();

  try {
    predictions = _pool.value<vector<vector<Real> > >("predictions");
  }
  catch (EssentiaException&) {
    predictions.clear();
  }

  reset();
}

void TensorflowPredictTempoCNN::reset() {
  _network->reset();
  _pool.remove("predictions");
}

} // namespace standard
} // namespace essentia
<commit_msg>Remove exception on batchSize==0 in TempoCNN<commit_after>/*
 * Copyright (C) 2006-2021  Music Technology Group - Universitat Pompeu Fabra
 *
 * This file is part of Essentia
 *
 * Essentia is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation (FSF), 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 Affero GNU General Public License
 * version 3 along with this program.  If not, see http://www.gnu.org/licenses/
 */

#include "tensorflowpredicttempocnn.h"

using namespace std;

namespace essentia {
namespace streaming {

const char* TensorflowPredictTempoCNN::name = essentia::standard::TensorflowPredictTempoCNN::name;
const char* TensorflowPredictTempoCNN::category = essentia::standard::TensorflowPredictTempoCNN::category;
const char* TensorflowPredictTempoCNN::description = essentia::standard::TensorflowPredictTempoCNN::description;

TensorflowPredictTempoCNN::TensorflowPredictTempoCNN() : AlgorithmComposite(),
    _frameCutter(0), _tensorflowInputTempoCNN(0), _vectorRealToTensor(0), _tensorNormalize(0),
    _tensorTranspose(0), _tensorToPool(0), _tensorflowPredict(0), _poolToTensor(0),
    _tensorToVectorReal(0), _configured(false) {

  declareInput(_signal, 4096, "signal", "the input audio signal sampled at 11025 Hz");
  declareOutput(_predictions, 0, "predictions", "the output values from the model node named after `output`");
}

void TensorflowPredictTempoCNN::createInnerNetwork() {
  AlgorithmFactory& factory = AlgorithmFactory::instance();

  _frameCutter             = factory.create("FrameCutter");
  _tensorflowInputTempoCNN = factory.create("TensorflowInputTempoCNN");
  _vectorRealToTensor      = factory.create("VectorRealToTensor");
  _tensorNormalize         = factory.create("TensorNormalize");
  _tensorTranspose         = factory.create("TensorTranspose");
  _tensorToPool            = factory.create("TensorToPool");
  _tensorflowPredict       = factory.create("TensorflowPredict");
  _poolToTensor            = factory.create("PoolToTensor");
  _tensorToVectorReal      = factory.create("TensorToVectorReal");

  _tensorflowInputTempoCNN->output("bands").setBufferType(BufferUsage::forMultipleFrames);

  _signal                                   >> _frameCutter->input("signal");
  _frameCutter->output("frame")             >> _tensorflowInputTempoCNN->input("frame");
  _tensorflowInputTempoCNN->output("bands") >> _vectorRealToTensor->input("frame");
  _vectorRealToTensor->output("tensor")     >> _tensorNormalize->input("tensor");
  _tensorNormalize->output("tensor")        >> _tensorTranspose->input("tensor");
  _tensorTranspose->output("tensor")        >> _tensorToPool->input("tensor");
  _tensorToPool->output("pool")             >> _tensorflowPredict->input("poolIn");
  _tensorflowPredict->output("poolOut")     >> _poolToTensor->input("pool");
  _poolToTensor->output("tensor")           >> _tensorToVectorReal->input("tensor");

  attach(_tensorToVectorReal->output("frame"), _predictions);

  _network = new scheduler::Network(_frameCutter);
}

void TensorflowPredictTempoCNN::clearAlgos() {
  if (!_configured) return;
  delete _network;
}

TensorflowPredictTempoCNN::~TensorflowPredictTempoCNN() {
  clearAlgos();
}

void TensorflowPredictTempoCNN::reset() {
  AlgorithmComposite::reset();
}

void TensorflowPredictTempoCNN::configure() {
  if (_configured) {
    clearAlgos();
  }

  createInnerNetwork();

  int patchHopSize = parameter("patchHopSize").toInt();
  string lastPatchMode = parameter("lastPatchMode").toString();
  int batchSize = parameter("batchSize").toInt();

  // Hardcoded parameters matching the training setup:
  // https://github.com/hendriks73/tempo-cnn/blob/master/tempocnn/feature.py
  int frameSize = 1024;
  int hopSize = 512;
  int patchSize = 256;
  int numberBands = 40;
  vector<int> inputShape({batchSize, 1, patchSize, numberBands});
  string scaler = "standard";

  // Hendrik's models expect data shaped as {Batch, Mels, Time, Channel}.
  vector<int> permutation({0, 3, 2, 1});

  _frameCutter->configure("frameSize", frameSize, "hopSize", hopSize);

  _vectorRealToTensor->configure("shape", inputShape,
                                 "lastPatchMode", lastPatchMode,
                                 "patchHopSize", patchHopSize);

  _tensorNormalize->configure("scaler", scaler);

  _tensorTranspose->configure("permutation", permutation);

  _configured = true;

  string input = parameter("input").toString();
  string output = parameter("output").toString();

  _tensorToPool->configure("namespace", input);

  _poolToTensor->configure("namespace", output);

  string graphFilename = parameter("graphFilename").toString();
  string savedModel = parameter("savedModel").toString();

  _tensorflowPredict->configure("graphFilename", graphFilename,
                                "savedModel", savedModel,
                                "squeeze", false,
                                "inputs", vector<string>({input}),
                                "outputs", vector<string>({output}));
}

} // namespace streaming
} // namespace essentia

namespace essentia {
namespace standard {

const char* TensorflowPredictTempoCNN::name = "TensorflowPredictTempoCNN";
const char* TensorflowPredictTempoCNN::category = "Machine Learning";
const char* TensorflowPredictTempoCNN::description = DOC(
  "This algorithm makes predictions using TempoCNN-based models.\n"
  "\n"
  "Internally, it uses TensorflowInputTempoCNN for the input feature "
  "extraction (mel bands). It feeds the model with patches of 256 mel bands "
  "frames and jumps a constant amount of frames determined by `patchHopSize`.\n"
  "\n"
  "With the `batchSize` parameter set to -1 or 0 the patches are stored to run a "
  "single TensorFlow session at the end of the stream. This allows to take "
  "advantage of parallelization when GPUs are available, but at the same time "
  "it can be memory exhausting for long files.\n"
  "\n"
  "The recommended pipeline is as follows::\n"
  "\n"
  "  MonoLoader(sampleRate=11025) >> TensorflowPredictTempoCNN\n"
  "\n"
  "Note: This algorithm does not make any check on the input model so it is "
  "the user's responsibility to make sure it is a valid one.\n"
  "\n"
  "References:\n"
  "\n"
  "1. Hendrik Schreiber, Meinard Müller, A Single-Step Approach to Musical "
  "Tempo Estimation Using a Convolutional Neural Network Proceedings of the "
  "19th International Society for Music Information Retrieval Conference "
  "(ISMIR), Paris, France, Sept. 2018.\n\n"
  "2. Hendrik Schreiber, Meinard Müller, Musical Tempo and Key Estimation "
  "using Convolutional Neural Networks with Directional Filters Proceedings of "
  "the Sound and Music Computing Conference (SMC), Málaga, Spain, 2019.\n\n"
  "3. Original models and code at https://github.com/hendriks73/tempo-cnn\n\n"
  "4. Supported models at https://essentia.upf.edu/models/\n\n");

TensorflowPredictTempoCNN::TensorflowPredictTempoCNN() {
    declareInput(_signal, "signal", "the input audio signal sampled at 11025 Hz");
    declareOutput(_predictions, "predictions", "the output values from the model node named after `output`");

    createInnerNetwork();
  }

TensorflowPredictTempoCNN::~TensorflowPredictTempoCNN() {
  delete _network;
}

void TensorflowPredictTempoCNN::createInnerNetwork() {
  _tensorflowPredictTempoCNN = streaming::AlgorithmFactory::create("TensorflowPredictTempoCNN");
  _vectorInput = new streaming::VectorInput<Real>();

  *_vectorInput  >> _tensorflowPredictTempoCNN->input("signal");
  _tensorflowPredictTempoCNN->output("predictions") >>  PC(_pool, "predictions");

  _network = new scheduler::Network(_vectorInput);
}

void TensorflowPredictTempoCNN::configure() {
  _tensorflowPredictTempoCNN->configure(INHERIT("graphFilename"),
                                        INHERIT("savedModel"),
                                        INHERIT("input"),
                                        INHERIT("output"),
                                        INHERIT("patchHopSize"),
                                        INHERIT("batchSize"),
                                        INHERIT("lastPatchMode"));
}

void TensorflowPredictTempoCNN::compute() {
  const vector<Real>& signal = _signal.get();
  vector<vector<Real> >& predictions = _predictions.get();

  if (!signal.size()) {
    throw EssentiaException("TensorflowPredictTempoCNN: empty input signal");
  }

  _vectorInput->setVector(&signal);

  _network->run();

  try {
    predictions = _pool.value<vector<vector<Real> > >("predictions");
  }
  catch (EssentiaException&) {
    predictions.clear();
  }

  reset();
}

void TensorflowPredictTempoCNN::reset() {
  _network->reset();
  _pool.remove("predictions");
}

} // namespace standard
} // namespace essentia
<|endoftext|>


<commit_before><commit_msg>fix crash when checking an empty error message of an exception<commit_after><|endoftext|>