Turi Create  4.0
fp_tree_header.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_FP_TREE_HEADER_H
7 #define TURI_FP_TREE_HEADER_H
8 
9 #include <vector>
10 #include <map>
11 #include <stack>
12 #include <string>
13 #include <memory>
14 #include <algorithm>
15 #include <iostream>
16 
17 #include <core/data/sframe/gl_sframe.hpp>
18 #include <core/data/sframe/gl_sarray.hpp>
19 
20 #include <toolkits/feature_engineering/topk_indexer.hpp>
21 #include <toolkits/feature_engineering/statistics_tracker.hpp>
22 
23 #include <toolkits/pattern_mining/fp_node.hpp>
24 
25 namespace turi {
26 namespace pattern_mining {
27 
28 /**
29  * Struct for fp_tree_header
30  *
31  * Vars
32  * support (size_t) - item supports in decreasing or:der
33  * id (size_t) - item ids
34  * pointer (pointers) - pointers to linked list of item id
35  */
37  public:
38  size_t id;
39  size_t support;
40  fp_node* pointer;
41 };
42 
43 /**
44  * Header for fp_tree
45  *
46  * Vars
47  * headings (vector of fp_tree_heading)
48  * index_map (map of id to index) - hash for searching headings by item id
49  */
51  public:
52  std::vector<fp_tree_heading> headings;
53  std::map<size_t, size_t> id_index_map;
54 
55  // Constructor
57  fp_tree_header(const std::vector<size_t>& ids, \
58  const std::vector<size_t>& supports);
59  fp_tree_header(const fp_tree_header& other_header);
60 
61  /**
62  * Get index of id in headings
63  */
64  size_t get_index(const size_t& id) const;
65  inline const size_t size() const {return headings.size();};
66 
67  /**
68  * Sort a transaction by heading's order
69  */
70  std::vector<std::pair<size_t, size_t>> sort_transaction( \
71  const std::vector<size_t>& new_transaction) const;
72 
73  /**
74  * Extract vectors and maps from headings
75  */
76  std::vector<size_t> get_ids();
77  std::vector<size_t> get_supports();
78  std::map<size_t, fp_node*> get_pointers();
79 
80  /**
81  * Extract headings for specific id
82  */
83  bool has_id(const size_t& id) const;
84  const fp_tree_heading get_heading(const size_t& id) const;
85 };
86 
87 /**
88  * Print header
89  */
90 std::ostream& operator<<(std::ostream& out, const fp_tree_header& header);
91 
92 } // pattern_mining
93 } // turicreate
94 #endif