- committed local changes

git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@270 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2015-05-28 17:41:06 +00:00
parent 549089a440
commit 7fada2a227
10 changed files with 467 additions and 640 deletions
+39 -174
View File
@@ -11,165 +11,77 @@
#ifndef LAYERARRAY_HPP
#define LAYERARRAY_HPP
#include <stdint.h>
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
template <class T>
class LayerArray;
template <class T>
class LayerArrayListener
{
public:
LayerArrayListener() {}
virtual~LayerArrayListener() {}
virtual void onChanged(const LayerArray<T> &obj) = 0;
virtual void onChanged(const LayerArray &obj) = 0;
};
template <class T>
class LayerArray
{
class Entry : public T
{
public:
Entry(uint32_t numUnits, const VectorXd *pInit)
: T(numUnits, pInit)
, pPrev(nullptr)
, pNext(nullptr)
{
}
~Entry()
{
}
Entry *pPrev;
Entry *pNext;
private:
};
public:
LayerArray(LayerArrayListener<T> *pListener = nullptr)
: m_size(0)
, m_pRoot(nullptr)
, m_ppIndex(nullptr)
, m_pListener(pListener)
LayerArray(LayerArrayListener *pListener = nullptr)
: m_pListener(pListener)
{
}
LayerArray(uint32_t numLayers, uint32_t numUnitsPerLayer)
: m_size(0)
, m_pRoot(nullptr)
, m_ppIndex(nullptr)
, m_pListener(nullptr)
{
uint32_t i;
for (i=0; i < numLayers; i++)
{
add(nullptr, numUnitsPerLayer);
}
}
LayerArray (const LayerArray<T> &rhs)
: m_size(0)
, m_pRoot(nullptr)
, m_ppIndex(nullptr)
, m_pListener(nullptr)
{
uint32_t i;
for (i=0; i < rhs.getSize(); i++)
{
this->add(&rhs[i].states(), rhs[i].states().size());
}
}
virtual ~LayerArray()
{
m_pListener = nullptr;
clear();
}
T* add(const VectorXd *pData, uint32_t size)
void add(const RowVectorXd &pData, uint32_t size)
{
Entry *pNew;
Entry *pL = m_pRoot;
pNew = new Entry(size, pData);
if (!m_pRoot)
{
m_pRoot = pNew;
}
else
{
pL = m_pRoot;
while(pL->pNext)
{
pL = pL->pNext;
}
pL->pNext = pNew;
pL->pNext->pPrev = pL;
}
rebuildIndex();
m_data << pData;
if (m_pListener)
{
m_pListener->onChanged(*this);
return (T*)pNew;
}
}
void clear()
{
if (m_ppIndex)
{
for (int i=0; i < m_size; i++)
{
if (m_ppIndex[i])
delete m_ppIndex[i];
m_ppIndex[i] = nullptr;
}
}
m_ppIndex = nullptr;
m_pRoot = nullptr;
m_size = 0;
allocIndex(0);
m_data.resize(0,0);
if (m_pListener)
m_pListener->onChanged(*this);
}
T& getAt(uint32_t index) const
const MatrixXd& data() const
{
return *m_ppIndex[index];
return m_data;
}
T& operator[](uint32_t index) const
RowVectorXd getAt(uint32_t index) const
{
return m_data.row(index);
}
RowVectorXd operator[](uint32_t index) const
{
return getAt(index);
}
void removeAt(uint32_t index)
{
if (m_ppIndex[index]->pPrev)
{
m_ppIndex[index]->pPrev->pNext = m_ppIndex[index]->pNext;
}
else
{
m_pRoot = m_ppIndex[index]->pNext;
m_ppIndex[index]->pNext->pPrev = nullptr;
}
delete m_ppIndex[index];
m_ppIndex[index] = nullptr;
rebuildIndex();
if (m_pListener)
m_pListener->onChanged(*this);
}
uint32_t getSize() const
{
return m_size;
return m_data.rows();
}
void save(const char *pFilename)
@@ -182,18 +94,16 @@ public:
return;
fprintf(pFile, "%d\n", m_size);
fprintf(pFile, "%u\n", (uint32_t)m_data.rows());
fprintf(pFile, "%u\n", (uint32_t)m_data.cols());
uint32_t i, j;
for (i=0; i < m_size; i++)
for (i=0; i < m_data.rows(); i++)
{
uint32_t numUnits = m_ppIndex[i]->getNumUnits();
fprintf(pFile, "%d\n", numUnits);
for (j=0; j < numUnits; j++)
for (j=0; j < m_data.cols(); j++)
{
fprintf(pFile, "%3.6f\n", m_ppIndex[i]->states()(j));
fprintf(pFile, "%3.6f\n", m_data.row(i)(j));
}
}
@@ -203,6 +113,7 @@ public:
void load(const char *pFilename)
{
uint32_t size = 0;
uint32_t numUnits = 0;
FILE *pFile;
pFile = fopen(pFilename,"r");
@@ -211,79 +122,33 @@ public:
return;
fscanf(pFile, "%d\n", &size);
fscanf(pFile, "%d\n", &numUnits);
m_data.resize(size, numUnits);
RowVectorXd data(numUnits);
uint32_t i, j;
float v;
double *pData;
for (i=0; i < size; i++)
{
uint32_t numUnits;
fscanf(pFile, "%d\n", &numUnits);
if (numUnits == 0)
break;
float v;
pData = new double[numUnits];
T* data = add(nullptr, numUnits);
for (j=0; j < numUnits; j++)
for (j=0; j < data.size(); j++)
{
fscanf(pFile, "%f", &v);
data->states()(j) = v;
data(j) = v;
}
delete [] pData;
fscanf(pFile, "%d\n", &numUnits);
m_data.row(i) = data;
}
fclose(pFile);
if (m_pListener)
{
m_pListener->onChanged(*this);
}
}
private:
uint32_t m_size;
Entry *m_pRoot;
Entry **m_ppIndex;
LayerArrayListener<T> *m_pListener;
void allocIndex(size_t size)
{
if (m_ppIndex)
{
delete [] m_ppIndex;
m_ppIndex = nullptr;
allocIndex(size);
}
else
{
if (size)
{
m_ppIndex = new Entry*[size];
}
}
}
void rebuildIndex()
{
Entry *pL;
uint32_t size;
size = 0;
pL = m_pRoot;
while(pL)
{
size++;
pL = pL->pNext;
}
allocIndex(size);
size = 0;
pL = m_pRoot;
while(pL)
{
m_ppIndex[size++] = pL;
pL = pL->pNext;
}
m_size = size;
}
LayerArrayListener *m_pListener;
MatrixXd m_data;
};
#endif // LAYERARRAY_HPP