├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── jmh └── java │ └── com │ └── trivago │ └── kangaroo │ ├── AbstractBenchHelper.java │ ├── AbstractCommonBenchHelper.java │ ├── FastutilWrapperBusyWaitingBenchmark.java │ ├── FastutilWrapperDefaultBenchmark.java │ └── JavaUtilWrapperBenchmark.java ├── main └── java │ └── com │ └── trivago │ └── fastutilconcurrentwrapper │ ├── ConcurrentIntFloatMapBuilder.java │ ├── ConcurrentIntIntMapBuilder.java │ ├── ConcurrentLongFloatMapBuilder.java │ ├── ConcurrentLongIntMapBuilder.java │ ├── ConcurrentLongLongMapBuilder.java │ ├── IntFloatMap.java │ ├── IntIntMap.java │ ├── LongFloatMap.java │ ├── LongIntMap.java │ ├── LongLongMap.java │ ├── PrimitiveIntKeyMap.java │ ├── PrimitiveKeyMap.java │ ├── PrimitiveLongKeyMap.java │ ├── map │ ├── ConcurrentBusyWaitingIntFloatMap.java │ ├── ConcurrentBusyWaitingIntIntMap.java │ ├── ConcurrentBusyWaitingLongFloatMap.java │ ├── ConcurrentBusyWaitingLongIntMap.java │ ├── ConcurrentBusyWaitingLongLongMap.java │ ├── ConcurrentIntFloatMap.java │ ├── ConcurrentIntIntMap.java │ ├── ConcurrentLongFloatMap.java │ ├── ConcurrentLongIntMap.java │ ├── ConcurrentLongLongMap.java │ └── PrimitiveConcurrentMap.java │ └── wrapper │ ├── PrimitiveFastutilIntFloatWrapper.java │ ├── PrimitiveFastutilIntIntWrapper.java │ ├── PrimitiveFastutilLongFloatWrapper.java │ ├── PrimitiveFastutilLongIntWrapper.java │ └── PrimitiveFastutilLongLongWrapper.java └── test └── java └── com └── trivago └── fastutilconcurrentwrapper ├── AbstractMapTest.java ├── ConcurrentIntFloatMapBuilderTest.java ├── ConcurrentIntIntMapBuilderTest.java ├── ConcurrentLongFloatMapBuilderTest.java ├── ConcurrentLongIntMapBuilderTest.java ├── ConcurrentLongLongMapBuilderTest.java ├── longint ├── AbstractLongIntMapTest.java ├── ConcurrentBusyWaitingLongIntMapTest.java ├── ConcurrentPrimitiveLongIntMapTest.java └── PrimitiveFastutilLongIntWrapperTest.java └── longlong ├── AbstractLongLongMapTest.java ├── ConcurrentBusyWaitingLongLongMapTest.java ├── ConcurrentLongLongMapTest.java └── PrimitiveFastutilLongLongWrapperTest.java /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | # This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time 6 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 7 | 8 | name: Java CI with Gradle 9 | 10 | on: [push, pull_request] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - name: Set up JDK 17 20 | uses: actions/setup-java@v3 21 | with: 22 | java-version: '17' 23 | distribution: 'adopt' 24 | - name: Clean and Build with Gradle 25 | run: ./gradlew clean build 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | build 3 | out/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | .java-version 20 | .vscode 21 | 22 | ### NetBeans ### 23 | nbproject/private/ 24 | build/ 25 | nbbuild/ 26 | dist/ 27 | nbdist/ 28 | .nb-gradle/ 29 | 30 | local/** 31 | 32 | **/out/**/*.class 33 | **/.DS_Store 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FastUtil Concurrent Wrapper 2 | 3 | ![Java CI](https://github.com/trivago/fastutil-concurrent-wrapper/actions/workflows/gradle.yml/badge.svg) 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.trivago/fastutil-concurrent-wrapper/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/com.trivago/fastutil-concurrent-wrapper/) 5 | 6 | ## Description 7 | 8 | Set of concurrent wrappers around [fastutil primitive maps](https://github.com/vigna/fastutil). 9 | 10 | [How we use it at trivago.](https://tech.trivago.com/post/2022-03-09-why-and-how-we-use-primitive-maps) 11 | 12 | Main purpose is to provide useful concurrent builders around 13 | fastutil primitive maps with flexible locking policy. 14 | 15 | Advantages over [java.util wrappers](https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html): 16 | 17 | - builders provide maps with buckets, 18 | - every map uses `striped ReadWriteLocks` instead of `synchronized(mutex)`; one RW-lock per map's bucket, 19 | - two lock modes: `standard` and `busy-waiting` (could be good for low-latency systems), 20 | - no extra memory on stack -- API based on `primitive types`. 21 | 22 | Check [usage](#usage) section for more details. 23 | 24 | _Note_: currently the lib contains wrappers not for every primitive map. Feel free to contribute. 25 | 26 | ## Install 27 | 28 | #### Maven 29 | 30 | ```xml 31 | 32 | 33 | com.trivago 34 | fastutil-concurrent-wrapper 35 | 0.2.2 36 | 37 | ``` 38 | 39 | #### Gradle 40 | 41 | ```groovy 42 | implementation group: 'com.trivago', name: 'fastutil-concurrent-wrapper', version: '0.2.2' 43 | ``` 44 | 45 | ## Usage 46 | 47 | ### Builder options 48 | - `number of buckets` -- number of buckets in the map (default `8`), 49 | - `default value` -- default value, for _getOrDefault()_ method 50 | - `initial capacity` -- initial map capacity (default `100_000`), 51 | - `concurrent mode` -- lock mode: _default_ and _busy-waiting_, 52 | - `load factor` -- map load factor (default `0.8f`). 53 | 54 | ### Basic usage 55 | 56 | ```java 57 | ConcurrentLongLongMapBuilder b = ConcurrentLongLongMapBuilder.newBuilder() 58 | .withBuckets(2) 59 | .withDefaultValue(0) 60 | .withInitialCapacity(100) 61 | .withMode(ConcurrentLongLongMapBuilder.MapMode.BUSY_WAITING) 62 | .withLoadFactor(0.9f); 63 | 64 | LongLongMap map = b.build(); 65 | 66 | map.put(1L, 10L); 67 | long v = map.get(1L); 68 | 69 | ``` 70 | 71 | Examples of creation and usage could be found inside 72 | [test directory](https://github.com/trivago/fastutil-concurrent-wrapper/tree/master/src/test/java/com/trivago/fastutilconcurrentwrapper); 73 | 74 | ### MapMode 75 | 76 | Currently, we offer two locking modes: 77 | 78 | - `blocking` (default), 79 | - `busy-waiting`. 80 | 81 | ### JMH tests 82 | 83 | For running JMH tests just execute: 84 | ```bash 85 | ./gradlew jmh 86 | ``` 87 | 88 | Results for `FastutilWrapper BusyWaiting mode` vs `FastutilWrapper Default mode` vs [java.util wrappers](https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html) 89 | 90 | Throughput (more is better) 91 | 92 | ```shell 93 | Benchmark Mode Cnt Score Error Units 94 | 95 | FastutilWrapperBusyWaitingBenchmark.testRandomAllOpsThroughput thrpt 15 14517457,055 ? 795637,784 ops/s 96 | FastutilWrapperBusyWaitingBenchmark.testRandomGetThroughput thrpt 15 16610181,320 ? 1456776,589 ops/s 97 | FastutilWrapperBusyWaitingBenchmark.testRandomPutThroughput thrpt 13 11706178,916 ? 2547333,524 ops/s 98 | 99 | FastutilWrapperDefaultBenchmark.testRandomAllOpsThroughput thrpt 15 7385357,514 ? 1127356,032 ops/s 100 | FastutilWrapperDefaultBenchmark.testRandomGetThroughput thrpt 15 16190621,923 ? 1836415,022 ops/s 101 | FastutilWrapperDefaultBenchmark.testRandomPutThroughput thrpt 15 8945369,395 ? 1225460,217 ops/s 102 | 103 | JavaUtilWrapperBenchmark.testRandomAllOpsThroughput thrpt 15 4921201,916 ? 410471,239 ops/s 104 | JavaUtilWrapperBenchmark.testRandomGetThroughput thrpt 15 7827123,690 ? 557193,670 ops/s 105 | JavaUtilWrapperBenchmark.testRandomPutThroughput thrpt 15 4832517,371 ? 1122344,647 ops/s 106 | ``` 107 | AverageTime per ops (less is better) 108 | 109 | ```shell 110 | Benchmark Mode Cnt Score Error Units 111 | 112 | FastutilWrapperBusyWaitingBenchmark.testRandomAllOpsAvgTime avgt 15 268,790 ? 22,526 ns/op 113 | FastutilWrapperBusyWaitingBenchmark.testRandomGetAvgTime avgt 15 231,552 ? 16,116 ns/op 114 | FastutilWrapperBusyWaitingBenchmark.testRandomPutAvgTime avgt 10 292,246 ? 49,757 ns/op 115 | 116 | FastutilWrapperDefaultBenchmark.testRandomAllOpsAvgTime avgt 15 467,381 ? 9,790 ns/op 117 | FastutilWrapperDefaultBenchmark.testRandomGetAvgTime avgt 15 237,683 ? 14,167 ns/op 118 | FastutilWrapperDefaultBenchmark.testRandomPutAvgTime avgt 15 427,441 ? 25,116 ns/op 119 | 120 | JavaUtilWrapperBenchmark.testRandomAllOpsAvgTime avgt 15 781,869 ? 191,081 ns/op 121 | JavaUtilWrapperBenchmark.testRandomGetAvgTime avgt 15 470,869 ? 33,198 ns/op 122 | JavaUtilWrapperBenchmark.testRandomPutAvgTime avgt 15 964,613 ? 422,648 ns/op 123 | ``` 124 | 125 | The machine 126 | ```shell 127 | MacBook Pro (15-inch, 2019) 128 | Processor 2,6 GHz 6-Core Intel Core i7 129 | Memory 16 GB 2400 MHz DDR4 130 | ``` 131 | 132 | ## Maintainers 133 | A-Z surname order 134 | 135 | - [@mchernyakov](https://github.com/mchernyakov) 136 | - [@erdoganf](https://github.com/erdoganf) 137 | - [@sarveswaran-m](https://github.com/sarveswaran-m) 138 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "idea" 3 | id "java-library" 4 | id "me.champeau.jmh" version "0.6.6" 5 | id "com.vanniktech.maven.publish" version "0.18.0" 6 | } 7 | 8 | allprojects { 9 | repositories { 10 | mavenLocal() 11 | mavenCentral() 12 | maven { 13 | url "https://plugins.gradle.org/m2/" 14 | } 15 | } 16 | } 17 | 18 | group 'com.trivago' 19 | version '0.2.3-SNAPSHOT' 20 | 21 | mavenPublishing { 22 | pom { 23 | name = "fastutil-concurrent-wrapper" 24 | description = "Set of concurrent wrappers around fastutil primitive maps." 25 | url = "https://github.com/trivago/fastutil-concurrent-wrapper" 26 | licenses { 27 | license { 28 | name = "The Apache Software License, Version 2.0" 29 | url = "https://opensource.org/licenses/Apache-2.0" 30 | distribution = "repo" 31 | } 32 | } 33 | developers { 34 | developer { 35 | id = "mchernyakov" 36 | name = "Mikhail Chernyakov" 37 | url = "https://github.com/mchernyakov" 38 | } 39 | developer { 40 | id = "erdoganf" 41 | name = "Fehim Erdogan" 42 | url = "https://github.com/erdoganf" 43 | } 44 | developer { 45 | id = "sarveswaran-m" 46 | name = "Sarveswaran Meenakshisundaram" 47 | url = "https://github.com/sarveswaran-m" 48 | } 49 | } 50 | scm { 51 | url = "https://github.com/trivago/fastutil-concurrent-wrapper" 52 | connection = "scm:git:https://github.com/trivago/fastutil-concurrent-wrapper" 53 | developerConnection = "scm:git:https://github.com/trivago/fastutil-concurrent-wrapper" 54 | } 55 | } 56 | } 57 | 58 | sourceCompatibility = 11 59 | targetCompatibility = 11 60 | 61 | sourceSets { 62 | jmh { 63 | java.srcDirs = ['src/jmh/java'] 64 | resources.srcDirs = ['src/jmh/resources'] 65 | compileClasspath += sourceSets.main.runtimeClasspath 66 | } 67 | } 68 | 69 | dependencies { 70 | api group: 'it.unimi.dsi', name: 'fastutil', version: '8.5.9' 71 | 72 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' 73 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' 74 | } 75 | 76 | test { 77 | useJUnitPlatform() 78 | } 79 | 80 | jar { 81 | archiveBaseName.set('fastutil-concurrent-wrapper') 82 | } 83 | 84 | // ./gradlew publish --no-daemon --no-parallel 85 | // ./gradlew closeAndReleaseRepository 86 | publishing { 87 | repositories { 88 | maven { 89 | def releasesRepoUrl = "$buildDir/repos/releases" 90 | def snapshotsRepoUrl = "$buildDir/repos/snapshots" 91 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl 92 | } 93 | } 94 | } 95 | 96 | // ./gradlew jmh 97 | jmh { 98 | jvmArgs = ["-Xms3072m","-Xmx3072m"] 99 | duplicateClassesStrategy = DuplicatesStrategy.EXCLUDE 100 | } 101 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trivago/fastutil-concurrent-wrapper/2bef79919268d316bb88c5161f2b8db306bf1e7a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MSYS* | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'fastutil-concurrent-wrapper' 2 | -------------------------------------------------------------------------------- /src/jmh/java/com/trivago/kangaroo/AbstractBenchHelper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.kangaroo; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.ConcurrentLongLongMapBuilder; 4 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public abstract class AbstractBenchHelper extends AbstractCommonBenchHelper { 9 | 10 | protected static final int NUM_VALUES = 1_000_000; 11 | 12 | protected LongLongMap map; 13 | 14 | public void initAndLoadData(ConcurrentLongLongMapBuilder.MapMode mode) { 15 | if (mode.equals(ConcurrentLongLongMapBuilder.MapMode.BUSY_WAITING)) { 16 | map = ConcurrentLongLongMapBuilder.newBuilder() 17 | .withBuckets(16) 18 | .withInitialCapacity(NUM_VALUES) 19 | .withMode(ConcurrentLongLongMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.8f) 21 | .build(); 22 | } else { 23 | map = ConcurrentLongLongMapBuilder.newBuilder() 24 | .withBuckets(16) 25 | .withInitialCapacity(NUM_VALUES) 26 | .withLoadFactor(0.8f) 27 | .build(); 28 | } 29 | 30 | for (int i = 0; i < NUM_VALUES; i++) { 31 | long key = ThreadLocalRandom.current().nextLong(); 32 | long value = ThreadLocalRandom.current().nextLong(); 33 | map.put(key, value); 34 | } 35 | } 36 | 37 | public void testGet() { 38 | long key = ThreadLocalRandom.current().nextLong(); 39 | map.get(key); 40 | } 41 | 42 | public void testPut() { 43 | long key = ThreadLocalRandom.current().nextLong(); 44 | long value = ThreadLocalRandom.current().nextLong(); 45 | map.put(key, value); 46 | } 47 | 48 | public void testAllOps() { 49 | int op = ThreadLocalRandom.current().nextInt(3); 50 | long key = ThreadLocalRandom.current().nextLong(); 51 | switch (op) { 52 | case 1: 53 | long value = ThreadLocalRandom.current().nextLong(); 54 | map.put(key, value); 55 | break; 56 | case 2: 57 | map.remove(key); 58 | break; 59 | default: 60 | map.get(key); 61 | break; 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/jmh/java/com/trivago/kangaroo/AbstractCommonBenchHelper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.kangaroo; 2 | 3 | import org.openjdk.jmh.annotations.Benchmark; 4 | import org.openjdk.jmh.annotations.BenchmarkMode; 5 | import org.openjdk.jmh.annotations.Mode; 6 | import org.openjdk.jmh.annotations.OutputTimeUnit; 7 | import org.openjdk.jmh.annotations.Threads; 8 | 9 | import java.util.concurrent.TimeUnit; 10 | 11 | abstract public class AbstractCommonBenchHelper { 12 | @Threads(4) 13 | @Benchmark 14 | @BenchmarkMode(Mode.Throughput) 15 | public void testRandomGetThroughput() { 16 | testGet(); 17 | } 18 | 19 | @Threads(4) 20 | @Benchmark 21 | @BenchmarkMode(Mode.Throughput) 22 | public void testRandomPutThroughput() { 23 | testPut(); 24 | } 25 | 26 | @Threads(4) 27 | @Benchmark 28 | @BenchmarkMode(Mode.Throughput) 29 | public void testRandomAllOpsThroughput() { 30 | testAllOps(); 31 | } 32 | 33 | @Threads(4) 34 | @Benchmark 35 | @BenchmarkMode(Mode.AverageTime) 36 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 37 | public void testRandomGetAvgTime() { 38 | testGet(); 39 | } 40 | 41 | @Threads(4) 42 | @Benchmark 43 | @BenchmarkMode(Mode.AverageTime) 44 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 45 | public void testRandomPutAvgTime() { 46 | testPut(); 47 | } 48 | 49 | @Threads(4) 50 | @Benchmark 51 | @BenchmarkMode(Mode.AverageTime) 52 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 53 | public void testRandomAllOpsAvgTime() { 54 | testAllOps(); 55 | } 56 | 57 | abstract public void testGet(); 58 | 59 | abstract public void testPut(); 60 | 61 | abstract public void testAllOps(); 62 | } 63 | -------------------------------------------------------------------------------- /src/jmh/java/com/trivago/kangaroo/FastutilWrapperBusyWaitingBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.trivago.kangaroo; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.ConcurrentLongLongMapBuilder; 4 | import org.openjdk.jmh.annotations.Level; 5 | import org.openjdk.jmh.annotations.Measurement; 6 | import org.openjdk.jmh.annotations.Scope; 7 | import org.openjdk.jmh.annotations.Setup; 8 | import org.openjdk.jmh.annotations.State; 9 | import org.openjdk.jmh.annotations.Warmup; 10 | 11 | @State(Scope.Benchmark) 12 | @Warmup(iterations = 3, time = 1) 13 | @Measurement(iterations = 3, time = 2) 14 | public class FastutilWrapperBusyWaitingBenchmark extends AbstractBenchHelper { 15 | 16 | @Setup(Level.Trial) 17 | public void loadData() { 18 | super.initAndLoadData(ConcurrentLongLongMapBuilder.MapMode.BUSY_WAITING); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/jmh/java/com/trivago/kangaroo/FastutilWrapperDefaultBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.trivago.kangaroo; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.ConcurrentLongLongMapBuilder; 4 | import org.openjdk.jmh.annotations.Level; 5 | import org.openjdk.jmh.annotations.Measurement; 6 | import org.openjdk.jmh.annotations.Scope; 7 | import org.openjdk.jmh.annotations.Setup; 8 | import org.openjdk.jmh.annotations.State; 9 | import org.openjdk.jmh.annotations.Warmup; 10 | 11 | @State(Scope.Benchmark) 12 | @Warmup(iterations = 3, time = 1) 13 | @Measurement(iterations = 3, time = 2) 14 | public class FastutilWrapperDefaultBenchmark extends AbstractBenchHelper { 15 | 16 | @Setup(Level.Trial) 17 | public void loadData() { 18 | super.initAndLoadData(ConcurrentLongLongMapBuilder.MapMode.BLOCKING); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/jmh/java/com/trivago/kangaroo/JavaUtilWrapperBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.trivago.kangaroo; 2 | 3 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; 4 | import org.openjdk.jmh.annotations.Level; 5 | import org.openjdk.jmh.annotations.Measurement; 6 | import org.openjdk.jmh.annotations.Scope; 7 | import org.openjdk.jmh.annotations.Setup; 8 | import org.openjdk.jmh.annotations.State; 9 | import org.openjdk.jmh.annotations.Warmup; 10 | 11 | import java.util.Collections; 12 | import java.util.Map; 13 | import java.util.concurrent.ThreadLocalRandom; 14 | 15 | @State(Scope.Benchmark) 16 | @Warmup(iterations = 3, time = 1) 17 | @Measurement(iterations = 3, time = 2) 18 | public class JavaUtilWrapperBenchmark extends AbstractCommonBenchHelper { 19 | 20 | Map map; 21 | 22 | @Setup(Level.Trial) 23 | public void loadData() { 24 | Long2LongOpenHashMap m = new Long2LongOpenHashMap(AbstractBenchHelper.NUM_VALUES, 0.8f); 25 | for (int i = 0; i < AbstractBenchHelper.NUM_VALUES; i++) { 26 | long key = ThreadLocalRandom.current().nextLong(); 27 | long value = ThreadLocalRandom.current().nextLong(); 28 | m.put(key, value); 29 | } 30 | map = Collections.synchronizedMap(m); 31 | } 32 | 33 | public void testGet() { 34 | long key = ThreadLocalRandom.current().nextLong(); 35 | map.get(key); 36 | } 37 | 38 | public void testPut() { 39 | long key = ThreadLocalRandom.current().nextLong(); 40 | long value = ThreadLocalRandom.current().nextLong(); 41 | map.put(key, value); 42 | } 43 | 44 | public void testAllOps() { 45 | int op = ThreadLocalRandom.current().nextInt(3); 46 | long key = ThreadLocalRandom.current().nextLong(); 47 | switch (op) { 48 | case 1: 49 | long value = ThreadLocalRandom.current().nextLong(); 50 | map.put(key, value); 51 | break; 52 | case 2: 53 | map.remove(key); 54 | break; 55 | default: 56 | map.get(key); 57 | break; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilder.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntFloatMap; 5 | 6 | public final class ConcurrentIntFloatMapBuilder { 7 | 8 | private MapMode mapMode = MapMode.BUSY_WAITING; 9 | private int buckets = 8; 10 | private float defaultValue = 0.0f; 11 | private int initialCapacity = 100_000; 12 | private float loadFactor = 0.8f; 13 | 14 | private ConcurrentIntFloatMapBuilder() { 15 | } 16 | 17 | public static ConcurrentIntFloatMapBuilder newBuilder() { 18 | return new ConcurrentIntFloatMapBuilder(); 19 | } 20 | 21 | public ConcurrentIntFloatMapBuilder withBuckets(int buckets) { 22 | this.buckets = buckets; 23 | return this; 24 | } 25 | 26 | public ConcurrentIntFloatMapBuilder withDefaultValue(float defaultValue) { 27 | this.defaultValue = defaultValue; 28 | return this; 29 | } 30 | 31 | public ConcurrentIntFloatMapBuilder withInitialCapacity(int initialCapacity) { 32 | this.initialCapacity = initialCapacity; 33 | return this; 34 | } 35 | 36 | public ConcurrentIntFloatMapBuilder withLoadFactor(float loadFactor) { 37 | this.loadFactor = loadFactor; 38 | return this; 39 | } 40 | 41 | public ConcurrentIntFloatMapBuilder withMode(MapMode mapMode) { 42 | this.mapMode = mapMode; 43 | return this; 44 | } 45 | 46 | public IntFloatMap build() { 47 | return mapMode.createMap(this); 48 | } 49 | 50 | public enum MapMode { 51 | BUSY_WAITING { 52 | @Override 53 | IntFloatMap createMap(ConcurrentIntFloatMapBuilder builder) { 54 | return new ConcurrentBusyWaitingIntFloatMap( 55 | builder.buckets, 56 | builder.initialCapacity, 57 | builder.loadFactor, 58 | builder.defaultValue); 59 | } 60 | }, 61 | BLOCKING { 62 | @Override 63 | IntFloatMap createMap(ConcurrentIntFloatMapBuilder builder) { 64 | return new ConcurrentIntFloatMap( 65 | builder.buckets, 66 | builder.initialCapacity, 67 | builder.loadFactor, 68 | builder.defaultValue); 69 | } 70 | }; 71 | 72 | abstract IntFloatMap createMap(ConcurrentIntFloatMapBuilder builder); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilder.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntIntMap; 5 | 6 | public final class ConcurrentIntIntMapBuilder { 7 | 8 | private MapMode mapMode = MapMode.BLOCKING; 9 | private int buckets = 8; 10 | private int defaultValue = 0; 11 | private int initialCapacity = 100_000; 12 | private float loadFactor = 0.8f; 13 | 14 | private ConcurrentIntIntMapBuilder() { 15 | } 16 | 17 | public static ConcurrentIntIntMapBuilder newBuilder() { 18 | return new ConcurrentIntIntMapBuilder(); 19 | } 20 | 21 | public ConcurrentIntIntMapBuilder withBuckets(int buckets) { 22 | this.buckets = buckets; 23 | return this; 24 | } 25 | 26 | public ConcurrentIntIntMapBuilder withDefaultValue(int defaultValue) { 27 | this.defaultValue = defaultValue; 28 | return this; 29 | } 30 | 31 | public ConcurrentIntIntMapBuilder withInitialCapacity(int initialCapacity) { 32 | this.initialCapacity = initialCapacity; 33 | return this; 34 | } 35 | 36 | public ConcurrentIntIntMapBuilder withLoadFactor(float loadFactor) { 37 | this.loadFactor = loadFactor; 38 | return this; 39 | } 40 | 41 | public ConcurrentIntIntMapBuilder withMode(MapMode mapMode) { 42 | this.mapMode = mapMode; 43 | return this; 44 | } 45 | 46 | public IntIntMap build() { 47 | return mapMode.createMap(this); 48 | } 49 | 50 | public enum MapMode { 51 | BLOCKING { 52 | @Override 53 | IntIntMap createMap(ConcurrentIntIntMapBuilder builder) { 54 | return new ConcurrentIntIntMap( 55 | builder.buckets, 56 | builder.initialCapacity, 57 | builder.loadFactor, 58 | builder.defaultValue); 59 | } 60 | }, 61 | BUSY_WAITING { 62 | @Override 63 | IntIntMap createMap(ConcurrentIntIntMapBuilder builder) { 64 | return new ConcurrentBusyWaitingIntIntMap( 65 | builder.buckets, 66 | builder.initialCapacity, 67 | builder.loadFactor, 68 | builder.defaultValue); 69 | } 70 | }; 71 | 72 | abstract IntIntMap createMap(ConcurrentIntIntMapBuilder builder); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilder.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongFloatMap; 5 | 6 | public final class ConcurrentLongFloatMapBuilder { 7 | 8 | private MapMode mapMode = MapMode.BLOCKING; 9 | private int buckets = 8; 10 | private int initialCapacity = 100_000; 11 | private float loadFactor = 0.8f; 12 | private float defaultValue = LongFloatMap.DEFAULT_VALUE; 13 | 14 | private ConcurrentLongFloatMapBuilder() { 15 | } 16 | 17 | public static ConcurrentLongFloatMapBuilder newBuilder() { 18 | return new ConcurrentLongFloatMapBuilder(); 19 | } 20 | 21 | public ConcurrentLongFloatMapBuilder withBuckets(int buckets) { 22 | this.buckets = buckets; 23 | return this; 24 | } 25 | 26 | public ConcurrentLongFloatMapBuilder withInitialCapacity(int initialCapacity) { 27 | this.initialCapacity = initialCapacity; 28 | return this; 29 | } 30 | 31 | public ConcurrentLongFloatMapBuilder withLoadFactor(float loadFactor) { 32 | this.loadFactor = loadFactor; 33 | return this; 34 | } 35 | 36 | public ConcurrentLongFloatMapBuilder withDefaultValue(float defaultValue) { 37 | this.defaultValue = defaultValue; 38 | return this; 39 | } 40 | 41 | public ConcurrentLongFloatMapBuilder withMode(MapMode mapMode) { 42 | this.mapMode = mapMode; 43 | return this; 44 | } 45 | 46 | public LongFloatMap build() { 47 | return mapMode.createMap(this); 48 | } 49 | 50 | public enum MapMode { 51 | BLOCKING { 52 | @Override 53 | LongFloatMap createMap(ConcurrentLongFloatMapBuilder builder) { 54 | return new ConcurrentLongFloatMap( 55 | builder.buckets, 56 | builder.initialCapacity, 57 | builder.loadFactor, 58 | builder.defaultValue); 59 | } 60 | }, 61 | BUSY_WAITING { 62 | @Override 63 | LongFloatMap createMap(ConcurrentLongFloatMapBuilder builder) { 64 | return new ConcurrentBusyWaitingLongFloatMap( 65 | builder.buckets, 66 | builder.initialCapacity, 67 | builder.loadFactor, 68 | builder.defaultValue); 69 | } 70 | }; 71 | 72 | abstract LongFloatMap createMap(ConcurrentLongFloatMapBuilder builder); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilder.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; 5 | 6 | public final class ConcurrentLongIntMapBuilder { 7 | private MapMode mapMode = MapMode.BLOCKING; 8 | private int buckets = 8; 9 | private int initialCapacity = 100_000; 10 | private float loadFactor = 0.8f; 11 | private int defaultValue = LongIntMap.DEFAULT_VALUE; 12 | 13 | private ConcurrentLongIntMapBuilder() { 14 | 15 | } 16 | 17 | public static ConcurrentLongIntMapBuilder newBuilder() { 18 | return new ConcurrentLongIntMapBuilder(); 19 | } 20 | 21 | public ConcurrentLongIntMapBuilder withBuckets(int buckets) { 22 | this.buckets = buckets; 23 | return this; 24 | } 25 | 26 | public ConcurrentLongIntMapBuilder withInitialCapacity(int initialCapacity) { 27 | this.initialCapacity = initialCapacity; 28 | return this; 29 | } 30 | 31 | public ConcurrentLongIntMapBuilder withLoadFactor(float loadFactor) { 32 | this.loadFactor = loadFactor; 33 | return this; 34 | } 35 | 36 | public ConcurrentLongIntMapBuilder withMode(MapMode mapMode) { 37 | this.mapMode = mapMode; 38 | return this; 39 | } 40 | 41 | public ConcurrentLongIntMapBuilder withDefaultValue(int defaultValue) { 42 | this.defaultValue = defaultValue; 43 | return this; 44 | } 45 | 46 | public LongIntMap build() { 47 | return mapMode.createMap(this); 48 | } 49 | 50 | public enum MapMode { 51 | BUSY_WAITING { 52 | @Override 53 | LongIntMap createMap(ConcurrentLongIntMapBuilder builder) { 54 | return new ConcurrentBusyWaitingLongIntMap( 55 | builder.buckets, 56 | builder.initialCapacity, 57 | builder.loadFactor, 58 | builder.defaultValue); 59 | } 60 | }, 61 | BLOCKING { 62 | @Override 63 | LongIntMap createMap(ConcurrentLongIntMapBuilder builder) { 64 | return new ConcurrentLongIntMap( 65 | builder.buckets, 66 | builder.initialCapacity, 67 | builder.loadFactor, 68 | builder.defaultValue); 69 | } 70 | }; 71 | 72 | abstract LongIntMap createMap(ConcurrentLongIntMapBuilder builder); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilder.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; 5 | 6 | public final class ConcurrentLongLongMapBuilder { 7 | private MapMode mapMode = MapMode.BLOCKING; 8 | private int buckets = 8; 9 | private int initialCapacity = 100_000; 10 | private float loadFactor = 0.8f; 11 | private long defaultValue = LongLongMap.DEFAULT_VALUE; 12 | 13 | private ConcurrentLongLongMapBuilder() { 14 | 15 | } 16 | 17 | public static ConcurrentLongLongMapBuilder newBuilder() { 18 | return new ConcurrentLongLongMapBuilder(); 19 | } 20 | 21 | public ConcurrentLongLongMapBuilder withBuckets(int buckets) { 22 | this.buckets = buckets; 23 | return this; 24 | } 25 | 26 | public ConcurrentLongLongMapBuilder withInitialCapacity(int initialCapacity) { 27 | this.initialCapacity = initialCapacity; 28 | return this; 29 | } 30 | 31 | public ConcurrentLongLongMapBuilder withLoadFactor(float loadFactor) { 32 | this.loadFactor = loadFactor; 33 | return this; 34 | } 35 | 36 | public ConcurrentLongLongMapBuilder withMode(MapMode mapMode) { 37 | this.mapMode = mapMode; 38 | return this; 39 | } 40 | 41 | public ConcurrentLongLongMapBuilder withDefaultValue(long defaultValue) { 42 | this.defaultValue = defaultValue; 43 | return this; 44 | } 45 | 46 | public LongLongMap build() { 47 | return mapMode.createMap(this); 48 | } 49 | 50 | public enum MapMode { 51 | BUSY_WAITING { 52 | @Override 53 | LongLongMap createMap(ConcurrentLongLongMapBuilder builder) { 54 | return new ConcurrentBusyWaitingLongLongMap( 55 | builder.buckets, 56 | builder.initialCapacity, 57 | builder.loadFactor, 58 | builder.defaultValue); 59 | } 60 | }, 61 | BLOCKING { 62 | @Override 63 | LongLongMap createMap(ConcurrentLongLongMapBuilder builder) { 64 | return new ConcurrentLongLongMap( 65 | builder.buckets, 66 | builder.initialCapacity, 67 | builder.loadFactor, 68 | builder.defaultValue); 69 | } 70 | }; 71 | 72 | abstract LongLongMap createMap(ConcurrentLongLongMapBuilder builder); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/IntFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import it.unimi.dsi.fastutil.ints.Int2FloatFunction; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public interface IntFloatMap extends PrimitiveIntKeyMap { 8 | 9 | float DEFAULT_VALUE = 0.0f; 10 | 11 | float get(int key); 12 | 13 | float put(int key, float value); 14 | 15 | float getDefaultValue(); 16 | 17 | float remove(int key); 18 | 19 | boolean remove(int key, float value); 20 | 21 | float computeIfAbsent(int key, Int2FloatFunction mappingFunction); 22 | 23 | float computeIfPresent(int key, BiFunction mappingFunction); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/IntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import it.unimi.dsi.fastutil.ints.Int2IntFunction; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public interface IntIntMap extends PrimitiveIntKeyMap { 8 | 9 | int DEFAULT_VALUE = 0; 10 | 11 | /** 12 | * @param key 13 | * @return 0 if the key is not present 14 | */ 15 | int get(int key); 16 | 17 | int put(int key, int value); 18 | 19 | int getDefaultValue(); 20 | 21 | int remove(int key); 22 | 23 | boolean remove(int key, int value); 24 | 25 | int computeIfAbsent(int key, Int2IntFunction mappingFunction); 26 | 27 | int computeIfPresent(int key, BiFunction mappingFunction); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/LongFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import it.unimi.dsi.fastutil.longs.Long2FloatFunction; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public interface LongFloatMap extends PrimitiveLongKeyMap { 8 | 9 | float DEFAULT_VALUE = 0.0f; 10 | 11 | /** 12 | * @param key key 13 | * @return 0.0 if the key is not present 14 | */ 15 | float get(long key); 16 | 17 | float put(long key, float value); 18 | 19 | float getDefaultValue(); 20 | 21 | float remove(long key); 22 | 23 | /** 24 | * Remove this key only if it has the given value. 25 | * 26 | * @param key 27 | * @param value 28 | * @return 29 | */ 30 | boolean remove(long key, float value); 31 | 32 | float computeIfAbsent(long key, Long2FloatFunction mappingFunction); 33 | 34 | float computeIfPresent(int key, BiFunction mappingFunction); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/LongIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import it.unimi.dsi.fastutil.longs.Long2IntFunction; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public interface LongIntMap extends PrimitiveLongKeyMap { 8 | 9 | int DEFAULT_VALUE = 0; 10 | 11 | /** 12 | * @param key key to get 13 | * @return configured LongIntMap.getDefaultValue(), if the key is not present 14 | */ 15 | int get(long key); 16 | 17 | int put(long key, int value); 18 | 19 | int getDefaultValue(); 20 | 21 | int remove(long key); 22 | 23 | boolean remove(long key, int value); 24 | 25 | int computeIfAbsent(long key, Long2IntFunction mappingFunction); 26 | 27 | int computeIfPresent(long key, BiFunction mappingFunction); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/LongLongMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public interface LongLongMap extends PrimitiveLongKeyMap { 8 | 9 | long DEFAULT_VALUE = 0; 10 | 11 | /** 12 | * @param key 13 | * @return 0 if the key is not present 14 | */ 15 | long get(long key); 16 | 17 | long put(long key, long value); 18 | 19 | long getDefaultValue(); 20 | 21 | long remove(long key); 22 | 23 | boolean remove(long key, long value); 24 | 25 | long computeIfAbsent(long key, Long2LongFunction mappingFunction); 26 | 27 | long computeIfPresent(long key, BiFunction mappingFunction); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveIntKeyMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | public interface PrimitiveIntKeyMap extends PrimitiveKeyMap { 4 | boolean containsKey(int key); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveKeyMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | public interface PrimitiveKeyMap { 4 | int size(); 5 | 6 | boolean isEmpty(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveLongKeyMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | public interface PrimitiveLongKeyMap extends PrimitiveKeyMap { 4 | boolean containsKey(long key); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntFloatWrapper; 5 | import it.unimi.dsi.fastutil.ints.Int2FloatFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentBusyWaitingIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap { 11 | 12 | private final IntFloatMap[] maps; 13 | private final float defaultValue; 14 | 15 | public ConcurrentBusyWaitingIntFloatMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | float defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new IntFloatMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public int size() { 31 | return super.size(maps); 32 | } 33 | 34 | @Override 35 | public boolean isEmpty() { 36 | return super.isEmpty(maps); 37 | } 38 | 39 | @Override 40 | public boolean containsKey(int key) { 41 | int bucket = getBucket(key); 42 | 43 | Lock readLock = locks[bucket].readLock(); 44 | 45 | while (true) { 46 | if (readLock.tryLock()) { 47 | try { 48 | return maps[bucket].containsKey(key); 49 | } finally { 50 | readLock.unlock(); 51 | } 52 | } 53 | } 54 | } 55 | 56 | @Override 57 | public float get(int key) { 58 | int bucket = getBucket(key); 59 | 60 | Lock readLock = locks[bucket].readLock(); 61 | 62 | while (true) { 63 | if (readLock.tryLock()) { 64 | try { 65 | return maps[bucket].get(key); 66 | } finally { 67 | readLock.unlock(); 68 | } 69 | } 70 | } 71 | } 72 | 73 | @Override 74 | public float put(int key, float value) { 75 | int bucket = getBucket(key); 76 | 77 | Lock writeLock = locks[bucket].writeLock(); 78 | 79 | while (true) { 80 | if (writeLock.tryLock()) { 81 | try { 82 | return maps[bucket].put(key, value); 83 | } finally { 84 | writeLock.unlock(); 85 | } 86 | } 87 | } 88 | } 89 | 90 | @Override 91 | public float getDefaultValue() { 92 | return defaultValue; 93 | } 94 | 95 | @Override 96 | public float remove(int key) { 97 | int bucket = getBucket(key); 98 | 99 | Lock writeLock = locks[bucket].writeLock(); 100 | 101 | while (true) { 102 | if (writeLock.tryLock()) { 103 | try { 104 | return maps[bucket].remove(key); 105 | } finally { 106 | writeLock.unlock(); 107 | } 108 | } 109 | } 110 | } 111 | 112 | @Override 113 | public boolean remove(int key, float value) { 114 | int bucket = getBucket(key); 115 | 116 | Lock writeLock = locks[bucket].writeLock(); 117 | 118 | while (true) { 119 | if (writeLock.tryLock()) { 120 | try { 121 | return maps[bucket].remove(key, value); 122 | } finally { 123 | writeLock.unlock(); 124 | } 125 | } 126 | } 127 | } 128 | 129 | @Override 130 | public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { 131 | int bucket = getBucket(key); 132 | 133 | Lock writeLock = locks[bucket].writeLock(); 134 | 135 | while (true) { 136 | if (writeLock.tryLock()) { 137 | try { 138 | return maps[bucket].computeIfAbsent(key, mappingFunction); 139 | } finally { 140 | writeLock.unlock(); 141 | } 142 | } 143 | } 144 | } 145 | 146 | @Override 147 | public float computeIfPresent(int key, BiFunction mappingFunction) { 148 | int bucket = getBucket(key); 149 | 150 | Lock writeLock = locks[bucket].writeLock(); 151 | 152 | while (true) { 153 | if (writeLock.tryLock()) { 154 | try { 155 | return maps[bucket].computeIfPresent(key, mappingFunction); 156 | } finally { 157 | writeLock.unlock(); 158 | } 159 | } 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper; 5 | import it.unimi.dsi.fastutil.ints.Int2IntFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentBusyWaitingIntIntMap extends PrimitiveConcurrentMap implements IntIntMap { 11 | 12 | private final IntIntMap[] maps; 13 | private final int defaultValue; 14 | 15 | public ConcurrentBusyWaitingIntIntMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | int defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new IntIntMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilIntIntWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public int size() { 31 | return super.size(maps); 32 | } 33 | 34 | @Override 35 | public boolean isEmpty() { 36 | return super.isEmpty(maps); 37 | } 38 | 39 | @Override 40 | public boolean containsKey(int key) { 41 | int bucket = getBucket(key); 42 | 43 | Lock readLock = locks[bucket].readLock(); 44 | 45 | while (true) { 46 | if (readLock.tryLock()) { 47 | try { 48 | return maps[bucket].containsKey(key); 49 | } finally { 50 | readLock.unlock(); 51 | } 52 | } 53 | } 54 | } 55 | 56 | @Override 57 | public int get(int key) { 58 | int bucket = getBucket(key); 59 | 60 | Lock readLock = locks[bucket].readLock(); 61 | 62 | while (true) { 63 | if (readLock.tryLock()) { 64 | try { 65 | return maps[bucket].get(key); 66 | } finally { 67 | readLock.unlock(); 68 | } 69 | } 70 | } 71 | } 72 | 73 | @Override 74 | public int put(int key, int value) { 75 | int bucket = getBucket(key); 76 | 77 | Lock writeLock = locks[bucket].writeLock(); 78 | 79 | while (true) { 80 | if (writeLock.tryLock()) { 81 | try { 82 | return maps[bucket].put(key, value); 83 | } finally { 84 | writeLock.unlock(); 85 | } 86 | } 87 | } 88 | } 89 | 90 | @Override 91 | public int getDefaultValue() { 92 | return defaultValue; 93 | } 94 | 95 | @Override 96 | public int remove(int key) { 97 | int bucket = getBucket(key); 98 | 99 | Lock writeLock = locks[bucket].writeLock(); 100 | 101 | while (true) { 102 | if (writeLock.tryLock()) { 103 | try { 104 | return maps[bucket].remove(key); 105 | } finally { 106 | writeLock.unlock(); 107 | } 108 | } 109 | } 110 | } 111 | 112 | @Override 113 | public boolean remove(int key, int value) { 114 | int bucket = getBucket(key); 115 | 116 | Lock writeLock = locks[bucket].writeLock(); 117 | 118 | while (true) { 119 | if (writeLock.tryLock()) { 120 | try { 121 | return maps[bucket].remove(key, value); 122 | } finally { 123 | writeLock.unlock(); 124 | } 125 | } 126 | } 127 | } 128 | 129 | @Override 130 | public int computeIfAbsent(int key, Int2IntFunction mappingFunction) { 131 | int bucket = getBucket(key); 132 | 133 | Lock writeLock = locks[bucket].writeLock(); 134 | 135 | while (true) { 136 | if (writeLock.tryLock()) { 137 | try { 138 | return maps[bucket].computeIfAbsent(key, mappingFunction); 139 | } finally { 140 | writeLock.unlock(); 141 | } 142 | } 143 | } 144 | } 145 | 146 | @Override 147 | public int computeIfPresent(int key, BiFunction mappingFunction) { 148 | int bucket = getBucket(key); 149 | 150 | Lock writeLock = locks[bucket].writeLock(); 151 | 152 | while (true) { 153 | if (writeLock.tryLock()) { 154 | try { 155 | return maps[bucket].computeIfPresent(key, mappingFunction); 156 | } finally { 157 | writeLock.unlock(); 158 | } 159 | } 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongFloatWrapper; 5 | import it.unimi.dsi.fastutil.longs.Long2FloatFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentBusyWaitingLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap { 11 | 12 | private final LongFloatMap[] maps; 13 | private final float defaultValue; 14 | 15 | public ConcurrentBusyWaitingLongFloatMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | float defaultValue) { 19 | super(numBuckets); 20 | this.defaultValue = defaultValue; 21 | this.maps = new LongFloatMap[numBuckets]; 22 | for (int i = 0; i < numBuckets; i++) { 23 | maps[i] = new PrimitiveFastutilLongFloatWrapper(initialCapacity, loadFactor, defaultValue); 24 | } 25 | } 26 | 27 | @Override 28 | public int size() { 29 | return super.size(maps); 30 | } 31 | 32 | @Override 33 | public boolean isEmpty() { 34 | return super.isEmpty(maps); 35 | } 36 | 37 | @Override 38 | public boolean containsKey(long key) { 39 | int bucket = getBucket(key); 40 | 41 | Lock readLock = locks[bucket].readLock(); 42 | 43 | while (true) { 44 | if (readLock.tryLock()) { 45 | try { 46 | return maps[bucket].containsKey(key); 47 | } finally { 48 | readLock.unlock(); 49 | } 50 | } 51 | } 52 | } 53 | 54 | @Override 55 | public float get(long key) { 56 | int bucket = getBucket(key); 57 | 58 | Lock readLock = locks[bucket].readLock(); 59 | 60 | while (true) { 61 | if (readLock.tryLock()) { 62 | try { 63 | return maps[bucket].get(key); 64 | } finally { 65 | readLock.unlock(); 66 | } 67 | } 68 | } 69 | } 70 | 71 | @Override 72 | public float put(long key, float value) { 73 | int bucket = getBucket(key); 74 | 75 | Lock writeLock = locks[bucket].writeLock(); 76 | 77 | while (true) { 78 | if (writeLock.tryLock()) { 79 | try { 80 | return maps[bucket].put(key, value); 81 | } finally { 82 | writeLock.unlock(); 83 | } 84 | } 85 | } 86 | } 87 | 88 | @Override 89 | public float getDefaultValue() { 90 | return defaultValue; 91 | } 92 | 93 | @Override 94 | public float remove(long key) { 95 | int bucket = getBucket(key); 96 | 97 | Lock writeLock = locks[bucket].writeLock(); 98 | 99 | while (true) { 100 | if (writeLock.tryLock()) { 101 | try { 102 | return maps[bucket].remove(key); 103 | } finally { 104 | writeLock.unlock(); 105 | } 106 | } 107 | } 108 | } 109 | 110 | @Override 111 | public boolean remove(long key, float value) { 112 | int bucket = getBucket(key); 113 | 114 | Lock writeLock = locks[bucket].writeLock(); 115 | 116 | while (true) { 117 | if (writeLock.tryLock()) { 118 | try { 119 | return maps[bucket].remove(key, value); 120 | } finally { 121 | writeLock.unlock(); 122 | } 123 | } 124 | } 125 | } 126 | 127 | @Override 128 | public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) { 129 | int bucket = getBucket(key); 130 | 131 | Lock writeLock = locks[bucket].writeLock(); 132 | 133 | while (true) { 134 | if (writeLock.tryLock()) { 135 | try { 136 | return maps[bucket].computeIfAbsent(key, mappingFunction); 137 | } finally { 138 | writeLock.unlock(); 139 | } 140 | } 141 | } 142 | } 143 | 144 | @Override 145 | public float computeIfPresent(int key, BiFunction mappingFunction) { 146 | int bucket = getBucket(key); 147 | 148 | Lock writeLock = locks[bucket].writeLock(); 149 | 150 | while (true) { 151 | if (writeLock.tryLock()) { 152 | try { 153 | return maps[bucket].computeIfPresent(key, mappingFunction); 154 | } finally { 155 | writeLock.unlock(); 156 | } 157 | } 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 5 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongIntWrapper; 6 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongLongWrapper; 7 | import it.unimi.dsi.fastutil.longs.Long2IntFunction; 8 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 9 | 10 | import java.util.concurrent.locks.Lock; 11 | import java.util.function.BiFunction; 12 | 13 | public class ConcurrentBusyWaitingLongIntMap extends PrimitiveConcurrentMap implements LongIntMap { 14 | 15 | private final LongIntMap[] maps; 16 | private final int defaultValue; 17 | 18 | public ConcurrentBusyWaitingLongIntMap(int numBuckets, 19 | int initialCapacity, 20 | float loadFactor, 21 | int defaultValue) { 22 | super(numBuckets); 23 | 24 | this.maps = new LongIntMap[numBuckets]; 25 | this.defaultValue = defaultValue; 26 | 27 | for (int i = 0; i < numBuckets; i++) { 28 | maps[i] = new PrimitiveFastutilLongIntWrapper(initialCapacity, loadFactor, defaultValue); 29 | } 30 | } 31 | 32 | @Override 33 | public int size() { 34 | return super.size(maps); 35 | } 36 | 37 | @Override 38 | public boolean isEmpty() { 39 | return super.isEmpty(maps); 40 | } 41 | 42 | @Override 43 | public boolean containsKey(long key) { 44 | int bucket = getBucket(key); 45 | 46 | Lock readLock = locks[bucket].readLock(); 47 | 48 | while (true) { 49 | if (readLock.tryLock()) { 50 | try { 51 | return maps[bucket].containsKey(key); 52 | } finally { 53 | readLock.unlock(); 54 | } 55 | } 56 | } 57 | } 58 | 59 | @Override 60 | public int get(long key) { 61 | int bucket = getBucket(key); 62 | 63 | Lock readLock = locks[bucket].readLock(); 64 | 65 | while (true) { 66 | if (readLock.tryLock()) { 67 | try { 68 | return maps[bucket].get(key); 69 | } finally { 70 | readLock.unlock(); 71 | } 72 | } 73 | } 74 | } 75 | 76 | @Override 77 | public int put(long key, int value) { 78 | int bucket = getBucket(key); 79 | 80 | Lock writeLock = locks[bucket].writeLock(); 81 | 82 | while (true) { 83 | if (writeLock.tryLock()) { 84 | try { 85 | return maps[bucket].put(key, value); 86 | } finally { 87 | writeLock.unlock(); 88 | } 89 | } 90 | } 91 | } 92 | 93 | @Override 94 | public int getDefaultValue() { 95 | return defaultValue; 96 | } 97 | 98 | @Override 99 | public int remove(long key) { 100 | int bucket = getBucket(key); 101 | 102 | Lock writeLock = locks[bucket].writeLock(); 103 | 104 | while (true) { 105 | if (writeLock.tryLock()) { 106 | try { 107 | return maps[bucket].remove(key); 108 | } finally { 109 | writeLock.unlock(); 110 | } 111 | } 112 | } 113 | } 114 | 115 | @Override 116 | public boolean remove(long key, int value) { 117 | int bucket = getBucket(key); 118 | 119 | Lock writeLock = locks[bucket].writeLock(); 120 | 121 | while (true) { 122 | if (writeLock.tryLock()) { 123 | try { 124 | return maps[bucket].remove(key, value); 125 | } finally { 126 | writeLock.unlock(); 127 | } 128 | } 129 | } 130 | } 131 | 132 | @Override 133 | public int computeIfAbsent(long key, Long2IntFunction mappingFunction) { 134 | int bucket = getBucket(key); 135 | 136 | Lock writeLock = locks[bucket].writeLock(); 137 | 138 | while (true) { 139 | if (writeLock.tryLock()) { 140 | try { 141 | return maps[bucket].computeIfAbsent(key, mappingFunction); 142 | } finally { 143 | writeLock.unlock(); 144 | } 145 | } 146 | } 147 | } 148 | 149 | @Override 150 | public int computeIfPresent(long key, BiFunction mappingFunction) { 151 | int bucket = getBucket(key); 152 | 153 | Lock writeLock = locks[bucket].writeLock(); 154 | 155 | while (true) { 156 | if (writeLock.tryLock()) { 157 | try { 158 | return maps[bucket].computeIfPresent(key, mappingFunction); 159 | } finally { 160 | writeLock.unlock(); 161 | } 162 | } 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongLongMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongLongWrapper; 5 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentBusyWaitingLongLongMap extends PrimitiveConcurrentMap implements LongLongMap { 11 | 12 | private final LongLongMap[] maps; 13 | private final long defaultValue; 14 | 15 | public ConcurrentBusyWaitingLongLongMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | long defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new LongLongMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilLongLongWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public int size() { 31 | return super.size(maps); 32 | } 33 | 34 | @Override 35 | public boolean isEmpty() { 36 | return super.isEmpty(maps); 37 | } 38 | 39 | @Override 40 | public boolean containsKey(long key) { 41 | int bucket = getBucket(key); 42 | 43 | Lock readLock = locks[bucket].readLock(); 44 | 45 | while (true) { 46 | if (readLock.tryLock()) { 47 | try { 48 | return maps[bucket].containsKey(key); 49 | } finally { 50 | readLock.unlock(); 51 | } 52 | } 53 | } 54 | } 55 | 56 | @Override 57 | public long get(long key) { 58 | int bucket = getBucket(key); 59 | 60 | Lock readLock = locks[bucket].readLock(); 61 | 62 | while (true) { 63 | if (readLock.tryLock()) { 64 | try { 65 | return maps[bucket].get(key); 66 | } finally { 67 | readLock.unlock(); 68 | } 69 | } 70 | } 71 | } 72 | 73 | @Override 74 | public long put(long key, long value) { 75 | int bucket = getBucket(key); 76 | 77 | Lock writeLock = locks[bucket].writeLock(); 78 | 79 | while (true) { 80 | if (writeLock.tryLock()) { 81 | try { 82 | return maps[bucket].put(key, value); 83 | } finally { 84 | writeLock.unlock(); 85 | } 86 | } 87 | } 88 | } 89 | 90 | @Override 91 | public long getDefaultValue() { 92 | return defaultValue; 93 | } 94 | 95 | @Override 96 | public long remove(long key) { 97 | int bucket = getBucket(key); 98 | 99 | Lock writeLock = locks[bucket].writeLock(); 100 | 101 | while (true) { 102 | if (writeLock.tryLock()) { 103 | try { 104 | return maps[bucket].remove(key); 105 | } finally { 106 | writeLock.unlock(); 107 | } 108 | } 109 | } 110 | } 111 | 112 | @Override 113 | public boolean remove(long key, long value) { 114 | int bucket = getBucket(key); 115 | 116 | Lock writeLock = locks[bucket].writeLock(); 117 | 118 | while (true) { 119 | if (writeLock.tryLock()) { 120 | try { 121 | return maps[bucket].remove(key, value); 122 | } finally { 123 | writeLock.unlock(); 124 | } 125 | } 126 | } 127 | } 128 | 129 | @Override 130 | public long computeIfAbsent(long key, Long2LongFunction mappingFunction) { 131 | int bucket = getBucket(key); 132 | 133 | Lock writeLock = locks[bucket].writeLock(); 134 | 135 | while (true) { 136 | if (writeLock.tryLock()) { 137 | try { 138 | return maps[bucket].computeIfAbsent(key, mappingFunction); 139 | } finally { 140 | writeLock.unlock(); 141 | } 142 | } 143 | } 144 | } 145 | 146 | @Override 147 | public long computeIfPresent(long key, BiFunction mappingFunction) { 148 | int bucket = getBucket(key); 149 | 150 | Lock writeLock = locks[bucket].writeLock(); 151 | 152 | while (true) { 153 | if (writeLock.tryLock()) { 154 | try { 155 | return maps[bucket].computeIfPresent(key, mappingFunction); 156 | } finally { 157 | writeLock.unlock(); 158 | } 159 | } 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntFloatWrapper; 5 | import it.unimi.dsi.fastutil.ints.Int2FloatFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap { 11 | 12 | private final IntFloatMap[] maps; 13 | private final float defaultValue; 14 | 15 | public ConcurrentIntFloatMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | float defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new IntFloatMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public int size() { 31 | return super.size(maps); 32 | } 33 | 34 | @Override 35 | public boolean isEmpty() { 36 | return super.isEmpty(maps); 37 | } 38 | 39 | @Override 40 | public boolean containsKey(int key) { 41 | int bucket = getBucket(key); 42 | 43 | Lock readLock = locks[bucket].readLock(); 44 | readLock.lock(); 45 | try { 46 | return maps[bucket].containsKey(key); 47 | } finally { 48 | readLock.unlock(); 49 | } 50 | } 51 | 52 | @Override 53 | public float get(int l) { 54 | int bucket = getBucket(l); 55 | 56 | float result; 57 | 58 | Lock readLock = locks[bucket].readLock(); 59 | readLock.lock(); 60 | try { 61 | result = maps[bucket].get(l); 62 | } finally { 63 | readLock.unlock(); 64 | } 65 | 66 | return result; 67 | } 68 | 69 | @Override 70 | public float put(int key, float value) { 71 | int bucket = getBucket(key); 72 | 73 | float result; 74 | 75 | Lock writeLock = locks[bucket].writeLock(); 76 | writeLock.lock(); 77 | try { 78 | result = maps[bucket].put(key, value); 79 | } finally { 80 | writeLock.unlock(); 81 | } 82 | 83 | return result; 84 | } 85 | 86 | @Override 87 | public float getDefaultValue() { 88 | return defaultValue; 89 | } 90 | 91 | @Override 92 | public float remove(int key) { 93 | int bucket = getBucket(key); 94 | 95 | Lock writeLock = locks[bucket].writeLock(); 96 | writeLock.lock(); 97 | try { 98 | return maps[bucket].remove(key); 99 | } finally { 100 | writeLock.unlock(); 101 | } 102 | } 103 | 104 | @Override 105 | public boolean remove(int key, float value) { 106 | int bucket = getBucket(key); 107 | 108 | Lock writeLock = locks[bucket].writeLock(); 109 | writeLock.lock(); 110 | try { 111 | return maps[bucket].remove(key, value); 112 | } finally { 113 | writeLock.unlock(); 114 | } 115 | } 116 | 117 | @Override 118 | public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { 119 | int bucket = getBucket(key); 120 | 121 | Lock writeLock = locks[bucket].writeLock(); 122 | writeLock.lock(); 123 | try { 124 | return maps[bucket].computeIfAbsent(key, mappingFunction); 125 | } finally { 126 | writeLock.unlock(); 127 | } 128 | } 129 | 130 | @Override 131 | public float computeIfPresent(int key, BiFunction mappingFunction) { 132 | int bucket = getBucket(key); 133 | 134 | Lock writeLock = locks[bucket].writeLock(); 135 | writeLock.lock(); 136 | try { 137 | return maps[bucket].computeIfPresent(key, mappingFunction); 138 | } finally { 139 | writeLock.unlock(); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper; 5 | import it.unimi.dsi.fastutil.ints.Int2IntFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentIntIntMap extends PrimitiveConcurrentMap implements IntIntMap { 11 | 12 | private final IntIntMap[] maps; 13 | private final int defaultValue; 14 | 15 | public ConcurrentIntIntMap( 16 | int numBuckets, 17 | int initialCapacity, 18 | float loadFactor, 19 | int defaultValue) { 20 | 21 | super(numBuckets); 22 | 23 | this.maps = new IntIntMap[numBuckets]; 24 | this.defaultValue = defaultValue; 25 | 26 | for (int i = 0; i < numBuckets; i++) { 27 | maps[i] = new PrimitiveFastutilIntIntWrapper(initialCapacity, loadFactor, defaultValue); 28 | } 29 | } 30 | 31 | @Override 32 | public int size() { 33 | return super.size(maps); 34 | } 35 | 36 | @Override 37 | public boolean isEmpty() { 38 | return super.isEmpty(maps); 39 | } 40 | 41 | @Override 42 | public boolean containsKey(int key) { 43 | int bucket = getBucket(key); 44 | 45 | Lock readLock = locks[bucket].readLock(); 46 | readLock.lock(); 47 | try { 48 | return maps[bucket].containsKey(key); 49 | } finally { 50 | readLock.unlock(); 51 | } 52 | } 53 | 54 | @Override 55 | public int get(int l) { 56 | int bucket = getBucket(l); 57 | 58 | int result; 59 | 60 | Lock readLock = locks[bucket].readLock(); 61 | readLock.lock(); 62 | try { 63 | result = maps[bucket].get(l); 64 | } finally { 65 | readLock.unlock(); 66 | } 67 | 68 | return result; 69 | } 70 | 71 | @Override 72 | public int put(int key, int value) { 73 | int bucket = getBucket(key); 74 | 75 | int result; 76 | 77 | Lock writeLock = locks[bucket].writeLock(); 78 | writeLock.lock(); 79 | try { 80 | result = maps[bucket].put(key, value); 81 | } finally { 82 | writeLock.unlock(); 83 | } 84 | 85 | return result; 86 | } 87 | 88 | @Override 89 | public int getDefaultValue() { 90 | return defaultValue; 91 | } 92 | 93 | @Override 94 | public int remove(int key) { 95 | int bucket = getBucket(key); 96 | 97 | Lock writeLock = locks[bucket].writeLock(); 98 | writeLock.lock(); 99 | try { 100 | return maps[bucket].remove(key); 101 | } finally { 102 | writeLock.unlock(); 103 | } 104 | } 105 | 106 | @Override 107 | public boolean remove(int key, int value) { 108 | int bucket = getBucket(key); 109 | 110 | Lock writeLock = locks[bucket].writeLock(); 111 | writeLock.lock(); 112 | try { 113 | return maps[bucket].remove(key, value); 114 | } finally { 115 | writeLock.unlock(); 116 | } 117 | } 118 | 119 | @Override 120 | public int computeIfAbsent(int key, Int2IntFunction mappingFunction) { 121 | int bucket = getBucket(key); 122 | 123 | Lock writeLock = locks[bucket].writeLock(); 124 | writeLock.lock(); 125 | try { 126 | return maps[bucket].computeIfAbsent(key, mappingFunction); 127 | } finally { 128 | writeLock.unlock(); 129 | } 130 | } 131 | 132 | @Override 133 | public int computeIfPresent(int key, BiFunction mappingFunction) { 134 | int bucket = getBucket(key); 135 | 136 | Lock writeLock = locks[bucket].writeLock(); 137 | writeLock.lock(); 138 | try { 139 | return maps[bucket].computeIfPresent(key, mappingFunction); 140 | } finally { 141 | writeLock.unlock(); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongFloatMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongFloatWrapper; 5 | import it.unimi.dsi.fastutil.longs.Long2FloatFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap { 11 | 12 | private final LongFloatMap[] maps; 13 | private final float defaultValue; 14 | 15 | public ConcurrentLongFloatMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | float defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new LongFloatMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilLongFloatWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public float getDefaultValue() { 31 | return defaultValue; 32 | } 33 | 34 | @Override 35 | public int size() { 36 | return super.size(maps); 37 | } 38 | 39 | @Override 40 | public boolean isEmpty() { 41 | return super.isEmpty(maps); 42 | } 43 | 44 | @Override 45 | public boolean containsKey(long key) { 46 | int bucket = getBucket(key); 47 | 48 | Lock readLock = locks[bucket].readLock(); 49 | readLock.lock(); 50 | try { 51 | return maps[bucket].containsKey(key); 52 | } finally { 53 | readLock.unlock(); 54 | } 55 | } 56 | 57 | @Override 58 | public float get(long l) { 59 | int bucket = getBucket(l); 60 | 61 | float result; 62 | 63 | Lock readLock = locks[bucket].readLock(); 64 | readLock.lock(); 65 | try { 66 | result = maps[bucket].get(l); 67 | } finally { 68 | readLock.unlock(); 69 | } 70 | 71 | return result; 72 | } 73 | 74 | @Override 75 | public float put(long key, float value) { 76 | int bucket = getBucket(key); 77 | 78 | float result; 79 | 80 | Lock writeLock = locks[bucket].writeLock(); 81 | writeLock.lock(); 82 | try { 83 | result = maps[bucket].put(key, value); 84 | } finally { 85 | writeLock.unlock(); 86 | } 87 | 88 | return result; 89 | } 90 | 91 | @Override 92 | public float remove(long key) { 93 | int bucket = getBucket(key); 94 | 95 | Lock writeLock = locks[bucket].writeLock(); 96 | writeLock.lock(); 97 | try { 98 | return maps[bucket].remove(key); 99 | } finally { 100 | writeLock.unlock(); 101 | } 102 | } 103 | 104 | @Override 105 | public boolean remove(long key, float value) { 106 | int bucket = getBucket(key); 107 | 108 | Lock writeLock = locks[bucket].writeLock(); 109 | writeLock.lock(); 110 | try { 111 | return maps[bucket].remove(key, value); 112 | } finally { 113 | writeLock.unlock(); 114 | } 115 | } 116 | 117 | @Override 118 | public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) { 119 | int bucket = getBucket(key); 120 | 121 | Lock writeLock = locks[bucket].writeLock(); 122 | writeLock.lock(); 123 | try { 124 | return maps[bucket].computeIfAbsent(key, mappingFunction); 125 | } finally { 126 | writeLock.unlock(); 127 | } 128 | } 129 | 130 | @Override 131 | public float computeIfPresent(int key, BiFunction mappingFunction) { 132 | int bucket = getBucket(key); 133 | 134 | Lock writeLock = locks[bucket].writeLock(); 135 | writeLock.lock(); 136 | try { 137 | return maps[bucket].computeIfPresent(key, mappingFunction); 138 | } finally { 139 | writeLock.unlock(); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongIntMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 5 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper; 6 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongIntWrapper; 7 | import it.unimi.dsi.fastutil.ints.Int2IntFunction; 8 | import it.unimi.dsi.fastutil.longs.Long2IntFunction; 9 | 10 | import java.util.concurrent.locks.Lock; 11 | import java.util.function.BiFunction; 12 | 13 | public class ConcurrentLongIntMap extends PrimitiveConcurrentMap implements LongIntMap { 14 | 15 | private final LongIntMap[] maps; 16 | private final int defaultValue; 17 | 18 | public ConcurrentLongIntMap( 19 | int numBuckets, 20 | int initialCapacity, 21 | float loadFactor, 22 | int defaultValue) { 23 | 24 | super(numBuckets); 25 | 26 | this.maps = new LongIntMap[numBuckets]; 27 | this.defaultValue = defaultValue; 28 | 29 | for (int i = 0; i < numBuckets; i++) { 30 | maps[i] = new PrimitiveFastutilLongIntWrapper(initialCapacity, loadFactor, defaultValue); 31 | } 32 | } 33 | 34 | @Override 35 | public int size() { 36 | return super.size(maps); 37 | } 38 | 39 | @Override 40 | public boolean isEmpty() { 41 | return super.isEmpty(maps); 42 | } 43 | 44 | @Override 45 | public boolean containsKey(long key) { 46 | int bucket = getBucket(key); 47 | 48 | Lock readLock = locks[bucket].readLock(); 49 | readLock.lock(); 50 | try { 51 | return maps[bucket].containsKey(key); 52 | } finally { 53 | readLock.unlock(); 54 | } 55 | } 56 | 57 | @Override 58 | public int get(long l) { 59 | int bucket = getBucket(l); 60 | 61 | int result; 62 | 63 | Lock readLock = locks[bucket].readLock(); 64 | readLock.lock(); 65 | try { 66 | result = maps[bucket].get(l); 67 | } finally { 68 | readLock.unlock(); 69 | } 70 | 71 | return result; 72 | } 73 | 74 | @Override 75 | public int put(long key, int value) { 76 | int bucket = getBucket(key); 77 | 78 | int result; 79 | 80 | Lock writeLock = locks[bucket].writeLock(); 81 | writeLock.lock(); 82 | try { 83 | result = maps[bucket].put(key, value); 84 | } finally { 85 | writeLock.unlock(); 86 | } 87 | 88 | return result; 89 | } 90 | 91 | @Override 92 | public int getDefaultValue() { 93 | return defaultValue; 94 | } 95 | 96 | @Override 97 | public int remove(long key) { 98 | int bucket = getBucket(key); 99 | 100 | Lock writeLock = locks[bucket].writeLock(); 101 | writeLock.lock(); 102 | try { 103 | return maps[bucket].remove(key); 104 | } finally { 105 | writeLock.unlock(); 106 | } 107 | } 108 | 109 | @Override 110 | public boolean remove(long key, int value) { 111 | int bucket = getBucket(key); 112 | 113 | Lock writeLock = locks[bucket].writeLock(); 114 | writeLock.lock(); 115 | try { 116 | return maps[bucket].remove(key, value); 117 | } finally { 118 | writeLock.unlock(); 119 | } 120 | } 121 | 122 | @Override 123 | public int computeIfAbsent(long key, Long2IntFunction mappingFunction) { 124 | int bucket = getBucket(key); 125 | 126 | Lock writeLock = locks[bucket].writeLock(); 127 | writeLock.lock(); 128 | try { 129 | return maps[bucket].computeIfAbsent(key, mappingFunction); 130 | } finally { 131 | writeLock.unlock(); 132 | } 133 | } 134 | 135 | @Override 136 | public int computeIfPresent(long key, BiFunction mappingFunction) { 137 | int bucket = getBucket(key); 138 | 139 | Lock writeLock = locks[bucket].writeLock(); 140 | writeLock.lock(); 141 | try { 142 | return maps[bucket].computeIfPresent(key, mappingFunction); 143 | } finally { 144 | writeLock.unlock(); 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongLongMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongLongWrapper; 5 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.function.BiFunction; 9 | 10 | public class ConcurrentLongLongMap extends PrimitiveConcurrentMap implements LongLongMap { 11 | 12 | private final LongLongMap[] maps; 13 | private final long defaultValue; 14 | 15 | public ConcurrentLongLongMap(int numBuckets, 16 | int initialCapacity, 17 | float loadFactor, 18 | long defaultValue) { 19 | super(numBuckets); 20 | 21 | this.maps = new LongLongMap[numBuckets]; 22 | this.defaultValue = defaultValue; 23 | 24 | for (int i = 0; i < numBuckets; i++) { 25 | maps[i] = new PrimitiveFastutilLongLongWrapper(initialCapacity, loadFactor, defaultValue); 26 | } 27 | } 28 | 29 | @Override 30 | public int size() { 31 | return super.size(maps); 32 | } 33 | 34 | @Override 35 | public boolean isEmpty() { 36 | return super.isEmpty(maps); 37 | } 38 | 39 | @Override 40 | public boolean containsKey(long key) { 41 | int bucket = getBucket(key); 42 | 43 | Lock readLock = locks[bucket].readLock(); 44 | readLock.lock(); 45 | try { 46 | return maps[bucket].containsKey(key); 47 | } finally { 48 | readLock.unlock(); 49 | } 50 | } 51 | 52 | @Override 53 | public long get(long l) { 54 | int bucket = getBucket(l); 55 | 56 | long result; 57 | 58 | Lock readLock = locks[bucket].readLock(); 59 | readLock.lock(); 60 | try { 61 | result = maps[bucket].get(l); 62 | } finally { 63 | readLock.unlock(); 64 | } 65 | 66 | return result; 67 | } 68 | 69 | @Override 70 | public long put(long key, long value) { 71 | int bucket = getBucket(key); 72 | 73 | long result; 74 | 75 | Lock writeLock = locks[bucket].writeLock(); 76 | writeLock.lock(); 77 | try { 78 | result = maps[bucket].put(key, value); 79 | } finally { 80 | writeLock.unlock(); 81 | } 82 | 83 | return result; 84 | } 85 | 86 | @Override 87 | public long getDefaultValue() { 88 | return defaultValue; 89 | } 90 | 91 | @Override 92 | public long remove(long key) { 93 | int bucket = getBucket(key); 94 | 95 | Lock writeLock = locks[bucket].writeLock(); 96 | writeLock.lock(); 97 | try { 98 | return maps[bucket].remove(key); 99 | } finally { 100 | writeLock.unlock(); 101 | } 102 | } 103 | 104 | @Override 105 | public boolean remove(long key, long value) { 106 | int bucket = getBucket(key); 107 | 108 | Lock writeLock = locks[bucket].writeLock(); 109 | writeLock.lock(); 110 | try { 111 | return maps[bucket].remove(key, value); 112 | } finally { 113 | writeLock.unlock(); 114 | } 115 | } 116 | 117 | @Override 118 | public long computeIfAbsent(long key, Long2LongFunction mappingFunction) { 119 | int bucket = getBucket(key); 120 | 121 | Lock writeLock = locks[bucket].writeLock(); 122 | writeLock.lock(); 123 | try { 124 | return maps[bucket].computeIfAbsent(key, mappingFunction); 125 | } finally { 126 | writeLock.unlock(); 127 | } 128 | } 129 | 130 | @Override 131 | public long computeIfPresent(long key, BiFunction mappingFunction) { 132 | int bucket = getBucket(key); 133 | 134 | Lock writeLock = locks[bucket].writeLock(); 135 | writeLock.lock(); 136 | try { 137 | return maps[bucket].computeIfPresent(key, mappingFunction); 138 | } finally { 139 | writeLock.unlock(); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMap.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.map; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.PrimitiveKeyMap; 4 | 5 | import java.util.concurrent.locks.Lock; 6 | import java.util.concurrent.locks.ReadWriteLock; 7 | import java.util.concurrent.locks.ReentrantReadWriteLock; 8 | 9 | public abstract class PrimitiveConcurrentMap { 10 | 11 | protected final int numBuckets; 12 | protected final ReadWriteLock[] locks; 13 | 14 | protected PrimitiveConcurrentMap(int numBuckets) { 15 | this.numBuckets = numBuckets; 16 | this.locks = new ReadWriteLock[numBuckets]; 17 | for (int i = 0; i < numBuckets; i++) { 18 | locks[i] = new ReentrantReadWriteLock(); 19 | } 20 | } 21 | 22 | protected int size(PrimitiveKeyMap[] maps) { 23 | int sum = 0; 24 | for (int i = 0; i < numBuckets; i++) { 25 | sum = sum + sizeOfMap(i, maps); 26 | } 27 | return sum; 28 | } 29 | 30 | private int sizeOfMap(int index, PrimitiveKeyMap[] maps) { 31 | Lock readLock = locks[index].readLock(); 32 | readLock.lock(); 33 | try { 34 | return maps[index].size(); 35 | } finally { 36 | readLock.unlock(); 37 | } 38 | } 39 | 40 | protected boolean isEmpty(PrimitiveKeyMap[] maps) { 41 | for (int i = 0; i < numBuckets; i++) { 42 | if (!isMapEmpty(i, maps)) { 43 | return false; 44 | } 45 | } 46 | return true; 47 | } 48 | 49 | private boolean isMapEmpty(int index, PrimitiveKeyMap[] maps) { 50 | Lock readLock = locks[index].readLock(); 51 | readLock.lock(); 52 | try { 53 | return maps[index].isEmpty(); 54 | } finally { 55 | readLock.unlock(); 56 | } 57 | } 58 | 59 | protected int getBucket(long key) { 60 | int hash = Long.hashCode(key); 61 | return getBucketCheckMinValue(hash); 62 | } 63 | 64 | protected int getBucket(int key) { 65 | int hash = Integer.hashCode(key); 66 | return getBucketCheckMinValue(hash); 67 | } 68 | 69 | private int getBucketCheckMinValue(int hash) { 70 | return Math.abs(hash == Integer.MIN_VALUE ? 0 : hash) % numBuckets; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/wrapper/PrimitiveFastutilIntFloatWrapper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.wrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntFloatMap; 4 | import it.unimi.dsi.fastutil.ints.Int2FloatFunction; 5 | import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap; 6 | 7 | import java.util.function.BiFunction; 8 | 9 | public class PrimitiveFastutilIntFloatWrapper implements IntFloatMap { 10 | private final float defaultValue; 11 | private final Int2FloatOpenHashMap map; 12 | 13 | public PrimitiveFastutilIntFloatWrapper(int initialCapacity, float loadFactor, float defaultValue) { 14 | this.defaultValue = defaultValue; 15 | this.map = new Int2FloatOpenHashMap(initialCapacity, loadFactor); 16 | 17 | } 18 | 19 | @Override 20 | public float get(int key) { 21 | return map.getOrDefault(key, defaultValue); 22 | } 23 | 24 | @Override 25 | public float put(int key, float value) { 26 | return map.put(key, value); 27 | } 28 | 29 | @Override 30 | public float getDefaultValue() { 31 | return defaultValue; 32 | } 33 | 34 | @Override 35 | public float remove(int key) { 36 | return map.remove(key); 37 | } 38 | 39 | @Override 40 | public boolean remove(int key, float value) { 41 | return map.remove(key, value); 42 | } 43 | 44 | @Override 45 | public boolean containsKey(int key) { 46 | return map.containsKey(key); 47 | } 48 | 49 | @Override 50 | public int size() { 51 | return map.size(); 52 | } 53 | 54 | @Override 55 | public boolean isEmpty() { 56 | return map.isEmpty(); 57 | } 58 | 59 | @Override 60 | public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { 61 | return map.computeIfAbsent(key, mappingFunction); 62 | } 63 | 64 | @Override 65 | public float computeIfPresent(int key, BiFunction mappingFunction) { 66 | return map.computeIfPresent(key, mappingFunction); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/wrapper/PrimitiveFastutilIntIntWrapper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.wrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.IntIntMap; 4 | import it.unimi.dsi.fastutil.ints.Int2IntFunction; 5 | import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; 6 | 7 | import java.util.function.BiFunction; 8 | 9 | public class PrimitiveFastutilIntIntWrapper implements IntIntMap { 10 | private final int defaultValue; 11 | private final Int2IntOpenHashMap map; 12 | 13 | public PrimitiveFastutilIntIntWrapper(int initialCapacity, float loadFactor, int defaultValue) { 14 | this.defaultValue = defaultValue; 15 | this.map = new Int2IntOpenHashMap(initialCapacity, loadFactor); 16 | } 17 | 18 | @Override 19 | public int get(int key) { 20 | return map.getOrDefault(key, defaultValue); 21 | } 22 | 23 | @Override 24 | public int put(int key, int value) { 25 | return map.put(key, value); 26 | } 27 | 28 | @Override 29 | public int getDefaultValue() { 30 | return defaultValue; 31 | } 32 | 33 | @Override 34 | public int remove(int key) { 35 | return map.remove(key); 36 | } 37 | 38 | @Override 39 | public boolean remove(int key, int value) { 40 | return map.remove(key, value); 41 | } 42 | 43 | @Override 44 | public int size() { 45 | return map.size(); 46 | } 47 | 48 | @Override 49 | public boolean containsKey(int key) { 50 | return map.containsKey(key); 51 | } 52 | 53 | @Override 54 | public boolean isEmpty() { 55 | return map.isEmpty(); 56 | } 57 | 58 | @Override 59 | public int computeIfAbsent(int key, Int2IntFunction mappingFunction) { 60 | return map.computeIfAbsent(key, mappingFunction); 61 | } 62 | 63 | @Override 64 | public int computeIfPresent(int key, BiFunction mappingFunction) { 65 | return map.computeIfPresent(key, mappingFunction); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/wrapper/PrimitiveFastutilLongFloatWrapper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.wrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongFloatMap; 4 | import it.unimi.dsi.fastutil.longs.Long2FloatFunction; 5 | import it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap; 6 | 7 | import java.util.function.BiFunction; 8 | 9 | public class PrimitiveFastutilLongFloatWrapper implements LongFloatMap { 10 | private final float defaultValue; 11 | private final Long2FloatOpenHashMap map; 12 | 13 | public PrimitiveFastutilLongFloatWrapper(int initialCapacity, float loadFactor, float defaultValue) { 14 | this.map = new Long2FloatOpenHashMap(initialCapacity, loadFactor); 15 | this.defaultValue = defaultValue; 16 | } 17 | 18 | @Override 19 | public float getDefaultValue() { 20 | return defaultValue; 21 | } 22 | 23 | @Override 24 | public float get(long key) { 25 | return map.getOrDefault(key, defaultValue); 26 | } 27 | 28 | @Override 29 | public float put(long key, float value) { 30 | return map.put(key, value); 31 | } 32 | 33 | @Override 34 | public float remove(long key) { 35 | return map.remove(key); 36 | } 37 | 38 | @Override 39 | public boolean remove(long key, float value) { 40 | return map.remove(key, value); 41 | } 42 | 43 | @Override 44 | public int size() { 45 | return map.size(); 46 | } 47 | 48 | @Override 49 | public boolean containsKey(long key) { 50 | return map.containsKey(key); 51 | } 52 | 53 | @Override 54 | public boolean isEmpty() { 55 | return map.isEmpty(); 56 | } 57 | 58 | @Override 59 | public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) { 60 | return map.computeIfAbsent(key, mappingFunction); 61 | } 62 | 63 | @Override 64 | public float computeIfPresent(int key, BiFunction mappingFunction) { 65 | return map.computeIfPresent(key, mappingFunction); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/wrapper/PrimitiveFastutilLongIntWrapper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.wrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 5 | import it.unimi.dsi.fastutil.longs.Long2IntFunction; 6 | import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; 7 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 8 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; 9 | 10 | import java.util.function.BiFunction; 11 | 12 | public class PrimitiveFastutilLongIntWrapper implements LongIntMap { 13 | private final Long2IntOpenHashMap map; 14 | private final int defaultValue; 15 | 16 | public PrimitiveFastutilLongIntWrapper(int initialCapacity, float loadFactor) { 17 | this(initialCapacity, loadFactor, LongIntMap.DEFAULT_VALUE); 18 | } 19 | 20 | public PrimitiveFastutilLongIntWrapper(int initialCapacity, float loadFactor, int defaultValue) { 21 | this.defaultValue = defaultValue; 22 | this.map = new Long2IntOpenHashMap(initialCapacity, loadFactor); 23 | } 24 | 25 | @Override 26 | public int get(long key) { 27 | return map.getOrDefault(key, defaultValue); 28 | } 29 | 30 | @Override 31 | public int put(long key, int value) { 32 | return map.put(key, value); 33 | } 34 | 35 | @Override 36 | public int getDefaultValue() { 37 | return defaultValue; 38 | } 39 | 40 | @Override 41 | public int remove(long key) { 42 | return map.remove(key); 43 | } 44 | 45 | @Override 46 | public boolean remove(long key, int value) { 47 | return map.remove(key, value); 48 | } 49 | 50 | @Override 51 | public int size() { 52 | return map.size(); 53 | } 54 | 55 | @Override 56 | public boolean containsKey(long key) { 57 | return map.containsKey(key); 58 | } 59 | 60 | @Override 61 | public boolean isEmpty() { 62 | return map.isEmpty(); 63 | } 64 | 65 | @Override 66 | public int computeIfAbsent(long key, Long2IntFunction mappingFunction) { 67 | return map.computeIfAbsent(key, mappingFunction); 68 | } 69 | 70 | @Override 71 | public int computeIfPresent(long key, BiFunction mappingFunction) { 72 | return map.computeIfPresent(key, mappingFunction); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/trivago/fastutilconcurrentwrapper/wrapper/PrimitiveFastutilLongLongWrapper.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.wrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import it.unimi.dsi.fastutil.longs.Long2LongFunction; 5 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; 6 | 7 | import java.util.function.BiFunction; 8 | 9 | public class PrimitiveFastutilLongLongWrapper implements LongLongMap { 10 | private final Long2LongOpenHashMap map; 11 | private final long defaultValue; 12 | 13 | public PrimitiveFastutilLongLongWrapper(int initialCapacity, float loadFactor) { 14 | this(initialCapacity, loadFactor, LongLongMap.DEFAULT_VALUE); 15 | } 16 | 17 | public PrimitiveFastutilLongLongWrapper(int initialCapacity, float loadFactor, long defaultValue) { 18 | this.defaultValue = defaultValue; 19 | this.map = new Long2LongOpenHashMap(initialCapacity, loadFactor); 20 | } 21 | 22 | @Override 23 | public long get(long key) { 24 | return map.getOrDefault(key, defaultValue); 25 | } 26 | 27 | @Override 28 | public long put(long key, long value) { 29 | return map.put(key, value); 30 | } 31 | 32 | @Override 33 | public long getDefaultValue() { 34 | return defaultValue; 35 | } 36 | 37 | @Override 38 | public long remove(long key) { 39 | return map.remove(key); 40 | } 41 | 42 | @Override 43 | public boolean remove(long key, long value) { 44 | return map.remove(key, value); 45 | } 46 | 47 | @Override 48 | public int size() { 49 | return map.size(); 50 | } 51 | 52 | @Override 53 | public boolean containsKey(long key) { 54 | return map.containsKey(key); 55 | } 56 | 57 | @Override 58 | public boolean isEmpty() { 59 | return map.isEmpty(); 60 | } 61 | 62 | @Override 63 | public long computeIfAbsent(long key, Long2LongFunction mappingFunction) { 64 | return map.computeIfAbsent(key, mappingFunction); 65 | } 66 | 67 | @Override 68 | public long computeIfPresent(long key, BiFunction mappingFunction) { 69 | return map.computeIfPresent(key, mappingFunction); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/AbstractMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | /* 8 | * Since there is no common Map interface for the typed maps, in fastutilsconcurrentwrapper, 9 | * test cases have to be repeated for each typed map interface. 10 | */ 11 | public abstract class AbstractMapTest { 12 | protected static long nextLong() { 13 | return ThreadLocalRandom.current().nextLong(); 14 | } 15 | 16 | protected static int nextInt() { 17 | return ThreadLocalRandom.current().nextInt(); 18 | } 19 | 20 | @Test 21 | protected abstract void containsKeyReturnsFalseIfMapIsEmpty(); 22 | 23 | @Test 24 | protected abstract void containsKeyReturnsTrueIfKeyExists(); 25 | 26 | @Test 27 | protected abstract void containsKeyReturnsFalseIfKeyWasRemoved(); 28 | 29 | @Test 30 | protected abstract void mapIsEmptyWhenNothingWasInserted(); 31 | 32 | @Test 33 | protected abstract void mapIsEmptyWhenAllKeysAreDeleted(); 34 | 35 | @Test 36 | protected abstract void sizeIsCorrect(); 37 | 38 | @Test 39 | protected abstract void gettingExistingValueReturnsCorrectValue(); 40 | 41 | @Test 42 | protected abstract void gettingNonExistingValueReturnsCorrectValue(); 43 | 44 | @Test 45 | protected abstract void removingNonExistingKeyReturnsDefaultValue(); 46 | 47 | @Test 48 | protected abstract void removingExistingKeyReturnsPreviousValue(); 49 | 50 | @Test 51 | protected abstract void removingWithValueWhenKeyDoesNotExistReturnsFalse(); 52 | 53 | @Test 54 | protected abstract void removingWithValueWhenValueIsDifferentReturnsFalse(); 55 | 56 | @Test 57 | protected abstract void removingWithValueWhenValueIsSameReturnsTrue(); 58 | 59 | @Test 60 | protected abstract void puttingValueIfAbsentReturnsSameValue(); 61 | 62 | @Test 63 | protected abstract void checkingValueIfNotAbsentReturnsSameValue(); 64 | 65 | @Test 66 | protected abstract void replacingValueIfPresentReturnsNewValue(); 67 | 68 | @Test 69 | protected abstract void checkingValueIfNotPresentReturnsDefaultValue(); 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntFloatMap; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | import static org.junit.jupiter.api.Assertions.assertTrue; 9 | 10 | public class ConcurrentIntFloatMapBuilderTest { 11 | private final float DEFAULT_VALUE = -1f; 12 | 13 | @Test 14 | public void buildsBusyWaitingMap() { 15 | ConcurrentIntFloatMapBuilder b = ConcurrentIntFloatMapBuilder.newBuilder() 16 | .withBuckets(2) 17 | .withDefaultValue(DEFAULT_VALUE) 18 | .withInitialCapacity(100) 19 | .withMode(ConcurrentIntFloatMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.8f); 21 | 22 | IntFloatMap map = b.build(); 23 | 24 | map.put(1, 10.1f); 25 | float v = map.get(1); 26 | 27 | assertTrue(map instanceof ConcurrentBusyWaitingIntFloatMap); 28 | assertEquals(10.1f, v); 29 | assertEquals(map.get(2), map.getDefaultValue()); 30 | } 31 | 32 | @Test 33 | public void buildsBlockingMap() { 34 | ConcurrentIntFloatMapBuilder b = ConcurrentIntFloatMapBuilder.newBuilder() 35 | .withBuckets(2) 36 | .withDefaultValue(DEFAULT_VALUE) 37 | .withInitialCapacity(100) 38 | .withMode(ConcurrentIntFloatMapBuilder.MapMode.BLOCKING) 39 | .withLoadFactor(0.8f); 40 | 41 | IntFloatMap map = b.build(); 42 | 43 | map.put(1, 10.1f); 44 | float v = map.get(1); 45 | 46 | assertTrue(map instanceof ConcurrentIntFloatMap); 47 | assertEquals(10.1f, v); 48 | assertEquals(map.get(2), map.getDefaultValue()); 49 | } 50 | } -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntIntMap; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | import static org.junit.jupiter.api.Assertions.assertTrue; 9 | 10 | public class ConcurrentIntIntMapBuilderTest { 11 | private final int DEFAULT_VALUE = -1; 12 | 13 | @Test 14 | public void buildsBusyWaitingMap() { 15 | ConcurrentIntIntMapBuilder b = ConcurrentIntIntMapBuilder.newBuilder() 16 | .withBuckets(2) 17 | .withDefaultValue(DEFAULT_VALUE) 18 | .withInitialCapacity(100) 19 | .withMode(ConcurrentIntIntMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.8f); 21 | 22 | IntIntMap map = b.build(); 23 | 24 | map.put(1, 10); 25 | float v = map.get(1); 26 | 27 | assertTrue(map instanceof ConcurrentBusyWaitingIntIntMap); 28 | assertEquals(10, v); 29 | assertEquals(map.get(2), map.getDefaultValue()); 30 | } 31 | 32 | @Test 33 | public void buildsBlockingMap() { 34 | ConcurrentIntIntMapBuilder b = ConcurrentIntIntMapBuilder.newBuilder() 35 | .withBuckets(2) 36 | .withDefaultValue(DEFAULT_VALUE) 37 | .withInitialCapacity(100) 38 | .withMode(ConcurrentIntIntMapBuilder.MapMode.BLOCKING) 39 | .withLoadFactor(0.8f); 40 | 41 | IntIntMap map = b.build(); 42 | 43 | map.put(1, 10); 44 | float v = map.get(1); 45 | 46 | assertTrue(map instanceof ConcurrentIntIntMap); 47 | assertEquals(10, v); 48 | assertEquals(map.get(2), map.getDefaultValue()); 49 | } 50 | } -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongFloatMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongFloatMap; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | import static org.junit.jupiter.api.Assertions.assertTrue; 9 | 10 | public class ConcurrentLongFloatMapBuilderTest { 11 | private final float DEFAULT_VALUE = -1f; 12 | 13 | @Test 14 | public void buildsBusyWaitingMap() { 15 | ConcurrentLongFloatMapBuilder b = ConcurrentLongFloatMapBuilder.newBuilder() 16 | .withBuckets(2) 17 | .withDefaultValue(DEFAULT_VALUE) 18 | .withInitialCapacity(100) 19 | .withMode(ConcurrentLongFloatMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.8f); 21 | 22 | LongFloatMap map = b.build(); 23 | 24 | map.put(1L, 10.1f); 25 | float v = map.get(1L); 26 | 27 | assertTrue(map instanceof ConcurrentBusyWaitingLongFloatMap); 28 | assertEquals(10.1f, v); 29 | assertEquals(map.get(2L), map.getDefaultValue()); 30 | } 31 | 32 | @Test 33 | public void buildsBlockingMap() { 34 | ConcurrentLongFloatMapBuilder b = ConcurrentLongFloatMapBuilder.newBuilder() 35 | .withBuckets(2) 36 | .withDefaultValue(DEFAULT_VALUE) 37 | .withInitialCapacity(100) 38 | .withMode(ConcurrentLongFloatMapBuilder.MapMode.BLOCKING) 39 | .withLoadFactor(0.8f); 40 | 41 | LongFloatMap map = b.build(); 42 | 43 | map.put(1L, 10.1f); 44 | float v = map.get(1L); 45 | 46 | assertTrue(map instanceof ConcurrentLongFloatMap); 47 | assertEquals(10.1f, v); 48 | assertEquals(map.get(2L), map.getDefaultValue()); 49 | } 50 | } -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | import static org.junit.jupiter.api.Assertions.assertTrue; 9 | 10 | public class ConcurrentLongIntMapBuilderTest { 11 | private final int DEFAULT_VALUE = -1; 12 | 13 | @Test 14 | public void buildsBusyWaitingMap() { 15 | ConcurrentLongIntMapBuilder b = ConcurrentLongIntMapBuilder.newBuilder() 16 | .withBuckets(2) 17 | .withDefaultValue(DEFAULT_VALUE) 18 | .withInitialCapacity(100) 19 | .withMode(ConcurrentLongIntMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.9f); 21 | 22 | LongIntMap map = b.build(); 23 | 24 | map.put(1L, 10); 25 | int v = map.get(1L); 26 | 27 | assertTrue(map instanceof ConcurrentBusyWaitingLongIntMap); 28 | assertEquals(10, v); 29 | assertEquals(map.get(2L), map.getDefaultValue()); 30 | } 31 | 32 | @Test 33 | public void buildsBlockingMap() { 34 | ConcurrentLongIntMapBuilder b = ConcurrentLongIntMapBuilder.newBuilder() 35 | .withBuckets(2) 36 | .withDefaultValue(DEFAULT_VALUE) 37 | .withInitialCapacity(100) 38 | .withMode(ConcurrentLongIntMapBuilder.MapMode.BLOCKING) 39 | .withLoadFactor(0.9f); 40 | 41 | LongIntMap map = b.build(); 42 | 43 | map.put(1L, 10); 44 | long v = map.get(1L); 45 | 46 | assertTrue(map instanceof ConcurrentLongIntMap); 47 | assertEquals(10, v); 48 | assertEquals(map.get(2L), map.getDefaultValue()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; 8 | import static org.junit.jupiter.api.Assertions.assertTrue; 9 | 10 | public class ConcurrentLongLongMapBuilderTest { 11 | private final long DEFAULT_VALUE = -1L; 12 | 13 | @Test 14 | public void simpleBuilderTest() { 15 | ConcurrentLongLongMapBuilder b = ConcurrentLongLongMapBuilder.newBuilder() 16 | .withBuckets(2) 17 | .withDefaultValue(DEFAULT_VALUE) 18 | .withInitialCapacity(100) 19 | .withMode(ConcurrentLongLongMapBuilder.MapMode.BUSY_WAITING) 20 | .withLoadFactor(0.9f); 21 | 22 | LongLongMap map = b.build(); 23 | 24 | map.put(1L, 10L); 25 | long v = map.get(1L); 26 | 27 | assertTrue(map instanceof ConcurrentBusyWaitingLongLongMap); 28 | assertEquals(10L, v); 29 | assertEquals(map.get(2L), map.getDefaultValue()); 30 | } 31 | 32 | @Test 33 | public void buildsBlockingMap() { 34 | ConcurrentLongLongMapBuilder b = ConcurrentLongLongMapBuilder.newBuilder() 35 | .withBuckets(2) 36 | .withDefaultValue(DEFAULT_VALUE) 37 | .withInitialCapacity(100) 38 | .withMode(ConcurrentLongLongMapBuilder.MapMode.BLOCKING) 39 | .withLoadFactor(0.9f); 40 | 41 | LongLongMap map = b.build(); 42 | 43 | map.put(1L, 10L); 44 | long v = map.get(1L); 45 | 46 | assertTrue(map instanceof ConcurrentLongLongMap); 47 | assertEquals(10L, v); 48 | assertEquals(map.get(2L), map.getDefaultValue()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longint/AbstractLongIntMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longint; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.AbstractMapTest; 4 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | import static org.junit.jupiter.api.Assertions.assertFalse; 10 | import static org.junit.jupiter.api.Assertions.assertTrue; 11 | 12 | abstract class AbstractLongIntMapTest extends AbstractMapTest { 13 | // Some methods return the default value of the underlying Fastutil implementation. 14 | private static final int FASTUTIL_DEFAULT_VALUE = 0; 15 | 16 | protected int defaultValue; 17 | private LongIntMap map; 18 | // Keep the default value to easily verify that this value is returned. 19 | 20 | abstract LongIntMap createMap(); 21 | 22 | @BeforeEach 23 | void initializeMap() { 24 | defaultValue = nextInt(); 25 | map = createMap(); 26 | } 27 | 28 | @Test 29 | protected void containsKeyReturnsFalseIfMapIsEmpty() { 30 | final int key = nextInt(); 31 | 32 | final boolean contains = map.containsKey(key); 33 | 34 | assertFalse(contains); 35 | } 36 | 37 | @Test 38 | protected void containsKeyReturnsTrueIfKeyExists() { 39 | int key = nextInt(); 40 | int value = nextInt(); 41 | map.put(key, value); 42 | 43 | final boolean contains = map.containsKey(key); 44 | 45 | assertTrue(contains); 46 | } 47 | 48 | @Test 49 | protected void containsKeyReturnsFalseIfKeyWasRemoved() { 50 | int key = nextInt(); 51 | int value = nextInt(); 52 | map.put(key, value); 53 | map.remove(key); 54 | 55 | final boolean contains = map.containsKey(key); 56 | 57 | assertFalse(contains); 58 | } 59 | 60 | @Test 61 | protected void mapIsEmptyWhenNothingWasInserted() { 62 | final boolean empty = map.isEmpty(); 63 | 64 | assertTrue(empty); 65 | } 66 | 67 | @Test 68 | protected void mapIsEmptyWhenAllKeysAreDeleted() { 69 | int entryCount = (Math.abs(nextInt()) % 100) + 1; 70 | int value = nextInt(); 71 | 72 | for (int key = 1; key <= entryCount; key++) { 73 | map.put(key, value); 74 | } 75 | for (int key = 1; key <= entryCount; key++) { 76 | map.remove(key); 77 | } 78 | 79 | final boolean empty = map.isEmpty(); 80 | 81 | assertTrue(empty); 82 | } 83 | 84 | @Test 85 | protected void sizeIsCorrect() { 86 | int entries = (Math.abs(nextInt()) % 50) + 1; 87 | int value = nextInt(); 88 | 89 | for (int key = 1; key <= entries; key++) { 90 | map.put(key, value); 91 | } 92 | 93 | final int size = map.size(); 94 | 95 | assertEquals(entries, size); 96 | } 97 | 98 | @Test 99 | protected void gettingExistingValueReturnsCorrectValue() { 100 | int key = nextInt(); 101 | int value = nextInt(); 102 | map.put(key, value); 103 | final int returnedValue = map.get(key); 104 | 105 | assertEquals(value, returnedValue); 106 | } 107 | 108 | @Test 109 | protected void gettingNonExistingValueReturnsCorrectValue() { 110 | int key = nextInt(); 111 | final int returnedValue = map.get(key); 112 | 113 | assertEquals(defaultValue, returnedValue); 114 | } 115 | 116 | @Test 117 | protected void removingNonExistingKeyReturnsDefaultValue() { 118 | int key = nextInt(); 119 | final int removedValue = map.remove(key); 120 | 121 | assertEquals(FASTUTIL_DEFAULT_VALUE, removedValue); 122 | } 123 | 124 | @Test 125 | protected void removingExistingKeyReturnsPreviousValue() { 126 | int key = nextInt(); 127 | int value = nextInt(); 128 | map.put(key, value); 129 | final int removedValue = map.remove(key); 130 | 131 | assertEquals(value, removedValue); 132 | } 133 | 134 | @Test 135 | protected void removingWithValueWhenKeyDoesNotExistReturnsFalse() { 136 | int key = nextInt(); 137 | int value = nextInt(); 138 | final boolean result = map.remove(key, value); 139 | 140 | assertFalse(result); 141 | } 142 | 143 | @Test 144 | protected void removingWithValueWhenValueIsDifferentReturnsFalse() { 145 | int key = nextInt(); 146 | int value = nextInt(); 147 | map.put(key, value); 148 | final boolean result = map.remove(key, value - 1); 149 | 150 | assertFalse(result); 151 | } 152 | 153 | @Test 154 | protected void removingWithValueWhenValueIsSameReturnsTrue() { 155 | int key = nextInt(); 156 | int value = nextInt(); 157 | map.put(key, value); 158 | final boolean result = map.remove(key, value); 159 | 160 | assertTrue(result); 161 | } 162 | 163 | @Test 164 | protected void puttingValueIfAbsentReturnsSameValue() { 165 | int key = nextInt(); 166 | int value = nextInt(); 167 | map.computeIfAbsent(key, l -> value); 168 | 169 | int result = map.get(key); 170 | 171 | assertEquals(result, value); 172 | } 173 | 174 | @Test 175 | protected void checkingValueIfNotAbsentReturnsSameValue() { 176 | int key = nextInt(); 177 | int value = nextInt(); 178 | map.put(key, value); 179 | int returned = map.computeIfAbsent(key, l -> value); 180 | 181 | int result = map.get(key); 182 | 183 | assertEquals(result, value); 184 | assertEquals(value, returned); 185 | } 186 | 187 | @Test 188 | protected void replacingValueIfPresentReturnsNewValue() { 189 | int key = nextInt(); 190 | int value = nextInt(); 191 | map.put(key, value); 192 | 193 | map.computeIfPresent(key, (aLongKey, anIntValue) -> 2 * anIntValue); // key + old value 194 | 195 | int result = map.get(key); 196 | 197 | assertEquals(result, 2 * value); 198 | } 199 | 200 | @Test 201 | protected void checkingValueIfNotPresentReturnsDefaultValue() { 202 | int key = nextInt(); 203 | map.computeIfPresent(key, (aLongKey, anIntValue) -> 2 * anIntValue); // key + old value 204 | 205 | int result = map.get(key); 206 | 207 | assertEquals(result, map.getDefaultValue()); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentBusyWaitingLongIntMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longint; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; 5 | 6 | public class ConcurrentBusyWaitingLongIntMapTest extends AbstractLongIntMapTest { 7 | 8 | @Override 9 | LongIntMap createMap() { 10 | return new ConcurrentBusyWaitingLongIntMap(16, 16, 0.9F, defaultValue); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentPrimitiveLongIntMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longint; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; 5 | 6 | 7 | public class ConcurrentPrimitiveLongIntMapTest extends AbstractLongIntMapTest { 8 | @Override 9 | LongIntMap createMap() { 10 | return new ConcurrentLongIntMap(16, 16, 0.9F, defaultValue); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longint/PrimitiveFastutilLongIntWrapperTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longint; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongIntMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongIntWrapper; 5 | 6 | public class PrimitiveFastutilLongIntWrapperTest extends AbstractLongIntMapTest { 7 | 8 | @Override 9 | LongIntMap createMap() { 10 | return new PrimitiveFastutilLongIntWrapper(5, 0.9F, defaultValue); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/AbstractLongLongMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longlong; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.AbstractMapTest; 4 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | import static org.junit.jupiter.api.Assertions.assertFalse; 10 | import static org.junit.jupiter.api.Assertions.assertTrue; 11 | 12 | abstract class AbstractLongLongMapTest extends AbstractMapTest { 13 | private LongLongMap map; 14 | // Keep the default value to easily verify that this value is returned. 15 | protected long defaultValue; 16 | // Some methods return the default value of the underlying Fastutil implementation. 17 | private static final long FASTUTIL_DEFAULT_VALUE = 0L; 18 | 19 | abstract LongLongMap createMap(); 20 | 21 | @BeforeEach 22 | void initializeMap() { 23 | defaultValue = nextLong(); 24 | map = createMap(); 25 | } 26 | 27 | @Test 28 | protected void containsKeyReturnsFalseIfMapIsEmpty() { 29 | final long key = nextLong(); 30 | 31 | final boolean contains = map.containsKey(key); 32 | 33 | assertFalse(contains); 34 | } 35 | 36 | @Test 37 | protected void containsKeyReturnsTrueIfKeyExists() { 38 | long key = nextLong(); 39 | long value = nextLong(); 40 | map.put(key, value); 41 | 42 | final boolean contains = map.containsKey(key); 43 | 44 | assertTrue(contains); 45 | } 46 | 47 | @Test 48 | protected void containsKeyReturnsFalseIfKeyWasRemoved() { 49 | long key = nextLong(); 50 | long value = nextLong(); 51 | map.put(key, value); 52 | map.remove(key); 53 | 54 | final boolean contains = map.containsKey(key); 55 | 56 | assertFalse(contains); 57 | } 58 | 59 | @Test 60 | protected void mapIsEmptyWhenNothingWasInserted() { 61 | final boolean empty = map.isEmpty(); 62 | 63 | assertTrue(empty); 64 | } 65 | 66 | @Test 67 | protected void mapIsEmptyWhenAllKeysAreDeleted() { 68 | int entryCount = (Math.abs(nextInt()) % 100) + 1; 69 | long value = nextLong(); 70 | 71 | for (long key = 1; key <= entryCount; key++) { 72 | map.put(key, value); 73 | } 74 | for (long key = 1; key <= entryCount; key++) { 75 | map.remove(key); 76 | } 77 | 78 | final boolean empty = map.isEmpty(); 79 | 80 | assertTrue(empty); 81 | } 82 | 83 | @Test 84 | protected void sizeIsCorrect() { 85 | int entries = (Math.abs(nextInt()) % 50) + 1; 86 | long value = nextLong(); 87 | 88 | for (long key = 1; key <= entries; key++) { 89 | map.put(key, value); 90 | } 91 | 92 | final int size = map.size(); 93 | 94 | assertEquals(entries, size); 95 | } 96 | 97 | @Test 98 | protected void gettingExistingValueReturnsCorrectValue() { 99 | long key = nextLong(); 100 | long value = nextLong(); 101 | map.put(key, value); 102 | final long returnedValue = map.get(key); 103 | 104 | assertEquals(value, returnedValue); 105 | } 106 | 107 | @Test 108 | protected void gettingNonExistingValueReturnsCorrectValue() { 109 | long key = nextLong(); 110 | final long returnedValue = map.get(key); 111 | 112 | assertEquals(defaultValue, returnedValue); 113 | } 114 | 115 | @Test 116 | protected void removingNonExistingKeyReturnsDefaultValue() { 117 | long key = nextLong(); 118 | final long removedValue = map.remove(key); 119 | 120 | assertEquals(FASTUTIL_DEFAULT_VALUE, removedValue); 121 | } 122 | 123 | @Test 124 | protected void removingExistingKeyReturnsPreviousValue() { 125 | long key = nextLong(); 126 | long value = nextLong(); 127 | map.put(key, value); 128 | final long removedValue = map.remove(key); 129 | 130 | assertEquals(value, removedValue); 131 | } 132 | 133 | @Test 134 | protected void removingWithValueWhenKeyDoesNotExistReturnsFalse() { 135 | long key = nextLong(); 136 | long value = nextLong(); 137 | final boolean result = map.remove(key, value); 138 | 139 | assertFalse(result); 140 | } 141 | 142 | @Test 143 | protected void removingWithValueWhenValueIsDifferentReturnsFalse() { 144 | long key = nextLong(); 145 | long value = nextLong(); 146 | map.put(key, value); 147 | final boolean result = map.remove(key, value - 1); 148 | 149 | assertFalse(result); 150 | } 151 | 152 | @Test 153 | protected void removingWithValueWhenValueIsSameReturnsTrue() { 154 | long key = nextLong(); 155 | long value = nextLong(); 156 | map.put(key, value); 157 | final boolean result = map.remove(key, value); 158 | 159 | assertTrue(result); 160 | } 161 | 162 | @Test 163 | protected void puttingValueIfAbsentReturnsSameValue() { 164 | long key = nextLong(); 165 | long value = nextLong(); 166 | map.computeIfAbsent(key, l -> value); 167 | 168 | long result = map.get(key); 169 | 170 | assertEquals(result, value); 171 | } 172 | 173 | @Test 174 | protected void checkingValueIfNotAbsentReturnsSameValue() { 175 | long key = nextLong(); 176 | long value = nextLong(); 177 | map.put(key, value); 178 | long returned = map.computeIfAbsent(key, l -> value); 179 | 180 | long result = map.get(key); 181 | 182 | assertEquals(result, value); 183 | assertEquals(value, returned); 184 | } 185 | 186 | @Test 187 | protected void replacingValueIfPresentReturnsNewValue() { 188 | long key = nextLong(); 189 | long value = nextLong(); 190 | map.put(key, value); 191 | 192 | map.computeIfPresent(key, Long::sum); // key + old value 193 | 194 | long result = map.get(key); 195 | 196 | assertEquals(result, key + value); 197 | } 198 | 199 | @Test 200 | protected void checkingValueIfNotPresentReturnsDefaultValue() { 201 | long key = nextLong(); 202 | map.computeIfPresent(key, Long::sum); 203 | 204 | long result = map.get(key); 205 | 206 | assertEquals(result, map.getDefaultValue()); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentBusyWaitingLongLongMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longlong; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; 5 | 6 | public class ConcurrentBusyWaitingLongLongMapTest extends AbstractLongLongMapTest { 7 | 8 | @Override 9 | LongLongMap createMap() { 10 | return new ConcurrentBusyWaitingLongLongMap(16, 16, 0.9F, defaultValue); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentLongLongMapTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longlong; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; 5 | 6 | public class ConcurrentLongLongMapTest extends AbstractLongLongMapTest { 7 | 8 | @Override 9 | LongLongMap createMap() { 10 | return new ConcurrentLongLongMap(16, 16, 0.9F, defaultValue); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/PrimitiveFastutilLongLongWrapperTest.java: -------------------------------------------------------------------------------- 1 | package com.trivago.fastutilconcurrentwrapper.longlong; 2 | 3 | import com.trivago.fastutilconcurrentwrapper.LongLongMap; 4 | import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongLongWrapper; 5 | 6 | public class PrimitiveFastutilLongLongWrapperTest extends AbstractLongLongMapTest { 7 | 8 | @Override 9 | LongLongMap createMap() { 10 | return new PrimitiveFastutilLongLongWrapper(5, 0.9F, defaultValue); 11 | } 12 | } 13 | --------------------------------------------------------------------------------