Skip to content

Commit

Permalink
Fixing #83, Fix #82 and Fix #84
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongho Nam committed Dec 26, 2020
1 parent 479e4ce commit 1480fbd
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 207 deletions.
16 changes: 8 additions & 8 deletions container/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IHashMap } from "../base/container/IHashMap";
import { IHashContainer } from "../internal/container/associative/IHashContainer";

import { MapElementList } from "../internal/container/associative/MapElementList";
import { MapHashBuckets } from "../internal/hash/MapHashBuckets";
import { HashBuckets } from "../internal/hash/HashBuckets";

import { IForwardIterator } from "../iterator/IForwardIterator";
import { IPair } from "../utility/IPair";
Expand All @@ -32,7 +32,7 @@ export class HashMap<Key, T>
HashMap.ReverseIterator<Key, T>>
implements IHashMap<Key, T, true, HashMap<Key, T>>
{
private buckets_!: MapHashBuckets<Key, T, true, HashMap<Key, T>>;
private buckets_!: HashBuckets<Key, HashMap.Iterator<Key, T>>;

/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
Expand Down Expand Up @@ -93,7 +93,7 @@ export class HashMap<Key, T>
this, HashMap,
(hash, pred) =>
{
this.buckets_ = new MapHashBuckets(this as HashMap<Key, T>, hash, pred);
this.buckets_ = new HashBuckets(it => it.first, hash, pred);
},
...args
);
Expand All @@ -118,11 +118,10 @@ export class HashMap<Key, T>
public swap(obj: HashMap<Key, T>): void
{
// SWAP CONTENTS
[this.data_, obj.data_] = [obj.data_, this.data_];
MapElementList._Swap_associative(this.data_ as Temporary, obj.data_ as Temporary);
[this.data_, obj.data_] = [obj.data_, this.data_];

// SWAP BUCKETS
MapHashBuckets._Swap_source(this.buckets_, obj.buckets_);
[this.buckets_, obj.buckets_] = [obj.buckets_, this.buckets_];
}

Expand All @@ -138,7 +137,8 @@ export class HashMap<Key, T>
*/
public find(key: Key): HashMap.Iterator<Key, T>
{
return this.buckets_.find(key);
const it: HashMap.Iterator<Key, T> | null = this.buckets_.find(key);
return (it !== null) ? it : this.end();
}

/**
Expand Down Expand Up @@ -210,7 +210,7 @@ export class HashMap<Key, T>
*/
public bucket_count(): number
{
return this.buckets_.length();
return this.buckets_.row_size();
}

/**
Expand Down Expand Up @@ -250,7 +250,7 @@ export class HashMap<Key, T>
*/
public bucket(key: Key): number
{
return this.hash_function()(key) % this.buckets_.length();
return this.hash_function()(key) % this.buckets_.row_size();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions container/HashMultiMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IHashMap } from "../base/container/IHashMap";
import { IHashContainer } from "../internal/container/associative/IHashContainer";

import { MapElementList } from "../internal/container/associative/MapElementList";
import { MapHashBuckets } from "../internal/hash/MapHashBuckets";
import { HashBuckets } from "../internal/hash/HashBuckets";

import { NativeArrayIterator } from "../internal/iterator/disposable/NativeArrayIterator";
import { IForwardIterator } from "../iterator/IForwardIterator";
Expand All @@ -32,7 +32,7 @@ export class HashMultiMap<Key, T>
HashMultiMap.ReverseIterator<Key, T>>
implements IHashMap<Key, T, false, HashMultiMap<Key, T>>
{
private buckets_!: MapHashBuckets<Key, T, false, HashMultiMap<Key, T>>;
private buckets_!: HashBuckets<Key, HashMultiMap.Iterator<Key, T>>;

/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
Expand Down Expand Up @@ -93,7 +93,7 @@ export class HashMultiMap<Key, T>
this, HashMultiMap,
(hash, pred) =>
{
this.buckets_ = new MapHashBuckets(this as HashMultiMap<Key, T>, hash, pred);
this.buckets_ = new HashBuckets(it => it.first, hash, pred);
},
...args
);
Expand All @@ -118,11 +118,10 @@ export class HashMultiMap<Key, T>
public swap(obj: HashMultiMap<Key, T>): void
{
// SWAP CONTENTS
[this.data_, obj.data_] = [obj.data_, this.data_];
MapElementList._Swap_associative(this.data_ as Temporary, obj.data_ as Temporary);
[this.data_, obj.data_] = [obj.data_, this.data_];

// SWAP BUCKETS
MapHashBuckets._Swap_source(this.buckets_, obj.buckets_);
[this.buckets_, obj.buckets_] = [obj.buckets_, this.buckets_];
}

Expand All @@ -138,7 +137,8 @@ export class HashMultiMap<Key, T>
*/
public find(key: Key): HashMultiMap.Iterator<Key, T>
{
return this.buckets_.find(key);
const it: HashMultiMap.Iterator<Key, T> | null = this.buckets_.find(key);
return (it !== null) ? it : this.end();
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export class HashMultiMap<Key, T>
*/
public bucket_count(): number
{
return this.buckets_.length();
return this.buckets_.row_size();
}

/**
Expand Down Expand Up @@ -268,7 +268,7 @@ export class HashMultiMap<Key, T>
*/
public bucket(key: Key): number
{
return this.hash_function()(key) % this.buckets_.length();
return this.hash_function()(key) % this.buckets_.row_size();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions container/HashMultiSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IHashSet } from "../base/container/IHashSet";
import { IHashContainer } from "../internal/container/associative/IHashContainer";

import { SetElementList } from "../internal/container/associative/SetElementList";
import { SetHashBuckets } from "../internal/hash/SetHashBuckets";
import { HashBuckets } from "../internal/hash/HashBuckets";

import { IForwardIterator } from "../iterator/IForwardIterator";
import { BinaryPredicator } from "../internal/functional/BinaryPredicator";
Expand All @@ -28,7 +28,7 @@ export class HashMultiSet<Key>
HashMultiSet.ReverseIterator<Key>>
implements IHashSet<Key, false, HashMultiSet<Key>>
{
private buckets_!: SetHashBuckets<Key, false, HashMultiSet<Key>>;
private buckets_!: HashBuckets<Key, HashMultiSet.Iterator<Key>>;

/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
Expand Down Expand Up @@ -89,7 +89,7 @@ export class HashMultiSet<Key>
this, HashMultiSet,
(hash, pred) =>
{
this.buckets_ = new SetHashBuckets(this, hash, pred);
this.buckets_ = new HashBuckets(it => it.value, hash, pred);
},
...args
);
Expand All @@ -114,11 +114,10 @@ export class HashMultiSet<Key>
public swap(obj: HashMultiSet<Key>): void
{
// SWAP CONTENTS
[this.data_, obj.data_] = [obj.data_, this.data_];
SetElementList._Swap_associative(this.data_ as Temporary, obj.data_ as Temporary);
[this.data_, obj.data_] = [obj.data_, this.data_];

// SWAP BUCKETS
SetHashBuckets._Swap_source(this.buckets_, obj.buckets_);
[this.buckets_, obj.buckets_] = [obj.buckets_, this.buckets_];
}

Expand All @@ -134,7 +133,8 @@ export class HashMultiSet<Key>
*/
public find(key: Key): HashMultiSet.Iterator<Key>
{
return this.buckets_.find(key);
const it: HashMultiSet.Iterator<Key> | null = this.buckets_.find(key);
return (it !== null) ? it : this.end();
}

/**
Expand Down Expand Up @@ -224,7 +224,7 @@ export class HashMultiSet<Key>
*/
public bucket_count(): number
{
return this.buckets_.length();
return this.buckets_.row_size();
}

/**
Expand Down Expand Up @@ -264,7 +264,7 @@ export class HashMultiSet<Key>
*/
public bucket(key: Key): number
{
return this.hash_function()(key) % this.buckets_.length();
return this.hash_function()(key) % this.buckets_.row_size();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions container/HashSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IHashSet } from "../base/container/IHashSet";
import { IHashContainer } from "../internal/container/associative/IHashContainer";

import { SetElementList } from "../internal/container/associative/SetElementList";
import { SetHashBuckets } from "../internal/hash/SetHashBuckets";
import { HashBuckets } from "../internal/hash/HashBuckets";

import { IForwardIterator } from "../iterator/IForwardIterator";
import { Pair } from "../utility/Pair";
Expand All @@ -30,7 +30,7 @@ export class HashSet<Key>
HashSet.ReverseIterator<Key>>
implements IHashSet<Key, true, HashSet<Key>>
{
private buckets_!: SetHashBuckets<Key, true, HashSet<Key>>;
private buckets_!: HashBuckets<Key, HashSet.Iterator<Key>>;

/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
Expand Down Expand Up @@ -91,7 +91,7 @@ export class HashSet<Key>
this, HashSet,
(hash, pred) =>
{
this.buckets_ = new SetHashBuckets(this, hash, pred);
this.buckets_ = new HashBuckets(it => it.value, hash, pred);
},
...args
);
Expand All @@ -116,11 +116,10 @@ export class HashSet<Key>
public swap(obj: HashSet<Key>): void
{
// SWAP CONTENTS
[this.data_, obj.data_] = [obj.data_, this.data_];
SetElementList._Swap_associative(this.data_ as Temporary, obj.data_ as Temporary);
[this.data_, obj.data_] = [obj.data_, this.data_];

// SWAP BUCKETS
SetHashBuckets._Swap_source(this.buckets_, obj.buckets_);
[this.buckets_, obj.buckets_] = [obj.buckets_, this.buckets_];
}

Expand All @@ -136,7 +135,8 @@ export class HashSet<Key>
*/
public find(key: Key): HashSet.Iterator<Key>
{
return this.buckets_.find(key);
const it: HashSet.Iterator<Key> | null = this.buckets_.find(key);
return (it !== null) ? it : this.end();
}

/**
Expand Down Expand Up @@ -208,7 +208,7 @@ export class HashSet<Key>
*/
public bucket_count(): number
{
return this.buckets_.length();
return this.buckets_.row_size();
}

/**
Expand Down Expand Up @@ -248,7 +248,7 @@ export class HashSet<Key>
*/
public bucket(key: Key): number
{
return this.hash_function()(key) % this.buckets_.length();
return this.hash_function()(key) % this.buckets_.row_size();
}

/**
Expand Down
14 changes: 10 additions & 4 deletions internal/hash/HashBuckets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@
* @packageDocumentation
* @module std.internal
*/
import { BinaryPredicator } from "../functional/BinaryPredicator";
import { Hasher } from "../functional/Hasher";
/**
* Hash buckets
*
* @author Jeongho Nam - https://github.com/samchon
*/
export declare class HashBuckets<Key, Elem> {
private readonly fetcher_;
private readonly hasher_;
private readonly predicator_;
private readonly fetcher_;
private max_load_factor_;
private data_;
private size_;
constructor(fetcher: Fetcher<Key, Elem>, hasher: Hasher<Key>);
constructor(fetcher: Fetcher<Key, Elem>, hasher: Hasher<Key>, pred: BinaryPredicator<Key>);
clear(): void;
rehash(length: number): void;
reserve(length: number): void;
private initialize;
length(): number;
size(): number;
row_size(): number;
capacity(): number;
at(index: number): Elem[];
load_factor(): number;
max_load_factor(): number;
max_load_factor(z: number): void;
hash_function(): Hasher<Key>;
private index;
key_eq(): BinaryPredicator<Key>;
private index_by_key;
private index_by_value;
find(key: Key): Elem | null;
insert(val: Elem): void;
erase(val: Elem): void;
}
Expand Down
Loading

0 comments on commit 1480fbd

Please sign in to comment.