Skip to content

Instantly share code, notes, and snippets.

@Verdagon
Created July 2, 2016 00:32
Show Gist options
  • Save Verdagon/e3279ac90399bc9a9eedf0e69afbe88b to your computer and use it in GitHub Desktop.
Save Verdagon/e3279ac90399bc9a9eedf0e69afbe88b to your computer and use it in GitHub Desktop.
template<typename T>
class LinkedList {
struct Node {
Node * previous;
Node * next;
T value;
};
Node * head = nullptr;
Node * tail = nullptr;
public:
class iterator {
Node * current;
public:
T operator*() {
return current->value;
}
bool operator==(const iterator & other) {
return current == other.current;
}
bool operator!=(const iterator & other) {
return current != other.current;
}
void operator++() {
current = current->next;
}
};
~LinkedList() {
while (!empty()) {
erase(begin());
}
}
iterator begin() {
return iterator(head);
}
iterator end() {
return iterator(nullptr);
}
void push_back(T value) {
Node * newNode = new Node{tail, nullptr, value};
if (tail) {
tail->next = newNode;
tail = tail->next;
} else {
head = tail = newNode;
}
}
void push_back(T && value) {
Node * newNode = new Node{tail, nullptr, std::move(value)};
if (tail) {
tail->next = newNode;
tail = tail->next;
} else {
head = tail = newNode;
}
}
template<typename ...ConstructorParams>
void emplace_back(ConstructorParams && ... allParams) {
tail->next = new Node{tail, nullptr, T(std::forward<ConstructorParams>(allParams)...)};
tail = tail->next;
}
// expanded step 1
void emplace_back(Roster && && roster, const std::string & && str, int & && size, int * & && array) {
tail->next = new Node{tail, nullptr, T(std::forward(roster), std::forward(str), std::forward(size), std::forward(array))};
tail = tail->next;
}
// with the && and &s collapsed
void emplace_back(Roster && roster, const std::string & str, int & size, int * & array) {
tail->next = new Node{tail, nullptr, T(std::forward(roster), std::forward(str), std::forward(size), std::forward(array))};
tail = tail->next;
}
};
void myfunc(SpiggleWigget && b) {
SpiggleWigget a = std::move(b);
}
& && -> &
&& & -> &
& & -> &
&& && -> &&
class MyArrayListThing {
MyRoster roster;
std::string mystring;
int size;
int * array;
public:
MyArrayListThing(MyRoster && roster, const std::string & newstring, int a, int b) :
roster(roster),
mystring(newstring),
size(3),
array(new int[]{ b, c }) {
}
MyArrayListThing(MyArrayListThing && other) :
size(other.size),
array(other.array) {
other.size = 0;
other.array = nullptr;
}
};
template<typename ...AllParams>
void print(AllParams... allParams);
template<>
void print() {
std::cout << endl;
}
template<typename First, typename ...Rest>
void print(First first, Rest... rest) {
std::cout << first;
print(rest...);
}
void moo(std::string & derp) {
}
std::string mystring = "lol";
moo(mystring);
std::string &
int main() {
LinkedList<MyArrayListThing> myList = someOtherList;
myList.push_back(MyArrayListThing(false, 4, 5));
myList.push_back(MyArrayListThing(true, 8, 9));
const std::string derp = "mystring";
myList.emplace_back(std::move(myRosterThatCameFromSomewhere), derp, 8, 9);
print("hello", 42);
for (iterator i = myList.begin(); i != myList.end(); i++) {
cout << *i << endl;
}
}
class SteeringWheel {
public:
boolean fuzzy;
SomeExternalResource * resource;
SteeringWheel(const SteeringWheel & other) {
fuzzy = other.fuzzy;
resource = new SomeExternalResource();
resource->copyContentsFrom(other.resource);
}
SteeringWheel(SteeringWheel && other) {
fuzzy = other.fuzzy;
resource = other.resource;
other.resource = NULL;
}
~SteeringWheel() {
if (resource != NULL) {
delete resource;
}
}
};
void doThings(SteeringWheel * derpWheel) {
// stuff
// change derpWheel's fuzzy to false
// now derpWheel dies
}
void makeMeAWheel(SteeringWheel ** uninitializedOHGODSteeringWheel) {
*uninitializedOHGODSteeringWheel = new SteeringWheel(true, new SomeExternalResource());
}
int main() {
SteeringWheel * coolWheel = NULL;
makeMeAWheel(&coolWheel);
doThings(coolWheel);
// myWheel dies here
}
void doThings(SteeringWheel derpWheel, function<void(float)> progressCallback) {
// stuff
// derpWheel fuzzy is still true
progressCallback(.1);
// derpWheel fuzzy is still true good
// change derpWheel's fuzzy to false
progressCallback(.2);
progressCallback(.4);
// now derpWheel dies
}
SteeringWheel makeMeAWheel() {
return SteeringWheel(true, new SomeExternalResource());
}
int main() {
SteeringWheel myWheel = makeMeAWheel();
// function<void(float)> has: SteeringWheel &
doThings(std::move(myWheel), [&](float progress){
myWheel.fuzzy = false;
cout << "Progress: " << progress * 100 << endl;
});
// myWheel dies here
}
[](int a, int b){ return a + b * 2; }
int sum = 0;
var myArray = [10, 20, 30];
goog.array.forEach(myArray, function(item) {
sum += item;
});
class Car {
const int a;
const SteeringWheel steeringWheel;
public:
Car(int a, boolean fuzzySteeringWheel) :
a(a),
steeringWheel(fuzzySteeringWheel) {
// other code
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment