-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EqualityComaprer for collections (Dictionary, HashSet) (#4)
- Loading branch information
Showing
56 changed files
with
1,019 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,89 @@ | ||
import { MapEnumerable } from '../enumerables/MapEnumerable'; | ||
import { EqualityComparer } from '../comparers/EqualityComparer'; | ||
import { Errors } from '../Errors'; | ||
import { Pair } from '../models/Pair'; | ||
|
||
import { Enumerable } from './Enumerable'; | ||
import { DictionaryAbstraction } from './internal/DictionaryAbstraction'; | ||
import { EqualityComparerDictionary } from './internal/EqualityComparerDictionary'; | ||
import { SimpleDictionary } from './internal/SimpleDictionary'; | ||
|
||
/** | ||
* Represents a read-only collection of keys and values. Values can be accessed by keys. | ||
*/ | ||
// @ts-ignore | ||
export class ReadOnlyDictionary<TKey, TValue> extends MapEnumerable<TKey, TValue> { | ||
public constructor(source?: Iterable<Pair<TKey, TValue>>) { | ||
super(ReadOnlyDictionary.getSourceMap(source)); | ||
export class ReadOnlyDictionary<TKey, TValue> extends Enumerable<Pair<TKey, TValue>> { | ||
protected readonly innerDictionary: DictionaryAbstraction<TKey, TValue>; | ||
|
||
public constructor(); | ||
public constructor(source?: Iterable<Pair<TKey, TValue>>); | ||
public constructor(comparer?: EqualityComparer<TKey>); | ||
public constructor(source?: Iterable<Pair<TKey, TValue>>, comparer?: EqualityComparer<TKey>); | ||
public constructor(a?: Iterable<Pair<TKey, TValue>> | EqualityComparer<TKey>, b?: EqualityComparer<TKey>) { | ||
super(); | ||
|
||
let source: Iterable<Pair<TKey, TValue>> | undefined; | ||
let comparer: EqualityComparer<TKey> | undefined; | ||
|
||
if (a instanceof EqualityComparer) { | ||
source = undefined; | ||
comparer = a; | ||
} else { | ||
source = a; | ||
comparer = b; | ||
} | ||
|
||
if (comparer == null) { | ||
this.innerDictionary = new SimpleDictionary<TKey, TValue>(); | ||
} else { | ||
this.innerDictionary = new EqualityComparerDictionary<TKey, TValue>(comparer); | ||
} | ||
|
||
if (source != null) { | ||
for (const pair of source) { | ||
this.addInternal(pair.key, pair.value); | ||
} | ||
} | ||
} | ||
|
||
public get size(): number { | ||
return this.map.size; | ||
return this.innerDictionary.getSize(); | ||
} | ||
|
||
private static getSourceMap<TKey, TValue>(source?: Iterable<Pair<TKey, TValue>>): Map<TKey, Pair<TKey, TValue>> { | ||
const sourceEnumerable = Enumerable.from(source ?? []); | ||
const mapped = sourceEnumerable.select<[TKey, Pair<TKey, TValue>]>(pair => [pair.key, pair]); | ||
return new Map<TKey, Pair<TKey, TValue>>(mapped); | ||
public [Symbol.iterator](): Iterator<Pair<TKey, TValue>> { | ||
return this.innerDictionary[Symbol.iterator](); | ||
} | ||
|
||
public containsKey(key: TKey): boolean { | ||
return this.map.has(key); | ||
return this.innerDictionary.containsKey(key); | ||
} | ||
|
||
public get(key: TKey): TValue { | ||
if (!this.map.has(key)) { | ||
const pair = this.innerDictionary.getPair(key); | ||
if (pair == null) { | ||
throw Errors.keyNotInDictionary(); | ||
} | ||
|
||
// tslint:disable-next-line:no-non-null-assertion // TODO MV remove | ||
return this.map.get(key)!.value; | ||
return pair.value; | ||
} | ||
|
||
public getOrDefault(key: TKey): TValue | undefined { | ||
if (!this.map.has(key)) { | ||
return undefined; | ||
} | ||
|
||
// tslint:disable-next-line:no-non-null-assertion // TODO MV remove | ||
return this.map.get(key)!.value; | ||
const pair = this.innerDictionary.getPair(key); | ||
return pair?.value; | ||
} | ||
|
||
public keys(): Enumerable<TKey> { | ||
return Enumerable.from(this.map.keys()); | ||
return this.innerDictionary.keys(); | ||
} | ||
|
||
public values(): Enumerable<TValue> { | ||
return Enumerable.from(this.map.values()) | ||
.select(x => x.value); | ||
return this.innerDictionary.values(); | ||
} | ||
|
||
protected addInternal(key: TKey, value: TValue): void { | ||
if (this.containsKey(key)) { | ||
throw Errors.itemWithKeyAlreadyAdded(); | ||
} | ||
|
||
this.innerDictionary.set(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
import { SetEnumerable } from '../enumerables/SetEnumerable'; | ||
import { EqualityComparer } from '../comparers/EqualityComparer'; | ||
import { Errors } from '../Errors'; | ||
|
||
import { Enumerable } from './Enumerable'; | ||
import { EqualityComparerHashSet } from './internal/EqualityComparerHashSet'; | ||
import { HashSetAbstraction } from './internal/HashSetAbstraction'; | ||
import { SimpleHashSet } from './internal/SimpleHashSet'; | ||
|
||
/** | ||
* Represents a read-only set of values. | ||
*/ | ||
export class ReadOnlyHashSet<T> extends SetEnumerable<T> { | ||
public constructor(source?: Iterable<T>) { | ||
super(new Set(source)); | ||
export class ReadOnlyHashSet<T> extends Enumerable<T> { | ||
protected readonly internalHashSet: HashSetAbstraction<T>; | ||
|
||
public constructor(); | ||
public constructor(source?: Iterable<T>); | ||
public constructor(comparer?: EqualityComparer<T>); | ||
public constructor(source?: Iterable<T>, comparer?: EqualityComparer<T>); | ||
public constructor(a?: Iterable<T> | EqualityComparer<T>, b?: EqualityComparer<T>) { | ||
super(); | ||
|
||
let source: Iterable<T> | undefined; | ||
let comparer: EqualityComparer<T> | undefined; | ||
|
||
if (a instanceof EqualityComparer) { | ||
source = undefined; | ||
comparer = a; | ||
} else { | ||
source = a; | ||
comparer = b; | ||
} | ||
|
||
if (comparer == null) { | ||
this.internalHashSet = new SimpleHashSet<T>(); | ||
} else { | ||
this.internalHashSet = new EqualityComparerHashSet<T>(comparer); | ||
} | ||
|
||
if (source != null) { | ||
for (const element of source) { | ||
this.addInternal(element); | ||
} | ||
} | ||
} | ||
|
||
public [Symbol.iterator](): Iterator<T> { | ||
return this.internalHashSet[Symbol.iterator](); | ||
} | ||
|
||
public get size(): number { | ||
return this.source.size; | ||
return this.internalHashSet.getSize(); | ||
} | ||
|
||
public contains(element: T): boolean { | ||
return this.source.has(element); | ||
return this.internalHashSet.contains(element); | ||
} | ||
|
||
protected addInternal(element: T): void { | ||
if (this.contains(element)) { | ||
throw Errors.elementAlreadyAdded(); | ||
} | ||
|
||
this.internalHashSet.set(element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Pair } from '../../models/Pair'; | ||
import { Enumerable } from '../Enumerable'; | ||
|
||
export interface DictionaryAbstraction<TKey, TValue> { | ||
[Symbol.iterator](): Iterator<Pair<TKey, TValue>>; | ||
|
||
containsKey(key: TKey): boolean; | ||
getPair(key: TKey): Pair<TKey, TValue> | undefined; | ||
getSize(): number; | ||
keys(): Enumerable<TKey>; | ||
values(): Enumerable<TValue>; | ||
|
||
clear(): void; | ||
remove(key: TKey): boolean; | ||
set(key: TKey, value: TValue): void; | ||
} |
Oops, something went wrong.