# Unexpected result of set-contains? #400

Closed
opened this Issue Dec 1, 2018 · 2 comments

Projects
None yet
2 participants

### zephyrfalcon commented Dec 1, 2018

 Hi, I am seeing unexpected results when creating a set, adding elements with `set-adjoin` and then checking if they are in the set with `set-contains?`. This is my code: ```(use scheme.set) (define my-set (let loop ((a-set (set eq-comparator)) (numbers (iota 20))) (if (null? numbers) a-set (loop (set-adjoin a-set (car numbers)) (cdr numbers))))) (print (set-size my-set)) ;; length is correct (print (set->list my-set)) ;; everything shows up ;; some of these show up as #f! (set-for-each (lambda (x) (print "Set contains " x "? " (set-contains? my-set x))) my-set)``` I am getting these results: ``````20 (14 15 16 17 12 13 18 19 1 0 11 10 9 8 7 6 5 4 3 2) Set contains 2? #f Set contains 3? #f Set contains 4? #f Set contains 5? #f Set contains 6? #t Set contains 7? #t Set contains 8? #f Set contains 9? #f Set contains 10? #f Set contains 11? #f Set contains 0? #t Set contains 1? #t Set contains 19? #t Set contains 18? #t Set contains 13? #t Set contains 12? #t Set contains 17? #t Set contains 16? #t Set contains 15? #t Set contains 14? #t `````` As far as I can tell, `set-size` works properly, so does `set->list` which shows all the elements I added. But when I loop over the set with `set-for-each`, and test if each element is in the set (the same set we're looping over), then `set-contains?` returns `#f` for a number of elements. The actual results may be different on other computers, I don't know. When I use 10 elements, they all show up. With 20, I am starting to see the above. If I create the set with `(list->set eq-comparator (iota 20))`, it does work as expected. Am I using `set-adjoin` incorrectly, or is this a bug? I am using Gauche 0.9.6 (Homebrew, latest version) on macOS Sierra (10.12.6). To be precise, `Gauche scheme shell, version 0.9.6 [utf-8,pthreads], x86_64-apple-darwin16.7.0`.
Owner

### shirok commented Dec 1, 2018

 Wow. Confirmed on Linux and development HEAD. That's perplexing, but something seems fishy in the underlying hashtable. Looking at it.

### shirok added a commit that referenced this issue Dec 1, 2018

``` Fix: hash-table-copy that didn't preserve entries' hashval ```
```The bug was lurking unnoticed since entries' hashval is only used
when hashtable is rehashed.

The cause of #400```
``` 34c9c1c ```
Owner

### shirok commented Dec 1, 2018

 Turned out this is an old bug in hash-table-copy that hadn't been noticed (amazingly!). It only surfaces when you copy a hashtable then add a bunch of entries to the copy so that it triggers rehashing. I guess it haven't been noticed since we've been using hashtables as mutable object for most of the time. The set interface is mostly functional, so internally it copies hashtables, hence it finally came up. Thanks for reporting!