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

Classes

Unlike ordinary STL (C++ STL), TypeScript-STL supports not only snake notation but also camel notation on class naming.

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

However, associative containers, their camel names are something different.

In pure JavaScript, class name of Map and Set are reserved for ES6 features. To avoid the name confliction, TypeScript-STL's associative containers, their camel notated class names are representing their own mapping algorithm. For example, std.map's camel name is TreeMap because it is mapping key elements via the B+ Tree.

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

Methods, Functions

Unllike the classses' naming, methods and functions, they're following only snake notation.

Operator

Operator overriding.

C++ TypeScript-STL
Object::operator< IComparable.less
Object::operator== IComparable.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 equals insteand of operator==.

std.Pair follows the operator overriding protocol.
namespace std
{
	export class Pair<First, Second> implements IComparable<Pair<First, Second>>
	{
		public less(pair: Pair<First, Second>): 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(pair: Pair<First, Second>): boolean
		{
			return std.equal_to(this.first, pair.first) && std.equal_to(this.second, pair.second);
		}
	}
}

Instead of using comparison operator.

C++ Global Method Member Method
A < B std.less(A, B) A.less(B)
A == B std.equal_to(A, B) A.equals(B)
A <= B std.less_equal(A, B) A.less(B) OR A.equals(B)
A > B std.greater(A, B) !A.less(B) AND !A.equals(B)
A >= B std.greater_equal(A, B) !A.less(B)
A != B std.not_equal_to(A, B) !A.equals(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.equals(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(pair1, pair2)); // true, IT'S OK

Iterator

Operators
C++/STL TypeScript-STL
Iterator::operator== Iterator.equals
Iterator::operator* Iterator.value
Iterator::operator-- Iterator.prev
Iterator::operator++ Iterator.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);
	};
	
	bool operator==(const Entry<Key, T> &obj) const
	{
		return key == obj.key;
	};
};

std::unordered_set<Entry, Entry::hashCode> entrySet;
TypeScript, custom hash function in member function
class Entry<Key, T> implements std.IComparable<Entry<Key, T>>
{
	private key: Key;
	private value: T;
	
	// WHEN MEMBER FUNCTION {hash()Code} is defined, then be used automatically.
	public hashCode(): number
	{
		return std.hash(key);
	}
	
	// LESS AND EQUALS
	public less(obj: Entry<Key, T>)): boolean
	{
		return std.less(this.key, obj.key);
	}
	public equals(obj: Entry<Key, T>): boolean
	{
		return std.equal_to(this.key, obj.key);
	}
}

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