Skip to content

Instantly share code, notes, and snippets.

@jordanluyke
Last active June 13, 2023 14:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jordanluyke/609a4fffb69d1ebafdadd313ee2ee804 to your computer and use it in GitHub Desktop.
Save jordanluyke/609a4fffb69d1ebafdadd313ee2ee804 to your computer and use it in GitHub Desktop.
TypeScript Multimap
export interface Multimap<K, V> {
clear(): void
containsKey(key: K): boolean
containsValue(value: V): boolean
containsEntry(key: K, value: V): boolean
delete(key: K, value?: V): boolean
entries: MultimapEntry<K, V>[]
get(key: K): V[]
keys(): K[]
put(key: K, value: V): MultimapEntry<K, V>[]
}
export class ArrayListMultimap<K, V> implements Multimap<K, V> {
private _entries: MultimapEntry<K, V>[] = []
public clear(): void {
this._entries = []
}
public containsKey(key: K): boolean {
return this._entries
.filter(entry => entry.key == key)
.length > 0
}
public containsValue(value: V): boolean {
return this._entries
.filter(entry => entry.value == value)
.length > 0
}
public containsEntry(key: K, value: V): boolean {
return this._entries
.filter(entry => entry.key == key && entry.value == value)
.length > 0
}
public delete(key: K, value?: V): boolean {
let temp = this._entries
this._entries = this._entries
.filter(entry => {
if(value)
return entry.key != key || entry.value != value
return entry.key != key
})
return temp.length != this._entries.length
}
public get entries(): MultimapEntry<K, V>[] {
return this._entries
}
public get(key: K): V[] {
return this._entries
.filter(entry => entry.key == key)
.map(entry => entry.value)
}
public keys(): K[] {
return Array.from(new Set(this._entries.map(entry => entry.key)))
}
public put(key: K, value: V): MultimapEntry<K, V>[] {
this._entries.push(new MultimapEntry(key, value))
return this._entries
}
}
class MultimapEntry<K, V> {
constructor(readonly key: K, readonly value: V) {}
}
/*
MIT License
Copyright (c) 2023 Jordan Luyke
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
@victor-gallet
Copy link

Thank you for sharing!

@jordanluyke
Copy link
Author

😄

@pombredanne
Copy link

Hey! what the license for this? Thanks!

@jordanluyke
Copy link
Author

@pombredanne I've added MIT license

@pombredanne
Copy link

@jordanluyke Thank you ++ for this quick response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment