Last active
October 14, 2015 05:25
-
-
Save gitzhou/f1a50ea736ac6a10068b to your computer and use it in GitHub Desktop.
随机-按权值-分发消息队列中的消息-消息队列内消息总数未知
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// director-random-distribute-via-weight-2 | |
// | |
// 有 N 个消费者,分别有相应的权值 | |
// 随机分发消息队列中的消息 | |
// 各个消费者消费的消息数量的比值,总体近似为权值的比值 | |
// | |
#include <iostream> | |
#include <vector> | |
#include <ctime> | |
#include <cstdlib> | |
using namespace std; | |
class Consumer { | |
public: | |
explicit Consumer(int weight = 0) : weight_(0) {} | |
void set_id(char id) { | |
id_ = id; | |
} | |
void set_weight(int weight) { | |
weight_ = weight; | |
} | |
inline int weight() const { | |
return weight_; | |
} | |
inline void consume(int message_id ) { | |
message_id_list_.push_back(message_id); | |
} | |
inline size_t consumed_messages_count() const { | |
return message_id_list_.size(); | |
} | |
void display_consumed_messages() const { | |
const int MESSAGE_ID_COUNT_PER_LINE = 15; | |
cout << id_ << ": " << message_id_list_.size(); | |
for (int i = 0; i < message_id_list_.size(); ++i) { | |
if (i % MESSAGE_ID_COUNT_PER_LINE == 0) { | |
cout << endl; | |
} | |
cout << message_id_list_[i] << ' '; | |
} | |
cout << endl; | |
} | |
private: | |
char id_; | |
int weight_; | |
vector<int> message_id_list_; | |
}; | |
vector<int> weight_index; | |
void distribute(vector<Consumer> &consumers, int message_id) { | |
consumers[weight_index[rand() % weight_index.size()]].consume(message_id); | |
} | |
int main(int argc, const char * argv[]) { | |
const int TOTAL_MESSAGE_COUNT = 1000; | |
srand((unsigned int)time(NULL)); | |
int consumer_count; | |
cin >> consumer_count; | |
vector<Consumer> consumers(consumer_count); | |
for (int i = 0; i < consumer_count; ++i) { | |
int weight; | |
cin >> weight; | |
consumers[i].set_id('A' + i); | |
consumers[i].set_weight(weight); | |
} | |
// build weight map | |
for (int i = 0; i < consumers.size(); ++i) { | |
for (int j = 0; j < consumers[i].weight(); ++j) { | |
weight_index.push_back(i); | |
} | |
} | |
for (int i = 0; i < TOTAL_MESSAGE_COUNT; ++i) { | |
distribute(consumers, i); | |
} | |
for (int i = 0; i < consumer_count; ++i) { | |
consumers[i].display_consumed_messages(); | |
cout << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 3 8 2 1 4 3 2 | |
A: 126 | |
14 16 22 28 33 41 49 55 57 65 95 104 109 115 123 | |
126 143 144 150 160 176 177 180 183 189 193 203 219 227 249 | |
252 255 256 259 262 266 267 282 290 292 293 305 311 318 320 | |
336 345 352 356 365 376 381 382 385 393 394 396 410 412 418 | |
432 443 444 464 465 483 504 508 519 531 537 560 563 569 572 | |
573 620 626 636 643 645 646 650 656 662 664 665 669 673 702 | |
718 721 723 742 751 760 772 773 783 798 807 811 816 819 823 | |
824 839 841 852 854 861 882 892 895 897 902 905 912 924 928 | |
936 955 976 985 992 999 | |
B: 360 | |
2 4 6 7 9 11 12 17 19 25 29 31 35 37 43 | |
44 45 46 64 66 67 68 69 71 72 74 75 76 77 78 | |
80 81 82 85 86 88 90 94 97 100 101 105 107 108 110 | |
111 112 114 116 118 120 122 128 132 134 135 137 138 139 140 | |
145 149 151 152 156 159 161 162 163 165 167 171 172 175 178 | |
181 185 187 191 195 196 199 200 202 205 207 215 222 223 224 | |
226 228 231 236 240 241 242 247 250 254 260 265 268 271 276 | |
279 283 289 295 297 298 300 301 302 306 315 316 324 328 333 | |
339 341 343 346 348 349 353 355 357 360 361 362 364 367 368 | |
369 371 372 383 384 387 389 399 401 403 404 408 415 416 417 | |
423 427 428 430 431 434 436 445 446 447 454 457 460 461 462 | |
466 469 471 472 476 477 482 494 500 501 502 506 509 511 512 | |
514 516 517 518 520 524 525 528 529 530 533 535 543 544 545 | |
546 547 549 550 551 557 568 571 574 575 576 578 584 586 591 | |
594 598 599 600 602 605 606 608 611 613 614 617 619 623 629 | |
632 633 634 638 639 640 642 647 653 654 658 663 668 670 671 | |
675 676 682 683 684 685 687 690 695 698 701 704 707 713 714 | |
720 722 726 727 728 730 737 739 740 743 744 745 750 755 761 | |
762 763 765 766 771 778 786 788 791 799 801 803 806 810 817 | |
820 821 825 828 829 830 832 834 836 840 844 845 853 857 867 | |
869 870 871 873 874 875 876 878 880 883 885 891 896 898 899 | |
901 904 910 911 913 914 917 921 922 925 929 930 931 932 934 | |
935 939 940 944 947 948 951 952 953 956 962 963 965 967 968 | |
969 971 974 975 978 981 984 986 987 989 993 994 996 997 998 | |
C: 80 | |
32 36 38 47 53 83 103 121 147 179 212 225 232 235 244 | |
248 278 280 285 291 294 310 338 354 375 377 378 380 386 392 | |
397 411 413 453 463 473 480 493 497 498 521 540 548 553 554 | |
555 565 577 588 597 641 649 652 657 672 679 700 708 725 731 | |
734 748 768 775 777 782 790 813 815 862 865 866 886 888 890 | |
903 926 941 942 946 | |
D: 45 | |
13 48 131 168 184 201 204 229 251 286 309 350 390 433 438 | |
481 488 491 499 513 541 566 581 589 593 595 651 660 686 696 | |
697 711 719 752 754 780 787 796 797 804 843 848 916 919 937 | |
E: 175 | |
1 3 8 15 18 23 26 34 42 51 59 62 73 79 89 | |
92 96 106 117 119 127 142 146 148 153 154 164 166 169 173 | |
174 188 198 206 211 213 217 220 234 238 239 243 246 253 258 | |
261 263 269 272 281 284 288 296 304 312 313 314 317 319 322 | |
323 326 327 330 332 334 335 337 340 342 344 347 358 359 395 | |
402 419 420 421 424 425 426 429 435 442 456 458 459 467 468 | |
470 474 475 478 479 486 487 489 495 505 507 510 515 522 526 | |
532 552 556 558 559 562 564 579 590 592 601 604 612 621 627 | |
628 630 631 635 648 661 666 674 677 688 705 710 715 724 729 | |
732 735 746 756 769 779 781 785 793 794 802 812 814 827 831 | |
835 838 842 847 850 855 860 868 894 906 908 915 923 927 938 | |
945 950 958 960 970 977 982 983 988 995 | |
F: 136 | |
10 24 27 39 40 50 52 54 58 61 84 87 91 98 99 | |
113 124 125 129 130 136 157 186 192 194 197 216 218 221 237 | |
245 257 264 270 273 274 275 277 303 307 308 325 329 331 351 | |
366 370 373 374 388 391 400 405 406 407 409 414 422 440 441 | |
448 449 450 451 452 484 485 492 496 503 523 527 534 538 542 | |
561 570 582 583 585 587 596 603 607 610 618 622 625 637 644 | |
655 667 680 689 693 694 699 706 709 716 733 736 738 741 749 | |
758 759 767 774 776 795 809 822 826 833 846 851 858 863 864 | |
872 879 881 884 887 893 907 909 918 943 954 957 961 972 979 | |
991 | |
G: 78 | |
0 5 20 21 30 56 60 63 70 93 102 133 141 155 158 | |
170 182 190 208 209 210 214 230 233 287 299 321 363 379 398 | |
437 439 455 490 536 539 567 580 609 615 616 624 659 678 681 | |
691 692 703 712 717 747 753 757 764 770 784 789 792 800 805 | |
808 818 837 849 856 859 877 889 900 920 933 949 959 964 966 | |
973 980 990 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 3 8 2 1 4 3 2 | |
A: 125 | |
3 15 27 45 50 67 68 85 91 128 132 142 147 150 151 | |
160 162 174 188 197 200 212 216 217 229 231 232 234 264 273 | |
287 289 290 291 295 301 306 312 315 327 355 362 376 390 409 | |
410 411 414 417 428 434 436 445 446 447 449 450 470 500 503 | |
505 511 518 520 525 535 544 553 557 570 574 579 587 589 595 | |
601 608 622 629 634 640 651 655 656 657 662 672 685 690 699 | |
704 707 712 724 732 754 755 762 777 784 789 790 804 807 816 | |
823 838 850 859 866 868 870 888 890 904 929 930 933 943 954 | |
962 971 976 979 983 | |
B: 327 | |
2 4 6 7 9 11 14 19 20 24 25 26 29 33 36 | |
40 53 56 57 58 60 61 62 65 66 71 72 75 76 79 | |
89 93 94 97 99 101 102 105 106 110 113 115 124 134 135 | |
136 137 138 143 145 146 153 154 156 157 159 165 166 167 168 | |
170 175 176 179 180 182 189 191 193 194 195 198 203 208 220 | |
221 225 228 235 236 238 241 242 251 252 256 258 259 260 269 | |
270 272 278 279 280 281 283 285 286 294 296 307 309 310 317 | |
321 324 325 333 334 346 348 350 351 352 353 354 361 363 364 | |
365 369 370 371 373 375 377 381 383 388 391 395 397 398 399 | |
403 406 408 412 415 418 419 420 426 427 429 430 435 437 440 | |
441 448 455 456 459 460 462 465 469 471 473 478 486 488 489 | |
491 492 493 499 501 502 504 509 515 517 522 523 524 527 528 | |
531 534 542 547 548 550 551 554 555 556 563 568 571 573 583 | |
584 586 591 596 597 598 600 604 606 613 619 623 628 630 632 | |
633 636 637 652 653 654 658 660 661 669 671 674 675 676 682 | |
683 686 687 691 695 696 700 709 713 714 716 718 719 720 721 | |
722 723 727 728 734 735 736 739 742 743 747 748 751 752 760 | |
766 767 776 778 781 785 786 792 797 800 802 803 806 808 811 | |
815 817 822 826 828 829 831 835 836 840 856 857 860 863 864 | |
878 885 887 891 896 899 900 907 909 914 915 916 921 923 925 | |
927 932 934 937 938 941 942 946 948 955 957 960 963 966 967 | |
968 970 972 974 982 988 990 992 993 994 996 997 | |
C: 84 | |
13 34 42 44 55 80 90 96 112 114 127 133 148 185 206 | |
214 222 226 227 239 246 247 253 274 277 300 322 339 359 360 | |
372 382 402 413 416 432 433 439 461 483 485 494 530 540 545 | |
552 564 565 585 603 626 631 641 642 644 664 673 677 678 681 | |
701 702 708 710 738 749 753 768 788 791 820 825 841 875 882 | |
897 908 910 918 926 945 949 953 975 | |
D: 45 | |
5 30 39 47 69 70 98 129 131 172 181 209 261 263 275 | |
293 336 337 343 366 385 386 393 401 404 495 546 581 609 625 | |
643 698 726 756 780 809 848 854 873 883 917 924 964 965 981 | |
E: 190 | |
1 10 12 18 21 22 28 31 35 37 46 48 54 63 64 | |
77 78 81 82 83 84 87 92 95 100 104 107 116 123 139 | |
140 141 144 152 155 161 163 164 171 184 186 187 190 192 199 | |
201 204 210 211 215 218 237 243 244 245 248 249 250 267 268 | |
271 292 298 308 318 320 323 332 338 344 345 356 368 374 378 | |
379 407 421 422 423 424 438 443 444 453 464 466 475 476 477 | |
482 484 487 496 497 506 510 526 536 537 538 541 560 561 562 | |
576 578 580 588 592 594 610 612 614 615 617 620 621 624 627 | |
635 646 647 648 649 659 663 665 679 684 689 697 703 705 717 | |
729 730 731 740 745 750 757 765 769 771 774 787 795 805 812 | |
813 827 833 837 839 843 846 849 853 855 861 862 867 874 876 | |
879 880 893 894 895 898 901 902 911 912 913 919 931 935 936 | |
956 959 961 969 973 980 984 987 991 998 | |
F: 138 | |
0 17 23 32 38 49 52 88 108 109 111 120 121 122 126 | |
130 158 183 205 207 213 219 223 224 230 240 255 257 265 282 | |
288 297 299 302 303 304 311 313 314 329 330 342 349 357 358 | |
367 380 384 389 392 396 405 431 454 457 463 467 468 472 474 | |
480 481 498 507 508 513 519 521 529 532 539 543 549 558 566 | |
567 572 577 582 593 599 602 611 616 638 650 666 668 688 692 | |
706 711 725 733 737 741 746 758 759 761 764 770 772 773 798 | |
799 801 810 814 818 819 830 832 834 842 845 847 851 865 871 | |
872 877 884 892 905 920 922 928 939 940 944 947 951 952 958 | |
986 989 995 | |
G: 91 | |
8 16 41 43 51 59 73 74 86 103 117 118 119 125 149 | |
169 173 177 178 196 202 233 254 262 266 276 284 305 316 319 | |
326 328 331 335 340 341 347 387 394 400 425 442 451 452 458 | |
479 490 512 514 516 533 559 569 575 590 605 607 618 639 645 | |
667 670 680 693 694 715 744 763 775 779 782 783 793 794 796 | |
821 824 844 852 858 869 881 886 889 903 906 950 977 978 985 | |
999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment