-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix handling of null and undefined in EdgeMap #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ import { SparseEdgeMap } from './SparseEdgeMap'; | |
export class SingletonEdgeMap<T> extends AbstractEdgeMap<T> { | ||
|
||
private key: number; | ||
private value: T; | ||
private value: T | undefined; | ||
|
||
constructor(minIndex: number, maxIndex: number, key: number, value: T) { | ||
super(minIndex, maxIndex); | ||
|
@@ -49,15 +49,15 @@ export class SingletonEdgeMap<T> extends AbstractEdgeMap<T> { | |
this.value = value; | ||
} else { | ||
this.key = 0; | ||
this.value = null; | ||
this.value = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 I know the value is already undefined. Does it help clarity to keep this assignment here and/or do we gain a performance benefit by removing the assignment? |
||
} | ||
} | ||
|
||
getKey(): number { | ||
return this.key; | ||
} | ||
|
||
getValue(): T { | ||
getValue(): T | undefined { | ||
return this.value; | ||
} | ||
|
||
|
@@ -77,12 +77,12 @@ export class SingletonEdgeMap<T> extends AbstractEdgeMap<T> { | |
} | ||
|
||
@Override | ||
get(key: number): T { | ||
get(key: number): T | undefined { | ||
if (key === this.key) { | ||
return this.value; | ||
} | ||
|
||
return null; | ||
return undefined; | ||
} | ||
|
||
@Override | ||
|
@@ -132,10 +132,11 @@ export class SingletonEdgeMap<T> extends AbstractEdgeMap<T> { | |
|
||
@Override | ||
entrySet(): Iterable<{ key: number, value: T }> { | ||
if (this.isEmpty()) { | ||
let value: T | undefined = this.value; | ||
if (!value) { | ||
return []; | ||
} else { | ||
return [{ key: this.key, value: this.value }]; | ||
} | ||
|
||
return [{ key: this.key, value: value }]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,17 +88,17 @@ export class SparseEdgeMap<T> extends AbstractEdgeMap<T> { | |
|
||
@Override | ||
containsKey(key: number): boolean { | ||
return this.get(key) != null; | ||
return !!this.get(key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 The form seen here is idiomatic C++. Since JavaScript behaves the same way, would it be idiomatic JavaScript/TypeScript as well? Also, would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think |
||
} | ||
|
||
@Override | ||
get(key: number): T { | ||
get(key: number): T | undefined { | ||
// Special property of this collection: values are only even added to | ||
// the end, else a new object is returned from put(). Therefore no lock | ||
// is required in this method. | ||
let index: number = Arrays.binarySearch(this.keys, 0, this.size(), key); | ||
if (index < 0) { | ||
return null; | ||
return undefined; | ||
} | ||
|
||
return this.values[index]; | ||
|
@@ -221,7 +221,7 @@ class EntrySetIterator<T> implements Iterator<{ key: number, value: T }> { | |
|
||
next(value?: any): IteratorResult<{ key: number, value: T }> { | ||
if (this.index >= this.values.length) { | ||
return { done: true, value: undefined }; | ||
return { done: true, value: undefined } as any as IteratorResult<{ key: number, value: T }> ; | ||
} | ||
|
||
let currentIndex = this.index++; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 The only cases where a "nullable" value was passed to this method are in
ArrayEdgeMap
. Rather than make the parameterT | undefined
here, I updated the argument inArrayEdgeMap.put
to allow those cases specifically when working with the concrete implementationArrayEdgeMap
.