

struct context
{


};

//--------------------------------
struct point_2
{
	double x_,y_;
};

struct st_coord :point_2
{
	uint64_t	gen_time_; //精确到毫秒的测距时间
	uint16_t    m_ct;//ct

};

template<typename T>
struct fixed_deque:std::deque<T>
{
	const size_t max_size=100;
	
	
	
	
	
};

struct card
{
	const static int MAX_HIS_LOCATION=100;
	
	std::shared_ptr<Card> m_card;
	//
	std::shared_ptr<area> m_area;
	//
	fixed_deque<st_coord> ori_list_;
	//
	fixed_deque<point_2> view_list_;
	
	st_coord pin_point()
	{
			ori_list_.push_back(...);
			
			if(ori_list_.size()>MAX_HIS_LOCATION)
			{
				ori_list_.pop_front();
			}
			
			return ori_list_.back();
	}
	
	void add_view_point(const point_2&pt)
	{
			view_list_.push_back(pt);
			
			if(view_list_.size()>MAX_HIS_LOCATION)
			{
				view_list_.pop_front();
			}			
	}
};

//
struct db_area
{
	int m_areaid;
	std::vector<point_2> m_point;
	double m_default_speed;
};

struct area
{
	area(std::shared_ptr<db_area> ptr)
		:db_area_(ptr)
	{
		
	}
	virtual ~area()
	{
		
	}
	std::shared_ptr<db_area> db_area_;
	std::shared_ptr<context> context_;
	

	virtual void on_card_enter(std::shared_ptr<Card> card, int prev_area){}
	
	virtual void on_card_move(const std::string& cardid){}
	
	virtual void on_card_leave(const std::string& cardid, int next_area){}

	virtual int get_card_count()const{}
	
	virtual void updata_position(){}
};

struct person
{
	std::shared_ptr<card> card_;
	time_t enter_time_;
};

struct comp_linear_fit
{
	std::map<int,linear_fit*> m_fit;
	comp_linear_fit(const int* begin,const int* end)
	{
		for(;begin!=end;++begin)
		{
			m_fit.insert(std::make_pair(*begin,new linear_fit(*begin)));
		}
	}

	~comp_linear_fit()
	{
		for_each(m_fit.begin(),m_fit.end(),[](auto it){delete it->second;});
	}
	
	double getY(int num_point,uint64_t time)const
	{
		auto it=m_fit.find(num_point);
		if(it==m_fit.end())
		{
			//throw 
			return 0.0;
		}
		
		return it->second->getY(time);
	}
	
	double getK(int num_point,uint64_t time)const
	{
		auto it=m_fit.find(num_point);
		if(it==m_fit.end())
		{
			//throw 
			return 0.0;
		}
		
		return it->second->getK(time);
	}
	
	void push(uint64_t time, double x)
	{
		for_each(m_fit.begin(),m_fit.end(),[&](auto it){it->second->push(time,x);});
	}
};


struct monkey_person 
{
	std::shared_ptr<monkey_bus> m_bus;
	std::shared_ptr<person> m_person;
	fixed_deque<st_coord> m_d_ori;

	st_coord  m_go_up_point;
	comp_linear_fit* m_linear_fit;
	
	
	monkey_person()
	{
		static const int num_fit_point[]={5,10,20,30};
		m_linear_fit=new comp_linear_fit(&num_fit_point[0],&num_fit_point[0]+sizeof(num_fit_point)/sizeof(num_fit_point[0]));
	}
	
	~monkey_person()
	{
		delete m_linear_fit;
	}
	
	bool is_on_bus() const
	{
		return !!m_bus;
	}
	
	void on_step_map(const fp_path&path)
	{
		st_coord pt=m_person->pin_point();
		pt.x_=path.map(pt);
		pt.y=0;
		
		m_d_ori.push_back(pt);
		m_linear_fit.push(pt.gen_time_,pt.x);
	}
	
	bool fit_speed(double*ret_speed)
	{
		int num_point=0;
		if()
			return false;
		
		*ret_speed=m_linear_fit.getK(num_point);
		return true;
	}
	
	
	void updata_position() 
	{
			
			
	}
	
}

//-----------------------------------------

struct monkey_bus
{
	monkey_bus(double speed, int direct)
		:m_speed(speed)
		,m_direct(direct)
	{
		
	}
	std::list<std::shared_ptr<monkey_person>> person_list_;
	std::vector<double> m_AverageSpeed;
	double m_speed;
	int    m_direct;
	
	bool test_get_on(double speed)
	{
	
	}

	bool test_get_off(double speed)
	{
		
	}
	void Set_AverageSpeed()
	{
		if(m_AverageSpeed.size()%2000 == 0)
		{
			//get Speed k-means
		}
	}
	void updata_position() 
	{
		for(auto person:person_list_)
			person.updata_position();

	}
};

struct monkey_area :area
{
	std::map<std::string,std::shared_ptr<monkey_person>> card_map_;
	std::vector<monkey_bus *> bus_;
	fp_path m_path;
	
	monkey_area(std::shared_ptr<db_area> db_areaptr)
		:area(db_areaptr)
		,m_path(db_areaptr->m_point.begin(),db_areaptr->m_point.end())
	{
		for(int i=0;i<2;i++)
		{
			//反向
			monkey_bus * bus = new monkey_bus(db_area_->m_default_speed,1);
			bus_.push_back(bus);
			//正向
			//这里需要吧vector里面的字段逆序，然后放入到fp_path里
			monkey_bus * bus = new monkey_bus(db_area_->m_default_speed,-1);
			bus_.push_back(bus);
		}
	}
	~monkey_area()
	{
		for_each(bus_.begin(),bus_.end(),[](auto bus){delete bus;});
	}

	std::shared_ptr<monkey_person> find(std::string cardid)
	{
		auto it =  card_map_.find(cardid)
		if(it == card_map_.end())
			return nullptr;
		return it->second;
	}

	virtual void updata_position()
	{
			for(auto bus:bus_)
				bus.updata_position();
	}

	virtual void on_card_leave(const std::string& cardid, int next_area)
	{
		
	}
	virtual void on_card_enter(std::shared_ptr<Card> card, int prev_area)
	{
		new monkey_person;
		insert(cardid,monkey_person);
		
	}
	virtual void on_card_move(const std::string& cardid)
	{
		auto mp=find(cardid);
		
		mp.on_step_map(m_path);
		

		double speed=0;
		bool b=mp.fit_speed(&speed);
			
	
		//on bus
		if(mp->is_on_bus())
		{
			if(mp->m_bus->test_get_off(mp,speed))
			{
				mp->m_bus->get_off();				
			}
		}
		else
		{
			monkey_bus*bus=m_bus[speed>0?0:1];
			if(bus->test_get_on(mp,speed))
			{
				bus->get_on(mp);
			}
		}
	}
};

