Skip to content

Commit

Permalink
Test #46 and improve #53
Browse files Browse the repository at this point in the history
  • Loading branch information
Samchon committed Dec 24, 2019
1 parent a13d64e commit a27f503
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 202 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "samchon@samchon.org",
"url": "http://samchon.org"
},
"version": "2.4.0-dev.20191223",
"version": "2.4.0-dev.20191224",
"main": "./index.js",
"typings": "./index.d.ts",
"scripts": {
Expand Down
7 changes: 4 additions & 3 deletions src/algorithm/binary_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//================================================================
import { IForwardIterator } from "../iterator/IForwardIterator";
import { IPointer } from "../functional/IPointer";
import { Pair } from "../utility/Pair";

import { distance, advance } from "../iterator/global";
import { less } from "../functional/comparators";
import { Pair } from "../utility/Pair";

/**
* @hidden
Expand Down Expand Up @@ -107,9 +107,10 @@ export function equal_range<ForwardIterator extends Readonly<IForwardIterator<IP
comp: Comparator<ForwardIterator> = less
): Pair<ForwardIterator, ForwardIterator>
{
let it: ForwardIterator = lower_bound(first, last, val, comp);
first = lower_bound(first, last, val, comp);
let second: ForwardIterator = upper_bound(first, last, val, comp);

return new Pair(it, upper_bound(it, last, val, comp));
return new Pair(first, second);
}

/**
Expand Down
18 changes: 6 additions & 12 deletions src/algorithm/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function inplace_merge<BidirectionalIterator extends General<IBidirection
): void
{
let vector: Vector<IPointer.ValueType<BidirectionalIterator>> = new Vector();
merge(first, middle, middle, last, <Temporary>back_inserter(vector), comp);
merge(first, middle, middle, last, back_inserter(vector), comp);

copy(vector.begin(), vector.end(), first);
}
Expand Down Expand Up @@ -203,26 +203,20 @@ export function set_intersection<
comp: Comparator<InputIterator1> = less
): OutputIterator
{
while (true)
{
if (first1.equals(last1))
return copy(<Temporary>first2, last2, output);
else if (first2.equals(last2))
return copy(first1, last1, output);

while (!first1.equals(last1) && !first2.equals(last2))
if (comp(first1.value, first2.value))
first1 = first1.next();
else if (comp(first2.value, first1.value))
first2 = first2.next();
else
{// equals
else
{
output.value = first1.value;

output = output.next();
first1 = first1.next();
first2 = first2.next();
}
}
return output;
}

/**
Expand Down Expand Up @@ -263,7 +257,7 @@ export function set_difference<
first1 = first1.next();
first2 = first2.next();
}

// return output;
return copy(first1, last1, output);
}

Expand Down
8 changes: 8 additions & 0 deletions src/base/disposable/IForwardContainer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//================================================================
/** @module std.base */
//================================================================
import { _ISize } from "./IPartialContainers";
import { IForwardIterator } from "../../iterator/IForwardIterator";
import { IPointer } from "../../functional";
import { Vector } from "../../container/Vector";
Expand Down Expand Up @@ -39,4 +40,11 @@ export namespace IForwardContainer

export type SimilarType<Container extends Array<any> | IForwardContainer<any>>
= Array<ValueType<Container>> | IForwardContainer<IForwardIterator<ValueType<Container>, any>>;

export type ISizable<Iterator extends IForwardIterator<IPointer.ValueType<Iterator>, Iterator>> = IForwardContainer<Iterator> & _ISize;
export namespace ISizable
{
export type SimilarType<Container extends Array<any> | ISizable<any>>
= Array<ValueType<Container>> | ISizable<IForwardIterator<ValueType<Container>, any>>;
}
}
18 changes: 11 additions & 7 deletions src/ranges/algorithm/iterations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import base = require("../../algorithm/iterations");
import { IForwardContainer } from "../../base/disposable/IForwardContainer";
import { Pair } from "../../utility/Pair";
import { begin, end } from "../../iterator/factory";
import { size } from "../../iterator/global";
import { equal_to, less } from "../../functional/comparators";

import { Temporary } from "../../base/Temporary";
Expand Down Expand Up @@ -77,21 +78,24 @@ export function none_of<Range extends Array<any> | IForwardContainer<any>>
}

export function equal<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>>
Range1 extends Array<any> | (IForwardContainer.ISizable<any>),
Range2 extends IForwardContainer.ISizable.SimilarType<Range1>>
(range1: Range1, range2: Range2): boolean;

export function equal<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends Array<any> | IForwardContainer<any>>
Range1 extends Array<any> | IForwardContainer.ISizable<any>,
Range2 extends Array<any> | IForwardContainer.ISizable<any>>
(range1: Range1, range2: Range2, pred: BinaryPredicator<Range1, Range2>): boolean;

export function equal<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends Array<any> | IForwardContainer<any>>
Range1 extends Array<any> | IForwardContainer.ISizable<any>,
Range2 extends Array<any> | IForwardContainer.ISizable<any>>
(range1: Range1, range2: Range2, pred: BinaryPredicator<Range1, Range2> = <any>equal_to): boolean
{
return base.equal(begin(range1), end(range1), begin(range2), pred);
if (size(range1) !== size(range2))
return false;
else
return base.equal(begin(range1), end(range1), begin(range2), pred);
}

export function lexicographical_compare<
Expand Down
10 changes: 7 additions & 3 deletions src/ranges/algorithm/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IForwardIterator } from "../../iterator/IForwardIterator";

import { Writeonly } from "../../iterator/IFake";
import { begin, end } from "../../iterator/factory";
import { size } from "../../iterator/global";
import { less } from "../../functional/comparators";

import { Temporary } from "../../base/Temporary";
Expand Down Expand Up @@ -52,11 +53,14 @@ export function inplace_merge<Range extends Array<any> | IBidirectionalContainer
SET OPERATIONS
--------------------------------------------------------- */
export function includes<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>>
Range1 extends Array<any> | IForwardContainer.ISizable<any>,
Range2 extends IForwardContainer.ISizable.SimilarType<Range1>>
(range1: Range1, range2: Range2, comp: Comparator<Range1> = less): boolean
{
return base.includes(begin(range1), end(range1), <Temporary>begin(range2), end(range2), comp);
if (size(range1) < size(range2))
return false;
else
return base.includes(begin(range1), end(range1), <Temporary>begin(range2), end(range2), comp);
}

export function set_union<
Expand Down
60 changes: 30 additions & 30 deletions src/test/algorithm/test_binary_searches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function test_binary_searches(): void

function _Test_binary_search_of_atom(): void
{
let vec = new std.Vector<number>();
let set = new std.TreeMultiSet<number>();
let v: std.Vector<number> = new std.Vector();
let s: std.TreeMultiSet<number> = new std.TreeMultiSet();

//----
// FILL VALUES
Expand All @@ -19,12 +19,12 @@ function _Test_binary_search_of_atom(): void
{
let val: number = Math.random();

vec.push_back(val);
set.insert(val);
v.push_back(val);
s.insert(val);
}

// SORT VECTOR
std.sort(vec.begin(), vec.end());
std.ranges.sort(v);

//----
// VALIDATE
Expand All @@ -33,33 +33,33 @@ function _Test_binary_search_of_atom(): void
{
let val: number = Math.random();

let v_it = std.equal_range(vec.begin(), vec.end(), val);
let s_it = set.equal_range(val);
let v_it = std.ranges.equal_range(v, val);
let s_it = s.equal_range(val);

// VALIDATE LOWER BOUND
if (v_it.first.equals(vec.end()) === true)
if (s_it.first.equals(set.end()) === false)
throw new std.DomainError("Error std.lower_bound or Set.lower_bound; someone is out bound but the other is not.");
if (v_it.first.equals(v.end()) === true)
if (s_it.first.equals(s.end()) === false)
throw new Error("Bug on std.lower_bound() or Set.lower_bound(); someone is out bound but the other is not.");
else
continue;
else if (v_it.first.value !== s_it.first.value)
throw new std.DomainError("Error std.lower_bound or Set.lower_bound; different value.");
throw new Error("Bug on std.lower_bound() or Set.lower_bound(); different value.");

// VALIDATE UPPER BOUND
if (v_it.second.equals(vec.end()) === true)
if (s_it.second.equals(set.end()) === false)
throw new std.DomainError("Error std.upper_bound or Set.upper_bound; someone is out bound but the other is not.");
if (v_it.second.equals(v.end()) === true)
if (s_it.second.equals(s.end()) === false)
throw new Error("Bug on std.upper_bound() or Set.upper_bound(); someone is out bound but the other is not.");
else
continue;
else if (v_it.second.value !== s_it.second.value)
throw new std.DomainError("Error std.upper_bound or Set.upper_bound; different value.");
throw new Error("Bug on std.upper_bound() or Set.upper_bound(); different value.");
}
}

function _Test_binary_search_of_pair(): void
{
let vec = new std.Vector<std.Pair<number, number>>();
let map = new std.TreeMultiMap<number, number>();
let v: std.Vector<std.Pair<number, number>> = new std.Vector();
let m: std.TreeMultiMap<number, number> = new std.TreeMultiMap();

//----
// FILL VALUES
Expand All @@ -69,12 +69,12 @@ function _Test_binary_search_of_pair(): void
{
let pair = std.make_pair(Math.random(), 0);

vec.push_back(pair);
map.insert(pair);
v.push_back(pair);
m.insert(pair);
}

// SORT VECTOR
std.sort(vec.begin(), vec.end(), _Compare_numbers_pair);
std.ranges.sort(v, _Compare_numbers_pair);

//----
// VALIDATE
Expand All @@ -83,26 +83,26 @@ function _Test_binary_search_of_pair(): void
{
let pair = std.make_pair(Math.random(), 0);

let v_it = std.equal_range(vec.begin(), vec.end(), pair, _Compare_numbers_pair);
let m_it = map.equal_range(pair.first);
let v_it = std.ranges.equal_range(v, pair, _Compare_numbers_pair);
let m_it = m.equal_range(pair.first);

// VALIDATE LOWER BOUND
if (v_it.first.equals(vec.end()) === true)
if (m_it.first.equals(map.end()) === false)
throw new std.DomainError("Error std.lower_bound or Set.lower_bound; someone is out bound but the other is not.");
if (v_it.first.equals(v.end()) === true)
if (m_it.first.equals(m.end()) === false)
throw new Error("Bug on std.lower_bound or Set.lower_bound; someone is out bound but the other is not.");
else
continue;
else if (v_it.first.value.first !== m_it.first.first)
throw new std.DomainError("Error std.lower_bound or Set.lower_bound; different value.");
throw new Error("Bug on std.lower_bound or Set.lower_bound; different value.");

// VALIDATE UPPER BOUND
if (v_it.second.equals(vec.end()) === true)
if (m_it.second.equals(map.end()) === false)
throw new std.DomainError("Error std.upper_bound or Set.upper_bound; someone is out bound but the other is not.");
if (v_it.second.equals(v.end()) === true)
if (m_it.second.equals(m.end()) === false)
throw new Error("Bug on std.upper_bound or Set.upper_bound; someone is out bound but the other is not.");
else
continue;
else if (v_it.second.value.first !== m_it.second.first)
throw new std.DomainError("Error std.upper_bound or Set.upper_bound; different value.");
throw new Error("Bug on std.upper_bound or Set.upper_bound; different value.");
}
}

Expand Down
60 changes: 25 additions & 35 deletions src/test/algorithm/test_heaps.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,62 @@
import * as std from "../../index";
import { Generator } from "../internal/Generator";

export function test_heaps(): void
{
for (let i: number = 0; i < 100; ++i)
{
let elems: std.Vector<number> = _Create_elements();
std.make_heap(elems.begin(), elems.end());
let elems: std.Vector<number> = Generator.fill(new std.Vector(), 100);
std.ranges.make_heap(elems);

if (std.is_heap(elems.begin(), elems.end()) === false)
throw new std.DomainError("Error on std.push_heap() or std.is_heap().");
if (std.ranges.is_heap(elems) === false)
throw new Error("Bug on std.push_heap() or std.is_heap()");

std.sort_heap(elems.begin(), elems.end());
if (std.is_sorted(elems.begin(), elems.end()) === false)
throw new std.DomainError("Error on std.pop_heap().");
std.ranges.sort_heap(elems);
if (std.ranges.is_sorted(elems) === false)
throw new Error("Bug on std.pop_heap()");
}

_Test_c_plus_plus();
_Test_cpp_reference();
}

function _Create_elements(): std.Vector<number>
{
let ret: std.Vector<number> = new std.Vector();
let size: number = std.randint(20, 80);

for (let i: number = 0; i < size; ++i)
ret.push_back(Math.random() * 100.0);

return ret;
}

function _Test_c_plus_plus(): void
{
let v: std.Vector<number> = new std.Vector([10, 20, 30, 5, 15]);

std.make_heap(v.begin(), v.end());
if (v.front() !== 30 || std.is_heap(v.begin(), v.end()) === false)
throw new std.DomainError("Error on std.make_heap()");
std.ranges.make_heap(v);
if (v.front() !== 30 || std.ranges.is_heap(v) === false)
throw new Error("Bug on std.make_heap()");

std.pop_heap(v.begin(), v.end());
std.ranges.pop_heap(v);
v.pop_back();
if (v.front() !== 20)
throw new std.DomainError("Error on std.pop_heap()");
throw new Error("Bug on std.pop_heap()");

v.push_back(99);
std.push_heap(v.begin(), v.end());
std.ranges.push_heap(v);
if (v.front() !== 99)
throw new std.DomainError("Error on std.push_heap()");
throw new Error("Bug on std.push_heap()");

std.sort_heap(v.begin(), v.end());
if (std.is_sorted(v.begin(), v.end()) === false)
throw new std.DomainError("Error on std.sort_heap()");
std.ranges.sort_heap(v);
if (std.ranges.is_sorted(v) === false)
throw new Error("Bug on std.sort_heap()");
}

function _Test_cpp_reference(): void
{
let v: std.Vector<number> = new std.Vector();
v.push(3, 1, 4, 1, 5, 9);

std.make_heap(v.begin(), v.end());
if (std.equal(v.begin(), v.end(), std.begin(std.Vector.wrap([9, 5, 4, 1, 1, 3]))) === false)
throw new std.DomainError("Error on std.make_heap()");
std.ranges.make_heap(v);
if (std.ranges.equal(v, [9, 5, 4, 1, 1, 3]) === false)
throw new Error("Bug on std.make_heap()");

std.pop_heap(v.begin(), v.end());
std.ranges.pop_heap(v);
if (v.back() !== 9)
throw new std.DomainError("Error on std.pop_heap()");
throw new Error("Bug on std.pop_heap()");

v.pop_back();
if (std.equal(v.begin(), v.end(), std.begin(std.Vector.wrap([5, 3, 4, 1, 1]))) === false)
throw new std.DomainError("Error on std.pop_heap() & Vector.pop()");
if (std.ranges.equal(v, [5, 3, 4, 1, 1]) === false)
throw new Error("Bug on std.pop_heap() & Vector.pop()");
}
Loading

0 comments on commit a27f503

Please sign in to comment.