//Programed by NAS6 //sptr.cpp #ifdef _MSC_VER #if _MSC_VER >=1400 // VC2005(VC8.0)以降 #define _CRTDBG_MAP_ALLOC #include #ifdef _DEBUG #define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__) #endif #endif #endif //#define NAS6_RELEASE_BLD //#define CHK_MEM_LEAK #include #include #include "NAS6_tree_clct.h" using namespace std; using namespace NAS6_Utl; //NAS6_cntn_ptr、NAS6_tree_clct使用例 class hoge { public: int m_data; hoge() : m_data(0) { ; } hoge(hoge* rh) :hoge(*rh) { ; } hoge(hoge& rh) { m_data = rh.m_data; } hoge(const int rh) : m_data(rh) { ; } }; template void disp_NAS6_cntn_ptr(NAS6_cntn_ptr rh) { rh.begin(); int i; for (i = 0; i < rh.size(); i++, rh++) { cout << (*rh) << endl; } } void disp_NAS6_cntn_ptr(NAS6_cntn_ptr rh) { rh.begin(); int i; for (i = 0; i < rh.size(); i++, rh++) { cout << (*rh).m_data << endl; } } int g_dt; #ifndef CHK_MEM_LEAK const int mem = 2; #else const int mem = 65536; #endif NAS6_tree_clct& fill_NAS6_tree_clct(NAS6_tree_clct* rh) { int crnt = 0; rh->resize(mem); for (int i = 0; i < mem; i++) (*rh)[i] = g_dt++; rh->begin(); if (rh->child().size() == crnt) { return (rh->parent()); } while (rh->child().size() != crnt) { crnt++; fill_NAS6_tree_clct(&(rh->child(crnt - 1))); } return (*rh); } NAS6_tree_clct& fill_NAS6_tree_clct(NAS6_tree_clct* rh) { int crnt = 0; rh->resize(mem); for (int i = 0; i < mem; i++) (*rh)[i].m_data = g_dt++; rh->begin(); if (rh->child().size() == crnt) { return (rh->parent()); } while (rh->child().size() != crnt) { crnt++; fill_NAS6_tree_clct(&(rh->child(crnt - 1))); } return (*rh); } template NAS6_tree_clct& disp_NAS6_tree_clct_size(NAS6_tree_clct* rh) { int crnt = 0; cout << rh->child().size() << " "; if (rh->child().size() == crnt) { return (rh->parent()); } while (rh->child().size() != crnt) { crnt++; disp_NAS6_tree_clct_size(&(rh->child(crnt - 1))); } return (*rh); } template void disp_NAS6_tree_clct(NAS6_tree_clct* root) { bool parent = false; if (&root->recursiveInit() == nullptr) return; NAS6_tree_clct* crnt = root; cout << endl; cout << "key : " << endl; cout << crnt->thiskey().c_str() << endl; cout << "value : " << endl; disp_NAS6_cntn_ptr(*crnt); cout << endl; while (!crnt->recursiveIsEnd(root)) { parent = false; crnt = &(crnt->recursiveNext(root, parent)); if (!parent && crnt != nullptr && crnt != root) { cout << "key : " << endl; cout << crnt->thiskey().c_str() << endl; cout << "value : " << endl; disp_NAS6_cntn_ptr(*crnt); cout << endl; } } } template void disp_NAS6_tree_clct_key(NAS6_tree_clct* root, string fk) { bool parent = false; if (&root->recursiveInit() == nullptr) return; NAS6_tree_clct* crnt = root; if (crnt->conformkey(crnt, fk) != string::npos) { cout << "key : " << endl; cout << crnt->thiskey().c_str() << endl; cout << "value : " << endl; disp_NAS6_cntn_ptr(*crnt); cout << endl; } cout << endl; while (!crnt->recursiveIsEnd(root)) { parent = false; crnt = &(crnt->recursiveKeyNext(root, fk, parent)); if (!parent && crnt != nullptr && crnt != root) { cout << "key : " << endl; cout << crnt->thiskey().c_str() << endl; cout << "value : " << endl; disp_NAS6_cntn_ptr(*crnt); cout << endl; } } } void main() { #ifdef _MSC_VER #if _MSC_VER >=1400 // VC2005(VC8.0)以降 //メモリリーク検出関数 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif #endif int i; NAS6_cntn_ptr ch = NAS6_cntn_ptr(256); *ch = 'y'; while (*ch == 'y' || *ch == 'Y') { cout << "looped in" << endl; cout << "select test (1 ~ 2)" << endl; #ifndef CHK_MEM_LEAK cin >> ch.ptr(); #else if (_kbhit()) return; #endif //NAS6_cntn_ptr使用例 if (*ch == '1') { cout << "NAS6_cntn_ptr" << endl; NAS6_cntn_ptr a = NAS6_cntn_ptr(10); for (i = 0; i < a.size(); i++, a++) { (*a) = i + 10; } a.begin(); a.resize(5); cout << "a" << endl; disp_NAS6_cntn_ptr(a); cout << a[3] << endl; a.begin(); cout << (&a) << endl; a++; cout << (*a) << endl; a--; cout << (&a) << endl; NAS6_cntn_ptr b(1); (*b) = hoge(20); cout << "b" << endl; cout << (*b).m_data << endl; cout << b->m_data << endl; NAS6_cntn_ptr c; c = a; //aはcにメモリ所有権を移譲、以降、aでのアクセス禁止、nullptr、ダミーが返ってくる cout << "c" << endl; a[1] = 30; //nullptr、ダミーが返ってくる *(c.at(1)) = 40; disp_NAS6_cntn_ptr(a); //nullptr、ダミーが返ってくる disp_NAS6_cntn_ptr(c); c.resize(8); a.at(1); //nullptr、ダミーが返ってくる *(a + 2) = 50; //nullptr、ダミーが返ってくる c.at(2); *(++c) = 60; *(a - 3) = 70; //nullptr、ダミーが返ってくる *(c.operator --(3)) = 80; disp_NAS6_cntn_ptr(a); //nullptr、ダミーが返ってくる disp_NAS6_cntn_ptr(c); a.copyFrom(c); //aにcをコピー&リサイズ&メモリ所有権新規取得 a[1] = 30; a.resize(4); a.at(1); *(a + 2) = 50; *(a - 3) = 70; disp_NAS6_cntn_ptr(a); disp_NAS6_cntn_ptr(c); NAS6_cntn_ptr d(true); //peeper cout << "d" << endl; d = a; disp_NAS6_cntn_ptr(d); d = c; disp_NAS6_cntn_ptr(d); NAS6_cntn_ptr e(a); //peeper cout << "e" << endl; disp_NAS6_cntn_ptr(a); disp_NAS6_cntn_ptr(e); int *p = new int[2]; p[0] = 69; p[1] = 96; e.assign(p, 2); //利用メモリ割り当て&メモリ所有権新規取得 disp_NAS6_cntn_ptr(e); cout << endl; cout << "scope out" << endl; cout << endl; } //NAS6_tree_clct使用例 else { //////////////////////////////////////////////////////////////////////////////// cout << "NAS6_tree_clct" << endl; cout << endl; cout << "a" << endl; NAS6_tree_clct a; a.deliver(); a.deliver("keytest1"); a.deliver("keytest2"); a.child(0).deliver(); a.child(0).deliver(); a.child(0).child(1).deliver(); a.child(2).deliver(); a.child(2).deliver("keytest"); a.child(2).child(0).deliver(); cout << endl; cout << "delivered" << endl; //delivered //0:0:0 // :1:0 // :1 > keytest1 // :2 > keytest2 // :0:0 // :1 > keytest g_dt = 0; fill_NAS6_tree_clct(&a); cout << endl; cout << "filled" << endl; cout << endl; cout << "disp recursive" << endl; NAS6_tree_clct* root = &(a); cout << endl; cout << "a" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif root = &(a.child(2)); cout << endl; cout << "a.child(2)" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif cout << "a.child(2).child(0).child(0)" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_cntn_ptr(a.child(2).child(0).child(0)); #endif cout << endl; cout << "disp recursive key" << endl; root = &(a); cout << endl; cout << "find keytest" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct_key(root, "keytest"); #endif cout << endl; cout << "delete a.child(2)" << endl; delete &(a.child(2)); root = &(a); #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif //////////////////////////////////////////////////////////////////////////////// cout << "more : enter any input" << endl; #ifndef CHK_MEM_LEAK cin >> ch.ptr(); #endif cout << endl; cout << "b" << endl; NAS6_tree_clct b; b.deliver("keytest"); b.deliver(); b.child(1).deliver(); b.child(1).deliver(); b.child(1).child(1).deliver("keytest"); cout << endl; cout << "delivered" << endl; //delivered //0:0 > keytest // :1:0 // :1:0 > keytest g_dt = 100; fill_NAS6_tree_clct(&b); cout << endl; cout << "filled" << endl; cout << endl; cout << "disp recursive" << endl; root = &(b); cout << endl; cout << "b" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif cout << endl; cout << "a.deliver(..., 1, &(b), ...);" << endl; a.deliver("keytest3", 1, &(b)); //bはpeeperになる //a.deliver("keytest", 1, &(b), false); //bはpeeperになる //キーのリセットをしない //delivered //0:0:0 // :1:0 // :1 > keytest3 // :0 > keytest // :1:0 // :1:0 > keytest // :2 > keytest1 cout << endl; cout << "a :size : "; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct_size(&a); #endif cout << endl; cout << "disp recursive" << endl; root = &(a); cout << endl; cout << "a" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif root = &(b); cout << endl; cout << "b" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(root); #endif //////////////////////////////////////////////////////////////////////////////// cout << "more : enter any input" << endl; #ifndef CHK_MEM_LEAK cin >> ch.ptr(); #endif cout << endl; cout << "c" << endl; NAS6_tree_clct c; c.deliver(); c.deliver("keytest1"); c.deliver("keytest2"); c.child(0).deliver(); c.child(0).deliver(); c.child(0).child(1).deliver(); c.child(2).deliver(); c.child(2).deliver("keytest"); c.child(2).child(0).deliver(); cout << endl; cout << "delivered" << endl; //delivered //0:0:0 // :1:0 // :1 > keytest1 // :2 > keytest2 // :0:0 // :1 > keytest g_dt = 200; fill_NAS6_tree_clct(&c); cout << endl; cout << "filled" << endl; cout << endl; cout << "disp recursive" << endl; NAS6_tree_clct* rootc = &(c); cout << endl; cout << "c" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(rootc); #endif rootc = &(c.child(2)); cout << endl; cout << "c.child(2)" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(rootc); #endif cout << "c.child(2).child(0).child(0)" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_cntn_ptr(c.child(2).child(0).child(0)); #endif cout << endl; cout << "disp recursive key" << endl; rootc = &(c); cout << endl; cout << "find keytest" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct_key(rootc, "keytest"); #endif cout << endl; cout << "delete c.child(2)" << endl; delete &(c.child(2)); rootc = &(c); #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(rootc); #endif //////////////////////////////////////////////////////////////////////////////// cout << "more : enter any input" << endl; #ifndef CHK_MEM_LEAK cin >> ch.ptr(); #endif cout << endl; cout << "d" << endl; NAS6_tree_clct d(a.child(1)); cout << "disp recursive" << endl; #ifndef CHK_MEM_LEAK disp_NAS6_tree_clct(&d); #endif cout << endl; cout << "scope out" << endl; cout << endl; } cout << "retry (y / n)" << endl; #ifndef CHK_MEM_LEAK cin >> ch.ptr(); #endif } return; }