常用函数收集

comm_fun.h

#ifndef _TIME_CTL_
#define _TIME_CTL_
#include <sys/times.h>
#include <iostream>
#include <unistd.h>
#include <math.h>

#define eps 0.000001
#define _time_ctl_init_  struct tms t_start; struct tms t_end; clock_t start; clock_t end;
#define _time_ctl_start_ do { start = times(&t_start); } while(0)
#define _time_ctl_stop_  do {  end  = times(&t_end);   } while(0)
#define _time_elapse     do { std::cout <<  "elapse time is: " << (double)(end - start) / (double)sysconf(_SC_CLK_TCK) << "s" << std::endl; } while(0)

#define _min_(a, b) ((((a) - (b)) < 0) ? (a) : (b))
#define _max_(a, b) ((((a) - (b)) > 0) ? (a) : (b))
#endif

resolve.h

#ifndef _RESOLVE_H_
#define _RESOLVE_H_
#include <pthread.h>
#include "comm_fun.h"

class Resolve
{
	private:
		pthread_mutex_t thread_lock;
        protected:
		void mutex_lock()
		{
			pthread_mutex_lock(&thread_lock);
		}
		void mutex_unlock()
		{
			pthread_mutex_unlock(&thread_lock);
		}
	public:
		Resolve() {}
		void mutex_init()
		{
			pthread_mutex_init(&thread_lock, NULL);
		}
		void mutex_destroy()
		{
			pthread_mutex_destroy(&thread_lock);
		}
		virtual ~Resolve() {}
		virtual void fun() = 0;
		virtual void read_para() = 0;
		virtual void print() = 0;
};

void thread_fun(void *(* problem_fun)(void *), Resolve *m_computer);
#endif

resolve.cpp

#include "resolve.h"

void thread_fun(void *(* problem_fun)(void *), Resolve *m_computer)
{
	_time_ctl_init_;
	_time_ctl_start_;
	m_computer->mutex_init();
	pthread_t thrd_computer;
	int ret = pthread_create(&thrd_computer, NULL, problem_fun, (void *)m_computer);
	if(ret) {
		perror("pthread_create: computer is wrong");
		exit(EXIT_FAILURE);
	}
	pthread_join(thrd_computer, NULL);
        m_computer->print();
	m_computer->mutex_destroy();
	_time_ctl_stop_;
	_time_elapse;
}

binary_search_tree.h

template <class _rep, class _comp = statistic_compare<_rep> >
    class binary_search_tree
    {
        typedef typename binary_search_tree<_rep, _comp> _Myt;
    public:
        typedef typename _rep value_type;
    private:
        bool isNil;
        const _rep m_instance;
        _Myt* m_left_tree;
        _Myt* m_right_tree;

    public:
        virtual ~binary_search_tree()
        {
            if (isNil == true) return;
            if (!m_left_tree->isNull())
            {
                delete m_left_tree;
                m_left_tree = NULL;
            }
            if (!m_right_tree->isNull())
            {
                delete m_right_tree;
                m_right_tree = NULL;
            }
        }
        binary_search_tree() : isNil(true), m_instance({ 0 }), m_left_tree(NULL), m_right_tree(NULL) {}
        binary_search_tree(const _rep& rhs_instance, _Myt* _left_tree, _Myt* _right_tree) : isNil(false), m_instance(rhs_instance), m_left_tree(_left_tree), m_right_tree(_right_tree) {}
        const _rep& node_instance() const { return m_instance; }
        _Myt* left_tree() const   { return m_left_tree; }
        _Myt* right_tree() const { return m_right_tree; }
        bool isNull()          { return isNil; }

    private:
        binary_search_tree(const _Myt& _rhs);
        _Myt& operator = (const _Myt& m_rhs);
    };

    template <class _rep, class _comp>
    bool operator == (const binary_search_tree<_rep, _comp>& _lhs, const binary_search_tree<_rep, _comp>& _rhs)
    {
        if ((_lhs.node_instance() == _rhs.node_instance()) == false)
            return false;
        if (_lhs.node_instance().NodeType == tree_ThreshContin && _rhs.node_instance().NodeType == tree_ThreshContin)
        {
            if (((*_lhs.left_tree()) == (*_rhs.left_tree())) == false)
                return false;
            if (((*_lhs.right_tree()) == (*_rhs.right_tree())) == false)
                return false;
        }
        return true;
    }
    template <class _rep, class _comp>
    std::ostream&
        operator <<  (_STD ostream& os, binary_search_tree<_rep, _comp>& _tree)
    {
            print_tree(os, _tree);
            return os;
    }
    template <class _Tree>
    void print_tree(_STD ostream& os, _Tree& m_tree, const std::size_t _tt = 0)
    {
        if (os.good())
        {
            if (m_tree.isNull()) return;
            for (std::size_t m_tt = 0; m_tt < _tt; ++m_tt) os << _T("\t");
            os << m_tree.node_instance() << std::endl;
            print_tree(os, *(m_tree.left_tree()), _tt + 1);
            print_tree(os, *(m_tree.right_tree()), _tt + 1);
        }
        else
            throw _T("std::ostream is bad");
    }
    template <class _rep, class _comp>
    bool _saveTree(const binary_search_tree<_rep, _comp>& _tree, std::ofstream& os)
    {
        const _rep& node = _tree.node_instance();
        write(os, node);
        if (node.NodeType == tree_ThreshContin)
        {
            _saveTree(*(_tree.left_tree()), os);
            _saveTree(*(_tree.right_tree()), os);
        }
        return true;
    }
    template <class _rep, class _comp>
    bool saveTree(binary_search_tree<_rep, _comp>& m_tree, _STD string& _file_name, _STD ios_base::openmode _Mode = _STD ios_base::out | std::ios_base::binary | std::ios_base::trunc)
    {
        _STD ofstream dataoutFile(_file_name.c_str(), _Mode);
        if (!dataoutFile) {
            _STD cerr << _T("error: unable to open output file : ") << _file_name.c_str() << _STD endl;
            dataoutFile.close();
            return false;
        }
        ofstream_init(dataoutFile);
        _saveTree(m_tree, dataoutFile);
        dataoutFile.close();
        return true;
    }
    
    typedef binary_search_tree<struct knn_node> knn_search_tree;

classification.h

template <class _Mat_Type>
    class classification
    {
        typedef typename classification<_Mat_Type> _Myt;
        typedef typename _Mat_Type::iterator iterator;
        typedef typename _Mat_Type::reference reference;
        typedef typename _Mat_Type::const_reference const_reference;

    private:
        classification(const _Myt& _rhs);
    protected:
        int m_main_class;
        const label_array& _labels;
        const label_array& _labels_count;
        const v_vector& m_mean;
        const v_vector& m_var;

    protected:
        virtual void training_func(_Mat_Type& _matt) = 0;
        virtual double classify_func(const_reference _meta, const int _class) = 0;
        virtual double regression_func(const_reference _meta) = 0;
        virtual void save_model_func() = 0;
        virtual void read_model_func(_Mat_Type& _matt) = 0;
    public:
        virtual ~classification() { }
        classification(const label_array& _labels, const label_array& _labels_count, const v_vector& _mean, const v_vector& _var) : _labels(_labels), _labels_count(_labels_count), m_mean(_mean), m_var(_var) {}
        void training(_Mat_Type& _matt)
        {
            training_func(_matt);
        }
        double classify(const_reference _meta, const int _class)
        {
            return classify_func(_meta, _class);
        }
        double regression(const_reference _meta)
        {
            return regression_func(_meta);
        }
        void save_model()
        {
            return save_model_func();
        }
        void read_model(_Mat_Type& _matt)
        {
            return read_model_func(_matt);
        }
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值