samchon / tstl

TypeScript-STL (Standard Template Library, migrated from C++)
MIT License
602 stars 49 forks source link

Wrong codes on HashBuckets.reserve() and HashBuckets.insert() #80

Closed samchon closed 4 years ago

samchon commented 4 years ago

Summary

Code occuring the bug

https://github.com/samchon/tstl/blob/b8502f7473df3c303b2090f00212d03ebd18473e/src/internal/hash/HashBuckets.ts#L59-L65

The reserve() method must not increase its stored elements' size.

https://github.com/samchon/tstl/blob/b8502f7473df3c303b2090f00212d03ebd18473e/src/internal/hash/HashBuckets.ts#L106-L114

When capacity is not enough, the insert() method must call not rehash() but reserve().

Detailed Story

The HashBuckets.reserve() method is a method increasing capacity, however, I'd implemented it to increase number of stored elements, too. By such mistake, whenever a new item has been inserted, length of buckets would be increased by square.

However, there was the second mistake in my implementation. When the HashBuckets.insert() method is called and capacity of the buckets are not enough, the HashBuckets.reserve() should be called to increase its capacity. However, I'd called not HashBuckets.reserve() but HashBuckets.rehash().

By the way, it had been very lucky that such wrong implementations had not affected to the program. As the HashBuckets.insert() method had called HashBuckets.rehash() instead of the HashBuckets.reserve(), error of the HashBuckets.reserve() could not be occured. Also, default max_load_factor of the hash buckets are 1, so reserve(n) and rehash(n) causes the same result. Therefore, until user calls configuration functions of the hash-associative-container like HashMap.reserve() or HashMap.max_load_factor(z), any error could not be occurred. Of course, such custom configuration functions are not called quite a bit. It was really lucky.

Anyway, whether it was luck or not, I must fix those bugs, as soon as possible.