// @function // @brief 计算next数组 // 描述: // 当next[j]=k时, 求 next[j+1]: // (1) 若p[k]==p[j]或者-1==k, 则next[j+1]=k+1 // (2) 若p[k]!=p[j], 则令k=next[k], 继续进行计算 void gen_next(const string& pattern) { auto size = pattern.size(); fill_next(size); int j = 0; int k = -1; while(j < size) { if (k == -1 || pattern[j] == pattern[k]) { ++j; ++k; m_next[j] = calc_next_value(j, k, pattern); } else { k = m_next[k]; } } }
int main(int argc, char *argv[]) { // Local variable declaration double t0 = 0; // Intial time , mostly 0 double x0 = 0; // Initial mean number of customers at time t0 double h = 1; double x1 = 0; int i, opt, stable_cnt = 0, n, pflag = 0; if (argc != 9 && argc != 11) { printf("argc %d\n", argc); usage(argv[0]); return 1; } while ((opt = getopt(argc, argv, ":h:x:l:u:p:")) != -1) { //printf("opt %c optarg %s\n", opt, optarg); switch (opt) { case 'h': h = strtod(optarg, NULL); //printf("optarg %s h = %lf\n", optarg, h); break; case 'x': x0 = strtod(optarg, NULL); //printf("optarg %s x0 = %lf\n", optarg, x0); //printf("val = %lf\n", strtod(optarg, NULL)); break; case 'l': lambda = strtod(optarg, NULL); //printf("optarg %s lambda = %f\n", optarg, lambda); //printf("val = %lf\n", strtod(optarg, NULL)); break; case 'u': mu = strtod(optarg, NULL); //printf("optarg %s mu = %f\n", optarg, mu); //printf("val = %lf\n", strtod(optarg, NULL)); break; case 'p': pflag = 1; n = atoi(optarg); break; default: usage(argv[0]); return 1; } } //lambda = 0.8; mu = 1.0; x0 = 0; h = 0.1; printf("# Average number of pkts in the system as a function of time t\n"); printf("# Parameters for this plot\n"); printf("# lambda = %lf mu = %f h = %f x0 = %f\n", lambda, mu, h, x0) ; printf("#\n"); if (pflag) { printf("# time \t\tAvg-number-of-pkts\t\t P(n=%d)\n", n); } else { printf("# time \t\tAvg-number-of-pkts\n"); } if (pflag) { printf("%lf\t\t%.7f\t\t%lf\n", t0, x0, find_pn(x0, n)); } else { printf("%lf\t\t%.7f\n", t0, x0); } /* * This loop continues to calculate until the value of x stays stable for * STABLE_CNT_MAX iterations or upto MAX_ITR iterations. */ for(i = 0; i < MAX_ITR; i++) { x1 = calc_next_value(t0, x0, h); if (pflag) { printf("%lf\t\t%.7f\t\t%lf\n", t0, x1, find_pn(x1, n)); } else { printf("%lf\t\t%.7f\n", t0, x1); } //printf("iter %d x0 = %f x1 = %f stable_cnt = %d\n", i, x0, x1, stable_cnt); if (roundit(x0) == roundit(x1)) { stable_cnt++; if (stable_cnt >= STABLE_CNT_MAX) { printf("Breaking early...\n"); break; } } else { /* * The value of x has not yet stabilized hence we reset the count. */ stable_cnt = 0; } x0 = x1; t0 = t0 + h; } return 0; }