<commit_before>/*!
 * \file util_private.hpp
 * \brief file util_private.hpp
 *
 * Copyright 2016 by Intel.
 *
 * Contact: <EMAIL>
 *
 * 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/.
 *
 * \author <NAME> <<EMAIL>>
 *
 */


#pragma once

#include <boost/thread.hpp>

#include <vector>
#include <fastuidraw/util/c_array.hpp>

namespace fastuidraw
{
  /*!
    Wrapper over mutex type so that we can replace
    mutex implementation easily.
   */
  class mutex:fastuidraw::noncopyable
  {
  public:
    void
    lock(void)
    {
      m_mutex.lock();
    }

    void
    unlock(void)
    {
      m_mutex.unlock();
    }

  private:
    boost::mutex m_mutex;
  };

  /*!
    Locks mutex on ctor and unlocks un dtor.
   */
  class autolock_mutex:fastuidraw::noncopyable
  {
  public:
    explicit
    autolock_mutex(mutex &m):
      m_mutex(m)
    {
      m_mutex.lock();
    }

    ~autolock_mutex()
    {
      m_mutex.unlock();
    }
  private:
    mutex &m_mutex;
  };

  template<typename T>
  c_array<T>
  make_c_array(std::vector<T> &p)
  {
    if(p.empty())
      {
        return c_array<T>();
      }
    else
      {
        return c_array<T>(&p[0], p.size());
      }
  }

  template<typename T>
  const_c_array<T>
  make_c_array(const std::vector<T> &p)
  {
    if(p.empty())
      {
        return const_c_array<T>();
      }
    else
      {
        return const_c_array<T>(&p[0], p.size());
      }
  }

  template<typename T>
  c_array<T>
  const_cast_c_array(const_c_array<T> p)
  {
    T *q;
    q = const_cast<T*>(p.c_ptr());
    return c_array<T>(q, p.size());
  }
}
<commit_msg>fastuidraw/private: add time difference routine to private utils<commit_after>/*!
 * \file util_private.hpp
 * \brief file util_private.hpp
 *
 * Copyright 2016 by Intel.
 *
 * Contact: <EMAIL>
 *
 * 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/.
 *
 * \author <NAME> <<EMAIL>>
 *
 */


#pragma once

#include <sys/time.h>
#include <boost/thread.hpp>

#include <vector>
#include <fastuidraw/util/c_array.hpp>

namespace fastuidraw
{
  /*!
    Wrapper over mutex type so that we can replace
    mutex implementation easily.
   */
  class mutex:fastuidraw::noncopyable
  {
  public:
    void
    lock(void)
    {
      m_mutex.lock();
    }

    void
    unlock(void)
    {
      m_mutex.unlock();
    }

  private:
    boost::mutex m_mutex;
  };

  /*!
    Locks mutex on ctor and unlocks un dtor.
   */
  class autolock_mutex:fastuidraw::noncopyable
  {
  public:
    explicit
    autolock_mutex(mutex &m):
      m_mutex(m)
    {
      m_mutex.lock();
    }

    ~autolock_mutex()
    {
      m_mutex.unlock();
    }
  private:
    mutex &m_mutex;
  };

  inline
  int32_t
  time_difference_ms(const struct timeval &end, const struct timeval &begin)
  {
    return (end.tv_sec - begin.tv_sec) * 1000 +
      (end.tv_usec - begin.tv_usec) / 1000;
  }

  inline
  int64_t
  time_difference_us(const struct timeval &end, const struct timeval &begin)
  {
    int64_t delta_usec, delta_sec;

    delta_usec = int64_t(end.tv_usec) - int64_t(begin.tv_usec);
    delta_sec = int64_t(end.tv_sec) - int64_t(begin.tv_sec);
    return delta_sec * int64_t(1000) * int64_t(1000) + delta_usec;
  }

  template<typename T>
  c_array<T>
  make_c_array(std::vector<T> &p)
  {
    if(p.empty())
      {
        return c_array<T>();
      }
    else
      {
        return c_array<T>(&p[0], p.size());
      }
  }

  template<typename T>
  const_c_array<T>
  make_c_array(const std::vector<T> &p)
  {
    if(p.empty())
      {
        return const_c_array<T>();
      }
    else
      {
        return const_c_array<T>(&p[0], p.size());
      }
  }

  template<typename T>
  c_array<T>
  const_cast_c_array(const_c_array<T> p)
  {
    T *q;
    q = const_cast<T*>(p.c_ptr());
    return c_array<T>(q, p.size());
  }
}

{'fraction_non_alphanumeric': 0.13604283280603552, 'fraction_numerical': 0.013141883669992699, 'mean_word_length': 2.413621262458472, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, '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': '658974', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1572, 'n_words': 332}