<commit_before>#ifndef CAFFE_DATA_LAYERS_HPP_
#define CAFFE_DATA_LAYERS_HPP_

#include <string>
#include <utility>
#include <vector>
#include "hdf5.h"

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/data_reader.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/filler.hpp"
#include "caffe/internal_thread.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/blocking_queue.hpp"
#include "caffe/util/db.hpp"

#define HDF5_DATA_DATASET_NAME "data"
#define HDF5_DATA_LABEL_NAME "label"

extern "C" {
  typedef void (*java_callback_t) (void* data, int batch_size, int num_dims, int* shape);
}

namespace caffe {

/**
 * @brief Provides base for data layers that feed blobs to the Net.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class BaseDataLayer : public Layer<Dtype> {
 public:
  explicit BaseDataLayer(const LayerParameter& param);
  // LayerSetUp: implements common data layer setup functionality, and calls
  // DataLayerSetUp to do special data layer setup for individual layer types.
  // This method may not be overridden except by the BasePrefetchingDataLayer.
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}

 protected:
  TransformationParameter transform_param_;
  shared_ptr<DataTransformer<Dtype> > data_transformer_;
  bool output_labels_;
};

template <typename Dtype>
class Batch {
 public:
  Blob<Dtype> data_, label_;
};

template <typename Dtype>
class BasePrefetchingDataLayer :
    public BaseDataLayer<Dtype>, public InternalThread {
 public:
  explicit BasePrefetchingDataLayer(const LayerParameter& param);
  // LayerSetUp: implements common data layer setup functionality, and calls
  // DataLayerSetUp to do special data layer setup for individual layer types.
  // This method may not be overridden.
  void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  // Prefetches batches (asynchronously if to GPU memory)
  static const int PREFETCH_COUNT = 3;

 protected:
  virtual void InternalThreadEntry();
  virtual void load_batch(Batch<Dtype>* batch) = 0;

  Batch<Dtype> prefetch_[PREFETCH_COUNT];
  BlockingQueue<Batch<Dtype>*> prefetch_free_;
  BlockingQueue<Batch<Dtype>*> prefetch_full_;

  Blob<Dtype> transformed_data_;
};

template <typename Dtype>
class DataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit DataLayer(const LayerParameter& param);
  virtual ~DataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // DataLayer uses DataReader instead for sharing for parallelism
  virtual inline bool ShareInParallel() const { return false; }
  virtual inline const char* type() const { return "Data"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }
  virtual inline int MaxTopBlobs() const { return 2; }

 protected:
  virtual void load_batch(Batch<Dtype>* batch);

  DataReader reader_;
};

/**
 * @brief Provides data to the Net generated by a Filler.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class DummyDataLayer : public Layer<Dtype> {
 public:
  explicit DummyDataLayer(const LayerParameter& param)
      : Layer<Dtype>(param) {}
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "DummyData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}

  vector<shared_ptr<Filler<Dtype> > > fillers_;
  vector<bool> refill_;
};

/**
 * @brief Provides data to the Net from HDF5 files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class HDF5DataLayer : public Layer<Dtype> {
 public:
  explicit HDF5DataLayer(const LayerParameter& param)
      : Layer<Dtype>(param) {}
  virtual ~HDF5DataLayer();
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "HDF5Data"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void LoadHDF5FileData(const char* filename);

  std::vector<std::string> hdf_filenames_;
  unsigned int num_files_;
  unsigned int current_file_;
  hsize_t current_row_;
  std::vector<shared_ptr<Blob<Dtype> > > hdf_blobs_;
  std::vector<unsigned int> data_permutation_;
  std::vector<unsigned int> file_permutation_;
};

/**
 * @brief Write blobs to disk as HDF5 files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class HDF5OutputLayer : public Layer<Dtype> {
 public:
  explicit HDF5OutputLayer(const LayerParameter& param)
      : Layer<Dtype>(param), file_opened_(false) {}
  virtual ~HDF5OutputLayer();
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "HDF5Output"; }
  // TODO: no limit on the number of blobs
  virtual inline int ExactNumBottomBlobs() const { return 2; }
  virtual inline int ExactNumTopBlobs() const { return 0; }

  inline std::string file_name() const { return file_name_; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void SaveBlobs();

  bool file_opened_;
  std::string file_name_;
  hid_t file_id_;
  Blob<Dtype> data_blob_;
  Blob<Dtype> label_blob_;
};

/**
 * @brief Provides data to the Net from image files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class ImageDataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit ImageDataLayer(const LayerParameter& param)
      : BasePrefetchingDataLayer<Dtype>(param) {}
  virtual ~ImageDataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "ImageData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

 protected:
  shared_ptr<Caffe::RNG> prefetch_rng_;
  virtual void ShuffleImages();
  virtual void load_batch(Batch<Dtype>* batch);

  vector<std::pair<std::string, int> > lines_;
  int lines_id_;
};

/**
 * @brief Provides data to the Net from memory.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class MemoryDataLayer : public BaseDataLayer<Dtype> {
 public:
  explicit MemoryDataLayer(const LayerParameter& param)
      : BaseDataLayer<Dtype>(param), has_new_data_(false) {}
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "MemoryData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

  virtual void AddDatumVector(const vector<Datum>& datum_vector);
#ifdef USE_OPENCV
  virtual void AddMatVector(const vector<cv::Mat>& mat_vector,
      const vector<int>& labels);
#endif  // USE_OPENCV

  // Reset should accept const pointers, but can't, because the memory
  //  will be given to Blob, which is mutable
  void Reset(Dtype* data, Dtype* label, int n);
  void set_batch_size(int new_size);

  int batch_size() { return batch_size_; }
  int channels() { return channels_; }
  int height() { return height_; }
  int width() { return width_; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  int batch_size_, channels_, height_, width_, size_;
  Dtype* data_;
  Dtype* labels_;
  int n_;
  size_t pos_;
  Blob<Dtype> added_data_;
  Blob<Dtype> added_label_;
  bool has_new_data_;
};

/**
 * @brief Provides data to the Net from Java. The data will come from RDDs
 * in most cases, but loading the data can involve arbitrary Java code. You
 * just need to provide a callback that takes a pointer to Dtypes, the
 * minibatch size, the shape of the data, and will make sure the data has
 * the right size and store the Dtypes consecutively at the memory location
 * provided by the pointer.
 *
 */

template <typename Dtype>
class JavaDataLayer : public BaseDataLayer<Dtype> {
 public:
  explicit JavaDataLayer(const LayerParameter& param)
      : BaseDataLayer<Dtype>(param), java_callback_(NULL) {}
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual ~JavaDataLayer() { delete[] buffer_; }

  virtual inline const char* type() const { return "JavaData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 1; }

  void SetCallback(java_callback_t callback);

  int batch_size() { return batch_size_; }
  int channels() { return shape_.dim(1); }
  int height() { return shape_.dim(2); }
  int width() { return shape_.dim(3); }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  int batch_size_;
  int size_;
  BlobShape shape_;
  int* java_shape_; // will be passed to java
  java_callback_t java_callback_;
  Dtype* buffer_;
};

/**
 * @brief Provides data to the Net from windows of images files, specified
 *        by a window data file.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class WindowDataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit WindowDataLayer(const LayerParameter& param)
      : BasePrefetchingDataLayer<Dtype>(param) {}
  virtual ~WindowDataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "WindowData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

 protected:
  virtual unsigned int PrefetchRand();
  virtual void load_batch(Batch<Dtype>* batch);

  shared_ptr<Caffe::RNG> prefetch_rng_;
  vector<std::pair<std::string, vector<int> > > image_database_;
  enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM };
  vector<vector<float> > fg_windows_;
  vector<vector<float> > bg_windows_;
  Blob<Dtype> data_mean_;
  vector<Dtype> mean_values_;
  bool has_mean_file_;
  bool has_mean_values_;
  bool cache_images_;
  vector<std::pair<std::string, Datum > > image_database_cache_;
};

}  // namespace caffe

#endif  // CAFFE_DATA_LAYERS_HPP_
<commit_msg>delete[] java_shape_ in ~JavaDataLayer<commit_after>#ifndef CAFFE_DATA_LAYERS_HPP_
#define CAFFE_DATA_LAYERS_HPP_

#include <string>
#include <utility>
#include <vector>
#include "hdf5.h"

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/data_reader.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/filler.hpp"
#include "caffe/internal_thread.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/blocking_queue.hpp"
#include "caffe/util/db.hpp"

#define HDF5_DATA_DATASET_NAME "data"
#define HDF5_DATA_LABEL_NAME "label"

extern "C" {
  typedef void (*java_callback_t) (void* data, int batch_size, int num_dims, int* shape);
}

namespace caffe {

/**
 * @brief Provides base for data layers that feed blobs to the Net.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class BaseDataLayer : public Layer<Dtype> {
 public:
  explicit BaseDataLayer(const LayerParameter& param);
  // LayerSetUp: implements common data layer setup functionality, and calls
  // DataLayerSetUp to do special data layer setup for individual layer types.
  // This method may not be overridden except by the BasePrefetchingDataLayer.
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}

 protected:
  TransformationParameter transform_param_;
  shared_ptr<DataTransformer<Dtype> > data_transformer_;
  bool output_labels_;
};

template <typename Dtype>
class Batch {
 public:
  Blob<Dtype> data_, label_;
};

template <typename Dtype>
class BasePrefetchingDataLayer :
    public BaseDataLayer<Dtype>, public InternalThread {
 public:
  explicit BasePrefetchingDataLayer(const LayerParameter& param);
  // LayerSetUp: implements common data layer setup functionality, and calls
  // DataLayerSetUp to do special data layer setup for individual layer types.
  // This method may not be overridden.
  void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  // Prefetches batches (asynchronously if to GPU memory)
  static const int PREFETCH_COUNT = 3;

 protected:
  virtual void InternalThreadEntry();
  virtual void load_batch(Batch<Dtype>* batch) = 0;

  Batch<Dtype> prefetch_[PREFETCH_COUNT];
  BlockingQueue<Batch<Dtype>*> prefetch_free_;
  BlockingQueue<Batch<Dtype>*> prefetch_full_;

  Blob<Dtype> transformed_data_;
};

template <typename Dtype>
class DataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit DataLayer(const LayerParameter& param);
  virtual ~DataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // DataLayer uses DataReader instead for sharing for parallelism
  virtual inline bool ShareInParallel() const { return false; }
  virtual inline const char* type() const { return "Data"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }
  virtual inline int MaxTopBlobs() const { return 2; }

 protected:
  virtual void load_batch(Batch<Dtype>* batch);

  DataReader reader_;
};

/**
 * @brief Provides data to the Net generated by a Filler.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class DummyDataLayer : public Layer<Dtype> {
 public:
  explicit DummyDataLayer(const LayerParameter& param)
      : Layer<Dtype>(param) {}
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "DummyData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}

  vector<shared_ptr<Filler<Dtype> > > fillers_;
  vector<bool> refill_;
};

/**
 * @brief Provides data to the Net from HDF5 files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class HDF5DataLayer : public Layer<Dtype> {
 public:
  explicit HDF5DataLayer(const LayerParameter& param)
      : Layer<Dtype>(param) {}
  virtual ~HDF5DataLayer();
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "HDF5Data"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int MinTopBlobs() const { return 1; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void LoadHDF5FileData(const char* filename);

  std::vector<std::string> hdf_filenames_;
  unsigned int num_files_;
  unsigned int current_file_;
  hsize_t current_row_;
  std::vector<shared_ptr<Blob<Dtype> > > hdf_blobs_;
  std::vector<unsigned int> data_permutation_;
  std::vector<unsigned int> file_permutation_;
};

/**
 * @brief Write blobs to disk as HDF5 files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class HDF5OutputLayer : public Layer<Dtype> {
 public:
  explicit HDF5OutputLayer(const LayerParameter& param)
      : Layer<Dtype>(param), file_opened_(false) {}
  virtual ~HDF5OutputLayer();
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  // Data layers should be shared by multiple solvers in parallel
  virtual inline bool ShareInParallel() const { return true; }
  // Data layers have no bottoms, so reshaping is trivial.
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {}

  virtual inline const char* type() const { return "HDF5Output"; }
  // TODO: no limit on the number of blobs
  virtual inline int ExactNumBottomBlobs() const { return 2; }
  virtual inline int ExactNumTopBlobs() const { return 0; }

  inline std::string file_name() const { return file_name_; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void SaveBlobs();

  bool file_opened_;
  std::string file_name_;
  hid_t file_id_;
  Blob<Dtype> data_blob_;
  Blob<Dtype> label_blob_;
};

/**
 * @brief Provides data to the Net from image files.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class ImageDataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit ImageDataLayer(const LayerParameter& param)
      : BasePrefetchingDataLayer<Dtype>(param) {}
  virtual ~ImageDataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "ImageData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

 protected:
  shared_ptr<Caffe::RNG> prefetch_rng_;
  virtual void ShuffleImages();
  virtual void load_batch(Batch<Dtype>* batch);

  vector<std::pair<std::string, int> > lines_;
  int lines_id_;
};

/**
 * @brief Provides data to the Net from memory.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class MemoryDataLayer : public BaseDataLayer<Dtype> {
 public:
  explicit MemoryDataLayer(const LayerParameter& param)
      : BaseDataLayer<Dtype>(param), has_new_data_(false) {}
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "MemoryData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

  virtual void AddDatumVector(const vector<Datum>& datum_vector);
#ifdef USE_OPENCV
  virtual void AddMatVector(const vector<cv::Mat>& mat_vector,
      const vector<int>& labels);
#endif  // USE_OPENCV

  // Reset should accept const pointers, but can't, because the memory
  //  will be given to Blob, which is mutable
  void Reset(Dtype* data, Dtype* label, int n);
  void set_batch_size(int new_size);

  int batch_size() { return batch_size_; }
  int channels() { return channels_; }
  int height() { return height_; }
  int width() { return width_; }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  int batch_size_, channels_, height_, width_, size_;
  Dtype* data_;
  Dtype* labels_;
  int n_;
  size_t pos_;
  Blob<Dtype> added_data_;
  Blob<Dtype> added_label_;
  bool has_new_data_;
};

/**
 * @brief Provides data to the Net from Java. The data will come from RDDs
 * in most cases, but loading the data can involve arbitrary Java code. You
 * just need to provide a callback that takes a pointer to Dtypes, the
 * minibatch size, the shape of the data, and will make sure the data has
 * the right size and store the Dtypes consecutively at the memory location
 * provided by the pointer.
 *
 */

template <typename Dtype>
class JavaDataLayer : public BaseDataLayer<Dtype> {
 public:
  explicit JavaDataLayer(const LayerParameter& param)
      : BaseDataLayer<Dtype>(param), java_callback_(NULL) {}
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual ~JavaDataLayer() { delete[] buffer_; delete[] java_shape_; }

  virtual inline const char* type() const { return "JavaData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 1; }

  void SetCallback(java_callback_t callback);

  int batch_size() { return batch_size_; }
  int channels() { return shape_.dim(1); }
  int height() { return shape_.dim(2); }
  int width() { return shape_.dim(3); }

 protected:
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  int batch_size_;
  int size_;
  BlobShape shape_;
  int* java_shape_; // will be passed to java
  java_callback_t java_callback_;
  Dtype* buffer_;
};

/**
 * @brief Provides data to the Net from windows of images files, specified
 *        by a window data file.
 *
 * TODO(dox): thorough documentation for Forward and proto params.
 */
template <typename Dtype>
class WindowDataLayer : public BasePrefetchingDataLayer<Dtype> {
 public:
  explicit WindowDataLayer(const LayerParameter& param)
      : BasePrefetchingDataLayer<Dtype>(param) {}
  virtual ~WindowDataLayer();
  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "WindowData"; }
  virtual inline int ExactNumBottomBlobs() const { return 0; }
  virtual inline int ExactNumTopBlobs() const { return 2; }

 protected:
  virtual unsigned int PrefetchRand();
  virtual void load_batch(Batch<Dtype>* batch);

  shared_ptr<Caffe::RNG> prefetch_rng_;
  vector<std::pair<std::string, vector<int> > > image_database_;
  enum WindowField { IMAGE_INDEX, LABEL, OVERLAP, X1, Y1, X2, Y2, NUM };
  vector<vector<float> > fg_windows_;
  vector<vector<float> > bg_windows_;
  Blob<Dtype> data_mean_;
  vector<Dtype> mean_values_;
  bool has_mean_file_;
  bool has_mean_values_;
  bool cache_images_;
  vector<std::pair<std::string, Datum > > image_database_cache_;
};

}  // namespace caffe

#endif  // CAFFE_DATA_LAYERS_HPP_
<|endoftext|>


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

  Program:   Visualization Toolkit
  Module:    vtkContingencyStatistics.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/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 notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkToolkits.h"

#include "vtkContingencyStatistics.h"

#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"

#include <vtkstd/map>
#include <vtkstd/set>

#include <vtksys/ios/sstream>

vtkCxxRevisionMacro(vtkContingencyStatistics, "1.6");
vtkStandardNewMacro(vtkContingencyStatistics);

// ----------------------------------------------------------------------
vtkContingencyStatistics::vtkContingencyStatistics()
{
  this->X = 0;
  this->Y = 0;
}

// ----------------------------------------------------------------------
vtkContingencyStatistics::~vtkContingencyStatistics()
{
  this->SetX( 0 );
  this->SetY( 0 );
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::PrintSelf( ostream &os, vtkIndent indent )
{
  this->Superclass::PrintSelf( os, indent );
  os << indent << "X: " << (this->X ? this->X : "(none)") << endl;
  os << indent << "Y: " << (this->Y ? this->Y : "(none)") << endl;
 }

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteLearn( vtkTable* dataset,
                                             vtkTable* output,
                                             bool finalize )
{
  if ( ! dataset->GetNumberOfColumns() )
    {
    this->SampleSize = 0;
    return;
    }

  this->SampleSize = dataset->GetNumberOfRows();
  if ( ! this->SampleSize )
    {
    return;
    }

  vtkStdString colX = this->X;
  if ( ! dataset->GetColumnByName( colX ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colX.c_str()<<". Doing nothing." );
    return;
    }

  vtkStdString colY = this->Y;
  if ( ! dataset->GetColumnByName( colY ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colY.c_str()<<". Doing nothing." );
    return;
    }

  vtkVariantArray* variantCol = vtkVariantArray::New();
  variantCol->SetName( X );
  output->AddColumn( variantCol );
  variantCol->Delete();

  variantCol = vtkVariantArray::New();
  variantCol->SetName( Y );
  output->AddColumn( variantCol );
  variantCol->Delete();

  vtkIdTypeArray* idTypeCol = vtkIdTypeArray::New();
  idTypeCol->SetName( "Cardinality" );
  output->AddColumn( idTypeCol );
  idTypeCol->Delete();

  if ( finalize )
    {
    vtkDoubleArray* doubleCol = vtkDoubleArray::New();
    doubleCol->SetName( "Probability" );
    output->AddColumn( doubleCol );
    doubleCol->Delete();
    }

  typedef vtkstd::map<double,vtkIdType> Distribution;
  vtkstd::map<double,Distribution> conTable;
  for ( vtkIdType r = 0; r < this->SampleSize; ++ r )
    {
    ++ conTable[dataset->GetValueByName( r, colX ).ToDouble()][dataset->GetValueByName( r, colY ).ToDouble()];
    }

  vtkVariantArray* row = vtkVariantArray::New();
  double n = static_cast<double>( this->SampleSize );
  if ( finalize )
    {
    row->SetNumberOfValues( 4 );

    for ( vtkstd::map<double,Distribution>::iterator it = conTable.begin();
          it != conTable.end(); ++ it )
      {
      row->SetValue( 0, it->first );
      for ( Distribution::iterator dit = it->second.begin(); dit != it->second.end(); ++ dit )
        {
        row->SetValue( 1, dit->first );
        row->SetValue( 2, dit->second );
        row->SetValue( 3, dit->second / n );

        output->InsertNextRow( row );
        }
      }
    }
  else
    {
    row->SetNumberOfValues( 3 );

    for ( vtkstd::map<double,Distribution>::iterator it = conTable.begin();
          it != conTable.end(); ++ it )
      {
      row->SetValue( 0, it->first );
      for ( Distribution::iterator dit = it->second.begin(); dit != it->second.end(); ++ dit )
        {
        row->SetValue( 1, dit->first );
        row->SetValue( 2, dit->second );

        output->InsertNextRow( row );
        }
      }
    }

  row->Delete();

  return;
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteValidate( vtkTable*,
                                                vtkTable*,
                                                vtkTable* )
{
  // Not implemented for this statistical engine
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteAssess( vtkTable* dataset,
                                              vtkTable* params,
                                              vtkTable* output)
{
  output->ShallowCopy( dataset );

  vtkIdType nColD = dataset->GetNumberOfColumns();
  if ( ! nColD )
    {
    return;
    }

  vtkIdType nRowD = dataset->GetNumberOfRows();
  if ( ! nRowD )
    {
    return;
    }

  vtkIdType nColP = params->GetNumberOfColumns();
  if ( nColP < 3 )
    {
    vtkWarningMacro( "Parameter table has "
                     << nColP
                     << " < 3 columns. Doing nothing." );
    return;
    }

  vtkIdType nRowP = params->GetNumberOfRows();
  if ( ! nRowP )
    {
    return;
    }

  vtkStdString colX = this->X;
  if ( ! dataset->GetColumnByName( colX ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colX.c_str()<<". Doing nothing." );
    return;
    }

  vtkStdString colY = this->Y;
  if ( ! dataset->GetColumnByName( colY ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colY.c_str()<<". Doing nothing." );
    return;
    }

  if ( ! params->GetColumnByName( "Probability" ) )
    {
    vtkWarningMacro( "Dataset table does not have a column Probability. Doing nothing." );
    return;
    }

  // Create the output columns
  vtkDoubleArray* pXY = vtkDoubleArray::New();
  vtksys_ios::ostringstream pXYName;
  pXYName
    << "p(" << ( colX.size() ? colX.c_str() : "X" )
    << ","  << ( colY.size() ? colY.c_str() : "Y" ) << ")";
  pXY->SetName( pXYName.str().c_str() );
  pXY->SetNumberOfTuples( nRowD );
  output->AddColumn( pXY );
  pXY->Delete();

  vtkDoubleArray* pYcondX = vtkDoubleArray::New();
  vtksys_ios::ostringstream pYCondXName;
  pYCondXName
    << "p(" << ( colY.size() ? colY.c_str() : "Y" )
    << "|"  << ( colX.size() ? colX.c_str() : "X" ) << ")";
  pYcondX->SetName( pYCondXName.str().c_str() );
  pYcondX->SetNumberOfTuples( nRowD );
  output->AddColumn( pYcondX );
  pYcondX->Delete();

  vtkDoubleArray* pXcondY = vtkDoubleArray::New();
  vtksys_ios::ostringstream pXCondYName;
  pXCondYName
    << "p(" << ( colX.size() ? colX.c_str() : "X" )
    << "|"  << ( colY.size() ? colY.c_str() : "Y" ) << ")";
  pXcondY->SetName( pXCondYName.str().c_str() );
  pXcondY->SetNumberOfTuples( nRowD );
  output->AddColumn( pXcondY );
  pXcondY->Delete();

  vtkstd::map<double,double> pdfX, pdfY;
  vtkstd::map<double,vtkstd::map<double,double> > pdfXY;
  double x, y, p;
  for ( vtkIdType r = 0; r < nRowP; ++ r )
    {
    x = params->GetValueByName( r, colX ).ToDouble();
    y = params->GetValueByName( r, colY ).ToDouble();
    p = params->GetValueByName( r, "Probability" ).ToDouble();

    pdfX[x] += p;
    pdfY[y] += p;
    pdfXY[x][y] = p;
    }

  vtkstd::map<double,vtkstd::map<double,double> > pdfYcondX;
  vtkstd::map<double,vtkstd::map<double,double> > pdfXcondY;
  for ( vtkstd::map<double,vtkstd::map<double,double> >::iterator xit = pdfXY.begin();
        xit != pdfXY.end(); ++ xit )
    {
    x = xit->first;
    for ( vtkstd::map<double,double>::iterator yit = xit->second.begin();
          yit != xit->second.end(); ++ yit )
      {
      y = yit->first;
      pdfYcondX[x][y] = yit->second / pdfX[x];
      pdfXcondY[x][y] = yit->second / pdfY[y];
      }
    }

  for ( vtkIdType r = 0; r < nRowD; ++ r )
    {
    x = dataset->GetValueByName( r, colX ).ToDouble();
    y = dataset->GetValueByName( r, colY ).ToDouble();

    output->SetValueByName( r, pXYName.str().c_str(), pdfXY[x][y] );
    output->SetValueByName( r, pYCondXName.str().c_str(), pdfYcondX[x][y] );
    output->SetValueByName( r, pXCondYName.str().c_str(), pdfXcondY[x][y] );
    }

  return;
}
<commit_msg>ENH: treat Variants as such (and thus, accomodate non-numerical types,      as allowed by contingency statistics).<commit_after>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkContingencyStatistics.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/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 notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkToolkits.h"

#include "vtkContingencyStatistics.h"

#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"

#include <vtkstd/map>

#include <vtksys/ios/sstream>

vtkCxxRevisionMacro(vtkContingencyStatistics, "1.7");
vtkStandardNewMacro(vtkContingencyStatistics);

// ----------------------------------------------------------------------
vtkContingencyStatistics::vtkContingencyStatistics()
{
  this->X = 0;
  this->Y = 0;
}

// ----------------------------------------------------------------------
vtkContingencyStatistics::~vtkContingencyStatistics()
{
  this->SetX( 0 );
  this->SetY( 0 );
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::PrintSelf( ostream &os, vtkIndent indent )
{
  this->Superclass::PrintSelf( os, indent );
  os << indent << "X: " << (this->X ? this->X : "(none)") << endl;
  os << indent << "Y: " << (this->Y ? this->Y : "(none)") << endl;
 }

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteLearn( vtkTable* dataset,
                                             vtkTable* output,
                                             bool finalize )
{
  if ( ! dataset->GetNumberOfColumns() )
    {
    this->SampleSize = 0;
    return;
    }

  this->SampleSize = dataset->GetNumberOfRows();
  if ( ! this->SampleSize )
    {
    return;
    }

  vtkStdString colX = this->X;
  if ( ! dataset->GetColumnByName( colX ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colX.c_str()<<". Doing nothing." );
    return;
    }

  vtkStdString colY = this->Y;
  if ( ! dataset->GetColumnByName( colY ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colY.c_str()<<". Doing nothing." );
    return;
    }

  vtkVariantArray* variantCol = vtkVariantArray::New();
  variantCol->SetName( X );
  output->AddColumn( variantCol );
  variantCol->Delete();

  variantCol = vtkVariantArray::New();
  variantCol->SetName( Y );
  output->AddColumn( variantCol );
  variantCol->Delete();

  vtkIdTypeArray* idTypeCol = vtkIdTypeArray::New();
  idTypeCol->SetName( "Cardinality" );
  output->AddColumn( idTypeCol );
  idTypeCol->Delete();

  if ( finalize )
    {
    vtkDoubleArray* doubleCol = vtkDoubleArray::New();
    doubleCol->SetName( "Probability" );
    output->AddColumn( doubleCol );
    doubleCol->Delete();
    }

  typedef vtkstd::map<vtkVariant,vtkIdType,vtkVariantLessThan> Distribution;
  vtkstd::map<vtkVariant,Distribution,vtkVariantLessThan> conTable;
  for ( vtkIdType r = 0; r < this->SampleSize; ++ r )
    {
    ++ conTable[dataset->GetValueByName( r, colX )][dataset->GetValueByName( r, colY )];
    }

  vtkVariantArray* row = vtkVariantArray::New();
  double n = static_cast<double>( this->SampleSize );
  if ( finalize )
    {
    row->SetNumberOfValues( 4 );

    for ( vtkstd::map<vtkVariant,Distribution,vtkVariantLessThan>::iterator it = conTable.begin();
          it != conTable.end(); ++ it )
      {
      row->SetValue( 0, it->first );
      for ( Distribution::iterator dit = it->second.begin(); dit != it->second.end(); ++ dit )
        {
        row->SetValue( 1, dit->first );
        row->SetValue( 2, dit->second );
        row->SetValue( 3, dit->second / n );

        output->InsertNextRow( row );
        }
      }
    }
  else
    {
    row->SetNumberOfValues( 3 );

    for ( vtkstd::map<vtkVariant,Distribution,vtkVariantLessThan>::iterator it = conTable.begin();
          it != conTable.end(); ++ it )
      {
      row->SetValue( 0, it->first );
      for ( Distribution::iterator dit = it->second.begin(); dit != it->second.end(); ++ dit )
        {
        row->SetValue( 1, dit->first );
        row->SetValue( 2, dit->second );

        output->InsertNextRow( row );
        }
      }
    }

  row->Delete();

  return;
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteValidate( vtkTable*,
                                                vtkTable*,
                                                vtkTable* )
{
  // Not implemented for this statistical engine
}

// ----------------------------------------------------------------------
void vtkContingencyStatistics::ExecuteAssess( vtkTable* dataset,
                                              vtkTable* params,
                                              vtkTable* output)
{
  output->ShallowCopy( dataset );

  vtkIdType nColD = dataset->GetNumberOfColumns();
  if ( ! nColD )
    {
    return;
    }

  vtkIdType nRowD = dataset->GetNumberOfRows();
  if ( ! nRowD )
    {
    return;
    }

  vtkIdType nColP = params->GetNumberOfColumns();
  if ( nColP < 3 )
    {
    vtkWarningMacro( "Parameter table has "
                     << nColP
                     << " < 3 columns. Doing nothing." );
    return;
    }

  vtkIdType nRowP = params->GetNumberOfRows();
  if ( ! nRowP )
    {
    return;
    }

  vtkStdString colX = this->X;
  if ( ! dataset->GetColumnByName( colX ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colX.c_str()<<". Doing nothing." );
    return;
    }

  vtkStdString colY = this->Y;
  if ( ! dataset->GetColumnByName( colY ) )
    {
    vtkWarningMacro( "Dataset table does not have a column "<<colY.c_str()<<". Doing nothing." );
    return;
    }

  if ( ! params->GetColumnByName( "Probability" ) )
    {
    vtkWarningMacro( "Dataset table does not have a column Probability. Doing nothing." );
    return;
    }

  // Create the output columns
  vtkDoubleArray* pXY = vtkDoubleArray::New();
  vtksys_ios::ostringstream pXYName;
  pXYName
    << "p(" << ( colX.size() ? colX.c_str() : "X" )
    << ","  << ( colY.size() ? colY.c_str() : "Y" ) << ")";
  pXY->SetName( pXYName.str().c_str() );
  pXY->SetNumberOfTuples( nRowD );
  output->AddColumn( pXY );
  pXY->Delete();

  vtkDoubleArray* pYcondX = vtkDoubleArray::New();
  vtksys_ios::ostringstream pYCondXName;
  pYCondXName
    << "p(" << ( colY.size() ? colY.c_str() : "Y" )
    << "|"  << ( colX.size() ? colX.c_str() : "X" ) << ")";
  pYcondX->SetName( pYCondXName.str().c_str() );
  pYcondX->SetNumberOfTuples( nRowD );
  output->AddColumn( pYcondX );
  pYcondX->Delete();

  vtkDoubleArray* pXcondY = vtkDoubleArray::New();
  vtksys_ios::ostringstream pXCondYName;
  pXCondYName
    << "p(" << ( colX.size() ? colX.c_str() : "X" )
    << "|"  << ( colY.size() ? colY.c_str() : "Y" ) << ")";
  pXcondY->SetName( pXCondYName.str().c_str() );
  pXcondY->SetNumberOfTuples( nRowD );
  output->AddColumn( pXcondY );
  pXcondY->Delete();

  typedef vtkstd::map<vtkVariant,double,vtkVariantLessThan> PDF;
  PDF pdfX, pdfY;
  vtkstd::map<vtkVariant,PDF,vtkVariantLessThan> pdfXY;
  vtkVariant x, y;
  double p;
  for ( vtkIdType r = 0; r < nRowP; ++ r )
    {
    x = params->GetValueByName( r, colX );
    y = params->GetValueByName( r, colY );
    p = params->GetValueByName( r, "Probability" ).ToDouble();

    pdfX[x] += p;
    pdfY[y] += p;
    pdfXY[x][y] = p;
    }

  vtkstd::map<vtkVariant,PDF,vtkVariantLessThan> pdfYcondX;
  vtkstd::map<vtkVariant,PDF,vtkVariantLessThan> pdfXcondY;
  for ( vtkstd::map<vtkVariant,PDF,vtkVariantLessThan>::iterator xit = pdfXY.begin();
        xit != pdfXY.end(); ++ xit )
    {
    x = xit->first;
    for ( vtkstd::map<vtkVariant,double>::iterator yit = xit->second.begin();
          yit != xit->second.end(); ++ yit )
      {
      y = yit->first;
      pdfYcondX[x][y] = yit->second / pdfX[x];
      pdfXcondY[x][y] = yit->second / pdfY[y];
      }
    }

  for ( vtkIdType r = 0; r < nRowD; ++ r )
    {
    x = dataset->GetValueByName( r, colX ).ToDouble();
    y = dataset->GetValueByName( r, colY ).ToDouble();

    output->SetValueByName( r, pXYName.str().c_str(), pdfXY[x][y] );
    output->SetValueByName( r, pYCondXName.str().c_str(), pdfYcondX[x][y] );
    output->SetValueByName( r, pXCondYName.str().c_str(), pdfXcondY[x][y] );
    }

  return;
}
<|endoftext|>


<commit_before>#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE JPetOptionsTest
#include <boost/test/unit_test.hpp>
#include "JPetOptionsTools.h"
#include <boost/filesystem.hpp>
#include<iostream>

const std::string dataDir = "unitTestData/JPetOptionsToolsTest/";

BOOST_AUTO_TEST_SUITE(FirstSuite)

BOOST_AUTO_TEST_CASE( createConfigFileFromOptions )
{
  std::map<std::string, std::string> options = {{"TimeWindow", "10"}, {"SomeOption", "true"}, {"AnotherOption", "4.5"}};
  std::string outFile = "test_cfg.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, outFile));
  boost::filesystem::remove("test_cfg.json");
}

BOOST_AUTO_TEST_CASE(createOptionsFromConfFile)
{
  auto inFile = "inputTestCfg.json";
  std::map<std::string, std::string> options = jpet_options_tools::createOptionsFromConfFile(dataDir + inFile);
  std::map<std::string, std::string> expected = {{"MyOption", "great"}, {"myAnotherOption", "wat"}, {"boolOption", "true"}, {"NumberOption", "12.2"}};
  BOOST_REQUIRE_EQUAL(options.size(), 4);

  std::vector<std::string> keys_expected;
  std::vector<std::string> values_expected;
  std::vector<std::string> keys;
  std::vector<std::string> values;
  for (const auto & el : expected) {
    keys_expected.push_back(el.first);
    values_expected.push_back(el.second);
  }
  for (const auto & el : options) {
    keys.push_back(el.first);
    values.push_back(el.second);
  }
  std::sort(keys.begin(), keys.end());
  std::sort(values.begin(), values.end());
  std::sort(keys_expected.begin(), keys_expected.end());
  std::sort(values_expected.begin(), values_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(keys.begin(), keys.end(), keys_expected.begin(), keys_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(values.begin(), values.end(), values_expected.begin(), values_expected.end());
}

BOOST_AUTO_TEST_CASE( createConfigFileFromOptionsAndReadItBack )
{
  jpet_options_tools::Options options = {{"TimeWindow", "11"}, {"SomeOption", "false"}, {"AnotherOption", "4.5"}};
  std::string cfgFile = "test_cfg2.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, cfgFile));
  auto loadedOptions = jpet_options_tools::createOptionsFromConfFile(cfgFile);
  BOOST_REQUIRE_EQUAL(options.size(), loadedOptions.size());
  std::vector<std::string> keys_expected;
  std::vector<std::string> values_expected;
  std::vector<std::string> keys;
  std::vector<std::string> values;
  for (const auto & el : loadedOptions) {
    keys_expected.push_back(el.first);
    values_expected.push_back(el.second);
  }
  for (const auto & el : options) {
    keys.push_back(el.first);
    values.push_back(el.second);
  }
  std::sort(keys.begin(), keys.end());
  std::sort(values.begin(), values.end());
  std::sort(keys_expected.begin(), keys_expected.end());
  std::sort(values_expected.begin(), values_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(keys.begin(), keys.end(), keys_expected.begin(), keys_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(values.begin(), values.end(), values_expected.begin(), values_expected.end());
  boost::filesystem::remove("test_cfg2.json");

}

BOOST_AUTO_TEST_CASE( createOptionsFromConfFileThatDoesNotExist )
{
  auto inFile = "nonExistingTestCfg.json";
  auto options = jpet_options_tools::createOptionsFromConfFile(inFile);
  BOOST_REQUIRE(options.empty());
}

BOOST_AUTO_TEST_CASE( createConfigFileFromEmptyMap )
{
  jpet_options_tools::Options options = {};
  std::string cfgFile = "test_cfg3.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, cfgFile));
  boost::filesystem::remove("test_cfg3.json");
}

BOOST_AUTO_TEST_CASE( createOptionsFromConfFileThatHasWrongFormat )
{
  auto inFile = "wrongInputFile.json";
  auto options = jpet_options_tools::createOptionsFromConfFile(inFile);
  BOOST_REQUIRE_EQUAL(options.size(),  0);
}

BOOST_AUTO_TEST_SUITE_END()
<commit_msg>Update JPetOptionsToolsTest.cpp<commit_after>#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE JPetOptionsTest
#include <boost/test/unit_test.hpp>
#include "JPetOptionsTools.h"
#include <boost/filesystem.hpp>
#include<iostream>

const std::string dataDir = "unitTestData/JPetOptionsToolsTest/";

BOOST_AUTO_TEST_SUITE(FirstSuite)

BOOST_AUTO_TEST_CASE( createConfigFileFromOptions )
{
  std::map<std::string, std::string> options = {{"TimeWindow", "10"}, {"SomeOption", "true"}, {"AnotherOption", "4.5"}};
  std::string outFile = "test_cfg.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, outFile));
  boost::filesystem::remove("test_cfg.json");
}

BOOST_AUTO_TEST_CASE(createOptionsFromConfFile)
{
  auto inFile = "inputTestCfg.json";
  std::map<std::string, std::string> options = jpet_options_tools::createOptionsFromConfFile(dataDir + inFile);
  std::map<std::string, std::string> expected = {{"myOption", "great"}, {"myAnotherOption", "wat"}, {"boolOption", "true"}, {"NumberOption", "12.2"}};
  BOOST_REQUIRE_EQUAL(options.size(), 4);

  std::vector<std::string> keys_expected;
  std::vector<std::string> values_expected;
  std::vector<std::string> keys;
  std::vector<std::string> values;
  for (const auto & el : expected) {
    keys_expected.push_back(el.first);
    values_expected.push_back(el.second);
  }
  for (const auto & el : options) {
    keys.push_back(el.first);
    values.push_back(el.second);
  }
  std::sort(keys.begin(), keys.end());
  std::sort(values.begin(), values.end());
  std::sort(keys_expected.begin(), keys_expected.end());
  std::sort(values_expected.begin(), values_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(keys.begin(), keys.end(), keys_expected.begin(), keys_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(values.begin(), values.end(), values_expected.begin(), values_expected.end());
}

BOOST_AUTO_TEST_CASE( createConfigFileFromOptionsAndReadItBack )
{
  jpet_options_tools::Options options = {{"TimeWindow", "11"}, {"SomeOption", "false"}, {"AnotherOption", "4.5"}};
  std::string cfgFile = "test_cfg2.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, cfgFile));
  auto loadedOptions = jpet_options_tools::createOptionsFromConfFile(cfgFile);
  BOOST_REQUIRE_EQUAL(options.size(), loadedOptions.size());
  std::vector<std::string> keys_expected;
  std::vector<std::string> values_expected;
  std::vector<std::string> keys;
  std::vector<std::string> values;
  for (const auto & el : loadedOptions) {
    keys_expected.push_back(el.first);
    values_expected.push_back(el.second);
  }
  for (const auto & el : options) {
    keys.push_back(el.first);
    values.push_back(el.second);
  }
  std::sort(keys.begin(), keys.end());
  std::sort(values.begin(), values.end());
  std::sort(keys_expected.begin(), keys_expected.end());
  std::sort(values_expected.begin(), values_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(keys.begin(), keys.end(), keys_expected.begin(), keys_expected.end());
  BOOST_REQUIRE_EQUAL_COLLECTIONS(values.begin(), values.end(), values_expected.begin(), values_expected.end());
  boost::filesystem::remove("test_cfg2.json");

}

BOOST_AUTO_TEST_CASE( createOptionsFromConfFileThatDoesNotExist )
{
  auto inFile = "nonExistingTestCfg.json";
  auto options = jpet_options_tools::createOptionsFromConfFile(inFile);
  BOOST_REQUIRE(options.empty());
}

BOOST_AUTO_TEST_CASE( createConfigFileFromEmptyMap )
{
  jpet_options_tools::Options options = {};
  std::string cfgFile = "test_cfg3.json";
  BOOST_REQUIRE(jpet_options_tools::createConfigFileFromOptions(options, cfgFile));
  boost::filesystem::remove("test_cfg3.json");
}

BOOST_AUTO_TEST_CASE( createOptionsFromConfFileThatHasWrongFormat )
{
  auto inFile = "wrongInputFile.json";
  auto options = jpet_options_tools::createOptionsFromConfFile(inFile);
  BOOST_REQUIRE_EQUAL(options.size(),  0);
}

BOOST_AUTO_TEST_SUITE_END()
<|endoftext|>


<commit_before>/*
    Copyright (c) 2005 by Volker Krause <volker.krause@rwth-aachen.de>
    Copyright (c) 2005 by Florian Schröder <florian@deltatauchi.de>

    This program 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 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <qfile.h>
#include <qdom.h>
#include <qstring.h>
//Added by qt3to4:
#include <QTextStream>

#include <kdebug.h>
#include <kio/job.h>
#include <kio/davjob.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include "sloxbase.h"
#include "sloxfolder.h"
#include "sloxfoldermanager.h"
#include "webdavhandler.h"

SloxFolderManager::SloxFolderManager( SloxBase *res, const KURL & baseUrl ) :
  mDownloadJob( 0 ),
  mBaseUrl( baseUrl ),
  mRes( res )
{
  kdDebug() << k_funcinfo << baseUrl << endl;
  readFolders();
}

SloxFolderManager::~SloxFolderManager()
{
  if ( mDownloadJob )
    mDownloadJob->kill();
  QMap<QString, SloxFolder*>::Iterator it;
  for ( it = mFolders.begin(); it != mFolders.end(); ++it )
    delete *it;
  mFolders.clear();
}

void SloxFolderManager::requestFolders()
{
  kdDebug() << k_funcinfo << endl;

  if ( mDownloadJob ) {
    kdDebug() << k_funcinfo << "Download still in progress" << endl;
    return;
  }

  KURL url = mBaseUrl;
  url.setPath( "/servlet/webdav.folders/file.xml" );

  QDomDocument doc;
  QDomElement root = WebdavHandler::addDavElement( doc, doc, "propfind" );
  QDomElement prop = WebdavHandler::addDavElement( doc, root, "prop" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "objectmode", "NEW_AND_MODIFIED" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "lastsync", "0" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "PRIVATE" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "PUBLIC" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "SHARED" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "GLOBALADDRESSBOOK" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "INTERNALUSERS" );

  kdDebug() << k_funcinfo << doc.toString( 2 ) << endl;

  mDownloadJob = KIO::davPropFind( url, doc, "0", false );

  connect( mDownloadJob, SIGNAL( result( KIO::Job * ) ),
           SLOT( slotResult( KIO::Job * ) ) );
}

void SloxFolderManager::slotResult( KIO::Job *job )
{
  kdDebug() << k_funcinfo << endl;

  if ( job->error() ) {
    job->showErrorDialog( 0 );
  } else {
    kdDebug() << k_funcinfo << " success, writing to " << cacheFile() << endl;
    QFile f( cacheFile() );
    if ( !f.open( QIODevice::WriteOnly ) ) {
      kdDebug() << "Unable to open '" << cacheFile() << "'" << endl;
      return;
    }
    QTextStream stream ( &f );
    stream << mDownloadJob->response();
    f.close();
    readFolders();
  }

  mDownloadJob = 0;
  emit foldersUpdated();
}

QString SloxFolderManager::cacheFile() const
{
  QString host = mBaseUrl.host();

  QString file = locateLocal( "cache", "slox/folders_" + host );

  kdDebug() << k_funcinfo << file << endl;

  return file;
}

void SloxFolderManager::readFolders()
{
  kdDebug() << k_funcinfo << endl;

  QFile f( cacheFile() );
  if ( !f.open( QIODevice::ReadOnly ) ) {
    kdDebug() << "Unable to open '" << cacheFile() << "'" << endl;
    requestFolders();
    return;
  }

  QDomDocument doc;
  doc.setContent( &f );

  mFolders.clear();

  QDomNodeList nodes = doc.elementsByTagName( "D:prop" );
  for( uint i = 0; i < nodes.count(); ++i ) {
    QDomElement element = nodes.item(i).toElement();
    QString id = "-1", parentId = "-1"; // OX default folder
    bool def = false;
    QString name, type;
    QDomNode n;
    for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
      QDomElement e = n.toElement();
      QString tag = e.tagName();
      QString value = e.text();
      if ( tag == "ox:object_id" ) id = value;
      else if ( tag == "ox:folder_id" ) parentId = value;
      else if ( tag == "ox:title" ) name = value;
      else if ( tag == "ox:module" ) type = value;
      else if ( tag == "ox:defaultfolder" ) def = (value == "true");
    }
    if ( id != "-1" && parentId != "-1" ) {
      SloxFolder *folder = new SloxFolder( id, parentId, type, name, def );
      mFolders[id] = folder;
      kdDebug() << k_funcinfo << "Found folder: " << folder->name() << endl;
    }
  }

  // add top-level system folders that are not contained in the folder listing
  SloxFolder *folder = new SloxFolder( "1", "0", "unbound", i18n("Private Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "2", "0", "unbound", i18n("Public Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "3", "0", "unbound", i18n("Shared Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "4", "0", "unbound", i18n("System Folder") );
  mFolders[folder->id()] = folder;
}

#include "sloxfoldermanager.moc"
<commit_msg>Warning--<commit_after>/*
    Copyright (c) 2005 by Volker Krause <volker.krause@rwth-aachen.de>
    Copyright (c) 2005 by Florian Schröder <florian@deltatauchi.de>

    This program 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 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <qfile.h>
#include <qdom.h>
#include <qstring.h>
//Added by qt3to4:
#include <QTextStream>

#include <kdebug.h>
#include <kio/job.h>
#include <kio/davjob.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include "sloxbase.h"
#include "sloxfolder.h"
#include "sloxfoldermanager.h"
#include "webdavhandler.h"

SloxFolderManager::SloxFolderManager( SloxBase *res, const KURL & baseUrl ) :
  mDownloadJob( 0 ),
  mBaseUrl( baseUrl ),
  mRes( res )
{
  kdDebug() << k_funcinfo << baseUrl << endl;
  readFolders();
}

SloxFolderManager::~SloxFolderManager()
{
  if ( mDownloadJob )
    mDownloadJob->kill();
  QMap<QString, SloxFolder*>::Iterator it;
  for ( it = mFolders.begin(); it != mFolders.end(); ++it )
    delete *it;
  mFolders.clear();
}

void SloxFolderManager::requestFolders()
{
  kdDebug() << k_funcinfo << endl;

  if ( mDownloadJob ) {
    kdDebug() << k_funcinfo << "Download still in progress" << endl;
    return;
  }

  KURL url = mBaseUrl;
  url.setPath( "/servlet/webdav.folders/file.xml" );

  QDomDocument doc;
  QDomElement root = WebdavHandler::addDavElement( doc, doc, "propfind" );
  QDomElement prop = WebdavHandler::addDavElement( doc, root, "prop" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "objectmode", "NEW_AND_MODIFIED" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "lastsync", "0" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "PRIVATE" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "PUBLIC" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "SHARED" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "GLOBALADDRESSBOOK" );
  WebdavHandler::addSloxElement( mRes, doc, prop, "foldertype", "INTERNALUSERS" );

  kdDebug() << k_funcinfo << doc.toString( 2 ) << endl;

  mDownloadJob = KIO::davPropFind( url, doc, "0", false );

  connect( mDownloadJob, SIGNAL( result( KIO::Job * ) ),
           SLOT( slotResult( KIO::Job * ) ) );
}

void SloxFolderManager::slotResult( KIO::Job *job )
{
  kdDebug() << k_funcinfo << endl;

  if ( job->error() ) {
    job->showErrorDialog( 0 );
  } else {
    kdDebug() << k_funcinfo << " success, writing to " << cacheFile() << endl;
    QFile f( cacheFile() );
    if ( !f.open( QIODevice::WriteOnly ) ) {
      kdDebug() << "Unable to open '" << cacheFile() << "'" << endl;
      return;
    }
    QTextStream stream ( &f );
    stream << mDownloadJob->response();
    f.close();
    readFolders();
  }

  mDownloadJob = 0;
  emit foldersUpdated();
}

QString SloxFolderManager::cacheFile() const
{
  QString host = mBaseUrl.host();

  QString file = locateLocal( "cache", "slox/folders_" + host );

  kdDebug() << k_funcinfo << file << endl;

  return file;
}

void SloxFolderManager::readFolders()
{
  kdDebug() << k_funcinfo << endl;

  QFile f( cacheFile() );
  if ( !f.open( QIODevice::ReadOnly ) ) {
    kdDebug() << "Unable to open '" << cacheFile() << "'" << endl;
    requestFolders();
    return;
  }

  QDomDocument doc;
  doc.setContent( &f );

  mFolders.clear();

  QDomNodeList nodes = doc.elementsByTagName( "D:prop" );
  for( int i = 0; i < nodes.count(); ++i ) {
    QDomElement element = nodes.item(i).toElement();
    QString id = "-1", parentId = "-1"; // OX default folder
    bool def = false;
    QString name, type;
    QDomNode n;
    for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
      QDomElement e = n.toElement();
      QString tag = e.tagName();
      QString value = e.text();
      if ( tag == "ox:object_id" ) id = value;
      else if ( tag == "ox:folder_id" ) parentId = value;
      else if ( tag == "ox:title" ) name = value;
      else if ( tag == "ox:module" ) type = value;
      else if ( tag == "ox:defaultfolder" ) def = (value == "true");
    }
    if ( id != "-1" && parentId != "-1" ) {
      SloxFolder *folder = new SloxFolder( id, parentId, type, name, def );
      mFolders[id] = folder;
      kdDebug() << k_funcinfo << "Found folder: " << folder->name() << endl;
    }
  }

  // add top-level system folders that are not contained in the folder listing
  SloxFolder *folder = new SloxFolder( "1", "0", "unbound", i18n("Private Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "2", "0", "unbound", i18n("Public Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "3", "0", "unbound", i18n("Shared Folder") );
  mFolders[folder->id()] = folder;
  folder = new SloxFolder( "4", "0", "unbound", i18n("System Folder") );
  mFolders[folder->id()] = folder;
}

#include "sloxfoldermanager.moc"
<|endoftext|>


<commit_before>#include "Visualization.h"

Visualization::Visualization(uint16_t columns,
  uint16_t rows,
  uint8_t hue,
  uint8_t saturation,
  CRGB * leds,
  uint8_t cycle) {
  this->columns = columns;
  this->rows = rows;
  this->leds = leds;
  this->hue = hue;
  this->saturation = saturation;
  this->frame = 0;
  this->nextTime = 0;
  this->interval = 20;
  this->value = 255;
  this->color = CHSV(hue, saturation, 255);
  this->setCycle(cycle);
  this->cycleOffset = 0;
}

void Visualization::setLEDColorXY(uint16_t x, uint16_t y) {
  this->setLEDColor(xy2Pos(x, y));
}

void Visualization::setLEDColor(uint16_t pos) {
  this->leds[pos] = this->color;
}

uint16_t Visualization::xy2Pos(uint16_t x, uint16_t y) {
  uint16_t pos = x * this->rows;
  pos = pos + y;

  // if (x % 2 == 0) {
  //   pos = pos + y;
  // } else {
  //   pos = pos + ((this->rows - 1) - y);
  // }

  return pos;
}

void Visualization::setHue(uint8_t hue) {
  this->hue = hue;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

uint8_t Visualization::getHue() {
  return this->hue;
}

void Visualization::setSaturation(uint8_t saturation) {
  this->saturation = saturation;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

void Visualization::setValue(uint8_t value) {
  this->value = value;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

uint8_t Visualization::getValue() {
  return this->value;
}

void Visualization::setInterval(uint16_t interval) {
  this->interval = interval;
}

void Visualization::setAllHue(uint8_t hue) {
  this->setAllCRGB(CHSV(hue, this->saturation, this->value));
}

void Visualization::setAll() {
  this->setAllCRGB(CHSV(this->hue, this->saturation, this->value));
}

void Visualization::setHalf() {
  CRGB c = CHSV(this->hue, this->saturation, this->value);
  for (uint16_t y=0; y<this->rows; y++) {
    for (uint16_t x=0; x<this->columns; x+=2) {
      uint16_t pos = this->xy2Pos(x, y);
      this->leds[pos] = c;
    }
  }
}

void Visualization::clearAll() {
  this->setAllCRGB(0x000000);
}

void Visualization::setAllCRGB(CRGB c) {
  for (uint16_t y=0; y<this->rows; y++) {
    for (uint16_t x=0; x<this->columns; x++) {
      uint16_t pos = this->xy2Pos(x, y);
      this->leds[pos] = c;
    }
  }
}

void Visualization::setCycle(uint8_t cycle) {
  this->cycle = cycle;
  this->cyclems = pow(this->cycle / 4, 2);
}

void Visualization::synchronize(uint32_t currentTime, uint32_t sync) {
  uint32_t cycleSync = this->sync +
  ((currentTime - this->lastSyncTime) * this->driftRatio);

  uint32_t lastCycleOffset = this->cycleOffset;

  this->lastSync = this->sync;

  // don't calculate drift on the first loop
  float lastDriftRatio = (float)(sync - this->lastSync) / (float)(currentTime - this->lastSyncTime);
  if (this->sync == 0) {
    // don't do anything on the first pass
  } else if (this->sync != 0 && this->driftRatio == 1.0) {
    // second pass, just set the drift
    this->driftRatio = lastDriftRatio;
  } else if (abs((int)cycleSync - (int)sync) > 10000) {
    this->driftRatio = lastDriftRatio;
  } else {
    // use a moving average
    this->driftRatio = (this->driftRatio * 0.9) + (lastDriftRatio * 0.1);
  }

  this->sync = sync;

  // Serial.print(this->lastSyncTime);
  // Serial.print("\t");
  // Serial.print(this->lastSync);
  // Serial.print("\t");
  // Serial.print(currentTime);
  // Serial.print("\t");
  // Serial.print(this->sync);
  // Serial.print("\t");
  // Serial.print((int)currentTime - (int)this->lastSyncTime);
  // Serial.print("\t");
  // Serial.print((int)this->sync - (int)this->lastSync);
  // Serial.print("\t");
  // Serial.print(this->driftRatio);
  // Serial.print("\t");
  // Serial.print(cycleSync);
  // Serial.print("\t");
  // Serial.print((int)cycleSync - (int)sync);
  // Serial.println();

  this->lastSyncTime = currentTime;
}

void Visualization::cycleLoop(uint32_t currentTime) {
  uint32_t cycleSync = this->sync +
    ((currentTime - this->lastSyncTime) * this->driftRatio);

  if (this->cycle > 0) {
    this->setHue((cycleSync / this->cyclems) % 256);
  }

  // if (currentTime > lastLog + 3000) {
  //   Serial.print(currentTime);
  //   Serial.print("\t");
  //   Serial.print(this->cycle);
  //   Serial.print("\t");
  //   Serial.print(this->cyclems);
  //   Serial.print("\t");
  //   Serial.print(this->sync);
  //   Serial.print("\t");
  //   Serial.print(cycleSync);
  //   Serial.print("\t");
  //   Serial.print(this->hue);
  //   Serial.println();
  //   lastLog = currentTime;
  // }
}
<commit_msg>fix wand reset sync<commit_after>#include "Visualization.h"

Visualization::Visualization(uint16_t columns,
  uint16_t rows,
  uint8_t hue,
  uint8_t saturation,
  CRGB * leds,
  uint8_t cycle) {
  this->columns = columns;
  this->rows = rows;
  this->leds = leds;
  this->hue = hue;
  this->saturation = saturation;
  this->frame = 0;
  this->nextTime = 0;
  this->interval = 20;
  this->value = 255;
  this->color = CHSV(hue, saturation, 255);
  this->setCycle(cycle);
  this->cycleOffset = 0;
}

void Visualization::setLEDColorXY(uint16_t x, uint16_t y) {
  this->setLEDColor(xy2Pos(x, y));
}

void Visualization::setLEDColor(uint16_t pos) {
  this->leds[pos] = this->color;
}

uint16_t Visualization::xy2Pos(uint16_t x, uint16_t y) {
  uint16_t pos = x * this->rows;
  pos = pos + y;

  // if (x % 2 == 0) {
  //   pos = pos + y;
  // } else {
  //   pos = pos + ((this->rows - 1) - y);
  // }

  return pos;
}

void Visualization::setHue(uint8_t hue) {
  this->hue = hue;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

uint8_t Visualization::getHue() {
  return this->hue;
}

void Visualization::setSaturation(uint8_t saturation) {
  this->saturation = saturation;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

void Visualization::setValue(uint8_t value) {
  this->value = value;
  this->color = CHSV(this->hue, this->saturation, this->value);
}

uint8_t Visualization::getValue() {
  return this->value;
}

void Visualization::setInterval(uint16_t interval) {
  this->interval = interval;
}

void Visualization::setAllHue(uint8_t hue) {
  this->setAllCRGB(CHSV(hue, this->saturation, this->value));
}

void Visualization::setAll() {
  this->setAllCRGB(CHSV(this->hue, this->saturation, this->value));
}

void Visualization::setHalf() {
  CRGB c = CHSV(this->hue, this->saturation, this->value);
  for (uint16_t y=0; y<this->rows; y++) {
    for (uint16_t x=0; x<this->columns; x+=2) {
      uint16_t pos = this->xy2Pos(x, y);
      this->leds[pos] = c;
    }
  }
}

void Visualization::clearAll() {
  this->setAllCRGB(0x000000);
}

void Visualization::setAllCRGB(CRGB c) {
  for (uint16_t y=0; y<this->rows; y++) {
    for (uint16_t x=0; x<this->columns; x++) {
      uint16_t pos = this->xy2Pos(x, y);
      this->leds[pos] = c;
    }
  }
}

void Visualization::setCycle(uint8_t cycle) {
  this->cycle = cycle;
  this->cyclems = pow(this->cycle / 4, 2);
}

void Visualization::synchronize(uint32_t currentTime, uint32_t sync) {
  uint32_t cycleSync = this->sync +
  ((currentTime - this->lastSyncTime) * this->driftRatio);

  uint32_t lastCycleOffset = this->cycleOffset;

  this->lastSync = this->sync;

  // don't calculate drift on the first loop
  float lastDriftRatio =
    (float)(sync - this->lastSync) / (float)(currentTime - this->lastSyncTime);

  if (this->sync == 0) {
    // don't do anything on the first pass
  } else if (this->sync != 0 && this->driftRatio == 1.0) {
    // second pass, just set the drift
    this->driftRatio = lastDriftRatio;
  } else if (abs((int)cycleSync - (int)sync) > 10000) {
    this->driftRatio = 1.0;
  } else {
    // use a moving average
    this->driftRatio = (this->driftRatio * 0.9) + (lastDriftRatio * 0.1);
  }

  this->sync = sync;

  // Serial.print(this->lastSyncTime);
  // Serial.print("\t");
  // Serial.print(this->lastSync);
  // Serial.print("\t");
  // Serial.print(currentTime);
  // Serial.print("\t");
  // Serial.print(this->sync);
  // Serial.print("\t");
  // Serial.print((int)currentTime - (int)this->lastSyncTime);
  // Serial.print("\t");
  // Serial.print((int)this->sync - (int)this->lastSync);
  // Serial.print("\t");
  // Serial.print(this->driftRatio);
  // Serial.print("\t");
  // Serial.print(cycleSync);
  // Serial.print("\t");
  // Serial.print((int)cycleSync - (int)sync);
  // Serial.println();

  this->lastSyncTime = currentTime;
}

void Visualization::cycleLoop(uint32_t currentTime) {
  uint32_t cycleSync = this->sync +
    ((currentTime - this->lastSyncTime) * this->driftRatio);

  if (this->cycle > 0) {
    this->setHue((cycleSync / this->cyclems) % 256);
  }

  // if (currentTime > lastLog + 3000) {
  //   Serial.print(currentTime);
  //   Serial.print("\t");
  //   Serial.print(this->cycle);
  //   Serial.print("\t");
  //   Serial.print(this->cyclems);
  //   Serial.print("\t");
  //   Serial.print(this->sync);
  //   Serial.print("\t");
  //   Serial.print(cycleSync);
  //   Serial.print("\t");
  //   Serial.print(this->hue);
  //   Serial.println();
  //   lastLog = currentTime;
  // }
}
<|endoftext|>


<commit_before>// $Id$
/***********************************************************************
 Moses - factored phrase-based, hierarchical and syntactic language decoder
 Copyright (C) 2009 Hieu Hoang

 This library 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.

 This library 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 this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/
#include <string>
#include <fstream>
#include "OnDiskWrapper.h"
#include "Vocab.h"
#include "../moses/src/FactorCollection.h"

using namespace std;

namespace OnDiskPt
{

bool Vocab::Load(OnDiskWrapper &onDiskWrapper)
{
  fstream &file = onDiskWrapper.GetFileVocab();

  string line;
  while(getline(file, line)) {
    vector<string> tokens;
    Moses::Tokenize(tokens, line);
    CHECK(tokens.size() == 2);
    const string &key = tokens[0];
    m_vocabColl[key] =  Moses::Scan<UINT64>(tokens[1]);
  }

  // create lookup
  // assume contiguous vocab id
  m_lookup.resize(m_vocabColl.size() + 1);

  CollType::const_iterator iter;
  for (iter = m_vocabColl.begin(); iter != m_vocabColl.end(); ++iter) {
    UINT32 vocabId = iter->second;
    const std::string &word = iter->first;

    m_lookup[vocabId] = word;
  }

  return true;
}

void Vocab::Save(OnDiskWrapper &onDiskWrapper)
{
  fstream &file = onDiskWrapper.GetFileVocab();
  CollType::const_iterator iterVocab;
  for (iterVocab = m_vocabColl.begin(); iterVocab != m_vocabColl.end(); ++iterVocab) {
    const string &word = iterVocab->first;
    UINT32 vocabId = iterVocab->second;

    file << word << " " << vocabId << endl;
  }
}

UINT64 Vocab::AddVocabId(const std::string &factorString)
{
  // find string id
  CollType::const_iterator iter = m_vocabColl.find(factorString);
  if (iter == m_vocabColl.end()) {
    // add new vocab entry
    m_vocabColl[factorString] = m_nextId;
    return m_nextId++;
  } else {
    // return existing entry
    return iter->second;
  }
}

UINT64 Vocab::GetVocabId(const std::string &factorString, bool &found) const
{
  // find string id
  CollType::const_iterator iter = m_vocabColl.find(factorString);
  if (iter == m_vocabColl.end()) {
    found = false;
    return 0; //return whatever
  } else {
    // return existing entry
    found = true;
    return iter->second;
  }
}

const Moses::Factor *Vocab::GetFactor(UINT32 vocabId, Moses::FactorType factorType, Moses::FactorDirection direction, bool isNonTerminal) const
{
  string str = GetString(vocabId);
  if (isNonTerminal) {
    str = str.substr(1, str.size() - 2);
  }
  const Moses::Factor *factor = Moses::FactorCollection::Instance().AddFactor(direction, factorType, str);
  return factor;
}

}
<commit_msg>bug. Unknown words given id starting from 1<commit_after>// $Id$
/***********************************************************************
 Moses - factored phrase-based, hierarchical and syntactic language decoder
 Copyright (C) 2009 Hieu Hoang

 This library 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.

 This library 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 this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/
#include <string>
#include <fstream>
#include "OnDiskWrapper.h"
#include "Vocab.h"
#include "../moses/src/FactorCollection.h"

using namespace std;

namespace OnDiskPt
{

bool Vocab::Load(OnDiskWrapper &onDiskWrapper)
{
  fstream &file = onDiskWrapper.GetFileVocab();

  string line;
  while(getline(file, line)) {
    vector<string> tokens;
    Moses::Tokenize(tokens, line);
    CHECK(tokens.size() == 2);
    const string &key = tokens[0];
    m_vocabColl[key] =  Moses::Scan<UINT64>(tokens[1]);
  }

  // create lookup
  // assume contiguous vocab id
  m_lookup.resize(m_vocabColl.size() + 1);
	m_nextId = m_lookup.size();

  CollType::const_iterator iter;
  for (iter = m_vocabColl.begin(); iter != m_vocabColl.end(); ++iter) {
    UINT32 vocabId = iter->second;
    const std::string &word = iter->first;

    m_lookup[vocabId] = word;
  }

  return true;
}

void Vocab::Save(OnDiskWrapper &onDiskWrapper)
{
  fstream &file = onDiskWrapper.GetFileVocab();
  CollType::const_iterator iterVocab;
  for (iterVocab = m_vocabColl.begin(); iterVocab != m_vocabColl.end(); ++iterVocab) {
    const string &word = iterVocab->first;
    UINT32 vocabId = iterVocab->second;

    file << word << " " << vocabId << endl;
  }
}

UINT64 Vocab::AddVocabId(const std::string &factorString)
{
  // find string id
  CollType::const_iterator iter = m_vocabColl.find(factorString);
  if (iter == m_vocabColl.end()) {
    // add new vocab entry
    m_vocabColl[factorString] = m_nextId;
    return m_nextId++;
  } else {
    // return existing entry
    return iter->second;
  }
}

UINT64 Vocab::GetVocabId(const std::string &factorString, bool &found) const
{
  // find string id
  CollType::const_iterator iter = m_vocabColl.find(factorString);
  if (iter == m_vocabColl.end()) {
    found = false;
    return 0; //return whatever
  } else {
    // return existing entry
    found = true;
    return iter->second;
  }
}

const Moses::Factor *Vocab::GetFactor(UINT32 vocabId, Moses::FactorType factorType, Moses::FactorDirection direction, bool isNonTerminal) const
{
  string str = GetString(vocabId);
  if (isNonTerminal) {
    str = str.substr(1, str.size() - 2);
  }
  const Moses::Factor *factor = Moses::FactorCollection::Instance().AddFactor(direction, factorType, str);
  return factor;
}

}
<|endoftext|>


<commit_before><commit_msg>[IndVars] Factor out some common code into a utility function<commit_after><|endoftext|>


<commit_before>// Test that TLS is unpoisoned on thread death.
// REQUIRES: x86_64-supported-target,i386-supported-target

// RUN: %clangxx_asan -m64 -O1 %p/SharedLibs/dlclose-test-so.cc \
// RUN:     -fPIC -shared -o %t-so.so
// RUN: %clangxx_asan -m64 -O1 %s -o %t && %t 2>&1
// RUN: %clangxx_asan -m32 -O1 %p/SharedLibs/dlclose-test-so.cc \
// RUN:     -fPIC -shared -o %t-so.so
// RUN: %clangxx_asan -m32 -O1 %s -o %t && %t 2>&1

#include <assert.h>
#include <pthread.h>
#include <stdio.h>

#include <sanitizer/asan_interface.h>

__thread int64_t tls_var[2];

volatile int64_t *p_tls_var;

void *first(void *arg) {
  ASAN_POISON_MEMORY_REGION(&tls_var, sizeof(tls_var));
  p_tls_var = tls_var;
  return 0;
}

void *second(void *arg) {
  assert(tls_var == p_tls_var);
  *p_tls_var = 1;
  return 0;
}

int main(int argc, char *argv[]) {
  pthread_t p;
  assert(0 == pthread_create(&p, 0, first, 0));
  assert(0 == pthread_join(p, 0));
  assert(0 == pthread_create(&p, 0, second, 0));
  assert(0 == pthread_join(p, 0));
  return 0;
}
<commit_msg>[asan] Fix r182858.<commit_after>// Test that TLS is unpoisoned on thread death.
// REQUIRES: x86_64-supported-target,i386-supported-target

// RUN: %clangxx_asan -m64 -O1 %s -o %t && %t 2>&1
// RUN: %clangxx_asan -m32 -O1 %s -o %t && %t 2>&1

#include <assert.h>
#include <pthread.h>
#include <stdio.h>

#include <sanitizer/asan_interface.h>

__thread int64_t tls_var[2];

volatile int64_t *p_tls_var;

void *first(void *arg) {
  ASAN_POISON_MEMORY_REGION(&tls_var, sizeof(tls_var));
  p_tls_var = tls_var;
  return 0;
}

void *second(void *arg) {
  assert(tls_var == p_tls_var);
  *p_tls_var = 1;
  return 0;
}

int main(int argc, char *argv[]) {
  pthread_t p;
  assert(0 == pthread_create(&p, 0, first, 0));
  assert(0 == pthread_join(p, 0));
  assert(0 == pthread_create(&p, 0, second, 0));
  assert(0 == pthread_join(p, 0));
  return 0;
}
<|endoftext|>


<commit_before>#include "PRINTOBJ.H"

#include "DRAW.H"
#include "SPECIFIC.H"
#include "MATHS.H"
#include "3D_GEN.H"
#include "CONTROL.H"
#include "SETUP.H"
#include "ITEMS.H"
#include "EFFECTS.H"
#include "LOAD_LEV.H"
#include "DELTAPAK.H"
#include "DRAWOBJ.H"

void CalcAllAnimatingItems_ASM()
{
	UNIMPLEMENTED();
}

void DrawEffect(short item_num)
{
	struct FX_INFO* fx = &effects[item_num];
	struct object_info* obj = &objects[fx->object_number];

	if (obj->draw_routine != NULL && obj->loaded)
	{
		mPushMatrix();
		mTranslateAbsXYZ(fx->pos.x_pos, fx->pos.y_pos, fx->pos.z_pos);

		if (Matrix->tz < 20480)
		{
			mRotYXZ(fx->pos.y_rot, fx->pos.x_rot, fx->pos.z_rot);

			S_CalculateLight(fx->pos.x_pos, fx->pos.y_pos, fx->pos.z_pos, fx->room_number, &duff_item.il);
			duff_item.il.Light[3].pad = 0;

			phd_PutPolygons(meshes[obj->nmeshes != 0 ? obj->mesh_index : fx->frame_number], -1);
		}

		mPopMatrix();
	}
}

void PrintAllOtherObjects_ASM(short room_num)//(F)
{
	struct room_info* r = &room[room_num];

	r->bound_active = 0;

	mPushMatrix();
	mTranslateAbsXYZ(r->x, r->y, r->z);

	phd_right = 512;
	phd_bottom = 240;
	phd_left = 0;
	phd_top = 0;

	for (short item_num = r->item_number; item_num != -1; item_num = items[item_num].next_item)
	{
		struct ITEM_INFO* item = &items[item_num];
		struct object_info* obj = &objects[item->object_number];

#if _DEBUG
		if (item->object_number == LARA)
		{
			phd_PutPolygons(meshes[obj->mesh_index], -1);
		}
#endif

		if (item->status != ITEM_INVISIBLE && !obj->using_drawanimating_item && obj->draw_routine != NULL)
		{
			obj->draw_routine(item);
		}

		if (obj->draw_routine_extra != NULL)
		{
			obj->draw_routine_extra(item);
		}

		if (item->after_death < 0x80)
		{
			if (++item->after_death == 0x80)
				KillItem(item_num);
		}
	}

	for (short item_num = r->fx_number; item_num != -1; item_num = effects[item_num].next_fx)
	{
		DrawEffect(item_num);
	}

	mPopMatrix();

	r->left = 511;
	r->top = 239;
}

void print_all_object_NOW()//8F474(<), 914B8(<) (F)
{
	CalcAllAnimatingItems_ASM();

	for (int i = 0; i < number_draw_rooms; i++)
	{
		PrintAllOtherObjects_ASM(draw_rooms[i]);
	}
}<commit_msg>[Specific-PSXPC_N] Correct PrintAllOtherObjects_ASM<commit_after>#include "PRINTOBJ.H"

#include "DRAW.H"
#include "SPECIFIC.H"
#include "MATHS.H"
#include "3D_GEN.H"
#include "CONTROL.H"
#include "SETUP.H"
#include "ITEMS.H"
#include "EFFECTS.H"
#include "LOAD_LEV.H"
#include "DELTAPAK.H"
#include "DRAWOBJ.H"

void CalcAllAnimatingItems_ASM()
{
	UNIMPLEMENTED();
}

void DrawEffect(short item_num)
{
	struct FX_INFO* fx = &effects[item_num];
	struct object_info* obj = &objects[fx->object_number];

	if (obj->draw_routine != NULL && obj->loaded)
	{
		mPushMatrix();
		mTranslateAbsXYZ(fx->pos.x_pos, fx->pos.y_pos, fx->pos.z_pos);

		if (Matrix->tz < 20480)
		{
			mRotYXZ(fx->pos.y_rot, fx->pos.x_rot, fx->pos.z_rot);

			S_CalculateLight(fx->pos.x_pos, fx->pos.y_pos, fx->pos.z_pos, fx->room_number, &duff_item.il);
			duff_item.il.Light[3].pad = 0;

			phd_PutPolygons(meshes[obj->nmeshes != 0 ? obj->mesh_index : fx->frame_number], -1);
		}

		mPopMatrix();
	}
}

void PrintAllOtherObjects_ASM(short room_num /*s3*/)//(F)
{
	struct room_info* r = &room[room_num];
	struct ITEM_INFO* item = NULL;
	struct object_info* object;
	short item_num;
	struct FX_INFO* effect;
	int mesh_num;
	short* mesh;
	int effect_num;

	r->bound_active = 0;
	mPushMatrix();
	mTranslateAbsXYZ(r->x, r->y, r->z);

	phd_right = 512;
	phd_bottom = 240;
	phd_left = 0;
	phd_top = 0;

	item_num = r->item_number;

	if (item_num != -1)
	{
		do
		{
			item = &items[item_num];
			object = &objects[item->object_number];

			if (item->status != 6)
			{
				if (!object->using_drawanimating_item && object->draw_routine != NULL)
				{
					object->draw_routine(item);
				}
			}
			//loc_8F5AC
			if (object->draw_routine_extra != NULL)
			{
				object->draw_routine_extra(item);
			}

			//loc_8F5C4
			if (item->after_death - 1 < 127)
			{
				item->after_death++;

				if (item->after_death == 128)
				{
					KillItem(r->item_number);
				}
			}//loc_8F5EC

			item_num = item->next_item;
		} while (item_num != -1);

	}//loc_8F5FC

	effect_num = r->fx_number;

	if (effect_num != -1)
	{
		//loc_8F608
		do
		{
			effect = &effect[effect_num];
			object = &objects[effect->object_number];

			if (object->draw_routine != NULL && object->loaded)
			{
				mPushMatrix();
				mTranslateAbsXYZ(effect->pos.x_pos, effect->pos.y_pos, effect->pos.z_pos);

				if (((int*)MatrixStack)[2] - 21 < 20459)
				{
					mRotYXZ(effect->pos.y_rot, effect->pos.x_rot, effect->pos.z_rot);

					duff_item.il.Light[3].pad = 0;
					S_CalculateLight(effect->pos.x_pos, effect->pos.y_pos, effect->pos.z_pos, effect->room_number, &duff_item.il);

					mesh_num = effect->frame_number;

					if (object->nmeshes != 0)
					{
						mesh_num = object->mesh_index;
					}//loc_8F6C0

					phd_PutPolygons(meshes[mesh_num], -1);
				}//loc_8F6D8

				mPopMatrix();

				//loc_8F6E0
				effect_num = effect->next_fx;
			}
		} while (effect_num != -1);//loc_8F6E0
	}//loc_8F6EC

	mPopMatrix();
	((int*)&r->left)[0] = 511;
	((int*)&r->top)[0] = 239;
}

void print_all_object_NOW()//8F474(<), 914B8(<) (F)
{
	CalcAllAnimatingItems_ASM();

	for (int i = 0; i < number_draw_rooms; i++)
	{
		PrintAllOtherObjects_ASM(draw_rooms[i]);
	}
}<|endoftext|>


<commit_before>//vbSort.cpp
//Copyright (c) 2015 mmYYmmdd
#include "stdafx.h"
#include <algorithm>
#include "VBA_NestFunc.hpp"

//r֐
class compareByVBAfunc   {
    VARIANT*        begin;
    functionExpr*   comp;
public:
    compareByVBAfunc(VARIANT* pA, functionExpr& f) noexcept : begin(pA), comp(&f)    {    }
    compareByVBAfunc(compareByVBAfunc const&) = default;
    compareByVBAfunc(compareByVBAfunc&&) = delete;
    ~compareByVBAfunc() = default;
    bool valid() const noexcept { return comp != nullptr;  }
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        return comp->eval(begin + i, begin + j)->lVal ? true: false;
    }
};

//1
class compFunctor  {
    VARIANT*    begin;
public:
    explicit compFunctor(VARIANT* pA) noexcept : begin(pA) { }
    compFunctor(compFunctor const&) = default;
    compFunctor(compFunctor&&) = delete;
    ~compFunctor() = default;
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        auto a = ::VarCmp(begin + i, begin + j, LANG_JAPANESE, 0);
        return (a == VARCMP_LT) || (a == VARCMP_EQ && (i < j));
    }
};

//2
class compDictionaryFunctor  {
    VARIANT*    begin;
public:
    explicit compDictionaryFunctor(VARIANT* pA) noexcept : begin(pA) { }
    compDictionaryFunctor(compDictionaryFunctor const&) = default;
    compDictionaryFunctor(compDictionaryFunctor&&) = delete;
    ~compDictionaryFunctor() = default;
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        safearrayRef arr1(begin + i);
        safearrayRef arr2(begin + j);
        if ( arr1.getDim() != 1 || arr2.getDim() != 1 )     return false;
        for ( ULONG k = 0; k < arr1.getSize(1) && k < arr2.getSize(1); ++k )
        {
            auto a = ::VarCmp(&arr1(k), &arr2(k), LANG_JAPANESE, 0);
            if ( a == VARCMP_LT )   return true;
            if ( a == VARCMP_GT )   return false;
        }
        return i < j;
    }
};

VARIANT __stdcall stdsort(VARIANT* array, __int32 defaultFlag, VARIANT* pComp)
{
    VARIANT      ret;
    ::VariantInit(&ret);
    safearrayRef arrIn(array);
    if ( 1 != arrIn.getDim() )          return ret;
    auto index = std::make_unique<__int32[]>(arrIn.getSize(1));
    auto VArray = std::make_unique<VARIANT[]>(arrIn.getSize(1));
    auto refFlag = std::make_unique<bool[]>(arrIn.getSize(1));
    for (std::size_t i = 0; i < arrIn.getSize(1); ++i)
    {
        index[i] = static_cast<__int32>(i);
        ::VariantInit(&VArray[i]);
        if ( VT_BYREF & arrIn(i).vt )
        {
            refFlag[i] = true;
            ::VariantCopyInd(&VArray[i], &arrIn(i));
        }
        else
        {
            refFlag[i] = false;
            VArray[i] = arrIn(i);
        }
    }
    if ( defaultFlag == 1 ) //1
    {
        compFunctor   functor(VArray.get());
        std::sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    else if ( defaultFlag == 2 ) //2
    {
        compDictionaryFunctor   functor(VArray.get());
        std::sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    else if ( pComp )
    {
        functionExpr comp(pComp);
        compareByVBAfunc functor(VArray.get(), comp);
        if ( functor.valid() )
            std::stable_sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    //-------------------------------------------------------
    SAFEARRAYBOUND boundRet = { static_cast<ULONG>(arrIn.getSize(1)), 0};   //vfALBound
    ret.vt = VT_ARRAY | VT_VARIANT;
    ret.parray = ::SafeArrayCreate(VT_VARIANT, 1, &boundRet);
    safearrayRef arrOut(&ret);
    VARIANT elem;
    ::VariantInit(&elem);
    elem.vt = VT_I4;
    for ( std::size_t i = 0; i < arrIn.getSize(1); ++i )
    {
        elem.lVal = static_cast<decltype(elem.lVal)>(index[i] + arrIn.getOriginalLBound(1));
        ::VariantCopy(&arrOut(i), &elem);
        if ( refFlag[i] )   ::VariantClear(&VArray[i]);
    }
    return      ret;
}
<commit_msg>Refactoring<commit_after>//vbSort.cpp
//Copyright (c) 2015 mmYYmmdd
#include "stdafx.h"
#include <algorithm>
#include "VBA_NestFunc.hpp"

//r֐
class compareByVBAfunc   {
    VARIANT*        begin;
    functionExpr*   comp;
public:
    compareByVBAfunc(VARIANT* pA, functionExpr& f) noexcept : begin(pA), comp(&f)    {    }
    compareByVBAfunc(compareByVBAfunc const&) = default;
    compareByVBAfunc(compareByVBAfunc&&) = delete;
    ~compareByVBAfunc() = default;
    bool valid() const noexcept { return comp != nullptr;  }
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        return comp->eval(begin + i, begin + j)->lVal ? true: false;
    }
};

//1
class compFunctor  {
    VARIANT*    begin;
public:
    explicit compFunctor(VARIANT* pA) noexcept : begin(pA) { }
    compFunctor(compFunctor const&) = default;
    compFunctor(compFunctor&&) = delete;
    ~compFunctor() = default;
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        auto a = ::VarCmp(begin + i, begin + j, LANG_JAPANESE, 0);
        return (a == VARCMP_LT) || (a == VARCMP_EQ && (i < j));
    }
};

//2
class compDictionaryFunctor  {
    VARIANT*    begin;
public:
    explicit compDictionaryFunctor(VARIANT* pA) noexcept : begin(pA) { }
    compDictionaryFunctor(compDictionaryFunctor const&) = default;
    compDictionaryFunctor(compDictionaryFunctor&&) = delete;
    ~compDictionaryFunctor() = default;
    bool operator ()(__int32 i, __int32 j) const noexcept
    {
        safearrayRef arr1{begin + i};
        safearrayRef arr2{begin + j};
        if ( arr1.getDim() != 1 || arr2.getDim() != 1 )     return false;
        for ( ULONG k = 0; k < arr1.getSize(1) && k < arr2.getSize(1); ++k )
        {
            auto a = ::VarCmp(&arr1(k), &arr2(k), LANG_JAPANESE, 0);
            if ( a == VARCMP_LT )   return true;
            if ( a == VARCMP_GT )   return false;
        }
        return i < j;
    }
};

VARIANT __stdcall stdsort(VARIANT* array, __int32 defaultFlag, VARIANT* pComp)
{
    safearrayRef arrIn{array};
    if ( 1 != arrIn.getDim() )          return iVariant();
    auto index = std::make_unique<__int32[]>(arrIn.getSize(1));
    auto VArray = std::make_unique<VARIANT[]>(arrIn.getSize(1));
    auto refFlag = std::make_unique<bool[]>(arrIn.getSize(1));
    for (std::size_t i = 0; i < arrIn.getSize(1); ++i)
    {
        index[i] = static_cast<__int32>(i);
        ::VariantInit(&VArray[i]);
        if ( VT_BYREF & arrIn(i).vt )
        {
            refFlag[i] = true;
            ::VariantCopyInd(&VArray[i], &arrIn(i));
        }
        else
        {
            refFlag[i] = false;
            VArray[i] = arrIn(i);
        }
    }
    if ( defaultFlag == 1 ) //1
    {
        compFunctor   functor{VArray.get()};
        std::sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    else if ( defaultFlag == 2 ) //2
    {
        compDictionaryFunctor   functor(VArray.get());
        std::sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    else if ( pComp )
    {
        functionExpr comp{pComp};
        compareByVBAfunc functor{VArray.get(), comp};
        if ( functor.valid() )
            std::stable_sort(index.get(), index.get() + arrIn.getSize(1), functor);
    }
    //-------------------------------------------------------
    SAFEARRAYBOUND boundRet = { static_cast<ULONG>(arrIn.getSize(1)), 0};   //vfALBound
    auto ret = iVariant(VT_ARRAY | VT_VARIANT);
    ret.parray = ::SafeArrayCreate(VT_VARIANT, 1, &boundRet);
    safearrayRef arrOut{&ret};
    auto elem= iVariant(VT_I4);
    for ( std::size_t i = 0; i < arrIn.getSize(1); ++i )
    {
        elem.lVal = static_cast<decltype(elem.lVal)>(index[i] + arrIn.getOriginalLBound(1));
        ::VariantCopy(&arrOut(i), &elem);
        if ( refFlag[i] )   ::VariantClear(&VArray[i]);
    }
    return  ret;
}
<|endoftext|>