This file contains hidden or 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
// std::hash<std::vector<int>> | |
struct VectorHash { template<class T> | |
std::size_t operator()(vector<T> const &vec) const noexcept { | |
std::size_t ans = 0; | |
for (const auto& v: vec) { | |
ans ^= std::hash<T>{}(n) + (ans << 6) + 0x9e3779b9 + (ans >> 2); | |
} | |
return ans; | |
} | |
}; |
This file contains hidden or 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
/// Taken from here: https://stackoverflow.com/a/46354989/491239 | |
public | |
extension Array where Element : Hashable { | |
static func removeDuplicates(_ elements: [Element]) -> [Element] { | |
var seen = Set<Element>() return elements.filter { seen.insert($0).inserted } | |
} | |
} |
This file contains hidden or 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
// MARK - Heap | |
public struct Heap<Element: Equatable> { | |
var elements: [Element] | |
let sort: (Element, Element) -> Bool | |
public init(sort: @escaping (Element, Element) -> Bool, elements: [Element] = []) { | |
self.sort = sort | |
self.elements = elements | |
// heapify |
This file contains hidden or 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
struct PairHash { | |
template<class T1, class T2> | |
std::size_t operator()(std::pair<T1, T2> const & p) const noexcept { | |
std::size_t hash1 = std::hash<T1>{}(p.first); | |
std::size_t hash2 = std::hash<T2>{}(p.second); | |
return hash1 ^ (hash2 << 1); | |
}; | |
}; |
This file contains hidden or 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
const std::string WHITESPACE = " \n\r\t\f\v"; | |
std::string ltrim(const std::string &s) { | |
size_t start = s.find_first_not_of(WHITESPACE); | |
return (start == std::string::npos) ? "" : s.substr(start); | |
} | |
std::string rtrim(const std::string &s) { | |
size_t end = s.find_last_not_of(WHITESPACE); | |
return (end == std::string::npos) ? "" : s.substr(0, end + 1); | |
} | |
std::string trim(const std::string &s) { |
This file contains hidden or 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
extension String { | |
func matches(for regexPattern: String) -> [[String]] { | |
do { | |
let text = self | |
let regex = try NSRegularExpression(pattern: regexPattern) | |
let matches = regex.matches(in: text, | |
range: NSRange(text.startIndex..., in: text)) | |
return matches.map { match in | |
return (0..<match.numberOfRanges).map { | |
let rangeBounds = match.range(at: $0) |
This file contains hidden or 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
// | |
// YTBParser.swift | |
// SideloadExt | |
// | |
// Created by chen he on 2020/7/30. | |
// Copyright © 2020 chen he. All rights reserved. | |
// | |
import Foundation |
This file contains hidden or 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
template<typename T> | |
void split(const std::string &s, char delim, T output) { | |
std::istringstream iss(s); | |
std::string item; | |
while(getline(iss, item, delim)) { | |
*output++ = item; | |
} | |
} | |
std::vector<std::string> split(const std::string &s, char delim) { | |
std::vector<std::string> result; |
This file contains hidden or 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
- (integer_t)checkCPUUsage { | |
thread_act_array_t threads; | |
mach_msg_type_number_t threadCount = 0; | |
const task_t thisTask = mach_task_self_; | |
kern_return_t ret = task_threads(thisTask, &threads, &threadCount); | |
if (ret != KERN_SUCCESS) { | |
return 0; | |
} |
This file contains hidden or 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
<table> | |
<tr> | |
<td>Name</td> | |
<td>Iterations</td> | |
<td>Total time (sec)</td> | |
<td>Time per (ns)</td> | |
</tr> | |
<tr> | |
<td>16 byte memcpy</td> | |
<td>1000000000</td> |
NewerOlder