<commit_before>/*
 *
 */
#ifndef UNIX_CONDITION_QUEUE_HPP
#define UNIX_CONDITION_QUEUE_HPP

#include <condition_variable>
#include <mutex>
#include <optional>

#include "condition-queue-detail.hpp"
#include "move-queue.hpp"

namespace unix
{

  // mutex_t is either std::mutex or std::ref(std::mutex)
  // similar for condition_t
  template <typename record_t, typename mutex_t, typename condition_t>
  class condition_queue_t
  {

  public:

    condition_queue_t () = default;
    condition_queue_t (mutex_t mutex, condition_t condition);
    ~condition_queue_t () = default;

    void push (record_t record);

    template <bool wait>
    std::optional<record_t> pop ();

    template <bool lock>
    bool empty ();

    template <bool lock>
    void notify_one ();

  private:

    mutex_t m_mutex;
    condition_t m_condition;

    move_queue_t<record_t> m_queue;
  };

  template <typename record_t, typename mutex_t, typename condition_t>
  condition_queue_t<record_t, mutex_t, condition_t>::
  condition_queue_t (mutex_t mutex, condition_t condition)
    : m_mutex (mutex),
      m_condition (condition)
  {
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  void condition_queue_t<record_t, mutex_t, condition_t>::push (record_t record)
  {
    // take a reference
    std::mutex &mutex (m_mutex);
    std::lock_guard lock (mutex);

    bool empty = m_queue.empty ();

    m_queue.push (std::move (record));

    if (empty == true) {
      std::condition_variable &condition (m_condition);
      condition.notify_one ();
    }
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_wait>
  std::optional<record_t>
  condition_queue_t<record_t, mutex_t, condition_t>::pop ()
  {
    std::mutex &mutex (m_mutex);
    std::unique_lock lock (mutex);
    std::condition_variable &condition (m_condition);

    if (m_queue.empty () == true)
      condition_queue_detail::wait<really_wait>(lock, condition);

    if (m_queue.empty ())
      return {};

    return std::optional<record_t>(m_queue.pop ());
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_lock>
  bool condition_queue_t<record_t, mutex_t, condition_t>::empty ()
  {
    std::mutex &mutex (m_mutex);
    typename condition_queue_detail::guard<really_lock>::lock lock (mutex);

    return m_queue.empty ();
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_lock>
  void condition_queue_t<record_t, mutex_t, condition_t>::notify_one ()
  {
    std::mutex &mutex (m_mutex);
    typename condition_queue_detail::unique<really_lock>::lock lock (mutex);

    std::condition_variable &condition (m_condition);
    condition.notify_one ();
  }

} // unix

#endif<commit_msg>pi: Simplify condition-queue, use if constexpr<commit_after>/*
 *
 */
#ifndef UNIX_CONDITION_QUEUE_HPP
#define UNIX_CONDITION_QUEUE_HPP

#include <condition_variable>
#include <mutex>
#include <optional>

// #include "condition-queue-detail.hpp"
#include "move-queue.hpp"

namespace unix
{

  // mutex_t is either std::mutex or std::ref(std::mutex)
  // similar for condition_t
  template <typename record_t, typename mutex_t, typename condition_t>
  class condition_queue_t
  {

  public:

    condition_queue_t () = default;
    condition_queue_t (mutex_t mutex, condition_t condition);
    ~condition_queue_t () = default;

    void push (record_t record);

    template <bool wait>
    std::optional<record_t> pop ();

    template <bool lock>
    bool empty ();

    template <bool lock>
    void notify_one ();

  private:

    mutex_t m_mutex;
    condition_t m_condition;

    move_queue_t<record_t> m_queue;
  };

  template <typename record_t, typename mutex_t, typename condition_t>
  condition_queue_t<record_t, mutex_t, condition_t>::
  condition_queue_t (mutex_t mutex, condition_t condition)
    : m_mutex (mutex),
      m_condition (condition)
  {
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  void condition_queue_t<record_t, mutex_t, condition_t>::push (record_t record)
  {
    // take a reference
    std::mutex &mutex (m_mutex);
    std::lock_guard lock (mutex);

    bool empty = m_queue.empty ();

    m_queue.push (std::move (record));

    if (empty == true) {
      std::condition_variable &condition (m_condition);
      condition.notify_one ();
    }
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_wait>
  std::optional<record_t>
  condition_queue_t<record_t, mutex_t, condition_t>::pop ()
  {
    std::mutex &mutex (m_mutex);
    std::unique_lock lock (mutex);
    std::condition_variable &condition (m_condition);

    if constexpr (really_wait)
      condition.wait (lock);
    
    //   }
    // a;
    // if (m_queue.empty () == true)
    //   condition_queue_detail::wait<really_wait>(lock, condition);

    if (m_queue.empty ())
      return {};

    return std::optional<record_t>(m_queue.pop ());
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_lock>
  bool condition_queue_t<record_t, mutex_t, condition_t>::empty ()
  {
    // std::mutex &mutex (m_mutex);
    if constexpr (really_lock)
      std::lock_guard lock (m_mutex);
    // typename condition_queue_detail::guard<really_lock>::lock lock (mutex);

    return m_queue.empty ();
  }

  template <typename record_t, typename mutex_t, typename condition_t>
  template <bool really_lock>
  void condition_queue_t<record_t, mutex_t, condition_t>::notify_one ()
  {
    // std::mutex &mutex (m_mutex);
    // typename condition_queue_detail::unique<really_lock>::lock lock (mutex);

    if constexpr (really_lock)
      std::lock_guard lock (m_mutex);

    // std::condition_variable &condition (m_condition);
    m_condition.notify_one ();
  }

} // unix

#endif
{'fraction_non_alphanumeric': 0.1158805564981337, 'fraction_numerical': 0.0, 'mean_word_length': 4.017021276595744, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5029951', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1918, 'n_words': 438}