Skip to content

Instantly share code, notes, and snippets.

@sehe
Last active October 24, 2015 22:21
Show Gist options
  • Save sehe/98cc855478e9ab2a5388 to your computer and use it in GitHub Desktop.
Save sehe/98cc855478e9ab2a5388 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <memory>
#include <boost/serialization/vector.hpp>
template<class T, class A = std::allocator<T> >
struct ColorMatrix {
typedef T value_type;
typedef std::vector<value_type, A> Container;
ColorMatrix() : _number_of_columns(0) {}
ColorMatrix(int no_rows, int no_columns, value_type const& initial = value_type())
: _number_of_columns(0)
{
resize(no_rows, no_columns, initial);
}
ColorMatrix(ColorMatrix const& other)
: _data(other._data), _number_of_columns(other._number_of_columns)
{}
ColorMatrix& operator=(ColorMatrix copy) {
swap(*this, copy);
return *this;
}
bool empty() const { return _data.empty(); }
void clear() { _data.clear(); _number_of_columns = 0; }
int rows() const { return _number_of_columns ? _data.size() / _number_of_columns : 0; }
int columns() const { return _number_of_columns; }
value_type* operator[](int row) {
return &_data[row * _number_of_columns];
}
value_type const* operator[](int row) const {
return &_data[row * _number_of_columns];
}
void resize(int no_rows, int no_columns, value_type const& initial = value_type()) {
if (no_rows == 0) {
no_columns = 0;
}
_data.resize(no_rows * no_columns, initial);
_number_of_columns = no_columns;
}
void copyTo(ColorMatrix<T, A> &other){
int myA = rows();
int myB = columns();
int otherB = other.columns();
for (int line = 0; line < myA; ++line){
int myStart = line * myB;
int myEnd = (line + 1) * myB;
int otherStart = line*otherB;
std::cout << "Line: " << line << " S1: " << myStart << " E1: " << myEnd << " S2: " << otherStart << std::endl;
std::copy(_data.begin() + myStart,
_data.begin() + myEnd,
other._data.begin() + otherStart);
}
}
friend void swap(ColorMatrix& a, ColorMatrix& b) {
using std::swap;
swap(a._data, b._data);
swap(a._number_of_columns, b._number_of_columns);
}
private:
Container _data;
int _number_of_columns;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/)
{
ar & _data;
ar & _number_of_columns;
}
};
all:test
CPPFLAGS+=-std=c++1y -Wall -pedantic -Wextra
CPPFLAGS+=-g -O2
#BOOST_DIR=/mnt/LARGE/MODULAR_BOOST/modular-boost
BOOST_DIR=/home/sehe/custom/boost
CPPFLAGS+=-isystem /home/sehe/custom/nonius/include
CPPFLAGS+=-isystem $(BOOST_DIR)
# CPPFLAGS+=-fopenmp
CPPFLAGS+=-pthread
CPPFLAGS+=-march=native
LDFLAGS+=-L $(BOOST_DIR)/stage/lib/ -Wl,-rpath,$(BOOST_DIR)/stage/lib
LDFLAGS+=-lboost_system -lboost_regex -lboost_thread -lboost_iostreams -lboost_serialization
LDFLAGS+=-lboost_date_time -lboost_chrono
CXX=g++-5
# CXX=/usr/lib/gcc-snapshot/bin/g++
# CC=/usr/lib/gcc-snapshot/bin/gcc
# CXX=clang++-3.6 -stdlib=libc++
# CC=clang
%.S:%.cpp
$(CXX) $(CPPFLAGS) $< -S -masm=intel -o - | egrep -v '\s*\.' | c++filt > $@
test.o: ColorMatrix.h
%.o:%.cpp
$(CXX) $(CPPFLAGS) $< -c -o $@
%:%.o
$(CXX) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)
#include <iostream>
#include <vector>
#include <math.h>
#include <fstream>
#include <map>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
//#include <boost/archive/text_oarchive.hpp>
//#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include "ColorMatrix.h"
using namespace std;
int main()
{
cout << "start program" << endl;
ColorMatrix<std::map<int, float>> mData;
const int mSize = 200;
mData.resize(mSize, mSize);
cout << "init" << endl;
for (int x = 0; x < mSize; x++){
for (int y = 0; y < mSize; y++){
if (y % 2 == 0){
mData[x][y][0] = 1.f;
mData[x][y][1] = 0.66666f;
}
else if (y % 3 == 0){
mData[x][y][0] = 1.f;
mData[x][y][1] = 0.1111111111f;
mData[x][y][3] = 0.44444444f;
}
else{
mData[x][y][0] = 41.f;
}
}
}
cout << "write data" << endl;
{
std::ofstream ofs("data.dat", std::ios::binary);
boost::archive::binary_oarchive out_arch(ofs);
//boost::archive::text_oarchive out_arch(ofs);
out_arch << mData;
}
cout << "read data" << endl;
{
std::ifstream ifs("data.dat", std::ios::binary);
if (!ifs) {
cout << "read error!" << endl;
return 1;
}
boost::archive::binary_iarchive in_arch(ifs);
//boost::archive::text_iarchive in_arch(ifs);
ColorMatrix<std::map<int, float>> mData2;
in_arch >> mData2;
}
cout << "complete" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment