Skip to content

Instantly share code, notes, and snippets.

View desperius's full-sized avatar
🙃
Weeks of coding can save you hours of planning

desperius

🙃
Weeks of coding can save you hours of planning
View GitHub Profile
@desperius
desperius / singly_linked_list.cpp
Last active March 21, 2023 13:28
Simple implementation of Singly Linked List.
#include <utility>
class SinglyLinkedList
{
public:
SinglyLinkedList() = default;
SinglyLinkedList(const SinglyLinkedList& that)
{
if (Node* that_c = that.head)
{
@desperius
desperius / thread_pool.cpp
Last active May 15, 2019 09:52
Simple implementation of a thread pool.
#include <thread>
#include <atomic>
#include <functional>
#include <vector>
#include "threadsafe_queue"
class join_threads
{
public:
@desperius
desperius / threadsafe_queue.cpp
Last active May 7, 2019 09:10
Threadsafe implementation of a queue with possibility to wait until appear at least one element in it.
#include <queue>
#include <mutex>
#include <memory>
#include <condition_variable>
template<class T>
class threadsafe_queue
{
public:
threadsafe_queue() {}
@desperius
desperius / spinlock_mutex.cpp
Created April 23, 2019 09:28
Implementation of a spinlock mutex using std::atomic_flag.
#include <atomic>
class spinlock_mutex
{
spinlock_mutex() : m_flag(ATOMIC_FLAG_INIT) {}
void lock()
{
// Function returns true if flag was set before the call and false otherwice.
while(m_flag.test_and_set(std::memory_order_acquire));
@desperius
desperius / threadsafe_stack.cpp
Last active April 22, 2019 11:54
Thread safe implementation of stack.
#include <exception>
#include <stack>
#include <mutex>
#include <memory>
struct empty_stack : std::exception
{
const char* what() const noexcept override;
};
@desperius
desperius / threadsafe_stack.cpp
Created April 20, 2019 17:47
Thread safe implementation of stack.
#include <exception>
#include <stack>
#include <mutex>
#include <memory>
struct empty_stack : std::exception
{
const char* what() const noexcept;
};
@desperius
desperius / modify_bit_via_operator.cpp
Last active September 5, 2018 13:03
If you want overload operator[] in such way that you can modify specific bit of a byte.
#include <iostream>
#include <bitset>
struct bitarr
{
bitarr(unsigned char v) : val(v) {}
void print()
{
std::bitset<8 * sizeof(unsigned char)> bits(val);
@desperius
desperius / hash_for_structure.cpp
Created May 13, 2017 18:06
Hash function for custom structure
#include <iostream>
#include <string>
#include <functional>
template <class T>
inline void hash_combined(std::size_t& res, const T& val)
{
std::hash<T> h;
res ^= h(val) + 0x9e3779b9 + (res << 6) + (res >> 2);
}
@desperius
desperius / string.cpp
Last active May 13, 2017 17:56
Custom string class for SW Certification
class Str
{
public:
Str();
Str(const char* src);
~Str();
Str(const Str& that);
Str& operator= (const Str& that);
@desperius
desperius / sequence.cpp
Last active May 13, 2017 17:52
All sequences of N-place number in range [1, ... , M]. Complexity: M ^ N
#include <iostream>
int n = 0;
int m = 0;
int* arr;
void increse(int index)
{
if (!(index < n))