ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.107 by root, Sun May 2 14:46:56 2010 UTC vs.
Revision 1.111 by root, Tue Jul 6 20:00:46 2010 UTC

429 { 429 {
430 p->~Tp (); 430 p->~Tp ();
431 } 431 }
432}; 432};
433 433
434// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
435// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
436// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
437struct tausworthe_random_generator
438{
439 uint32_t state [4];
440
441 void operator =(const tausworthe_random_generator &src)
442 {
443 state [0] = src.state [0];
444 state [1] = src.state [1];
445 state [2] = src.state [2];
446 state [3] = src.state [3];
447 }
448
449 void seed (uint32_t seed);
450 uint32_t next ();
451};
452
453// Xorshift RNGs, George Marsaglia
454// http://www.jstatsoft.org/v08/i14/paper
455// this one is about 40% faster than the tausworthe one above (i.e. not much),
456// despite the inlining, and has the issue of only creating 2**32-1 numbers.
457// see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
458struct xorshift_random_generator
459{
460 uint32_t x, y;
461
462 void operator =(const xorshift_random_generator &src)
463 {
464 x = src.x;
465 y = src.y;
466 }
467
468 void seed (uint32_t seed)
469 {
470 x = seed;
471 y = seed * 69069U;
472 }
473
474 uint32_t next ()
475 {
476 uint32_t t = x ^ (x << 10);
477 x = y;
478 y = y ^ (y >> 13) ^ t ^ (t >> 10);
479 return y;
480 }
481};
482
483template<class generator>
484struct random_number_generator : generator
485{
486 // uniform distribution, [0 .. num - 1]
487 uint32_t operator ()(uint32_t num)
488 {
489 return !is_constant (num) ? get_range (num) // non-constant
490 : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two
491 : this->next () & (num - 1); // constant, power-of-two
492 }
493
494 // return a number within the closed interval [min .. max]
495 int operator () (int r_min, int r_max)
496 {
497 return is_constant (r_min <= r_max) && r_min <= r_max
498 ? r_min + operator ()(r_max - r_min + 1)
499 : get_range (r_min, r_max);
500 }
501
502 // return a number within the half-open interval [0..1[
503 double operator ()()
504 {
505 return this->next () / (double)0x100000000;
506 }
507
508protected:
509 uint32_t get_range (uint32_t r_max);
510 int get_range (int r_min, int r_max);
511};
512
513typedef random_number_generator<tausworthe_random_generator> rand_gen;
514
515extern rand_gen rndm, rmg_rndm;
516
517INTERFACE_CLASS (attachable) 434INTERFACE_CLASS (attachable)
518struct refcnt_base 435struct refcnt_base
519{ 436{
520 typedef int refcnt_t; 437 typedef int refcnt_t;
521 mutable refcnt_t ACC (RW, refcnt); 438 mutable refcnt_t ACC (RW, refcnt);
724 { 641 {
725 erase (&obj); 642 erase (&obj);
726 } 643 }
727}; 644};
728 645
646/////////////////////////////////////////////////////////////////////////////
647
648// something like a vector or stack, but without
649// out of bounds checking
650template<typename T>
651struct fixed_stack
652{
653 T *data;
654 int size;
655 int max;
656
657 fixed_stack ()
658 : size (0), data (0)
659 {
660 }
661
662 fixed_stack (int max)
663 : size (0), max (max)
664 {
665 data = salloc<T> (max);
666 }
667
668 void reset (int new_max)
669 {
670 sfree (data, max);
671 size = 0;
672 max = new_max;
673 data = salloc<T> (max);
674 }
675
676 void free ()
677 {
678 sfree (data, max);
679 data = 0;
680 }
681
682 ~fixed_stack ()
683 {
684 sfree (data, max);
685 }
686
687 T &operator[](int idx)
688 {
689 return data [idx];
690 }
691
692 void push (T v)
693 {
694 data [size++] = v;
695 }
696
697 T &pop ()
698 {
699 return data [--size];
700 }
701
702 T remove (int idx)
703 {
704 T v = data [idx];
705
706 data [idx] = data [--size];
707
708 return v;
709 }
710};
711
712/////////////////////////////////////////////////////////////////////////////
713
729// basically does what strncpy should do, but appends "..." to strings exceeding length 714// basically does what strncpy should do, but appends "..." to strings exceeding length
730// returns the number of bytes actually used (including \0) 715// returns the number of bytes actually used (including \0)
731int assign (char *dst, const char *src, int maxsize); 716int assign (char *dst, const char *src, int maxsize);
732 717
733// type-safe version of assign 718// type-safe version of assign

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines