-
-
Notifications
You must be signed in to change notification settings - Fork 49
Differences
Let's talk about which differences exist between C++/STL and TypeScript-STL.
Unlike ordinary STL (C++ STL), TypeScript-STL supports not only snake notation but also camel notation on class naming.
- Snake Notation: https://en.wikipedia.org/wiki/Snake_case
- Camel Notation: https://en.wikipedia.org/wiki/Camel_case
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 |
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.
Unllike the classses' naming, methods and functions, they're following only snake notation.
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==
.
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);
}
}
}
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)
.
// 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
C++/STL | TypeScript-STL |
---|---|
Iterator::operator== | Iterator.equals |
Iterator::operator* | Iterator.value |
Iterator::operator-- | Iterator.prev |
Iterator::operator++ | Iterator.next |
std::vector<int> int_array(5, 1);
for (auto it = int_array.begin(); it != int_array.end(); it++)
std::cout << *it << std::endl;
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;
C++/STL | TypeScript-STL |
---|---|
it++; |
it = it.next(); |
next(it); |
it = it.next(); |
advance(it, 5); |
it = it.advance(5); |
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());
let defaultMap = new std.TreeMap<string, number>(std.greater);
let assignedMap = new std.TreeMap<string, number>(defaultMap.begin(), defaultMap.end(), std.greater);
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;
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>();