<commit_before>//---------------------------------------------------------------------------
//      $Id$
//    Version: $Name$
//
//    Copyright (C) 2005 by the deal.II authors
//
//    This file is subject to QPL and may not be  distributed
//    without copyright and license information. Please refer
//    to the file deal.II/doc/license.html for the  text  and
//    further information on this license.
//
//---------------------------------------------------------------------------

#include <base/utilities.h>
#include <base/exceptions.h>

#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cerrno>
#include <cmath>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_STD_STRINGSTREAM
#  include <sstream>
#else
#  include <strstream>
#endif
#ifdef HAVE_STD_NUMERIC_LIMITS
#  include <limits>
#else
#  include <limits.h>
#endif

namespace Utilities
{

  DeclException2 (ExcInvalidNumber2StringConversersion,
		  unsigned int, unsigned int,
		  << "When trying to convert " << arg1
		  << " to a string with " << arg2 << " digits");
  DeclException1 (ExcInvalidNumber,
		  unsigned int,
		  << "Invalid number " << arg1);
  DeclException1 (ExcCantConvertString,
		  std::string,
		  << "Can't convert the string " << arg1
                  << " to the desired type");

  std::string
  int_to_string (const unsigned int i,
		 const unsigned int digits)
  {
    AssertThrow ( ! ((digits==1 && i>=10)   ||
		     (digits==2 && i>=100)  ||
		     (digits==3 && i>=1000) ||
		     (digits==4 && i>=10000)||
		     (i>=100000)),
		  ExcInvalidNumber2StringConversersion(i, digits));

    std::string s;
    switch (digits)
      {
	case 4:
	      s += '0' + i/1000;
	case 3:
	      s += '0' + (i%1000)/100;
	case 2:
	      s += '0' + (i%100)/10;
	case 1:
	      s += '0' + i%10;
	      break;
	default:
	      s += "invalid digits information";
      };
    return s;
  }

  unsigned int
  needed_digits (const unsigned int max_number)
  {
    if (max_number < 10)
      return 1;
    if (max_number < 100)
      return 2;
    if (max_number < 1000)
      return 3;
    if (max_number < 10000)
      return 4;
    if (max_number < 100000)
      return 5;
    if (max_number < 1000000)
      return 6;
    AssertThrow (false, ExcInvalidNumber(max_number));
    return 0;
  }

  int
  string_to_int (const std::string &s)
  {
#ifdef HAVE_STD_STRINGSTREAM
    std::istringstream ss(s);
#else
    std::ostrstream ss(s.c_str());
#endif

#ifdef HAVE_STD_NUMERIC_LIMITS
    static const int max_int = std::numeric_limits<int>::max();
#else
    static const int max_int = INT_MAX;
#endif

    int i = max_int;
    ss >> i;

                                     // check for errors
    AssertThrow (i != max_int, ExcCantConvertString (s));

    return i;
  }

  std::vector<int>
  string_to_int (const std::vector<std::string> &s)
  {
    std::vector<int> tmp (s.size());
    for (unsigned int i=0; i<s.size(); ++i)
      tmp[i] = string_to_int (s[i]);
    return tmp;
  }

  std::vector<std::string>
  split_string_list (const std::string &s,
                     const char         delimiter)
  {
    std::string tmp = s;
    std::vector<std::string> split_list;
    split_list.reserve (std::count (tmp.begin(), tmp.end(), delimiter)+1);

				     // split the input list
    while (tmp.length() != 0)
      {
        std::string name;
	name = tmp;

	if (name.find(delimiter) != std::string::npos)
	  {
	    name.erase (name.find(delimiter), std::string::npos);
	    tmp.erase (0, tmp.find(delimiter)+1);
	  }
	else
	  tmp = "";

	while ((name.length() != 0) &&
	       (name[0] == ' '))
	  name.erase (0,1);

	while (name[name.length()-1] == ' ')
	  name.erase (name.length()-1, 1);

	split_list.push_back (name);
      }

    return split_list;
  }

  double
  generate_normal_random_number (const double a,
				 const double sigma)
  {
				     // if no noise: return now
    if (sigma == 0)
      return a;

#ifdef HAVE_RAND_R
    static unsigned int seed = 0xabcd1234;
    const double y = 1.0*rand_r(&seed)/RAND_MAX;
#else
    const double y = 1.0*rand()/RAND_MAX;
#endif

				     // find x such that y=erf(x). do so
				     // using a Newton method to find
				     // the zero of F(x)=erf(x)-y. start
				     // at x=0
    double x = 0;
    unsigned int iteration = 0;
    while (true)
      {
	const double residual = 0.5+erf(x/std::sqrt(2.)/sigma)/2-y;
	if (std::fabs(residual) < 1e-7)
	  break;
	const double F_prime = 1./std::sqrt(2*3.1415926536)/sigma *
			       std::exp(-x*x/sigma/sigma/2);
	x += -residual / F_prime;

					 // make sure that we don't
					 // recurse endlessly
	++iteration;
	Assert (iteration < 20, ExcInternalError());
      };
    return x+a;
  }

  namespace System
  {
#if defined(__linux__)

    double get_cpu_load ()
    {
      std::ifstream cpuinfo;
      cpuinfo.open("/proc/loadavg");

      AssertThrow(cpuinfo, ExcIO());

      double load;
      cpuinfo >> load;

      return load;
    }

#else

    double get_cpu_load ()
    {
      return 0.;
    }

#endif

    std::string get_hostname ()
    {
      const unsigned int N=1024;
      char hostname[N];
      gethostname (&(hostname[0]), N-1);
      return hostname;
    }

    std::string get_time ()
    {
      std::time_t  time1= std::time (0);
      std::tm     *time = std::localtime(&time1);

#ifdef HAVE_STD_STRINGSTREAM
      std::ostringstream o;
#else
      std::ostrstream o;
#endif
      o << time->tm_hour << ":"
        << (time->tm_min < 10 ? "0" : "") << time->tm_min << ":"
        << (time->tm_sec < 10 ? "0" : "") << time->tm_sec;
#ifndef HAVE_STD_STRINGSTREAM
      o << std::ends;
#endif
      return o.str();
    }

  }

}
<commit_msg>Use the has_documentation flag.<commit_after>//---------------------------------------------------------------------------
//      $Id$
//    Version: $Name$
//
//    Copyright (C) 2005 by the deal.II authors
//
//    This file is subject to QPL and may not be  distributed
//    without copyright and license information. Please refer
//    to the file deal.II/doc/license.html for the  text  and
//    further information on this license.
//
//---------------------------------------------------------------------------

#include <base/utilities.h>
#include <base/exceptions.h>

#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cerrno>
#include <cmath>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_STD_STRINGSTREAM
#  include <sstream>
#else
#  include <strstream>
#endif
#ifdef HAVE_STD_NUMERIC_LIMITS
#  include <limits>
#else
#  include <limits.h>
#endif

namespace Utilities
{

  DeclException2 (ExcInvalidNumber2StringConversersion,
		  unsigned int, unsigned int,
		  << "When trying to convert " << arg1
		  << " to a string with " << arg2 << " digits");
  DeclException1 (ExcInvalidNumber,
		  unsigned int,
		  << "Invalid number " << arg1);
  DeclException1 (ExcCantConvertString,
		  std::string,
		  << "Can't convert the string " << arg1
                  << " to the desired type");

  std::string
  int_to_string (const unsigned int i,
		 const unsigned int digits)
  {
    AssertThrow ( ! ((digits==1 && i>=10)   ||
		     (digits==2 && i>=100)  ||
		     (digits==3 && i>=1000) ||
		     (digits==4 && i>=10000)||
		     (i>=100000)),
		  ExcInvalidNumber2StringConversersion(i, digits));

    std::string s;
    switch (digits)
      {
	case 4:
	      s += '0' + i/1000;
	case 3:
	      s += '0' + (i%1000)/100;
	case 2:
	      s += '0' + (i%100)/10;
	case 1:
	      s += '0' + i%10;
	      break;
	default:
	      s += "invalid digits information";
      };
    return s;
  }

  unsigned int
  needed_digits (const unsigned int max_number)
  {
    if (max_number < 10)
      return 1;
    if (max_number < 100)
      return 2;
    if (max_number < 1000)
      return 3;
    if (max_number < 10000)
      return 4;
    if (max_number < 100000)
      return 5;
    if (max_number < 1000000)
      return 6;
    AssertThrow (false, ExcInvalidNumber(max_number));
    return 0;
  }

  int
  string_to_int (const std::string &s)
  {
#ifdef HAVE_STD_STRINGSTREAM
    std::istringstream ss(s);
#else
    std::ostrstream ss(s.c_str());
#endif

#ifdef HAVE_STD_NUMERIC_LIMITS
    static const int max_int = std::numeric_limits<int>::max();
#else
    static const int max_int = INT_MAX;
#endif

    int i = max_int;
    ss >> i;

                                     // check for errors
    AssertThrow (i != max_int, ExcCantConvertString (s));

    return i;
  }

  std::vector<int>
  string_to_int (const std::vector<std::string> &s)
  {
    std::vector<int> tmp (s.size());
    for (unsigned int i=0; i<s.size(); ++i)
      tmp[i] = string_to_int (s[i]);
    return tmp;
  }

  std::vector<std::string>
  split_string_list (const std::string &s,
                     const char         delimiter)
  {
    std::string tmp = s;
    std::vector<std::string> split_list;
    split_list.reserve (std::count (tmp.begin(), tmp.end(), delimiter)+1);

				     // split the input list
    while (tmp.length() != 0)
      {
        std::string name;
	name = tmp;

	if (name.find(delimiter) != std::string::npos)
	  {
	    name.erase (name.find(delimiter), std::string::npos);
	    tmp.erase (0, tmp.find(delimiter)+1);
	  }
	else
	  tmp = "";

	while ((name.length() != 0) &&
	       (name[0] == ' '))
	  name.erase (0,1);

	while (name[name.length()-1] == ' ')
	  name.erase (name.length()-1, 1);

	split_list.push_back (name);
      }

    return split_list;
  }

  std::vector<std::string>
  break_text_into_lines (const std::string &original_text,
                         const unsigned int width)
  {
    std::string              text = original_text;
    std::vector<std::string> lines;

                                     // remove trailing spaces
    while ((text.length() != 0) && (text[text.length()-1] == ' '))
      text.erase(text.length()-1,1);

                                     // then split the text into lines
    while (text.length() != 0)
      {
                                         // in each iteration, first remove
                                         // leading spaces
        while ((text.length() != 0) && (text[0] == ' '))
          text.erase(0, 1);

                                         // if we can fit everything into one
                                         // line, then do so. otherwise, we have
                                         // to keep breaking
        if (text.length() < width)
          {
            lines.push_back (text);
            text = "";
          }
        else
          {
                                             // starting at position width, find the
                                             // location of the previous space, so
                                             // that we can break around there
            int location = std::min(width,text.length()-1);
            for (; location>=0; --location)
              if (text[location] == ' ')
                break;

                                             // if there are no spaces, then try if
                                             // there are spaces coming up
            if (location == 0)
              for (location = std::min(width,text.length()-1);
                   location<static_cast<int>(text.length());
                   ++location)
                if (text[location] == ' ')
                  break;

                                             // now take the text up to the found
                                             // location and put it into a single
                                             // line, and remove it from 'text'
            lines.push_back (std::string (text, 0, location));
            text.erase (0, location);
          }
      }

    return lines;
  }

  double
  generate_normal_random_number (const double a,
				 const double sigma)
  {
				     // if no noise: return now
    if (sigma == 0)
      return a;

#ifdef HAVE_RAND_R
    static unsigned int seed = 0xabcd1234;
    const double y = 1.0*rand_r(&seed)/RAND_MAX;
#else
    const double y = 1.0*rand()/RAND_MAX;
#endif

				     // find x such that y=erf(x). do so
				     // using a Newton method to find
				     // the zero of F(x)=erf(x)-y. start
				     // at x=0
    double x = 0;
    unsigned int iteration = 0;
    while (true)
      {
	const double residual = 0.5+erf(x/std::sqrt(2.)/sigma)/2-y;
	if (std::fabs(residual) < 1e-7)
	  break;
	const double F_prime = 1./std::sqrt(2*3.1415926536)/sigma *
			       std::exp(-x*x/sigma/sigma/2);
	x += -residual / F_prime;

					 // make sure that we don't
					 // recurse endlessly
	++iteration;
	Assert (iteration < 20, ExcInternalError());
      };
    return x+a;
  }

  namespace System
  {
#if defined(__linux__)

    double get_cpu_load ()
    {
      std::ifstream cpuinfo;
      cpuinfo.open("/proc/loadavg");

      AssertThrow(cpuinfo, ExcIO());

      double load;
      cpuinfo >> load;

      return load;
    }

#else

    double get_cpu_load ()
    {
      return 0.;
    }

#endif

    std::string get_hostname ()
    {
      const unsigned int N=1024;
      char hostname[N];
      gethostname (&(hostname[0]), N-1);
      return hostname;
    }

    std::string get_time ()
    {
      std::time_t  time1= std::time (0);
      std::tm     *time = std::localtime(&time1);

#ifdef HAVE_STD_STRINGSTREAM
      std::ostringstream o;
#else
      std::ostrstream o;
#endif
      o << time->tm_hour << ":"
        << (time->tm_min < 10 ? "0" : "") << time->tm_min << ":"
        << (time->tm_sec < 10 ? "0" : "") << time->tm_sec;
#ifndef HAVE_STD_STRINGSTREAM
      o << std::ends;
#endif
      return o.str();
    }

  }

}
<|endoftext|>


<commit_before>/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

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

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

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

#include "tensorflow/cc/framework/ops.h"
#include "tensorflow/cc/ops/array_ops.h"
#include "tensorflow/cc/ops/resource_variable_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/framework/grad_op_registry.h"
#include "tensorflow/cc/framework/gradient_checker.h"
#include "tensorflow/cc/framework/gradients.h"
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/framework/testutil.h"
#include "tensorflow/cc/gradients/grad_testutil.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"

namespace tensorflow {
namespace {

using namespace ops;  // NOLINT(build/namespaces)

class ResourceVariableGradTest : public ::testing::Test {
 protected:
  ResourceVariableGradTest() : scope_(Scope::NewRootScope()) {}

  void RunTest(const Output& x, const TensorShape& x_shape, const Output& y,
               const TensorShape& y_shape) {
    TF_ASSERT_OK(scope_.status());
    float max_error;
    TF_ASSERT_OK((ComputeGradientError<float, float, float>(
        scope_, {x}, {x_shape}, {y}, {y_shape}, &max_error)));
    EXPECT_LT(max_error, 1e-3);
  }

  void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
               const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
    TF_ASSERT_OK(scope_.status());
    float max_error;
    TF_ASSERT_OK((ComputeGradientError<float, float, float>(
        scope_, xs, x_shapes, ys, y_shapes, &max_error)));
    EXPECT_LT(max_error, 1e-3);
  }

  Scope scope_;
};

TEST_F(ResourceVariableGradTest, ReadVariableOpGrad) {
  TensorShape shape({});
  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));

  auto var = VarHandleOp(scope_, DT_FLOAT, shape);
  auto init = AssignVariableOp(scope_, var, Const(scope_, (float) 2, {}));

  auto temp = ReadVariableOp(scope_, var, DT_FLOAT);

  auto y = Mul(scope_, temp, x);

  auto dy = ops::Cast(scope_, ops::Const(scope_, 1.0, shape), DT_FLOAT);

  OutputList dxs;
  TF_ASSERT_OK(AddSymbolicGradients(scope_, {y}, {var}, {dy}, &dxs));

  ClientSession::FeedType feed_list;
  feed_list.insert({x, (float) 5});
  feed_list.insert({dy, (float) 1});

  std::vector<Tensor> dxout;
  ClientSession session(scope_);
  TF_ASSERT_OK(session.Run(feed_list, dxs, &dxout));

  auto grad = dxout[0].scalar<float>()();
  EXPECT_EQ(grad, 5);
}

}  // namespace
}  // namespace tensorflow
<commit_msg>Fix test nits<commit_after>/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

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

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

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

#include <iostream>
#include "tensorflow/cc/framework/ops.h"
#include "tensorflow/cc/ops/array_ops.h"
#include "tensorflow/cc/ops/resource_variable_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/framework/grad_op_registry.h"
#include "tensorflow/cc/framework/gradient_checker.h"
#include "tensorflow/cc/framework/gradients.h"
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/framework/testutil.h"
#include "tensorflow/cc/gradients/grad_testutil.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"

namespace tensorflow {
namespace {

using namespace ops;  // NOLINT(build/namespaces)

TEST(ResourceVariableGradTest, ReadVariableOpGrad) {
  TensorShape shape({});
  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));

  auto var = VarHandleOp(scope_, DT_FLOAT, shape);
  auto init = AssignVariableOp(scope_, var, Const(scope_, (float) 2, shape));

  auto temp = ReadVariableOp(scope_, var, DT_FLOAT);

  auto y = Mul(scope_, temp, x);

  auto dy = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));

  OutputList dxs;
  TF_ASSERT_OK(AddSymbolicGradients(scope_, {y}, {var}, {dy}, &dxs));

  ClientSession::FeedType feed_list;
  feed_list.insert({x, (float) 5});
  feed_list.insert({dy, (float) 1});

  std::vector<Tensor> dxout;
  ClientSession session(scope_);
  TF_ASSERT_OK(session.Run(feed_list, dxs, &dxout));

  auto grad = dxout[0].scalar<float>()();
  EXPECT_EQ(grad, 5);
}

}  // namespace
}  // namespace tensorflow
<|endoftext|>


<commit_before><commit_msg>CR mode update for mode 3<commit_after><|endoftext|>


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

#include <sstream>
#include "tablecountnode.h"
#include "common/common.h"
#include "expressions/abstractexpression.h"
#include "storage/table.h"

namespace voltdb {

TableCountPlanNode::~TableCountPlanNode() {
}

std::string TableCountPlanNode::debugInfo(const std::string &spacer) const {
    std::ostringstream buffer;
    buffer << this->AbstractScanPlanNode::debugInfo(spacer);
    assert(m_predicate == NULL);
    buffer << spacer << "TABLE COUNT Expression: <NULL>";
    return (buffer.str());
}

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

#include <sstream>
#include "tablecountnode.h"
#include "common/common.h"
#include "expressions/abstractexpression.h"
#include "storage/table.h"

namespace voltdb {

TableCountPlanNode::~TableCountPlanNode() {
    delete getOutputTable();
    setOutputTable(NULL);
}

std::string TableCountPlanNode::debugInfo(const std::string &spacer) const {
    std::ostringstream buffer;
    buffer << this->AbstractScanPlanNode::debugInfo(spacer);
    assert(m_predicate == NULL);
    buffer << spacer << "TABLE COUNT Expression: <NULL>";
    return (buffer.str());
}

}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2001 Fabrice Bellard
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "gen_audio.hpp"

extern "C" {
    #include <libavformat/avformat.h>
    #include <libavutil/imgutils.h>
    #include <libswscale/swscale.h>
    #include <libavcodec/avcodec.h>
    #include <libavutil/common.h>
    #include <libavutil/opt.h>
}

// this stuff makes me cranky!
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc  avcodec_alloc_frame
#define av_frame_free   avcodec_free_frame
#endif

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

using namespace std;

gen_audio::gen_audio() :
    r{42}
{
}

vector<unsigned char> gen_audio::render_target( int datumNumber ) {
    std::poisson_distribution<int> _word_count(3);
    std::uniform_int_distribution<int> _word(0,vocab.size()-1);
    vector<unsigned char> rc;

    int word_count = _word_count(r)+1;
    for(int i=0; i<word_count; i++) {
        int word = _word(r);
        string w = vocab[word];
        if( i > 0 ) rc.push_back(' ');
        rc.insert( rc.end(), w.begin(), w.end() );
    }
//    string result((char*)rc.data(),rc.size());
//    cout << "'" << result << "'\n";
    return rc;
}

vector<unsigned char> gen_audio::render_datum( int datumNumber ) {
    int frequency = ((datumNumber % 7) + 1) * 1000;
    return encode(frequency, 2000);
}

/*
 * Audio encoding example
 */
vector<unsigned char> gen_audio::encode(float frequencyHz, int duration) {
    AVCodec *codec;
    AVCodecContext *c= nullptr;
    int frame_size, i, j, out_size, outbuf_size;
    short *samples;
    float t, tincr;
    uint8_t *outbuf;
    vector<unsigned char> rc;

    /* find the MP2 encoder */
    codec = avcodec_find_encoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);

    c->sample_fmt = c->codec->sample_fmts[0];

    /* put sample parameters */
    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;

    /* open it */
    if (avcodec_open2(c, codec, nullptr) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */
    frame_size = c->frame_size;
    float frame_duration = (float)(c->frame_size) / (float)(c->sample_rate);
    int frames = ceil((float)duration / frame_duration / 1000.);
    samples = (short*)malloc(frame_size * 2 * c->channels);
    outbuf_size = 10000;
    outbuf = (uint8_t*)malloc(outbuf_size);

    /* encode a single tone sound */
    t = 0;
    tincr = 2 * M_PI * frequencyHz / c->sample_rate;
    for(i=0;i<frames;i++) {
        for(j=0;j<frame_size;j++) {
            samples[2*j] = (int)(sin(t) * 10000);
            samples[2*j+1] = samples[2*j];
            t += tincr;
        }
        /* encode the samples */
        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
        for(int i=0; i<out_size; i++) { rc.push_back(outbuf[i]); }
    }
    free(outbuf);
    free(samples);

    avcodec_close(c);
    av_free(c);

    return rc;
}

void gen_audio::encode(const std::string& filename, float frequencyHz, int duration)
{
    FILE* f;
    f = fopen(filename.c_str(), "wb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename.c_str());
        exit(1);
    }

    vector<unsigned char> data = encode(frequencyHz,duration);
    fwrite(data.data(), 1, data.size(), f);
    fclose(f);
}

/*
 * Audio decoding.
 */
void gen_audio::decode(const std::string& outfilename, const std::string& filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int out_size, len;
    FILE *f, *outfile;
    uint8_t *outbuf;
    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;

    av_init_packet(&avpkt);

    printf("Audio decoding\n");

    /* find the mpeg audio decoder */
    codec = avcodec_find_decoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context3(codec);

    /* open it */
    if (avcodec_open2(c, codec, nullptr) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    outbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

    f = fopen(filename.c_str(), "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename.c_str());
        exit(1);
    }
    outfile = fopen(outfilename.c_str(), "wb");
    if (!outfile) {
        av_free(c);
        exit(1);
    }

    /* decode until eof */
    avpkt.data = inbuf;
    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

    while (avpkt.size > 0) {
        out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
        len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
        if (len < 0) {
            fprintf(stderr, "Error while decoding\n");
            exit(1);
        }
        if (out_size > 0) {
            /* if a frame has been decoded, output it */
            fwrite(outbuf, 1, out_size, outfile);
        }
        avpkt.size -= len;
        avpkt.data += len;
        if (avpkt.size < AUDIO_REFILL_THRESH) {
            /* Refill the input buffer, to avoid trying to decode
             * incomplete frames. Instead of this, one could also use
             * a parser, or use a proper container format through
             * libavformat. */
            memmove(inbuf, avpkt.data, avpkt.size);
            avpkt.data = inbuf;
            len = fread(avpkt.data + avpkt.size, 1,
                        AUDIO_INBUF_SIZE - avpkt.size, f);
            if (len > 0)
                avpkt.size += len;
        }
    }

    fclose(outfile);
    fclose(f);
    free(outbuf);

    avcodec_close(c);
    av_free(c);
}

vector<string> gen_audio::get_codec_list() {
    vector<string> rc;
    AVCodec* current_codec = av_codec_next(nullptr);
    while (current_codec != NULL)
    {
        if (!av_codec_is_encoder(current_codec))
        {
            current_codec = av_codec_next(current_codec);
            continue;
        }
        rc.push_back(string(current_codec->name));
        current_codec = av_codec_next(current_codec);
    }
    return rc;
}

//int main(int argc, char **argv)
//{
//    const char *filename;

//    /* must be called before using avcodec lib */
////    avcodec_init();

//    /* register all the codecs */
//    avcodec_register_all();

//    if (argc <= 1) {
//        audio_encode_example("/tmp/test.mp2");
//        audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");

//        video_encode_example("/tmp/test.mpg");
//        filename = "/tmp/test.mpg";
//    } else {
//        filename = argv[1];
//    }

//    //    audio_decode_example("/tmp/test.sw", filename);
//    video_decode_example("/tmp/test%d.pgm", filename);

//    return 0;
//}
<commit_msg>add more documentation to gen_audio.cpp<commit_after>/*
 * Copyright (c) 2001 Fabrice Bellard
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "gen_audio.hpp"

extern "C" {
    #include <libavformat/avformat.h>
    #include <libavutil/imgutils.h>
    #include <libswscale/swscale.h>
    #include <libavcodec/avcodec.h>
    #include <libavutil/common.h>
    #include <libavutil/opt.h>
}

// this stuff makes me cranky!
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc  avcodec_alloc_frame
#define av_frame_free   avcodec_free_frame
#endif

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

using namespace std;

gen_audio::gen_audio() :
    r{42}
{
}

vector<unsigned char> gen_audio::render_target( int datumNumber ) {
    std::poisson_distribution<int> _word_count(3);
    std::uniform_int_distribution<int> _word(0,vocab.size()-1);
    vector<unsigned char> rc;

    int word_count = _word_count(r)+1;
    for(int i=0; i<word_count; i++) {
        int word = _word(r);
        string w = vocab[word];
        if( i > 0 ) rc.push_back(' ');
        rc.insert( rc.end(), w.begin(), w.end() );
    }
//    string result((char*)rc.data(),rc.size());
//    cout << "'" << result << "'\n";
    return rc;
}

vector<unsigned char> gen_audio::render_datum( int datumNumber ) {
    int frequency = ((datumNumber % 7) + 1) * 1000;
    return encode(frequency, 2000);
}

/*
 * Audio encoding example
 *
 * generate an mp2 encoded sin wave with `frequencyHz` and length `duration`
 * in milliseconds.
 */
vector<unsigned char> gen_audio::encode(float frequencyHz, int duration) {
    AVCodec *codec;
    AVCodecContext *c= nullptr;
    int frame_size, i, j, out_size, outbuf_size;
    short *samples;
    float t, tincr;
    uint8_t *outbuf;
    vector<unsigned char> rc;

    /* find the MP2 encoder */
    codec = avcodec_find_encoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);

    c->sample_fmt = c->codec->sample_fmts[0];

    /* put sample parameters */
    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;

    /* open it */
    if (avcodec_open2(c, codec, nullptr) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */
    frame_size = c->frame_size;
    float frame_duration = (float)(c->frame_size) / (float)(c->sample_rate);
    int frames = ceil((float)duration / frame_duration / 1000.);
    samples = (short*)malloc(frame_size * 2 * c->channels);
    outbuf_size = 10000;
    outbuf = (uint8_t*)malloc(outbuf_size);

    /* encode a single tone sound */
    t = 0;
    tincr = 2 * M_PI * frequencyHz / c->sample_rate;
    for(i=0;i<frames;i++) {
        for(j=0;j<frame_size;j++) {
            samples[2*j] = (int)(sin(t) * 10000);
            samples[2*j+1] = samples[2*j];
            t += tincr;
        }
        /* encode the samples */
        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
        for(int i=0; i<out_size; i++) { rc.push_back(outbuf[i]); }
    }
    free(outbuf);
    free(samples);

    avcodec_close(c);
    av_free(c);

    return rc;
}

/*
 * Audio encoding example
 *
 * generate an mp2 encoded sin wave with `frequencyHz` and length `duration`
 * in milliseconds.  write audio file to `filename`.
 */
void gen_audio::encode(const std::string& filename, float frequencyHz, int duration)
{
    FILE* f;
    f = fopen(filename.c_str(), "wb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename.c_str());
        exit(1);
    }

    vector<unsigned char> data = encode(frequencyHz,duration);
    fwrite(data.data(), 1, data.size(), f);
    fclose(f);
}

/*
 * Audio decoding.
 */
void gen_audio::decode(const std::string& outfilename, const std::string& filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int out_size, len;
    FILE *f, *outfile;
    uint8_t *outbuf;
    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;

    av_init_packet(&avpkt);

    printf("Audio decoding\n");

    /* find the mpeg audio decoder */
    codec = avcodec_find_decoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context3(codec);

    /* open it */
    if (avcodec_open2(c, codec, nullptr) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    outbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

    f = fopen(filename.c_str(), "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename.c_str());
        exit(1);
    }
    outfile = fopen(outfilename.c_str(), "wb");
    if (!outfile) {
        av_free(c);
        exit(1);
    }

    /* decode until eof */
    avpkt.data = inbuf;
    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

    while (avpkt.size > 0) {
        out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
        len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
        if (len < 0) {
            fprintf(stderr, "Error while decoding\n");
            exit(1);
        }
        if (out_size > 0) {
            /* if a frame has been decoded, output it */
            fwrite(outbuf, 1, out_size, outfile);
        }
        avpkt.size -= len;
        avpkt.data += len;
        if (avpkt.size < AUDIO_REFILL_THRESH) {
            /* Refill the input buffer, to avoid trying to decode
             * incomplete frames. Instead of this, one could also use
             * a parser, or use a proper container format through
             * libavformat. */
            memmove(inbuf, avpkt.data, avpkt.size);
            avpkt.data = inbuf;
            len = fread(avpkt.data + avpkt.size, 1,
                        AUDIO_INBUF_SIZE - avpkt.size, f);
            if (len > 0)
                avpkt.size += len;
        }
    }

    fclose(outfile);
    fclose(f);
    free(outbuf);

    avcodec_close(c);
    av_free(c);
}

vector<string> gen_audio::get_codec_list() {
    vector<string> rc;
    AVCodec* current_codec = av_codec_next(nullptr);
    while (current_codec != NULL)
    {
        if (!av_codec_is_encoder(current_codec))
        {
            current_codec = av_codec_next(current_codec);
            continue;
        }
        rc.push_back(string(current_codec->name));
        current_codec = av_codec_next(current_codec);
    }
    return rc;
}

//int main(int argc, char **argv)
//{
//    const char *filename;

//    /* must be called before using avcodec lib */
////    avcodec_init();

//    /* register all the codecs */
//    avcodec_register_all();

//    if (argc <= 1) {
//        audio_encode_example("/tmp/test.mp2");
//        audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");

//        video_encode_example("/tmp/test.mpg");
//        filename = "/tmp/test.mpg";
//    } else {
//        filename = argv[1];
//    }

//    //    audio_decode_example("/tmp/test.sw", filename);
//    video_decode_example("/tmp/test%d.pgm", filename);

//    return 0;
//}
<|endoftext|>


<commit_before>/**
** Copyright (C) 2010 Aldebaran Robotics
*/

#include <gtest/gtest.h>
#include <vector>
#include <iostream>
#include <cmath>
#include <qi/transport/zeromq/zmqpublisher.hpp>
#include <qi/transport/zeromq/zmqsubscriber.hpp>
#include <qi/perf/sleep.hpp>
#include <boost/timer.hpp>

struct SubscribePerfHandler : qi::transport::ISubscribeHandler {
  int fCount;
  int fExpectedMessages;
  boost::timer timer;

  // conforms to ISubscriberHandler
  void subscribeHandler(const std::string& msg) {
    if (fCount == 0) {
      timer.restart();
    }
    fCount++;
    if (fCount != fExpectedMessages) {
      return;
    }
    // print results
    double elapsed = timer.elapsed();
    double msgPerSecond = 1.0 /(elapsed / fExpectedMessages);
    std::cout << "SUB: msg/s: " <<
      std::setprecision(12) << msgPerSecond << std::endl;
  }

  int getCount() {
    return fCount;
  }

  SubscribePerfHandler() : fCount(0) {}

  SubscribePerfHandler(int expectedMessages) :
    fCount(0),
    fExpectedMessages(expectedMessages) {}
};

TEST(TransportZMQPublisher , MillionPerSecond)
{
  int numMillions = 1;
  int numMessages = numMillions * 1000000;

  SubscribePerfHandler handler(numMessages);
  qi::transport::ZMQPublisher publisher1("tcp://127.0.0.1:5556");
  qi::transport::ZMQPublisher publisher("tcp://127.0.0.1:5555");
  qi::transport::ZMQSubscriber subscriber("tcp://127.0.0.1:5555");

  subscriber.setSubscribeHandler(&handler);
  subscriber.subscribe();
  std::string msg = "Hello";
  sleep(1);
  for (int i=0; i < numMessages; i++) {
    publisher.publish(msg);
  }

  sleep(2);
  int result = handler.getCount();
  ASSERT_EQ( numMessages, result) << "Did not receive all messages";
}

TEST(TransportZMQPublisher , MultipleSubscribers)
{
  int numMessages = 100000;

  qi::transport::ZMQPublisher publisher("tcp://127.0.0.1:5555");

  const int numSubscribers = 50;
  std::vector<SubscribePerfHandler*>         handlers;
  std::vector< qi::transport::ZMQSubscriber*> subscribers;
  boost::shared_ptr<zmq::context_t> subContext(new zmq::context_t(1));
  for (unsigned int i = 0; i < numSubscribers; ++i) {
    SubscribePerfHandler* hand = new SubscribePerfHandler(numMessages);
    qi::transport::ZMQSubscriber* sub = new qi::transport::ZMQSubscriber(subContext, "tcp://127.0.0.1:5555");
    sub->setSubscribeHandler(hand);
    sub->subscribe();
    handlers.push_back(hand);
    subscribers.push_back(sub);
  }
  sleep(1);
  std::string msg = "Hello";

  std::cout << "Publishing...";
  for (int i=0; i < numMessages; i++) {
    publisher.publish(msg);
  }
  std::cout << " Done." << std::endl;
  sleep(1);
  int result = 0;
  for(unsigned int i=0; i < numSubscribers; ++i) {
    result += handlers[i]->getCount();
  }
  ASSERT_EQ( numMessages * numSubscribers, result) << "Did not receive all messages";
}

<commit_msg>publish test fixes for stash<commit_after>/**
** Copyright (C) 2010 Aldebaran Robotics
*/

#include <gtest/gtest.h>
#include <vector>
#include <iostream>
#include <cmath>
#include <qi/transport/zeromq/zmqpublisher.hpp>
#include <qi/transport/zeromq/zmqsubscriber.hpp>
#include <qi/perf/sleep.hpp>
#include <boost/timer.hpp>

struct SubscribePerfHandler : qi::transport::ISubscribeHandler {
  int fCount;
  int fExpectedMessages;
  boost::timer timer;

  // conforms to ISubscriberHandler
  void subscribeHandler(const std::string& msg) {
    if (fCount == 0) {
      timer.restart();
    }
    fCount++;
    if (fCount != fExpectedMessages) {
      return;
    }
    // print results
    double elapsed = timer.elapsed();
    double msgPerSecond = 1.0 /(elapsed / fExpectedMessages);
    std::cout << "SUB: msg/s: " <<
      std::setprecision(12) << msgPerSecond << std::endl;
  }

  int getCount() {
    return fCount;
  }

  SubscribePerfHandler() : fCount(0) {}

  SubscribePerfHandler(int expectedMessages) :
    fCount(0),
    fExpectedMessages(expectedMessages) {}
};

TEST(TransportZMQPublisher , MillionPerSecond)
{
  int numMillions = 1;
  int numMessages = numMillions * 1000000;

  SubscribePerfHandler handler(numMessages);
  //qi::transport::ZMQPublisher publisher1("tcp://127.0.0.1:5556");
  qi::transport::ZMQPublisher publisher("tcp://127.0.0.1:5555");
  qi::transport::ZMQSubscriber subscriber("tcp://127.0.0.1:5555");

  subscriber.setSubscribeHandler(&handler);
  subscriber.subscribe();
  std::string msg = "Hello";
  sleep(1);
  for (int i=0; i < numMessages; i++) {
    publisher.publish(msg);
  }

  sleep(2);
  int result = handler.getCount();
  ASSERT_EQ( numMessages, result) << "Did not receive all messages";
}

TEST(TransportZMQPublisher , MultipleSubscribers)
{
  int numMessages = 100000;

  const int numSubscribers = 50;
  std::vector<SubscribePerfHandler*>         handlers;
  std::vector< qi::transport::ZMQSubscriber*> subscribers;
  //boost::shared_ptr<zmq::context_t> subContext(new zmq::context_t(1));
  for (unsigned int i = 0; i < numSubscribers; ++i) {
    SubscribePerfHandler* hand = new SubscribePerfHandler(numMessages);
    qi::transport::ZMQSubscriber* sub = new qi::transport::ZMQSubscriber(/*subContext, */"tcp://127.0.0.1:5555");
    sub->setSubscribeHandler(hand);
    sub->subscribe();
    handlers.push_back(hand);
    subscribers.push_back(sub);
  }
  sleep(1);
  qi::transport::ZMQPublisher publisher("tcp://127.0.0.1:5555");

  sleep(1);
  std::string msg = "Hello";

  std::cout << "Publishing...";
  for (int i=0; i < numMessages; i++) {
    publisher.publish(msg);
  }
  std::cout << " Done." << std::endl;
  sleep(1);
  int result = 0;
  for(unsigned int i=0; i < numSubscribers; ++i) {
    result += handlers[i]->getCount();
  }
  ASSERT_EQ( numMessages * numSubscribers, result) << "Did not receive all messages";
}

<|endoftext|>


<commit_before>// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "base/logging.h"

#include "base/mutex.h"

using ::std::string;

DEFINE_int32(stderrthreshold,
             LOGLEVEL_ERROR,
             "log messages at or above this level are copied to stderr");
DEFINE_int32(minloglevel,
             LOGLEVEL_INFO,
             "Messages logged at a lower level than this don't actually get "
             "logged anywhere");

namespace internal {

void DefaultLogHandler(LogLevel level, const char* filename, int line,
                       const string& message) {
  static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"};

  // Only log errors above minloglevel.
  if (level >= FLAGS_minloglevel) {
    // We use fprintf() instead of cerr because we want this to work at static
    // initialization time.
    fprintf(stderr, "[lmctfy %s %s:%d] %s\n", level_names[level], filename,
            line, message.c_str());
    fflush(stderr);  // Needed on MSVC.
  }
}

void NullLogHandler(LogLevel level, const char* filename, int line,
                    const string& message) {
  // Nothing.
}

static LogHandler* log_handler_ = &DefaultLogHandler;
static int log_silencer_count_ = 0;

static Mutex* log_silencer_count_mutex_ = nullptr;

Mutex *InitLogSilencerMutex() {
  static Mutex *m = new Mutex();
  return m;
}

void InitLogSilencerCount() {
  log_silencer_count_mutex_ = InitLogSilencerMutex();
}
void InitLogSilencerCountOnce() {
  log_silencer_count_mutex_ = new Mutex();
}

LogMessage& LogMessage::operator<<(const string& value) {
  message_ += value;
  return *this;
}

LogMessage& LogMessage::operator<<(const char* value) {
  message_ += value;
  return *this;
}

// Since this is just for logging, we don't care if the current locale changes
// the results -- in fact, we probably prefer that.  So we use snprintf()
// instead of Simple*toa().
#undef DECLARE_STREAM_OPERATOR
#define DECLARE_STREAM_OPERATOR(TYPE, FORMAT)                       \
  LogMessage& LogMessage::operator<<(TYPE value) {                  \
    /* 128 bytes should be big enough for any of the primitive */   \
    /* values which we print with this, but well use snprintf() */  \
    /* anyway to be extra safe. */                                  \
    char buffer[128];                                               \
    snprintf(buffer, sizeof(buffer), FORMAT, value);                \
    /* Guard against broken MSVC snprintf(). */                     \
    buffer[sizeof(buffer)-1] = '\0';                                \
    message_ += buffer;                                             \
    return *this;                                                   \
  }

DECLARE_STREAM_OPERATOR(char              , "%c"  )
DECLARE_STREAM_OPERATOR(int               , "%d"  )
DECLARE_STREAM_OPERATOR(uint              , "%u"  )
DECLARE_STREAM_OPERATOR(long              , "%ld" )
DECLARE_STREAM_OPERATOR(long long         , "%lld")
DECLARE_STREAM_OPERATOR(unsigned long     , "%lu" )
DECLARE_STREAM_OPERATOR(unsigned long long, "%llu")
DECLARE_STREAM_OPERATOR(double            , "%g"  )
#undef DECLARE_STREAM_OPERATOR

LogMessage::LogMessage(LogLevel level, const char* filename, int line)
  : level_(level), filename_(filename), line_(line) {}
LogMessage::~LogMessage() {}

void LogMessage::Finish() {
  bool suppress = false;

  if (level_ != LOGLEVEL_FATAL) {
    InitLogSilencerCountOnce();
    MutexLock lock(log_silencer_count_mutex_);
    suppress = log_silencer_count_ > 0;
  }

  if (!suppress) {
    log_handler_(level_, filename_, line_, message_);
  }

  if (level_ == LOGLEVEL_FATAL) {
#if PROTOBUF_USE_EXCEPTIONS
    throw FatalException(filename_, line_, message_);
#else
    abort();
#endif
  }
}

void LogFinisher::operator=(LogMessage& other) {
  other.Finish();
}

}  // namespace internal

LogHandler* SetLogHandler(LogHandler* new_func) {
  LogHandler* old = internal::log_handler_;
  if (old == &internal::NullLogHandler) {
    old = NULL;
  }
  if (new_func == NULL) {
    internal::log_handler_ = &internal::NullLogHandler;
  } else {
    internal::log_handler_ = new_func;
  }
  return old;
}

LogSilencer::LogSilencer() {
  internal::InitLogSilencerCountOnce();
  MutexLock lock(internal::log_silencer_count_mutex_);
  ++internal::log_silencer_count_;
};

LogSilencer::~LogSilencer() {
  internal::InitLogSilencerCountOnce();
  MutexLock lock(internal::log_silencer_count_mutex_);
  --internal::log_silencer_count_;
};

static int posix_strerror_r(int err, char *buf, size_t len) {
  // Sanity check input parameters
  if (buf == NULL || len <= 0) {
    errno = EINVAL;
    return -1;
  }

  // Reset buf and errno, and try calling whatever version of strerror_r()
  // is implemented by glibc
  buf[0] = '\000';
  int old_errno = errno;
  errno = 0;
  char* rc = reinterpret_cast<char*>(strerror_r(err, buf, len));

  // Both versions set errno on failure
  if (errno) {
    // Should already be there, but better safe than sorry
    buf[0] = '\000';
    return -1;
  }
  errno = old_errno;

  // If the function succeeded, we can use its exit code to determine the
  // semantics implemented by glibc
  if (!rc) {
    // POSIX is vague about whether the string will be terminated, although
    // is indirectly implies that typically ERANGE will be returned, instead
    // of truncating the string. This is different from the GNU implementation.
    // We play it safe by always terminating the string explicitly.
    buf[len - 1] = '\000';
    return 0;
  } else {
    // GNU semantics detected
    if (rc == buf) {
      return 0;
    } else {
      buf[0] = '\000';
      strncat(buf, rc, len - 1);
      return 0;
    }
  }
}

string StrError(int err) {
  char buf[100];
  int rc = posix_strerror_r(err, buf, sizeof(buf));
  if ((rc < 0) || (buf[0] == '\000')) {
    snprintf(buf, sizeof(buf), "Error number %d", err);
  }
  return buf;
}
<commit_msg>Fix a memory leak in logging<commit_after>// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "base/logging.h"

#include "base/mutex.h"

using ::std::string;

DEFINE_int32(stderrthreshold,
             LOGLEVEL_ERROR,
             "log messages at or above this level are copied to stderr");
DEFINE_int32(minloglevel,
             LOGLEVEL_INFO,
             "Messages logged at a lower level than this don't actually get "
             "logged anywhere");

namespace internal {

void DefaultLogHandler(LogLevel level, const char* filename, int line,
                       const string& message) {
  static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"};

  // Only log errors above minloglevel.
  if (level >= FLAGS_minloglevel) {
    // We use fprintf() instead of cerr because we want this to work at static
    // initialization time.
    fprintf(stderr, "[lmctfy %s %s:%d] %s\n", level_names[level], filename,
            line, message.c_str());
    fflush(stderr);  // Needed on MSVC.
  }
}

void NullLogHandler(LogLevel level, const char* filename, int line,
                    const string& message) {
  // Nothing.
}

static LogHandler* log_handler_ = &DefaultLogHandler;
static int log_silencer_count_ = 0;

static Mutex* log_silencer_count_mutex_ = nullptr;

Mutex *InitLogSilencerMutex() {
  static Mutex *m = new Mutex();
  return m;
}

void InitLogSilencerCount() {
  log_silencer_count_mutex_ = InitLogSilencerMutex();
}

LogMessage& LogMessage::operator<<(const string& value) {
  message_ += value;
  return *this;
}

LogMessage& LogMessage::operator<<(const char* value) {
  message_ += value;
  return *this;
}

// Since this is just for logging, we don't care if the current locale changes
// the results -- in fact, we probably prefer that.  So we use snprintf()
// instead of Simple*toa().
#undef DECLARE_STREAM_OPERATOR
#define DECLARE_STREAM_OPERATOR(TYPE, FORMAT)                       \
  LogMessage& LogMessage::operator<<(TYPE value) {                  \
    /* 128 bytes should be big enough for any of the primitive */   \
    /* values which we print with this, but well use snprintf() */  \
    /* anyway to be extra safe. */                                  \
    char buffer[128];                                               \
    snprintf(buffer, sizeof(buffer), FORMAT, value);                \
    /* Guard against broken MSVC snprintf(). */                     \
    buffer[sizeof(buffer)-1] = '\0';                                \
    message_ += buffer;                                             \
    return *this;                                                   \
  }

DECLARE_STREAM_OPERATOR(char              , "%c"  )
DECLARE_STREAM_OPERATOR(int               , "%d"  )
DECLARE_STREAM_OPERATOR(uint              , "%u"  )
DECLARE_STREAM_OPERATOR(long              , "%ld" )
DECLARE_STREAM_OPERATOR(long long         , "%lld")
DECLARE_STREAM_OPERATOR(unsigned long     , "%lu" )
DECLARE_STREAM_OPERATOR(unsigned long long, "%llu")
DECLARE_STREAM_OPERATOR(double            , "%g"  )
#undef DECLARE_STREAM_OPERATOR

LogMessage::LogMessage(LogLevel level, const char* filename, int line)
  : level_(level), filename_(filename), line_(line) {}
LogMessage::~LogMessage() {}

void LogMessage::Finish() {
  bool suppress = false;

  if (level_ != LOGLEVEL_FATAL) {
    InitLogSilencerCount();
    MutexLock lock(log_silencer_count_mutex_);
    suppress = log_silencer_count_ > 0;
  }

  if (!suppress) {
    log_handler_(level_, filename_, line_, message_);
  }

  if (level_ == LOGLEVEL_FATAL) {
#if PROTOBUF_USE_EXCEPTIONS
    throw FatalException(filename_, line_, message_);
#else
    abort();
#endif
  }
}

void LogFinisher::operator=(LogMessage& other) {
  other.Finish();
}

}  // namespace internal

LogHandler* SetLogHandler(LogHandler* new_func) {
  LogHandler* old = internal::log_handler_;
  if (old == &internal::NullLogHandler) {
    old = NULL;
  }
  if (new_func == NULL) {
    internal::log_handler_ = &internal::NullLogHandler;
  } else {
    internal::log_handler_ = new_func;
  }
  return old;
}

LogSilencer::LogSilencer() {
  internal::InitLogSilencerCount();
  MutexLock lock(internal::log_silencer_count_mutex_);
  ++internal::log_silencer_count_;
};

LogSilencer::~LogSilencer() {
  internal::InitLogSilencerCount();
  MutexLock lock(internal::log_silencer_count_mutex_);
  --internal::log_silencer_count_;
};

static int posix_strerror_r(int err, char *buf, size_t len) {
  // Sanity check input parameters
  if (buf == NULL || len <= 0) {
    errno = EINVAL;
    return -1;
  }

  // Reset buf and errno, and try calling whatever version of strerror_r()
  // is implemented by glibc
  buf[0] = '\000';
  int old_errno = errno;
  errno = 0;
  char* rc = reinterpret_cast<char*>(strerror_r(err, buf, len));

  // Both versions set errno on failure
  if (errno) {
    // Should already be there, but better safe than sorry
    buf[0] = '\000';
    return -1;
  }
  errno = old_errno;

  // If the function succeeded, we can use its exit code to determine the
  // semantics implemented by glibc
  if (!rc) {
    // POSIX is vague about whether the string will be terminated, although
    // is indirectly implies that typically ERANGE will be returned, instead
    // of truncating the string. This is different from the GNU implementation.
    // We play it safe by always terminating the string explicitly.
    buf[len - 1] = '\000';
    return 0;
  } else {
    // GNU semantics detected
    if (rc == buf) {
      return 0;
    } else {
      buf[0] = '\000';
      strncat(buf, rc, len - 1);
      return 0;
    }
  }
}

string StrError(int err) {
  char buf[100];
  int rc = posix_strerror_r(err, buf, sizeof(buf));
  if ((rc < 0) || (buf[0] == '\000')) {
    snprintf(buf, sizeof(buf), "Error number %d", err);
  }
  return buf;
}
<|endoftext|>


<commit_before>#include <cstdlib>
#include <iostream>

#include <ioa.hpp>
#include <queue>
#include <uuid/uuid.h>

#include <boost/type_traits.hpp>

struct uuid {
  uuid_t u;

  uuid () {
    uuid_generate (u);
  }

  uuid (const uuid& x) {
    uuid_copy (u, x.u);
  }

  uuid& operator= (const uuid& x) {
    if (this != &x) {
      uuid_copy (u, x.u);
    }
    return *this;
  }

  bool operator> (const uuid& x) const {
    return uuid_compare (u, x.u) > 0;
  }

  bool operator== (const uuid& x) const {
    return uuid_compare (u, x.u) == 0;
  }

  void print_on (std::ostream& os) const {
    char s[37];
    uuid_unparse (u, s);
    os << s;
  }
};

std::ostream& operator<< (std::ostream& strm,
			  const uuid& u) {
  u.print_on (strm);
  return strm;
}

/*
  Channel I/O Automaton
  Distributed Algorithms, p. 204.
*/

template <class T>
class channel_automaton :
  public ioa::dispatching_automaton
{
private:
  std::queue<T> m_queue;

  V_UP_INPUT (channel_automaton, send, T, t) {
    m_queue.push (t);
    schedule ();
  }

  bool receive_precondition () const {
    return !m_queue.empty () && receive.is_bound ();
  }

  V_UP_OUTPUT (channel_automaton, receive, T) {
    std::pair<bool, T> retval;

    if (receive_precondition ()) {
      retval = std::make_pair (true, m_queue.front ());
      m_queue.pop ();
    }

    schedule ();
    return retval;
  }

  void schedule () {
    if (receive_precondition ()) {
      ioa::scheduler.schedule (this, &channel_automaton::receive);
    }
  }

public:

  channel_automaton () :
    ACTION (channel_automaton, send),
    ACTION (channel_automaton, receive)
  { }

  void init () { }
};

/*
  AsyncLCR Automaton
  Distributed Algorithms, p. 204.
*/

class asynch_lcr_automaton :
  public ioa::dispatching_automaton
{
private:
  typedef enum {
    UNKNOWN,
    CHOSEN,
    REPORTED
  } status_t;

  uuid m_u;
  std::queue<uuid> m_send;
  status_t m_status;

  V_UP_INPUT (asynch_lcr_automaton, receive, uuid, v) {

    if (v > m_u) {
      m_send.push (v);
    }
    else if (v == m_u) {
      m_status = CHOSEN;
    }
    else {
      // Do nothing.
    }

    schedule ();
  }

  bool send_precondition () const {
    return !m_send.empty () && send.is_bound ();
  }

  V_UP_OUTPUT (asynch_lcr_automaton, send, uuid) {

    std::pair<bool, uuid> retval;

    if (send_precondition ()) {
      retval = std::make_pair (true, m_send.front ());
      m_send.pop ();
    }

    schedule ();
    return retval;
  }

  bool leader_precondition () const {
    return m_status == CHOSEN && leader.is_bound ();
  }

  UV_UP_OUTPUT (asynch_lcr_automaton, leader) {
    bool retval = false;

    if (leader_precondition ()) {
      retval = true;
      m_status = REPORTED;
    }

    schedule ();
    return retval;
  }

  void schedule () {
    if (send_precondition ()) {
      ioa::scheduler.schedule (this, &asynch_lcr_automaton::send);
    }

    if (leader_precondition ()) {
      ioa::scheduler.schedule (this, &asynch_lcr_automaton::leader);
    }
  }

public:

  asynch_lcr_automaton () :
    m_status (UNKNOWN),
    ACTION (asynch_lcr_automaton, receive),
    ACTION (asynch_lcr_automaton, send),
    ACTION (asynch_lcr_automaton, leader)
  {
    m_send.push (m_u);
  }

  void init () {
    schedule ();
  }

};

template <size_t N>
class composer :
  public ioa::dispatching_automaton
{
private:

  UV_P_INPUT (composer, leader, size_t, i) {
    std::cout << i << " is the leader." << std::endl;
  }

  typedef ioa::instance_generator<asynch_lcr_automaton> asynch_lcr_automaton_generator_type;
  typedef ioa::instance_generator<channel_automaton<uuid> > channel_automaton_generator_type;

  typedef ioa::automaton_helper<composer, asynch_lcr_automaton_generator_type> asynch_lcr_automaton_helper_type;
  typedef ioa::automaton_helper<composer, channel_automaton_generator_type> channel_automaton_helper_type;
  typedef ioa::self_helper<composer> composer_helper_type;

  typedef ioa::bind_helper<composer, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::send_type, channel_automaton_helper_type, channel_automaton<uuid>::send_type> send_bind_helper_type;
  typedef ioa::bind_helper<composer, channel_automaton_helper_type, channel_automaton<uuid>::receive_type, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::receive_type> receive_bind_helper_type;
  typedef ioa::bind_helper<composer, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::leader_type, composer_helper_type, typename composer::leader_type> leader_bind_helper_type;

  std::vector<asynch_lcr_automaton_helper_type*> asynch_lcr_automaton_helpers;
  std::vector<channel_automaton_helper_type*> channel_automaton_helpers;
  composer_helper_type composer_helper;
  std::vector<send_bind_helper_type*> send_bind_helpers;
  std::vector<receive_bind_helper_type*> receive_bind_helpers;
  std::vector<leader_bind_helper_type*> leader_bind_helpers;

public:

  composer () :
    ACTION (composer, leader),
    composer_helper (this)
  {
    for (size_t i = 0; i < N; ++i) {
      asynch_lcr_automaton_helpers.push_back (new asynch_lcr_automaton_helper_type (this, asynch_lcr_automaton_generator_type ()));
      channel_automaton_helpers.push_back (new channel_automaton_helper_type (this, channel_automaton_generator_type ()));
    }

    for (size_t i = 0; i < N; ++i) {
      send_bind_helpers.push_back (new send_bind_helper_type (this,
							      asynch_lcr_automaton_helpers[i],
							      &asynch_lcr_automaton::send,
							      channel_automaton_helpers[i],
							      &channel_automaton<uuid>::send));
      receive_bind_helpers.push_back (new receive_bind_helper_type (this,
								    channel_automaton_helpers[i],
								    &channel_automaton<uuid>::receive,
								    asynch_lcr_automaton_helpers[(i + 1) % N],
								    &asynch_lcr_automaton::receive));
      leader_bind_helpers.push_back (new leader_bind_helper_type (this,
								  asynch_lcr_automaton_helpers[i],
								  &asynch_lcr_automaton::leader,
								  &composer_helper,
								  &composer::leader,
								  i));
    }

  }

  void init () {
    for (size_t i = 0; i < N; ++i) {
      asynch_lcr_automaton_helpers[i]->create ();
      channel_automaton_helpers[i]->create ();
      send_bind_helpers[i]->bind ();
      receive_bind_helpers[i]->bind ();
      leader_bind_helpers[i]->bind ();
    }
    composer_helper.create ();
  }

};

int
main () {
  ioa::scheduler.run (ioa::instance_generator<composer<100> > ());
  return 0;
}
<commit_msg>Minor changes.<commit_after>#include <ioa.hpp>
#include <queue>
#include <iostream>
#include <uuid/uuid.h>

struct uuid {
  uuid_t u;

  uuid () {
    uuid_generate (u);
  }

  uuid (const uuid& x) {
    uuid_copy (u, x.u);
  }

  uuid& operator= (const uuid& x) {
    if (this != &x) {
      uuid_copy (u, x.u);
    }
    return *this;
  }

  bool operator> (const uuid& x) const {
    return uuid_compare (u, x.u) > 0;
  }

  bool operator== (const uuid& x) const {
    return uuid_compare (u, x.u) == 0;
  }

  void print_on (std::ostream& os) const {
    char s[37];
    uuid_unparse (u, s);
    os << s;
  }
};

std::ostream& operator<< (std::ostream& strm,
			  const uuid& u) {
  u.print_on (strm);
  return strm;
}

/*
  Channel I/O Automaton
  Distributed Algorithms, p. 204.
*/

template <class T>
class channel_automaton :
  public ioa::dispatching_automaton
{
private:
  std::queue<T> m_queue;

  V_UP_INPUT (channel_automaton, send, T, t) {
    m_queue.push (t);
    schedule ();
  }

  bool receive_precondition () const {
    return !m_queue.empty () && receive.is_bound ();
  }

  V_UP_OUTPUT (channel_automaton, receive, T) {
    std::pair<bool, T> retval;

    if (receive_precondition ()) {
      retval = std::make_pair (true, m_queue.front ());
      m_queue.pop ();
    }

    schedule ();
    return retval;
  }

  void schedule () {
    if (receive_precondition ()) {
      ioa::scheduler.schedule (this, &channel_automaton::receive);
    }
  }

public:

  channel_automaton () :
    ACTION (channel_automaton, send),
    ACTION (channel_automaton, receive)
  { }

  void init () { }
};

/*
  AsyncLCR Automaton
  Distributed Algorithms, p. 204.
*/

class asynch_lcr_automaton :
  public ioa::dispatching_automaton
{
private:
  typedef enum {
    UNKNOWN,
    CHOSEN,
    REPORTED
  } status_t;

  uuid m_u;
  std::queue<uuid> m_send;
  status_t m_status;

  V_UP_INPUT (asynch_lcr_automaton, receive, uuid, v) {

    if (v > m_u) {
      m_send.push (v);
    }
    else if (v == m_u) {
      m_status = CHOSEN;
    }
    else {
      // Do nothing.
    }

    schedule ();
  }

  bool send_precondition () const {
    return !m_send.empty () && send.is_bound ();
  }

  V_UP_OUTPUT (asynch_lcr_automaton, send, uuid) {

    std::pair<bool, uuid> retval;

    if (send_precondition ()) {
      retval = std::make_pair (true, m_send.front ());
      m_send.pop ();
    }

    schedule ();
    return retval;
  }

  bool leader_precondition () const {
    return m_status == CHOSEN && leader.is_bound ();
  }

  UV_UP_OUTPUT (asynch_lcr_automaton, leader) {
    bool retval = false;

    if (leader_precondition ()) {
      retval = true;
      m_status = REPORTED;
    }

    schedule ();
    return retval;
  }

  void schedule () {
    if (send_precondition ()) {
      ioa::scheduler.schedule (this, &asynch_lcr_automaton::send);
    }

    if (leader_precondition ()) {
      ioa::scheduler.schedule (this, &asynch_lcr_automaton::leader);
    }
  }

public:

  asynch_lcr_automaton () :
    m_status (UNKNOWN),
    ACTION (asynch_lcr_automaton, receive),
    ACTION (asynch_lcr_automaton, send),
    ACTION (asynch_lcr_automaton, leader)
  {
    m_send.push (m_u);
  }

  void init () {
    schedule ();
  }

};

template <size_t N>
class composer :
  public ioa::dispatching_automaton
{
private:

  UV_P_INPUT (composer, leader, size_t, i) {
    std::cout << i << " is the leader." << std::endl;
  }

  typedef ioa::instance_generator<asynch_lcr_automaton> asynch_lcr_automaton_generator_type;
  typedef ioa::instance_generator<channel_automaton<uuid> > channel_automaton_generator_type;

  typedef ioa::automaton_helper<composer, asynch_lcr_automaton_generator_type> asynch_lcr_automaton_helper_type;
  typedef ioa::automaton_helper<composer, channel_automaton_generator_type> channel_automaton_helper_type;
  typedef ioa::self_helper<composer> composer_helper_type;

  typedef ioa::bind_helper<composer, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::send_type, channel_automaton_helper_type, channel_automaton<uuid>::send_type> send_bind_helper_type;
  typedef ioa::bind_helper<composer, channel_automaton_helper_type, channel_automaton<uuid>::receive_type, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::receive_type> receive_bind_helper_type;
  typedef ioa::bind_helper<composer, asynch_lcr_automaton_helper_type, asynch_lcr_automaton::leader_type, composer_helper_type, typename composer::leader_type> leader_bind_helper_type;

  std::vector<asynch_lcr_automaton_helper_type*> asynch_lcr_automaton_helpers;
  std::vector<channel_automaton_helper_type*> channel_automaton_helpers;
  composer_helper_type composer_helper;
  std::vector<send_bind_helper_type*> send_bind_helpers;
  std::vector<receive_bind_helper_type*> receive_bind_helpers;
  std::vector<leader_bind_helper_type*> leader_bind_helpers;

public:

  composer () :
    ACTION (composer, leader),
    composer_helper (this)
  {
    for (size_t i = 0; i < N; ++i) {
      asynch_lcr_automaton_helpers.push_back (new asynch_lcr_automaton_helper_type (this, asynch_lcr_automaton_generator_type ()));
      channel_automaton_helpers.push_back (new channel_automaton_helper_type (this, channel_automaton_generator_type ()));
    }

    for (size_t i = 0; i < N; ++i) {
      send_bind_helpers.push_back (new send_bind_helper_type (this,
							      asynch_lcr_automaton_helpers[i],
							      &asynch_lcr_automaton::send,
							      channel_automaton_helpers[i],
							      &channel_automaton<uuid>::send));
      receive_bind_helpers.push_back (new receive_bind_helper_type (this,
								    channel_automaton_helpers[i],
								    &channel_automaton<uuid>::receive,
								    asynch_lcr_automaton_helpers[(i + 1) % N],
								    &asynch_lcr_automaton::receive));
      leader_bind_helpers.push_back (new leader_bind_helper_type (this,
								  asynch_lcr_automaton_helpers[i],
								  &asynch_lcr_automaton::leader,
								  &composer_helper,
								  &composer::leader,
								  i));
    }

  }

  void init () {
    for (size_t i = 0; i < N; ++i) {
      asynch_lcr_automaton_helpers[i]->create ();
      channel_automaton_helpers[i]->create ();
      send_bind_helpers[i]->bind ();
      receive_bind_helpers[i]->bind ();
      leader_bind_helpers[i]->bind ();
    }
    composer_helper.create ();
  }

};

int
main () {
  ioa::scheduler.run (ioa::instance_generator<composer<100> > ());
  return 0;
}
<|endoftext|>


<commit_before>/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkImagePCAShapeModelEstimatorTest.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Insight classes
#include "itkImage.h"
#include "itkVector.h"
#include "vnl/vnl_matrix_fixed.h"
#include "vnl/vnl_math.h"
#include "itkImageRegionIterator.h"
#include "itkLightProcessObject.h"
#include "itkTextOutput.h"

#include "itkImagePCAShapeModelEstimator.h"

// class to support progress feeback

class ShowProgressObject
{
public:
  ShowProgressObject(itk::LightProcessObject * o)
    {m_Process = o;}
  void ShowProgress()
    {std::cout << "Progress " << m_Process->GetProgress() << std::endl;}
  itk::LightProcessObject::Pointer m_Process;
};

int itkImagePCAShapeModelEstimatorTest(int, char* [] )
{
  //Data definitions
  int IMGWIDTH          = 2;
  int IMGHEIGHT         = 2;
  const int NDIMENSION  = 2;
  int NUMTRAINIMAGES    = 3;
  int NUMLARGESTPC      = 2;

  itk::OutputWindow::SetInstance(itk::TextOutput::New().GetPointer());

  //------------------------------------------------------
  //Create 3 simple test images with
  //------------------------------------------------------
  typedef itk::Image<double,NDIMENSION> InputImageType;
  typedef itk::Image<double,NDIMENSION> OutputImageType;
  typedef itk::Image<double,NDIMENSION> MeanImageType;

  typedef InputImageType::PixelType ImagePixelType;

  typedef InputImageType::PixelType InputImagePixelType;

  typedef
    itk::ImageRegionIterator< InputImageType > InputImageIterator;

  typedef
    itk::ImageRegionIterator< OutputImageType > OutputImageIterator;

  InputImageType::Pointer image1 = InputImageType::New();

  InputImageType::Pointer image2 = InputImageType::New();

  InputImageType::Pointer image3 = InputImageType::New();

  InputImageType::SizeType inputImageSize = {{ IMGWIDTH, IMGHEIGHT }};

  InputImageType::IndexType index;
  index.Fill(0);
  InputImageType::RegionType region;

  region.SetSize( inputImageSize );
  region.SetIndex( index );

  //------------------------------------------------------------------------
  // Set up Image 1 first
  //------------------------------------------------------------------------

  image1->SetLargestPossibleRegion( region );
  image1->SetBufferedRegion( region );
  image1->Allocate();

  // setup the iterators
  InputImageIterator image1It( image1, image1->GetBufferedRegion() );

  //------------------------------------------------------------------------
  // Set up Image 2 first
  //------------------------------------------------------------------------

  image2->SetLargestPossibleRegion( region );
  image2->SetBufferedRegion( region );
  image2->Allocate();

  // setup the iterators
  InputImageIterator image2It( image2, image2->GetBufferedRegion() );

  //------------------------------------------------------------------------
  // Set up Image 3 first
  //------------------------------------------------------------------------

  image3->SetLargestPossibleRegion( region );
  image3->SetBufferedRegion( region );
  image3->Allocate();

  // setup the iterators
  InputImageIterator image3It( image3, image3->GetBufferedRegion() );

  //------------------------------------------------------------------------
  //Manually create and store each vector
  //------------------------------------------------------------------------
  //Image no. 1
  for( int i = 0; i< 4; i++ )
    {
    image1It.Set( 1 ); ++image1It;
    }
  //Image no. 2
  image2It.Set( 2 ); ++image2It;
  image2It.Set( 0 ); ++image2It;
  image2It.Set( 0 ); ++image2It;
  image2It.Set( 2 ); ++image2It;

  //Image no. 3
  image3It.Set( 0 ); ++image3It;
  image3It.Set( 3 ); ++image3It;
  image3It.Set( 3 ); ++image3It;
  image3It.Set( 0 ); ++image3It;

  //----------------------------------------------------------------------
  // Test code for the Shape model estimator
  //----------------------------------------------------------------------

  //----------------------------------------------------------------------
  //Set the image model estimator
  //----------------------------------------------------------------------
  typedef itk::ImagePCAShapeModelEstimator<InputImageType, OutputImageType>
    ImagePCAShapeModelEstimatorType;

  ImagePCAShapeModelEstimatorType::Pointer
    applyPCAShapeEstimator = ImagePCAShapeModelEstimatorType::New();

  //----------------------------------------------------------------------
  //Set the parameters of the clusterer
  //----------------------------------------------------------------------
  applyPCAShapeEstimator->SetNumberOfTrainingImages( NUMTRAINIMAGES );
  applyPCAShapeEstimator->SetNumberOfPrincipalComponentsRequired( NUMLARGESTPC + 1 );
  applyPCAShapeEstimator->SetNumberOfPrincipalComponentsRequired( NUMLARGESTPC );
  applyPCAShapeEstimator->SetInput(0, image1);
  applyPCAShapeEstimator->SetInput(1, image2);
  applyPCAShapeEstimator->SetInput(2, image3);

  applyPCAShapeEstimator->Update();

  //Test the printself function to increase coverage
  applyPCAShapeEstimator->Print(std::cout);

  //Exercise TypeMacro in superclass
  typedef ImagePCAShapeModelEstimatorType::Superclass GenericEstimatorType;
  std::cout << applyPCAShapeEstimator->GenericEstimatorType::GetNameOfClass() << std::endl;

  //Print out the number of training images and the number of principal
  //components
  std::cout << "The number of training images are: " <<
    applyPCAShapeEstimator->GetNumberOfTrainingImages() << std::endl;

  std::cout << "The number of principal components desired are: " <<
    applyPCAShapeEstimator->GetNumberOfPrincipalComponentsRequired() << std::endl;

  //Print the eigen vectors
  vnl_vector<double> eigenValues =
    applyPCAShapeEstimator->GetEigenValues();
  unsigned int numEigVal =  eigenValues.size();
  std::cout << "Number of returned eign-values: " << numEigVal << std::endl;

  std::cout << "The " <<
    applyPCAShapeEstimator->GetNumberOfPrincipalComponentsRequired() <<
    " largest eigen values are:" << std::endl;

  for(unsigned int i= 0; i< vnl_math_min( numEigVal, (unsigned int)NUMLARGESTPC ); i++ )
    {
    std::cout << eigenValues[ i ] << std::endl;
    }
  std::cout << "" << std::endl;
  std::cout << "" << std::endl;

  //Print the MeanImage
  OutputImageType::Pointer outImage = applyPCAShapeEstimator->GetOutput( 0 );
  OutputImageIterator outImageIt( outImage, outImage->GetBufferedRegion() );
  outImageIt.GoToBegin();

  std::cout << "The mean image is:" << std::endl;
  while(!outImageIt.IsAtEnd() )
    {
    std::cout << (double)(outImageIt.Get()) << ";"  << std::endl;
    ++outImageIt;
    }
  std::cout << "  " << std::endl;

  //Print the largest two eigen vectors
  for (unsigned int j=1; j< NUMLARGESTPC + 1; j++ )
    {
    OutputImageType::Pointer outImage2 = applyPCAShapeEstimator->GetOutput( j );
    OutputImageIterator outImage2It( outImage2, outImage2->GetBufferedRegion() );
    outImage2It.GoToBegin();

    std::cout << "" << std::endl;
    std::cout << "The eigen vector number: " << j << " is:" << std::endl;
    while(!outImage2It.IsAtEnd() )
      {
      std::cout << (double) (outImage2It.Get()) << ";"  << std::endl;
      ++outImage2It;
      }
    std::cout << "  " << std::endl;

    }

  //Test for the eigen values for the test case precomputed using Matlab/Splus
  std::cout << "" << std::endl;
  if( (eigenValues[2] < 6 || eigenValues[2] > 6.1) || (eigenValues[1] >0.1) )
    {
    std::cout<< "Test Passed" << std::endl;
    }
  else
    {
    std::cout<< "Test failed" << std::endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}
<commit_msg>COMP: fix warning NUMLARGESTPC is an unsigned int<commit_after>/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkImagePCAShapeModelEstimatorTest.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Insight classes
#include "itkImage.h"
#include "itkVector.h"
#include "vnl/vnl_matrix_fixed.h"
#include "vnl/vnl_math.h"
#include "itkImageRegionIterator.h"
#include "itkLightProcessObject.h"
#include "itkTextOutput.h"

#include "itkImagePCAShapeModelEstimator.h"

// class to support progress feeback

class ShowProgressObject
{
public:
  ShowProgressObject(itk::LightProcessObject * o)
    {m_Process = o;}
  void ShowProgress()
    {std::cout << "Progress " << m_Process->GetProgress() << std::endl;}
  itk::LightProcessObject::Pointer m_Process;
};

int itkImagePCAShapeModelEstimatorTest(int, char* [] )
{
  //Data definitions
  int IMGWIDTH          = 2;
  int IMGHEIGHT         = 2;
  const int NDIMENSION  = 2;
  unsigned int NUMTRAINIMAGES = 3;
  unsigned int NUMLARGESTPC = 2;

  itk::OutputWindow::SetInstance(itk::TextOutput::New().GetPointer());

  //------------------------------------------------------
  //Create 3 simple test images with
  //------------------------------------------------------
  typedef itk::Image<double,NDIMENSION> InputImageType;
  typedef itk::Image<double,NDIMENSION> OutputImageType;
  typedef itk::Image<double,NDIMENSION> MeanImageType;

  typedef InputImageType::PixelType ImagePixelType;

  typedef InputImageType::PixelType InputImagePixelType;

  typedef
    itk::ImageRegionIterator< InputImageType > InputImageIterator;

  typedef
    itk::ImageRegionIterator< OutputImageType > OutputImageIterator;

  InputImageType::Pointer image1 = InputImageType::New();

  InputImageType::Pointer image2 = InputImageType::New();

  InputImageType::Pointer image3 = InputImageType::New();

  InputImageType::SizeType inputImageSize = {{ IMGWIDTH, IMGHEIGHT }};

  InputImageType::IndexType index;
  index.Fill(0);
  InputImageType::RegionType region;

  region.SetSize( inputImageSize );
  region.SetIndex( index );

  //------------------------------------------------------------------------
  // Set up Image 1 first
  //------------------------------------------------------------------------

  image1->SetLargestPossibleRegion( region );
  image1->SetBufferedRegion( region );
  image1->Allocate();

  // setup the iterators
  InputImageIterator image1It( image1, image1->GetBufferedRegion() );

  //------------------------------------------------------------------------
  // Set up Image 2 first
  //------------------------------------------------------------------------

  image2->SetLargestPossibleRegion( region );
  image2->SetBufferedRegion( region );
  image2->Allocate();

  // setup the iterators
  InputImageIterator image2It( image2, image2->GetBufferedRegion() );

  //------------------------------------------------------------------------
  // Set up Image 3 first
  //------------------------------------------------------------------------

  image3->SetLargestPossibleRegion( region );
  image3->SetBufferedRegion( region );
  image3->Allocate();

  // setup the iterators
  InputImageIterator image3It( image3, image3->GetBufferedRegion() );

  //------------------------------------------------------------------------
  //Manually create and store each vector
  //------------------------------------------------------------------------
  //Image no. 1
  for( int i = 0; i< 4; i++ )
    {
    image1It.Set( 1 ); ++image1It;
    }
  //Image no. 2
  image2It.Set( 2 ); ++image2It;
  image2It.Set( 0 ); ++image2It;
  image2It.Set( 0 ); ++image2It;
  image2It.Set( 2 ); ++image2It;

  //Image no. 3
  image3It.Set( 0 ); ++image3It;
  image3It.Set( 3 ); ++image3It;
  image3It.Set( 3 ); ++image3It;
  image3It.Set( 0 ); ++image3It;

  //----------------------------------------------------------------------
  // Test code for the Shape model estimator
  //----------------------------------------------------------------------

  //----------------------------------------------------------------------
  //Set the image model estimator
  //----------------------------------------------------------------------
  typedef itk::ImagePCAShapeModelEstimator<InputImageType, OutputImageType>
    ImagePCAShapeModelEstimatorType;

  ImagePCAShapeModelEstimatorType::Pointer
    applyPCAShapeEstimator = ImagePCAShapeModelEstimatorType::New();

  //----------------------------------------------------------------------
  //Set the parameters of the clusterer
  //----------------------------------------------------------------------
  applyPCAShapeEstimator->SetNumberOfTrainingImages( NUMTRAINIMAGES );
  applyPCAShapeEstimator->SetNumberOfPrincipalComponentsRequired( NUMLARGESTPC + 1 );
  applyPCAShapeEstimator->SetNumberOfPrincipalComponentsRequired( NUMLARGESTPC );
  applyPCAShapeEstimator->SetInput(0, image1);
  applyPCAShapeEstimator->SetInput(1, image2);
  applyPCAShapeEstimator->SetInput(2, image3);

  applyPCAShapeEstimator->Update();

  //Test the printself function to increase coverage
  applyPCAShapeEstimator->Print(std::cout);

  //Exercise TypeMacro in superclass
  typedef ImagePCAShapeModelEstimatorType::Superclass GenericEstimatorType;
  std::cout << applyPCAShapeEstimator->GenericEstimatorType::GetNameOfClass() << std::endl;

  //Print out the number of training images and the number of principal
  //components
  std::cout << "The number of training images are: " <<
    applyPCAShapeEstimator->GetNumberOfTrainingImages() << std::endl;

  std::cout << "The number of principal components desired are: " <<
    applyPCAShapeEstimator->GetNumberOfPrincipalComponentsRequired() << std::endl;

  //Print the eigen vectors
  vnl_vector<double> eigenValues =
    applyPCAShapeEstimator->GetEigenValues();
  unsigned int numEigVal =  eigenValues.size();
  std::cout << "Number of returned eign-values: " << numEigVal << std::endl;

  std::cout << "The " <<
    applyPCAShapeEstimator->GetNumberOfPrincipalComponentsRequired() <<
    " largest eigen values are:" << std::endl;

  for(unsigned int i= 0; i< vnl_math_min( numEigVal, NUMLARGESTPC ); i++ )
    {
    std::cout << eigenValues[ i ] << std::endl;
    }
  std::cout << "" << std::endl;
  std::cout << "" << std::endl;

  //Print the MeanImage
  OutputImageType::Pointer outImage = applyPCAShapeEstimator->GetOutput( 0 );
  OutputImageIterator outImageIt( outImage, outImage->GetBufferedRegion() );
  outImageIt.GoToBegin();

  std::cout << "The mean image is:" << std::endl;
  while(!outImageIt.IsAtEnd() )
    {
    std::cout << (double)(outImageIt.Get()) << ";"  << std::endl;
    ++outImageIt;
    }
  std::cout << "  " << std::endl;

  //Print the largest two eigen vectors
  for (unsigned int j=1; j< NUMLARGESTPC + 1; j++ )
    {
    OutputImageType::Pointer outImage2 = applyPCAShapeEstimator->GetOutput( j );
    OutputImageIterator outImage2It( outImage2, outImage2->GetBufferedRegion() );
    outImage2It.GoToBegin();

    std::cout << "" << std::endl;
    std::cout << "The eigen vector number: " << j << " is:" << std::endl;
    while(!outImage2It.IsAtEnd() )
      {
      std::cout << (double) (outImage2It.Get()) << ";"  << std::endl;
      ++outImage2It;
      }
    std::cout << "  " << std::endl;

    }

  //Test for the eigen values for the test case precomputed using Matlab/Splus
  std::cout << "" << std::endl;
  if( (eigenValues[2] < 6 || eigenValues[2] > 6.1) || (eigenValues[1] >0.1) )
    {
    std::cout<< "Test Passed" << std::endl;
    }
  else
    {
    std::cout<< "Test failed" << std::endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before>#pragma once
#include <Utility/MemoryManager/Include/Allocator/MemoryAllocator.hpp>
#include <exception>

namespace Utility
{
    namespace MemoryManager
    {
        class ApplicationAllocator;

        template <typename T> class PoolAllocator : public MemoryAllocator
        {

            PoolAllocator::PoolAllocator() {}

            /**
            Start the pool and allocate their own memory
            */
            void Initialize(const size_t& p_memorySize, MemoryAllocator& p_applicationAllocator, const uint8_t& p_alignment, const bool& p_threadShared)
            {
                // TODORT move to cpp
                MemoryAllocator::Initialize(p_memorySize, p_alignment, p_threadShared);
            }

            /**
            Start the pool with our application allocator as base, distributes already allocated memory from "above".
            */
            void Initialize(const size_t& p_memorySize, MemoryAllocator& p_applicationAllocator, const uint8_t& p_alignment, const bool& p_threadShared)
            {
                // TODORT move to cpp
                MemoryAllocator::Initialize(p_memorySize, p_applicationAllocator, p_alignment, p_threadShared);
            }

            /**
                TODORT docs
            */
            void RawAllocator::InternalInitialize()
            {
                m_firstFree = m_raw;
                if(m_alignment > m_headerSizeInBytes)
                {
                    // TODORT fix freelist
                }
                else
                {
                    // TODORT
                    throw std::runtime_error("m_alignment must be larger than m_headerSizeInBytes:" + m_headerSizeInBytes);
                }
            }

            // void IncreasePoolSize(){} //TODORT
            // void GetPossibleMemoryLeaks
            // void GetOccupiedBlocks (Iterate through all blocks and check flags)

            virtual PoolAllocator::~PoolAllocator() {}

            T* Allocate()
            {
                if((m_numberOfTotalBlocks - m_numberOfOccupiedBlocks) > 0)
                {
                    // Compute adjustment
                    const uint8_t mask = m_alignment - m_headerSizeInBytes;
                    const uint8_t misalignment = (reinterpret_cast<size_t>(m_raw) & mask);
                    const uint8_t adjustment = m_alignment - misalignment;

                    // Get next pointer

                    // Compute

                    // Compute start
                    m_currentFree = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) + adjustment);

                    // Set adjustment metadata
                    size_t* adjustmentMetaData = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) - m_headerSizeInBytes);
                    AllocationHeaderBuilder::SetByte(adjustmentMetaData, adjustment);

                    // Set adjustment metadata
                    size_t* adjustmentMetaData = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) - m_headerSizeInBytes + 1);
                    AllocationHeaderBuilder::SetByte(adjustmentMetaData, adjustment);

                    T* current = m_firstFree;
                    m_firstFree = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_firstFree) + sizeof(T) + m_alignment);
                    // TODORT alignment on each segment?
                    ++m_numberOfOccupiedBlocks;
                }
                else
                {
                    // TODORT logging
                    // TODORT could maybe return null instead, dangerous to throw exception if this is supposed to be threadsafe and the exceptions is
                    // not handled corretly
                    throw std::exception("pool is full"); // TODORT better message
                }
            }

            T* Free(T* t)
            {
                delete t;
                ++m_numberOfOccupiedBlocks;
            }

            private:
            void* m_start;
            T* m_firstFree;
            T* m_lastFree;

            bool m_shared;
            size_t m_numberOfOccupiedBlocks;

            // Not sure about
            uint8_t m_alignment;
            uint8_t m_headerSizeInBytes = 3; // [next, flags, adjustment] data
        };
    }
}<commit_msg>Fixed bug which used the wrong operator<commit_after>#pragma once
#include <Utility/MemoryManager/Include/Allocator/MemoryAllocator.hpp>
#include <exception>

namespace Utility
{
    namespace MemoryManager
    {
        class ApplicationAllocator;

        template <typename T> class PoolAllocator : public MemoryAllocator
        {

            PoolAllocator::PoolAllocator() {}

            /**
            Start the pool and allocate their own memory
            */
            void Initialize(const size_t& p_memorySize, MemoryAllocator& p_applicationAllocator, const uint8_t& p_alignment, const bool& p_threadShared)
            {
                // TODORT move to cpp
                MemoryAllocator::Initialize(p_memorySize, p_alignment, p_threadShared);
            }

            /**
            Start the pool with our application allocator as base, distributes already allocated memory from "above".
            */
            void Initialize(const size_t& p_memorySize, MemoryAllocator& p_applicationAllocator, const uint8_t& p_alignment, const bool& p_threadShared)
            {
                // TODORT move to cpp
                MemoryAllocator::Initialize(p_memorySize, p_applicationAllocator, p_alignment, p_threadShared);
            }

            /**
                TODORT docs
            */
            void RawAllocator::InternalInitialize()
            {
                m_firstFree = m_raw;
                if(m_alignment > m_headerSizeInBytes)
                {
                    // TODORT fix freelist
                }
                else
                {
                    // TODORT
                    throw std::runtime_error("m_alignment must be larger than m_headerSizeInBytes:" + m_headerSizeInBytes);
                }
            }

            // void IncreasePoolSize(){} //TODORT
            // void GetPossibleMemoryLeaks
            // void GetOccupiedBlocks (Iterate through all blocks and check flags)

            virtual PoolAllocator::~PoolAllocator() {}

            T* Allocate()
            {
                if((m_numberOfTotalBlocks - m_numberOfOccupiedBlocks) > 0)
                {
                    // Compute adjustment
                    const uint8_t mask = m_alignment - m_headerSizeInBytes;
                    const uint8_t misalignment = (reinterpret_cast<size_t>(m_raw) & mask);
                    const uint8_t adjustment = m_alignment - misalignment;

                    // Get next pointer

                    // Compute

                    // Compute start
                    m_currentFree = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) + adjustment);

                    // Set adjustment metadata
                    size_t* adjustmentMetaData = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) - m_headerSizeInBytes);
                    AllocationHeaderBuilder::SetByte(adjustmentMetaData, adjustment);

                    // Set adjustment metadata
                    size_t* adjustmentMetaData = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_currentFree) - m_headerSizeInBytes + 1);
                    AllocationHeaderBuilder::SetByte(adjustmentMetaData, adjustment);

                    T* current = m_firstFree;
                    m_firstFree = reinterpret_cast<size_t*>(reinterpret_cast<size_t>(m_firstFree) + sizeof(T) + m_alignment);
                    // TODORT alignment on each segment?
                    ++m_numberOfOccupiedBlocks;
                }
                else
                {
                    // TODORT logging
                    // TODORT could maybe return null instead, dangerous to throw exception if this is supposed to be threadsafe and the exceptions is
                    // not handled corretly
                    throw std::exception("pool is full"); // TODORT better message
                }
            }

            T* Free(T* t)
            {
                delete t;
                --m_numberOfOccupiedBlocks;

            }

            private:
            void* m_start;
            T* m_firstFree;
            T* m_lastFree;

            bool m_shared;
            size_t m_numberOfOccupiedBlocks;

            // Not sure about
            uint8_t m_alignment;
            uint8_t m_headerSizeInBytes = 3; // [next, flags, adjustment] data
        };
    }
}<|endoftext|>