OSDN Git Service

6e159423a691921b617be268e4c7ef59a00925b0
[nysol/mining.git] / include / kgFldHash.h
1 /* ////////// LICENSE INFO ////////////////////
2
3  * Copyright (C) 2013 by NYSOL CORPORATION
4  *
5  * Unless you have received this program directly from NYSOL pursuant
6  * to the terms of a commercial license agreement with NYSOL, then
7  * this program is licensed to you under the terms of the GNU Affero General
8  * Public License (AGPL) as published by the Free Software Foundation,
9  * either version 3 of the License, or (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF 
13  * NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * Please refer to the AGPL (http://www.gnu.org/licenses/agpl-3.0.txt)
16  * for more details.
17
18  ////////// LICENSE INFO ////////////////////*/
19 // =============================================================================
20 // kgFldHash.h 項目ハッシュ(hashavg,hashsumで使用)
21 // =============================================================================
22 #pragma once
23 #include <vector>
24 #include <kgCSV.h>
25 #include <kgArgFld.h>
26 #include <kgMethod.h>
27
28 namespace kglib {////////////////////////////////////////////// namespace start
29
30 class kgFldHashNode;
31
32 class kgFldHash 
33 {
34                 int _hashVal;                 // hashのバケットサイズ
35                 int _cnt;                     // 現在登録されている値の数
36                 vector<kgFldHashNode*> _node; // バッケトへのポインタ
37                 kgCSVfld* _csv;
38                 kgArgFld* _key;
39                 kgArgFld* _fld;
40
41                 unsigned int calHashVal(const char** flds);
42
43         public:
44                 // コンストラクタ パラメータ省略時のバケット数は199999
45                 kgFldHash(int hashVal, kgCSVfld* csv, kgArgFld* key, kgArgFld* fld);
46
47                 // デストラクタ
48                 ~kgFldHash(void);
49
50                 // 挿入
51                 kgFldHashNode* insert(const char** flds);
52                 kgFldHashNode* node(int num) const { return _node.at(num); }
53
54                 int recLen(void)  { return _csv->recLen(); }    // レコード長
55                 int csvSize(void) { return _csv->fldSize(); }   // データ項目数
56                 char* csv(int num){ return _csv->getVal(num); } // データのnum番目項目
57                 
58                 int key(int num) { return _key->num(num);} // k=で指定した第num番目項目番号
59                 int keySize(void) { return _key->size(); } // k=で指定した項目数
60                 int fldSize(void) { return _fld->size(); } // f=で指定した項目数
61
62                 int hashVal(void){ return  _hashVal;}
63 };
64
65
66 class kgFldHashNode 
67 {
68         kgFldHash*        _hash;
69         kgAutoPtr2<char>  _ap1;  // _bufをラッピングするsmart pointer
70         char*             _buf;  // 一行まるまま登録領域
71         kgAutoPtr2<char*> _ap2;  // _idxをラッピングするsmart pointer
72         char**            _idx;  // 登録されたデータの各項目へのポインタ
73         vector<double>    _cnt;  // null以外件数
74         vector<double>    _sum;  // 合計
75         vector<bool>      _nul;  // nullが一行でもあればtrue
76         kgFldHashNode*    _next; // 次nodeへのリンク
77
78         public:
79         // コンストラクタ
80         kgFldHashNode(const char** flds, kgFldHash* hash);
81         virtual ~kgFldHashNode(void) {};
82
83         // keyの比較
84         bool keycmp(const char** flds);
85
86         void next(kgFldHashNode* next) {_next=next;}
87         void cnt(int num, double v) { _cnt.at(num)+=v;}  // f=num番目の項目cnt up
88         void sum(int num, double v) { _sum.at(num)+=v;}  // f=num番目の項目sum up
89         void nul(int num, bool   v) { _nul.at(num) =v;}
90
91         // accessors(get)
92         kgFldHashNode* next(void) const {return _next;}
93         char* idx(int num) const { return *(_idx+num); }
94         double cnt(int num) const {return _cnt.at(num);} // f=num番目の件数を返す
95         double sum(int num) const {return _sum.at(num);} // f=num番目の合計を返す
96         bool   nul(int num) const {return _nul.at(num);}
97 };
98 }////////////////////////////////////////////// namespace end