From 8a77228e32dcf4930d31629e15a28f07145f41d5 Mon Sep 17 00:00:00 2001 From: Mavo Date: Wed, 24 Feb 2016 13:28:13 +0100 Subject: [PATCH] Set value in BitVectorHashMap Former-commit-id: 2083df9c4abfa5484a694b59bc5e63be41361146 --- src/storage/BitVectorHashMap.cpp | 24 ++++++++++++++++++++++++ src/storage/BitVectorHashMap.h | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/storage/BitVectorHashMap.cpp b/src/storage/BitVectorHashMap.cpp index 4d6238350..08875af3b 100644 --- a/src/storage/BitVectorHashMap.cpp +++ b/src/storage/BitVectorHashMap.cpp @@ -140,6 +140,11 @@ namespace storm { return findOrAddAndGetBucket(key, value).first; } + template + void BitVectorHashMap::setOrAdd(storm::storage::BitVector const& key, ValueType const& value) { + setOrAddAndGetBucket(key, value); + } + template std::pair BitVectorHashMap::findOrAddAndGetBucket(storm::storage::BitVector const& key, ValueType const& value) { // If the load of the map is too high, we increase the size. @@ -161,6 +166,25 @@ namespace storm { } } + template + std::size_t BitVectorHashMap::setOrAddAndGetBucket(storm::storage::BitVector const& key, ValueType const& value) { + // If the load of the map is too high, we increase the size. + if (numberOfElements >= loadFactor * *currentSizeIterator) { + this->increaseSize(); + } + + std::tuple flagBucketTuple = this->findBucketToInsert(key); + STORM_LOG_ASSERT(!std::get<2>(flagBucketTuple), "Failed to find bucket for insertion."); + if (!std::get<0>(flagBucketTuple)) { + // Insert the new bits into the bucket. + buckets.set(std::get<1>(flagBucketTuple) * bucketSize, key); + occupied.set(std::get<1>(flagBucketTuple)); + ++numberOfElements; + } + values[std::get<1>(flagBucketTuple)] = value; + return std::get<1>(flagBucketTuple); + } + template ValueType BitVectorHashMap::getValue(storm::storage::BitVector const& key) const { std::pair flagBucketPair = this->findBucket(key); diff --git a/src/storage/BitVectorHashMap.h b/src/storage/BitVectorHashMap.h index d50dac59e..18ca52186 100644 --- a/src/storage/BitVectorHashMap.h +++ b/src/storage/BitVectorHashMap.h @@ -66,6 +66,15 @@ namespace storm { * @return The found value if the key is already contained in the map and the provided new value otherwise. */ ValueType findOrAdd(storm::storage::BitVector const& key, ValueType const& value); + + /*! + * Sets the given key value pain in the map. If the key is found in the map, the corresponding value is + * overwritten with the given value. Otherwise, the key is inserted with the given value. + * + * @param key The key to search or insert. + * @param value The value to set. + */ + void setOrAdd(storm::storage::BitVector const& key, ValueType const& value); /*! * Searches for the given key in the map. If it is found, the mapped-to value is returned. Otherwise, the @@ -79,6 +88,16 @@ namespace storm { */ std::pair findOrAddAndGetBucket(storm::storage::BitVector const& key, ValueType const& value); + /*! + * Sets the given key value pain in the map. If the key is found in the map, the corresponding value is + * overwritten with the given value. Otherwise, the key is inserted with the given value. + * + * @param key The key to search or insert. + * @param value The value to set. + * @return The index of the bucket into which the key was inserted. + */ + std::size_t setOrAddAndGetBucket(storm::storage::BitVector const& key, ValueType const& value); + /*! * Retrieves the key stored in the given bucket (if any) and the value it is mapped to. *