Giter VIP home page Giter VIP logo

kotlinx-bimap's Introduction

kotlinx-bimap

Java 6+ Maven metadata URL Bintray JitPack Build Status codecov codebeat badge

A bimap (bidirectional map) implementation for Kotlin.

Prerequisites

  • JDK 1.6 or higher

Interfaces and implementations

This library provides interfaces for read-only/mutable bimap:

Interface Bases Implementations
BiMap Map emptyBiMap, biMapOf, toBiMap
MutableBiMap MutableMap, BiMap mutableBiMapOf, toMutableBiMap

Operations

Create read-only bimap

  • Create an empty read-only bimap:

    val newBiMap = emptyBiMap()
  • Create a new read-only bimap from pairs:

    val newBiMap = biMapOf(1 to "1", 2 to "2", 3 to "3")
  • Create a new read-only bimap from map:

    val newBiMap = mapOf(1 to "1", 2 to "2", 3 to "3").toBiMap()

Create mutable bimap

  • Create an empty mutable bimap:

    val newBiMap = mutableBiMapOf()
  • Create a new mutable bimap from pairs:

    val newBiMap = mutableBiMapOf(1 to "1", 2 to "2", 3 to "3")
  • Create a new mutable bimap from map:

    val newBiMap = mapOf(1 to "1", 2 to "2", 3 to "3").toMutableBiMap()

Query bimap

  • Bimap support all operations of map:

    val biMap = biMapOf(1 to "1", 2 to "2", 3 to "3")
    biMap.size // 3
    biMap.isEmpty() // false
    biMap.values // ["1", "2", "3"]
    biMap[1] // "1"
    biMap.containsKey(4) // false
  • Get the inverse view of bimap:

    val biMap = biMapOf(1 to "1", 2 to "2", 3 to "3")
    val inverseBiMap = biMap.inverse
    inverseBiMap.values // [1, 2, 3]

Mutate mutable bimap

Mutable bimap support all operations of mutable map:

val mutableBiMap = mutableBiMapOf(1 to "1", 2 to "2", 3 to "3")
mutableBiMap[3] = "4"
mutableBiMap.remove(1)
mutableBiMap.clear()

When using put operation (i.e., mutableBiMap[3] = "4"), the bimap throws IllegalArgumentException if the given value is already bound to a different key in it. The bimap will remain unmodified in this event. To avoid this exception, call forcePut instead:

mutableBiMap.forcePut(4, "2")

The forcePut operation will silently remove any existing entry with the value before proceeding with the put operation.

Interoperability with Guava bimap

  • Views a mutable bimap as a Guava bimap:

    val mutableBiMap = mutableBiMapOf(1 to "1", 2 to "2", 3 to "3")
    val guavaBiMap = mutableBiMap.asGuavaBiMap()
  • Views a Guava bimap as a mutable bimap:

    val guavaBiMap = HashBiMap.create(mapOf(1 to "1", 2 to "2", 3 to "3"))
    val mutableBiMap = guavaBiMap.asMutableBiMap()

Using in your projects

This library has been published to Maven Central, JCenter and JitPack.

Maven

<dependency>
  <groupId>com.uchuhimo</groupId>
  <artifactId>kotlinx-bimap</artifactId>
  <version>1.2</version>
</dependency>

Gradle

compile 'com.uchuhimo:kotlinx-bimap:1.2'

Gradle Kotlin DSL

compile(group = "com.uchuhimo", name = "kotlinx-bimap", version = "1.2")

Building from source

Build library with Gradle using the following command:

gradlew clean assemble

Test library with Gradle using the following command:

gradlew clean test

Since Gradle has excellent incremental build support, you can usually omit executing the clean task.

Install library in a local Maven repository for consumption in other projects via the following command:

gradlew clean install

License

© uchuhimo, 2017-2018. Licensed under an Apache 2.0 license.

kotlinx-bimap's People

Contributors

uchuhimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.