Skip to content

Differences

Jeongho Nam edited this page Dec 3, 2016 · 5 revisions

Differences between C++

Let's talk about which differences exist between C++/STL and TypeScript-STL.

Naming convention

TypeScript-STL follows Camel notation in class name.

C++/STL TypeScript-STL
std::vector std.Vector
std::list std.List
std::deque std.Deque
std::queue std.Queue
std::priority_queue std.PriorityQueue
std::stack std.Stack

Unlike ordinary STL; C++/STL, TypeScript-STL is following Camel notation on class name. The first letter begins from capital letter and spaced word also begins from capital letter with concatenation.

Those naming notation (Camel) is also adjusted in Exception classes.

Map and Set containers have their own specific name following algorithm.

C++/STL TypeScript-STL
std::set std.TreeSet
std::multiset std.TreeMultiSet
std::unordered_set std.HashSet
std::unordered_multiset std.HashMultiSet
std::map std.TreeMap
std::multimap std.TreeMultiMap
std::unordered_map std.HashMap
std::unordered_multimap std.HashMultiMap

In pure JavaScript, Name of Map and Set are reserved for ES6 hash-map and hash-set containers. Thus unlike linear containers like Vector and List who are following same words of C++/STL, Set and Map containers' names are extremely different with C++/STL.

Their name is following their own mapping algorithm. TreeMap is a map container mapping key elements by tree algorithm. Of course, HashMap is a map container mapping key elements by hashing algorithm.

However, function names are following snake notation like C++/STL.

C++/STL TypeScript-STL
std::bind std.bind
std::find_if std.find_if
std::vector::push_back std.Vector.push_back
std::map::equal_range std.TreeMap.equal_range

Class name is following Camel notation, however, function name is following Snake notation which is being followed by C++/STL too.

Operator

Operator overriding.

C++/STL TypeScript-STL
operator< less
operator== equals

In C++, operator overriding in a class level is possible. Unlike C++, JavScript doesn't support the operator overriding, so we cannot do such thing like C++ in JavaScript. However, we need similar function for standard comparison to use STL containers. For an example, TreeMap requires comparison method for sorting and constructing a B+ Tree. Of course, many algorithms in STL also requires it, operator overriding.

To substitute the operator overriding, we promise a protocol method. Use less instead of operator< and use equal_to insteand of operator==.

std.Pair follows the operator overriding protocol.
namespace std
{
	export class Pair<First, Second>
	{
		public less<U1 extends T1, U2 extends T2>(pair: Pair<U1, U2>): boolean
		{
			if (std.equal_to(this.first, pair.first) == false)
				return std.less(this.first, pair.first);
			else
				return std.less(this.second, pair.second);
		}
	
		public equals<U1 extends T1, U2 extends T2>(pair: Pair<U1, U2>): boolean
		{
			return std.equal_to(this.first, pair.first) && std.equal_to(this.second, pair.second);
		}
	}
}

Instead of using comparison operator.

C++/STL | TypeScript-STL Global Method | TypeScript-STL Member Method ----|--------------------------- A < B | std.less(A, B) | A.less(B) A == B | std.equal_to(A, B) | A.equal_to(B) A <= B | std.less_equal(A, B) | A.less(B) OR A.less(B) A > B | std.greater(A, B) | !A.less(B) AND !A.equal_to(B) A >= B | std.greater_equal(A, B) | !A.less(B) A != B | std.not_equal_to(A, B) | !A.equal_to(B)

We promised a method protocol for operator overriding. To keep it, we don't have to use local operator symbol like == to objects. Instead of the A == B, use std.equal_to(A, B) or A.equal_to(B).

Use promised methods
// TWO PAIRS HAVE SAME VALUE
let pair1 = std.make_pair("samchon", "Jeongho Nam");
let pair2 = std.make_pair("samchon", "Jeongho Nam");

// VALIDATE
console.log(pair1 == pair2); // false, DON'T USE IT.
console.log(pair.equals(pair2)); // true, RECOMMENDED
console.log(std.equal_to(pair2)); // true, IT'S OK

Iterator

Operators
C++/STL TypeScript-STL
operator== equals
operator* value
operator-- prev
operator++ next
C++ Style Iteration
std::vector<int> int_array(5, 1);

for (auto it = int_array.begin(); it != int_array.end(); it++)
	std::cout << *it << std::endl;
TypeScript Style Iteration
let intArray = new std.Vector<number>(5, 1);

// for (auto it = intArray.begin(); it != intArray.end(); it++)
for (let it = intArray.begin(); !it.equals(intArray.end()); it = it.next())
	console.log(it.value); // std::cout << *it << std::endl;
Advance Returns
C++/STL TypeScript-STL
it++; it = it.next();
next(it); it = it.next();
advance(it, 5); it = it.advance(5);

Tree Container, Sequence Template Parameter

C++, custom comparison function in template parameter
std::map<int, std::string, std::greater<std::string>> default_map;
std::map<int, std::string, std::greater<std::string>> assigned_map(default_map.begin(), default_map.end());

####### TypeScript, custom comparison function in tail parameter

let defaultMap = new std.TreeMap<string, number>(std.greater);
let assignedMap = new std.TreeMap<string, number>(defaultMap.begin(), defaultMap.end(), std.greater);

Hash Container, Custom Hash Function

C++, custom hash function in template parameter
template <typename Key, typename T>
class Entry
{
private:
	Key key;
	T value;
	
public:
	// MUST BE SPECIFIED IN TEMPLATE PARAMETER
	static size_t hashCode()
	{
		return std::hash(key);
	}
};

std::unordered_set<Entry, Entry::hash> entrySet;
TypeScript, custom hash function in member function
class Entry<Key, T>
{
	private key: Key;
	private value: T;

	// WHEN MEMBER FUNCTION {hash()} is defined, then be used automatically.
	public hashCode(): number
	{
		return std.hash(key);
	}
}

let entrySet = new std.HashMap<Entiry>();