cii icon indicating copy to clipboard operation
cii copied to clipboard

In chapter 9, Set_union(s,NULL) clones a set.Its member size is smaller than expected

Open GoogleCodeExporter opened this issue 10 years ago • 1 comments

What steps will reproduce the problem?
In CII's chapter 9, Set_union(s,NULL) clones a set, but the cloned set's size 
is smaller.

   Here is test code:

   Set_T s,t;
   s = Set_new(1024,NULL,NULL);
   t = Set_union(s,NULL);
   printf("s:%d, t:%d\n", s->size,t->size);    

   output is: 
   s:1021, t:509 

   The problem seems related to Set_new's implementation:
        static int primes[] = { 509, 509, 1021, 2053, 4093,
                8191, 16381, 32771, 65521, INT_MAX };
        assert(hint >= 0);
        for (i = 1; primes[i] < hint; i++)
                ;

   When giving it a hint, it searches the primes table and use the greatest one less than hint.
   If  the hint happens to be the number in primes, a much less value will be used.

What is the expected output? What do you see instead?
   When clone a set, it's member size should be the same.

What version of the product are you using? On what operating system?
 N/A

Please provide any additional information below.
 N/A

Original issue reported on code.google.com by [email protected] on 27 Jun 2012 at 12:46

GoogleCodeExporter avatar Mar 23 '15 17:03 GoogleCodeExporter

Yeah, so I change primes[i] < hint to primes[i] <= hint. So we will can get the exact value when primes[i] == hint.

ylme avatar Oct 14 '19 03:10 ylme