grelationaltable.cpp
来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 223 行
CPP
223 行
#include "GRelationalTable.h"#include "GAVLTree.h"#include "GMacros.h"class GRelationalTableNode : public GAVLNode{protected: GRelationalTableFieldComparerFunc m_pCompareFunc; const void* m_pObj;public: GRelationalTableNode(GRelationalTableFieldComparerFunc pCompareFunc, const void* pObj) { Set(pCompareFunc, pObj); } void Set(GRelationalTableFieldComparerFunc pCompareFunc, const void* pObj) { m_pCompareFunc = pCompareFunc; m_pObj = pObj; } virtual ~GRelationalTableNode() { } virtual int Compare(GAVLNode* pThat) { return m_pCompareFunc(m_pObj, ((GRelationalTableNode*)pThat)->m_pObj); } void* GetObject() { return (void*)m_pObj; }};// -------------------------------------------------------------------GRelationalTableEnumerator::GRelationalTableEnumerator(){ m_pFieldEnumerator = NULL; m_pInternalModel = NULL;}GRelationalTableEnumerator::~GRelationalTableEnumerator(){ delete(m_pFieldEnumerator); delete(m_pInternalModel);}void* GRelationalTableEnumerator::GetNext(){ if(!m_pFieldEnumerator) return NULL; GRelationalTableNode* pNode = (GRelationalTableNode*)m_pFieldEnumerator->GetNext(); if(!pNode) return NULL; return pNode->GetObject();}void GRelationalTableEnumerator::InternalInit(GAVLTree* pField, GRelationalTableFieldComparerFunc pCompareFunc, const void* pObj){ if(!m_pInternalModel) m_pInternalModel = new GRelationalTableNode(pCompareFunc, pObj); else m_pInternalModel->Set(pCompareFunc, pObj); delete(m_pFieldEnumerator); m_pFieldEnumerator = new GAVLEnumerator(pField, m_pInternalModel);}// -------------------------------------------------------------------GRelationalTable::GRelationalTable(int nFields, const GRelationalTableFieldComparerFunc pCompareFuncs[]){ m_nFields = nFields; m_pFields = new GAVLTree[nFields]; m_pComparers = new GRelationalTableFieldComparerFunc[nFields]; memcpy(m_pComparers, pCompareFuncs, sizeof(GRelationalTableFieldComparerFunc) * nFields);}GRelationalTable::~GRelationalTable(){ delete[] m_pFields; delete[] m_pComparers;}void GRelationalTable::AddRow(const void* pObj){ int i; for(i = 0; i < m_nFields; i++) { GRelationalTableNode* pNode = new GRelationalTableNode(m_pComparers[i], pObj); m_pFields[i].Insert(pNode); }}void GRelationalTable::Query(GRelationalTableEnumerator* pEnumerator, int nField, const void* pModelObj){ pEnumerator->InternalInit(&m_pFields[nField], m_pComparers[nField], pModelObj);}#ifndef NO_TEST_CODEstruct GRelationalTableTestObject{ int m_age; const char* m_name; int m_shoesize;};int GRelationalTableCompareAges(const void* pA, const void* pB){ struct GRelationalTableTestObject* a = (struct GRelationalTableTestObject*)pA; struct GRelationalTableTestObject* b = (struct GRelationalTableTestObject*)pB; if(a->m_age > b->m_age) return 1; if(a->m_age < b->m_age) return -1; else return 0;}int GRelationalTableCompareNames(const void* pA, const void* pB){ struct GRelationalTableTestObject* a = (struct GRelationalTableTestObject*)pA; struct GRelationalTableTestObject* b = (struct GRelationalTableTestObject*)pB; return stricmp(a->m_name, b->m_name);}int GRelationalTableCompareShoeSizes(const void* pA, const void* pB){ struct GRelationalTableTestObject* a = (struct GRelationalTableTestObject*)pA; struct GRelationalTableTestObject* b = (struct GRelationalTableTestObject*)pB; if(a->m_shoesize > b->m_shoesize) return 1; if(a->m_shoesize < b->m_shoesize) return -1; else return 0;}// staticvoid GRelationalTable::Test(){ // Define a data set const struct GRelationalTableTestObject pObjects[10] = { { 14, "bob", 9 }, { 13, "joe", 8 }, { 15, "sam", 7 }, { 12, "tom", 10 }, { 16, "hal", 11 }, { 11, "amy", 12 }, { 19, "sue", 6 }, { 18, "vin", 13 }, { 20, "les", 5 }, { 17, "jim", 14 }, }; // make the table const GRelationalTableFieldComparerFunc pComparers[] = { GRelationalTableCompareAges, GRelationalTableCompareNames, GRelationalTableCompareShoeSizes }; GRelationalTable table(3, pComparers); int i; for(i = 0; i < 10; i++) { const struct GRelationalTableTestObject* pObj = &pObjects[i]; table.AddRow(pObj); } const struct GRelationalTableTestObject model = { 15, "gil", 2 }; // test a query by age const int answer1[] = { 15, 16, 17, 18, 19, 20 }; GRelationalTableEnumerator e; table.Query(&e, 0, &model); for(i = 0; i < 6; i++) { struct GRelationalTableTestObject* pObj = (struct GRelationalTableTestObject*)e.GetNext(); if(!pObj) throw "Expected more objects in the results"; if(pObj->m_age != answer1[i]) throw "Wrong object"; } if(e.GetNext() != NULL) throw "Expected no more objects"; // test a query by name const char* answer2[] = { "hal", "jim", "joe", "les", "sam", "sue", "tom", "vin" }; table.Query(&e, 1, &model); for(i = 0; i < 8; i++) { struct GRelationalTableTestObject* pObj = (struct GRelationalTableTestObject*)e.GetNext(); if(!pObj) throw "Expected more objects in the results"; if(stricmp(pObj->m_name, answer2[i]) != 0) throw "Wrong object"; } if(e.GetNext() != NULL) throw "Expected no more objects"; // test a query by shoesize const int answer3[] = { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; table.Query(&e, 2, &model); for(i = 0; i < 10; i++) { struct GRelationalTableTestObject* pObj = (struct GRelationalTableTestObject*)e.GetNext(); if(!pObj) throw "Expected more objects in the results"; if(pObj->m_shoesize != answer3[i]) throw "Wrong object"; } if(e.GetNext() != NULL) throw "Expected no more objects";}#endif // NO_TEST_CODE
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?