void check_eol(iter_type &end_token_, iter_type &curr_, const id_type npos, const char_type eoi_, const true_ &) { if (_eol_state._EOL_state != npos && curr_ == eoi_) { _eol_state._EOL_state = _ptr[eol_index]; if (_eol_state._EOL_state) { _ptr = &_dfa[_eol_state._EOL_state * _dfa_alphabet]; end_state(end_token_, curr_); } } }
//give a network,and generate its corresponding DN:dynamical-network or SN:state-network std::unordered_multimap<int,std::pair<int,int>> genDN(int *network,int size) { bool set[2] = {false,true}; //it is useless Sequence start_state(size,2); Sequence end_state(size,2); std::unordered_multimap<int,std::pair<int,int>> stateNetwork; int stateSum = pow(2,size); for(int i = 0;i < size;i++) start_state[i] = true; for(int stateNum = 0;stateNum < stateSum;stateNum++){ //calculate the end state nextEndState(network,start_state,end_state,size); //store it to a map:start_state->(end_state,b) stateNetwork.insert(std::unordered_multimap<int,std::pair<int,int>>::value_type ((int)end_state,std::make_pair((int)start_state,0))); //for every starting state,get the next state start_state.nextSequence(); } return stateNetwork; }
static void dump_ex(const typename char_state_machine::dfa &dfa_, ostream &stream_) { const std::size_t states_ = dfa_._states.size(); const id_type bol_index_ = dfa_._bol_index; typename dfa_state::id_type_string_token_map::const_iterator iter_; typename dfa_state::id_type_string_token_map::const_iterator end_; for (std::size_t i_ = 0; i_ < states_; ++i_) { const dfa_state &state_ = dfa_._states[i_]; state(stream_); stream_ << i_ << std::endl; if (state_._end_state) { end_state(stream_); if (state_._push_pop_dfa == dfa_state::push_dfa) { push(stream_); stream_ << state_._push_dfa; } else if (state_._push_pop_dfa == dfa_state::pop_dfa) { pop(stream_); } id(stream_); stream_ << static_cast<std::size_t>(state_._id); user_id(stream_); stream_ << static_cast<std::size_t>(state_._user_id); dfa(stream_); stream_ << static_cast<std::size_t>(state_._next_dfa); stream_ << std::endl; } if (i_ == 0 && bol_index_ != char_state_machine::npos()) { bol(stream_); stream_ << static_cast<std::size_t>(bol_index_) << std::endl; } if (state_._eol_index != char_state_machine::npos()) { eol(stream_); stream_ << static_cast<std::size_t>(state_._eol_index) << std::endl; } iter_ = state_._transitions.begin(); end_ = state_._transitions.end(); for (; iter_ != end_; ++iter_) { string_token token_ = iter_->second; open_bracket(stream_); if (!iter_->second.any() && iter_->second.negatable()) { token_.negate(); negated(stream_); } string chars_; typename string_token::range_vector::const_iterator ranges_iter_ = token_._ranges.begin(); typename string_token::range_vector::const_iterator ranges_end_ = token_._ranges.end(); for (; ranges_iter_ != ranges_end_; ++ranges_iter_) { if (ranges_iter_->first == '-' || ranges_iter_->first == '^' || ranges_iter_->first == ']') { stream_ << '\\'; } chars_ = string_token::escape_char (ranges_iter_->first); if (ranges_iter_->first != ranges_iter_->second) { if (ranges_iter_->first + 1 < ranges_iter_->second) { chars_ += '-'; } if (ranges_iter_->second == '-' || ranges_iter_->second == '^' || ranges_iter_->second == ']') { stream_ << '\\'; } chars_ += string_token::escape_char (ranges_iter_->second); } stream_ << chars_; } close_bracket(stream_); stream_ << static_cast<std::size_t>(iter_->first) << std::endl; } stream_ << std::endl; } }
static void dump (const basic_state_machine<CharT> &state_machine_, ostream &stream_) { typename basic_state_machine<CharT>::iterator iter_ = state_machine_.begin (); typename basic_state_machine<CharT>::iterator end_ = state_machine_.end (); for (std::size_t dfa_ = 0, dfas_ = state_machine_.size (); dfa_ < dfas_; ++dfa_) { const std::size_t states_ = iter_->states; for (std::size_t i_ = 0; i_ < states_; ++i_) { state (stream_); stream_ << i_ << std::endl; if (iter_->end_state) { end_state (stream_); stream_ << iter_->id; dfa (stream_); stream_ << iter_->goto_dfa; stream_ << std::endl; } if (iter_->bol_index != npos) { bol (stream_); stream_ << iter_->bol_index << std::endl; } if (iter_->eol_index != npos) { eol (stream_); stream_ << iter_->eol_index << std::endl; } const std::size_t transitions_ = iter_->transitions; if (transitions_ == 0) { ++iter_; } for (std::size_t t_ = 0; t_ < transitions_; ++t_) { std::size_t goto_state_ = iter_->goto_state; if (iter_->token.any ()) { any (stream_); } else { open_bracket (stream_); if (iter_->token._negated) { negated (stream_); } string charset_; CharT c_ = 0; escape_control_chars (iter_->token._charset, charset_); c_ = *charset_.c_str (); if (!iter_->token._negated && (c_ == '^' || c_ == ']')) { stream_ << '\\'; } stream_ << charset_; close_bracket (stream_); } stream_ << goto_state_ << std::endl; ++iter_; } stream_ << std::endl; } } }
//give a network,and generate its B:basin,W:the overlap of all trajectory void genBW(int *network,int size) { bool set[2] = {false,true}; //it is useless Sequence start_state(size,2); Sequence end_state(size,2); std::unordered_multimap<int,std::pair<int,int>> stateNetwork; int stateSum = pow(2,size); double *TF = new double [stateSum]; for(int i = 0;i < size;i++) start_state[i] = true; for(int stateNum = 0;stateNum < stateSum;stateNum++){ //calculate the end state nextEndState(network,start_state,end_state,size); //store it to a map:start_state->(end_state,b) stateNetwork.insert(std::unordered_multimap<int,std::pair<int,int>>::value_type ((int)end_state,std::make_pair((int)start_state,0))); //for every starting state,get the next state start_state.nextSequence(); } auto bw = findMaxBW(stateNetwork,TF); //statistics the checkpoint double *weight1 = new double[size]; double *weight2 = new double[size]; double *weight = new double[size]; int *count1 = new int[size]; int *count2 = new int[size]; int *count = new int[size]; for(int i = 0; i < size; ++i){ count1[i] = 0; count2[i] = 0; count[i] = 0; } int state; start_state.reset(); for(int i = 0; i < stateSum;i++){ state = (int)start_state; for(int j = 0; j < size; j++) std::cout<<start_state[j]<<" "; std::cout<<TF[state]<<std::endl; if(TF[state] >= 1){ for(int j = 0; j < size; j++){ if(start_state[j] == 1){ weight1[j] += TF[state]; count1[j] += 1; } else{ weight2[j] -= TF[state]; count2[j] += 1; } weight[j] += TF[state]; count[j] += 1; } } start_state.nextSequence(); } std::cout<<"B = "<<bw.first<<" W = "<<bw.second<<std::endl; for(int j = 0; j < size; j++) std::cout<<(j+1)<<" "<<count1[j]<<" "<<count2[j]<<" "<< weight1[j]<<" "<<weight2[j]<<" "<<(weight1[j]+weight2[j])/weight[j]<<std::endl; delete weight1; delete weight2; delete weight; delete count1; delete count2; delete count; delete TF; }