about summary refs log tree commit diff
path: root/src/libcollections/hashmap.rs
AgeCommit message (Collapse)AuthorLines
2014-06-05std: Recreate a `collections` moduleAlex Crichton-2498/+0
As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2014-06-04collections: optimize `HashMap`. Add `DefaultResizePolicy`.Piotr Czarnecki-72/+117
Refactored the load factor and the minimum capacity out of HashMap. The size of HashMap<K, V> is now 64 bytes by default on a 64-bit platform (or 48 bytes, that is 2 words less, with FNV) Added a documentation in a few places to clarify the behavior.
2014-06-01std: Drop Total from Total{Eq,Ord}Alex Crichton-26/+26
This completes the last stage of the renaming of the comparison hierarchy of traits. This change renames TotalEq to Eq and TotalOrd to Ord. In the future the new Eq/Ord will be filled out with their appropriate methods, but for now this change is purely a renaming change. [breaking-change]
2014-05-30std: Rename {Eq,Ord} to Partial{Eq,Ord}Alex Crichton-9/+9
This is part of the ongoing renaming of the equality traits. See #12517 for more details. All code using Eq/Ord will temporarily need to move to Partial{Eq,Ord} or the Total{Eq,Ord} traits. The Total traits will soon be renamed to {Eq,Ord}. cc #12517 [breaking-change]
2014-05-30auto merge of #14524 : ahmedcharles/rust/to_string, r=alexcrichtonbors-2/+2
2014-05-29std: Recreate a `rand` moduleAlex Crichton-2/+2
This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc #13851 [breaking-change]
2014-05-29Change to_owned() to to_string().Ahmed Charles-2/+2
2014-05-27auto merge of #14414 : richo/rust/features/nerf_unused_string_fns, ↵bors-5/+5
r=alexcrichton This should block on #14323
2014-05-27std: Remove String's to_ownedRicho Healey-5/+5
2014-05-27collections: add Show impl test for HashMapErick Tryzelaar-0/+14
2014-05-22Spelling/doc formatting fixes.Huon Wilson-1/+1
2014-05-15Updates with core::fmt changesAlex Crichton-8/+8
1. Wherever the `buf` field of a `Formatter` was used, the `Formatter` is used instead. 2. The usage of `write_fmt` is minimized as much as possible, the `write!` macro is preferred wherever possible. 3. Usage of `fmt::write` is minimized, favoring the `write!` macro instead.
2014-05-16Work around parse error caused by #14240.Chris Morgan-2/+1
2014-05-15Use assertions in find_with_or_insert_with exampleChris Morgan-3/+4
This lets us test it automatically and also makes it more obvious what the expected result is.
2014-05-15Rename HashMap.mangle to find_with_or_insert_with.Chris Morgan-22/+24
This also entails swapping the order of the find and insert callbacks so that their order matches the order of the terms in the method name.
2014-05-15Tidy up the HashMap.mangle example.Chris Morgan-8/+7
2014-05-15Reinstate HashMap.mangle().Chris Morgan-21/+57
This was removed when the Robin Hood hash map came along, but it is a useful thing to have. The comment is taken directly from what was there before (e.g. in 0.9) but with appropriate language changes (like `StrBuf` instead of `~str`).
2014-05-14Suppress warnings on 32bit platforms.OGINO Masanori-2/+2
On 32bit platforms, int is the same as i32, so 0xffffffff is "out of range." Annotating variables as u32 fixes the problems. Signed-off-by: OGINO Masanori <masanori.ogino@gmail.com>
2014-05-11hashmap: port to the new allocator APIDaniel Micay-17/+13
2014-05-10rename `global_heap` -> `libc_heap`Daniel Micay-2/+2
This module only contains wrappers for malloc and realloc with out-of-memory checks.
2014-05-07std: Modernize the local_data apiAlex Crichton-37/+33
This commit brings the local_data api up to modern rust standards with a few key improvements: * The `pop` and `set` methods have been combined into one method, `replace` * The `get_mut` method has been removed. All interior mutability should be done through `RefCell`. * All functionality is now exposed as a method on the keys themselves. Instead of importing std::local_data, you now use "key.replace()" and "key.get()". * All closures have been removed in favor of RAII functionality. This means that get() and get_mut() no long require closures, but rather return Option<SmartPointer> where the smart pointer takes care of relinquishing the borrow and also implements the necessary Deref traits * The modify() function was removed to cut the local_data interface down to its bare essentials (similarly to how RefCell removed set/get). [breaking-change]
2014-05-01Add debug_assert and debug_assert_eq macrosSteven Fackler-12/+6
I also switched some `assert!` calls over to `debug_assert!`. Closes #12049. RFC: 0015-assert
2014-05-01remove leftover obsolete string literalsDaniel Micay-2/+2
2014-04-22auto merge of #13653 : jbcrail/rust/fix-comment-mistakes, r=alexcrichtonbors-4/+4
2014-04-21Just some general cleanup in the HashMap moduleClark Gaebel-112/+168
I went through the HashMap module, fixed spelling mistakes, minor inefficiencies, added tests, and other trivial changes.
2014-04-21Fix misspellings in comments.Joseph Crail-4/+4
2014-04-19auto merge of #13614 : cgaebel/rust/master, r=brsonbors-30/+66
We previously allocated 3x for every HashMap creation and resize. This patch reduces it to 1x.
2014-04-18Replace all ~"" with "".to_owned()Richo Healey-5/+5
2014-04-18Reduce HashMap allocations.Clark Gaebel-30/+66
2014-04-15Add a default impl for Set::is_supersetSteven Fackler-15/+0
I also deleted a bunch of documentation that was copy/pasted from the trait definition.
2014-04-11libtest: rename `BenchHarness` to `Bencher`Liigo Zhuang-6/+6
Closes #12640
2014-04-10auto merge of #13440 : huonw/rust/strbuf, r=alexcrichtonbors-70/+119
libstd: Implement `StrBuf`, a new string buffer type like `Vec`, and port all code over to use it. Rebased & tests-fixed version of https://github.com/mozilla/rust/pull/13269
2014-04-11Fix tests. Add Vec<u8> conversion to StrBuf.Huon Wilson-2/+10
2014-04-10libstd: Implement `StrBuf`, a new string buffer type like `Vec`, andPatrick Walton-70/+111
port all code over to use it.
2014-04-09collections: replace all ~[T] with Vec<T>.Huon Wilson-11/+11
2014-04-04Fix fallout from std::libc separationCorey Richardson-1/+2
2014-04-02Fix fallout of requiring uint indicesAlex Crichton-9/+9
2014-03-31collections: Switch field privacy as necessaryAlex Crichton-23/+23
2014-03-30Rename `from_iterator` to `from_iter` for consistency.Brian Anderson-2/+2
2014-03-28Rename Pod into CopyFlavio Percoco-7/+7
Summary: So far, we've used the term POD "Plain Old Data" to refer to types that can be safely copied. However, this term is not consistent with the other built-in bounds that use verbs instead. This patch renames the Pod kind into Copy. RFC: 0003-opt-in-builtin-traits Test Plan: make check Reviewers: cmr Differential Revision: http://phabricator.octayn.net/D3
2014-03-25Changed `iter::Extendable` and `iter::FromIterator` to take a `Iterator` by ↵Marvin Löbel-6/+6
value
2014-03-23use TotalEq for HashMapDaniel Micay-30/+27
Closes #5283
2014-03-20rename std::vec_ng -> std::vecDaniel Micay-3/+3
Closes #12771
2014-03-20rename std::vec -> std::sliceDaniel Micay-2/+2
Closes #12702
2014-03-14auto merge of #12867 : alexcrichton/rust/issue-12860, r=thestingerbors-31/+37
This switches a "tail call" to a manual loop to get around LLVM not optimizing to a tail call. Close #12860
2014-03-13collections: Don't recurse in hashmap robin_hoodAlex Crichton-31/+37
This switches a "tail call" to a manual loop to get around LLVM not optimizing to a tail call. Close #12860
2014-03-13collections: move hashmap's example to the struct.Huon Wilson-53/+53
Most people go straight to the struct, not looking at the module, so the example was well hidden.
2014-03-12Performance-oriented hashtable.Clark Gaebel-589/+1407
Previously, rust's hashtable was totally unoptimized. It used an Option per key-value pair, and used very naive open allocation. The old hashtable had very high variance in lookup time. For an example, see the 'find_nonexisting' benchmark below. This is fixed by keys in 'lucky' spots with a low probe sequence length getting their good spots stolen by keys with long probe sequence lengths. This reduces hashtable probe length variance, while maintaining the same mean. Also, other optimization liberties were taken. Everything is as cache aware as possible, and this hashtable should perform extremely well for both large and small keys and values. Benchmarks: comprehensive_old_hashmap 378 ns/iter (+/- 8) comprehensive_new_hashmap 206 ns/iter (+/- 4) 1.8x faster old_hashmap_as_queue 238 ns/iter (+/- 8) new_hashmap_as_queue 119 ns/iter (+/- 2) 2x faster old_hashmap_insert 172 ns/iter (+/- 8) new_hashmap_insert 146 ns/iter (+/- 11) 1.17x faster old_hashmap_find_existing 50 ns/iter (+/- 12) new_hashmap_find_existing 35 ns/iter (+/- 6) 1.43x faster old_hashmap_find_notexisting 49 ns/iter (+/- 49) new_hashmap_find_notexisting 34 ns/iter (+/- 4) 1.44x faster Memory usage of old hashtable (64-bit assumed): aligned(8+sizeof(K)+sizeof(V))/0.75 + 6 words Memory usage of new hashtable: (aligned(sizeof(K)) + aligned(sizeof(V)) + 8)/0.9 + 6.5 words BUT accesses are much more cache friendly. In fact, if the probe sequence length is below 8, only two cache lines worth of hashes will be pulled into cache. This is unlike the old version which would have to stride over the stoerd keys and values, and would be more cache unfriendly the bigger the stored values got. And did you notice the higher load factor? We can now reasonably get a load factor of 0.9 with very good performance.
2014-03-12Update users for the std::rand -> librand move.Huon Wilson-2/+2
2014-03-06collections: Correct with_capacity_and_hasherAlex Crichton-10/+11
The arguments were accidentally swapped in the wrong order. Closes #12743