-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
319 additions
and
212 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
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,14 +1,49 @@ | ||
import pivotPosition from './pivotPosition'; | ||
|
||
export default function partialQuickSort<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, minIndex: number, maxIndex: number): void { | ||
if (left < right) { | ||
const pivotIndex = left + ((right - left) >> 1); | ||
const pivotNewIndex = pivotPosition(array, comparer, left, right, pivotIndex); | ||
|
||
if (pivotNewIndex < left + minIndex) { | ||
partialQuickSort(array, comparer, pivotNewIndex + 1, right, minIndex, maxIndex); | ||
} else if (pivotNewIndex > right + maxIndex) { | ||
partialQuickSort(array, comparer, left, pivotNewIndex - 1, minIndex, maxIndex); | ||
do { | ||
let i = left; | ||
let j = right; | ||
let x = array[i + ((j - i) >> 1)]; | ||
do { | ||
while (i < array.length && comparer(x, array[i]) > 0) { | ||
i++; | ||
} | ||
|
||
while (j >= 0 && comparer(x, array[j]) < 0) { | ||
j--; | ||
} | ||
|
||
if (i > j) { | ||
break; | ||
} | ||
|
||
if (i < j) { | ||
const tmp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = tmp; | ||
} | ||
|
||
i++; | ||
j--; | ||
} while (i <= j); | ||
|
||
if (minIndex >= i) { | ||
left = i + 1; | ||
} else if (maxIndex <= j) { | ||
right = j - 1; | ||
} | ||
|
||
if (j - left <= right - i) { | ||
if (left < j) { | ||
partialQuickSort(array, comparer, left, j, minIndex, maxIndex); | ||
} | ||
|
||
left = i; | ||
} else { | ||
if (i < right) { | ||
partialQuickSort(array, comparer, i, right, minIndex, maxIndex); | ||
} | ||
|
||
right = j; | ||
} | ||
} | ||
} while (left < right); | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,51 @@ | ||
import pivotPosition from './pivotPosition'; | ||
export default function quickSelect<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, index: number): T { | ||
do { | ||
let i = left; | ||
let j = right; | ||
let x = array[i + ((j - i) >> 1)]; | ||
do { | ||
while (i < array.length && comparer(x, array[i]) > 0) { | ||
i++; | ||
} | ||
|
||
export default function quickSelect<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, n: number): T { | ||
while (true) { | ||
if (left === right) return array[left]; | ||
while (j >= 0 && comparer(x, array[j]) < 0) { | ||
j--; | ||
} | ||
|
||
const pivotIndex = left + ((right - left) >> 1); | ||
const pivotNewIndex = pivotPosition(array, comparer, left, right, pivotIndex); | ||
if (i > j) { | ||
break; | ||
} | ||
|
||
if (pivotNewIndex === n) return array[n]; | ||
if (i < j) { | ||
const tmp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = tmp; | ||
} | ||
|
||
if (pivotNewIndex > n) { | ||
right = pivotNewIndex - 1; | ||
i++; | ||
j--; | ||
} while (i <= j); | ||
|
||
if (i <= index) { | ||
left = i + 1; | ||
} else { | ||
left = pivotNewIndex + 1; | ||
right = j - 1; | ||
} | ||
} | ||
|
||
if (j - left <= right - i) { | ||
if (left < j) { | ||
right = j; | ||
} | ||
|
||
left = i; | ||
} else { | ||
if (i < right) { | ||
left = i; | ||
} | ||
|
||
right = j; | ||
} | ||
} while (left < right); | ||
|
||
return array[index]; | ||
} |
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,43 +1,28 @@ | ||
export default function memoize<TSource>(this: Iterable<TSource>): Iterable<TSource> { | ||
return new MemoizeIterator(this[Symbol.iterator]() as Iterator<TSource>); | ||
} | ||
|
||
class MemoizeIterator<TElement> implements Iterable<TElement> { | ||
private _buffer: TElement[] = []; | ||
|
||
private _bufferSize: number = 0; | ||
let buffer: TSource[] = []; | ||
let iterator = this[Symbol.iterator](); | ||
|
||
private _error: any; | ||
|
||
constructor(private _iterator: Iterator<TElement>) { | ||
} | ||
return { | ||
*[Symbol.iterator]() { | ||
yield* buffer; | ||
|
||
*[Symbol.iterator](): Iterator<TElement> { | ||
let index = 0; | ||
while (true) { | ||
if (index < this._bufferSize) { | ||
yield this._buffer[index]; | ||
} else { | ||
if (this._iterator != null) { | ||
let result: IteratorResult<TElement>; | ||
if (iterator != null) { | ||
while (true) { | ||
let result: IteratorResult<TSource>; | ||
try { | ||
result = this._iterator.next(); | ||
result = iterator.next(); | ||
} catch (e) { | ||
this._error = e; | ||
iterator = null; | ||
throw e; | ||
} | ||
if (result.done) { | ||
this._iterator = null; | ||
iterator = null; | ||
break; | ||
} | ||
this._bufferSize = this._buffer.push(result.value); | ||
buffer.push(result.value); | ||
yield result.value; | ||
} else { | ||
if (this._error) throw this._error; | ||
break; | ||
} | ||
} | ||
index++; | ||
} | ||
} | ||
} |
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,5 +1,15 @@ | ||
import EmptyPartition from '../internal/EmptyPartition'; | ||
import { argumentOutOfRange } from '../internal/errors'; | ||
import RepeatPartition from '../internal/RepeatPartition'; | ||
|
||
export default function repeat<TSource>(element: TSource, count?: number): Iterable<TSource> { | ||
return new RepeatPartition(element, count != null ? count : Infinity); | ||
export default function repeat<TSource>(element: TSource, count: number = Infinity): Iterable<TSource> { | ||
if (count < 0) { | ||
throw argumentOutOfRange('count'); | ||
} | ||
|
||
if (count == 0) { | ||
return new EmptyPartition<TSource>(); | ||
} | ||
|
||
return new RepeatPartition(element, count); | ||
} |
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
Oops, something went wrong.