Skip to content

Instantly share code, notes, and snippets.

View rajkumar-p's full-sized avatar

Rajkumar rajkumar-p

View GitHub Profile
@rajkumar-p
rajkumar-p / add.cpp
Last active March 2, 2020 17:21
Add element to a coarse grained list
template<typename T>
bool List<T>::add(T elem)
{
NodeList<T> *node = new NodeList<T>(elem);
std::lock_guard<std::mutex> lock(this->_mux);
if (this->empty()) {
this->_head = node;
this->_tail = node;
@rajkumar-p
rajkumar-p / concurrent_list.hpp
Last active March 10, 2020 09:14
Types in a Concurrent List
template<typename T>
class Set {
public:
virtual bool add(T elem) = 0;
virtual bool remove(T elem) = 0;
virtual bool exists(T elem) = 0;
virtual ~Set();
};
@rajkumar-p
rajkumar-p / concurrent_list_client.cpp
Last active March 2, 2020 17:40
Client for a Concurrent List
int main() {
/*
* Single threaded version
*/
List<int> simple_list;
Set<int> &simple_list_ref = simple_list;
// Adding elements
std::cout << "1. Adding elements" << std::endl;
simple_list_ref.add(22);
@rajkumar-p
rajkumar-p / segments.c
Created November 21, 2019 15:32
Sample program to explain what variables get placed in what segments
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
extern char etext, edata, end;
int a = 1;
int b;
static int c = 1;
@rajkumar-p
rajkumar-p / common.hpp
Created September 14, 2019 15:01
Strategy Pattern Refactoring
void DiskJanitor::clean_files(const std::string &root)
{
_purger->iterate(root);
}
void FilesPurger::iterate(const std::string &root)
{
namespace fs = std::experimental::filesystem;
fs::path root_path(root);
if (fs::exists(root_path) && fs::is_directory(root_path)) {
@rajkumar-p
rajkumar-p / strategy.cpp
Created September 14, 2019 05:48
Strategy Pattern Usage
time_t now;
struct tm t;
time(&now);
t = *localtime(&now);
t.tm_mon = 0; t.tm_mday = 1;
t.tm_hour = 0; t.tm_min = 0; t.tm_sec = 0;
std::unique_ptr<FilesPurger> purger =
std::make_unique<ATFilesPurger>(mktime(&t));
@rajkumar-p
rajkumar-p / szfilespurger.cpp
Created September 14, 2019 05:45
SZFilesPurger - Strategy Design Pattern
void SZFilesPurger::purge(const std::string &root)
{
namespace fs = std::experimental::filesystem;
fs::path root_path(root);
if (fs::exists(root_path) && fs::is_directory(root_path)) {
for (const fs::directory_entry &entry :
fs::recursive_directory_iterator(root_path)) {
if (entry.is_directory()) {
continue;
}
@rajkumar-p
rajkumar-p / atfilespurger.cpp
Created September 14, 2019 05:42
ATFilesPurger - Strategy Design Pattern
void ATFilesPurger::purge(const std::string &root)
{
namespace fs = std::experimental::filesystem;
fs::path root_path(root);
if (fs::exists(root_path) && fs::is_directory(root_path)) {
for (const fs::directory_entry &entry :
fs::recursive_directory_iterator(root_path)) {
if (entry.is_directory()) {
continue;
}
@rajkumar-p
rajkumar-p / mtfilespurger.cpp
Created September 14, 2019 05:33
MTFilesPurger - Strategy Design Pattern
void MTFilesPurger::purge(const std::string &root)
{
namespace fs = std::experimental::filesystem;
fs::path root_path(root);
if (fs::exists(root_path) && fs::is_directory(root_path)) {
for (const fs::directory_entry &entry :
fs::recursive_directory_iterator(root_path)) {
if (entry.is_directory()) {
continue;
}
@rajkumar-p
rajkumar-p / purgers.hpp
Created September 14, 2019 05:17
Purges - Strategy Pattern
class MTFilesPurger : public FilesPurger {
private:
time_t _rel_time;
public:
MTFilesPurger(const time_t rel_time);
void purge(const std::string &root) override;
};