1
0
Fork 0

Compare commits

..

No commits in common. "3c197c647d2f7ee67cbcd02fa5f0668a1546a032" and "e363d023cdae4ebcd7ee5b0f46c2c12d8deaff94" have entirely different histories.

72 changed files with 146 additions and 704 deletions

View file

@ -1,12 +0,0 @@
#!/usr/bin/env python3
import numpy as np
import sys
N = 1e6
if len(sys.argv) > 1:
N = float(sys.argv[1])
for _ in range(int(N)):
print( np.random.rand(1) )

View file

@ -1,10 +1,4 @@
Running `time ./gen_rand.py "1e6"` gives
Running `time ./gen_rand.py "1e9"` gives
real 0m0.237s
user 0m0.160s
Contrast this to the following:
Running `time ./gen_rand_explicit.py "1e6"` gives
real 2m11.564s
user 2m1.950s
real 0m13.803s
user 0m8.874s

View file

@ -19,15 +19,15 @@ int main() {
unsigned int chars_capital = 0, chars_lower = 0, chars_digits = 0, chars_other = 0;
while ( *ptr != 0 )
{
if ( *ptr >= 'A' && *ptr <= 'Z' )
if ( *ptr >= int('A') && *ptr <= int('Z') )
{
++chars_capital;
}
else if ( *ptr >= 'a' && *ptr <= 'z' )
else if ( *ptr >= int('a') && *ptr <= int('z') )
{
++chars_lower;
}
else if ( *ptr >= '0' && *ptr <= '9' )
else if ( *ptr >= int('0') && *ptr <= int('9') )
{
++chars_digits;
}

View file

@ -39,9 +39,7 @@ char* join(const char* str1 , const char* str2)
char* joinb(const char* str1 , const char* str2)
{
// size should be 2 more than required for the strlens to
// incorporate the space and the null byte
int size = strlen(str1) + strlen(str2) + 2;
int size = strlen(str1) + strlen(str2) + 1;
char* new_str = new char[size];
*new_str = 0;

View file

@ -15,25 +15,12 @@
#define MEMORY_SIZE_IN_CHARS G
void make_throwaway_ptrs(int leak_size ) {
for ( int i = 0; i < leak_size ; i++)
{
char* throwaway_ptr = new char;
}
// oops, we are not returning the pointers
// this means it goes out of scope when the function ends
}
int main() {
make_throwaway_ptrs(MEMORY_SIZE_IN_CHARS);
char* throwaway_ptr = new char[MEMORY_SIZE_IN_CHARS];
std::cout << "We leaked " << MEMORY_SIZE_IN_CHARS * sizeof " " << " bits" << std::endl;
std::cout << "Ready to kill the process? (press Enter)" << std::endl;
std::cin.ignore();
return 0;
}

View file

@ -23,10 +23,10 @@
#include <iostream>
#include <cmath>
#define VERBOSE true
#define VERBOSE false
char b32_lookup_table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
int base = 32;
char b32_lookup_table[] = "ABCDEFGHIJKLMNOPQRSTUV";
int base = 8;
int main() {
//a
@ -75,7 +75,7 @@ int main() {
digits[digit_num] = number & lsb_bitmask;
// Shift off the bits we have put in to the array
number >>= bits_in_char;
number = ( number >> bits_in_char );
++digit_num;
}
@ -91,7 +91,14 @@ int main() {
// Loop the other way so the first digit we get out is MSB
while ( digit_num >= 0 )
{
std::cout << b32_lookup_table[digits[digit_num]];
if ( digits[digit_num] < 10 )
{
std::cout << digits[digit_num];
}
else
{
std::cout << b32_lookup_table[digits[digit_num] - 10];
}
digit_num--;
}

View file

@ -1,77 +0,0 @@
// ThreadPool.cc
#include "threadpool.hh"
#include "shared_queue.hh"
#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
void producer( const int id, SharedQueue<int>& q, int max_t, int max_n )
{
std::random_device d;
std::mt19937 mt(d());
std::uniform_int_distribution<> distr(0., max_t);
std::cout << " -- Starting Producer " << id << " (max_t: " << max_t << " max_n: " << max_n << ")" << std::endl;
for ( int n = 0; n < max_n ; ++n )
{
std::cout << " -- Producer " << id << "; n = " << n << std::endl;
int l = distr(mt);
std::this_thread::sleep_for(std::chrono::milliseconds(l));
// Lock q so we can push a new element
std::unique_lock<std::mutex> lock(*q.mutex());
q.queue()->push(l);
std::cout << " -- Producer " << id << " pushed = " << l << std::endl;
// Notify one of the consumers
q.cv()->notify_one();
}
std::cout << "Exiting Producer " << id << std::endl;
}
void consumer( const int id, SharedQueue<int>& q, int max_t, int max_n )
{
const int time_out = 5 ;// seconds
int b = 0;
std::cout << " -- Starting Consumer " << id << " (max_t: " << max_t << " max_n: " << max_n << ")" << std::endl;
for ( int n = 0; n < max_n ; n++ )
{
std::cout << " -- Consumer " << id << "; n = " << n << std::endl;
{ // Scope the lock of the queue
std::unique_lock<std::mutex> q_lock(*q.mutex());
while ( q.queue()->empty() ) {
// Wait until we get a signal from the condition variable
// or we reach a timeout
if ( q.cv()->wait_for(q_lock, std::chrono::seconds(time_out)) == std::cv_status::timeout )
{
// We reached the timeout
std::cout << " -- Consumer " << id << " timed out (" << time_out << "s)" << std::endl;
// this consumer does nothing after stopping the loop
// so we can return to break the loops
return;
}
}
// Get a value and pop it off
b = q.queue()->front();
q.queue()->pop();
}
std::cout << " -- Consumer " << id << " read: " << b << std::endl;
// Slow down this thread to have a race between the consumers and the producers
// This only works after we have successfully read an int
std::this_thread::sleep_for(std::chrono::milliseconds(max_t));
}
std::cout << "Exiting Consumer " << id << std::endl;
}

View file

@ -1,24 +0,0 @@
#include "shared_queue.hh"
#include "threadpool.hh"
#include <string>
#include <iostream>
int main() {
std::cout << "Set up ThreadPool" << std::endl;
ThreadPool tp;
std::cout << "Add Producer" << std::endl;
tp.addProducer();
std::cout << "Add Consumers" << std::endl;
tp.addConsumer();
tp.addConsumer();
std::cout << "Run" << std::endl;
tp.run();
std::cout << "Finishing up" << std::endl;
return 0;
}

View file

@ -1,20 +0,0 @@
#!/usr/bin/env python3
import ThreadPool
if __name__ == "__main__":
print(dir(ThreadPool))
tp = ThreadPool.ThreadPool()
print("Creating Consumers")
tp.addConsumer( 2000, 20 )
tp.addConsumer( 2000, 10 )
print("Create Producers")
tp.addProducer( 4000, 5 )
print("Run all Threads")
tp.run()
print("End of Program")

View file

@ -1,8 +0,0 @@
main:
g++ -pthread ThreadPool.cc main.cpp
threadpool:
g++ -pthread -D ENABLE_PYBIND=1 -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` ThreadPool.cc -o ThreadPool`python3-config --extension-suffix`
clean:
rm -rf *.so __pycache__

View file

@ -1,39 +0,0 @@
// Shared_Queue.hh
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <queue>
#ifndef SHARED_QUEUE_HH
#define SHARED_QUEUE_HH
template <class T>
class SharedQueue
{
public:
SharedQueue() :
_q(),
_q_mutex(),
_q_cv()
{};
SharedQueue( const SharedQueue& other )
{
std::cout << "Copy Constructor SharedQueue" << std::endl;
// (Read)Lock the other queue for copying
std::unique_lock<std::mutex> other_lock(other._q_mutex);
_q = other._q;
}
std::queue<T> * queue() { return& _q; }
std::mutex * mutex() { return& _q_mutex; }
std::condition_variable * cv() { return& _q_cv; }
private:
// Queue
std::queue<T> _q;
// Synchronisation
std::mutex _q_mutex;
std::condition_variable _q_cv;
};
#endif

View file

@ -1,79 +0,0 @@
// ThreadPool.hh
#include "shared_queue.hh"
#include <thread>
#include <vector>
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
// Implementations for producers and consumers
void producer( const int id, SharedQueue<int>& _q, int max_t, int max_n );
void consumer( const int id, SharedQueue<int>& _q, int max_t, int max_n );
class ThreadPool
{
public:
ThreadPool() :
_q(),
_consumers(),
_producers()
{};
void addProducer( int max_t = 3000, int max_n = 10)
{
std::thread * tp = new std::thread( producer, _producers.size(), std::ref(_q), max_t, max_n );
_producers.push_back(tp);
}
void addConsumer( int max_t = 2000, int max_n = 10)
{
std::thread * tp = new std::thread( consumer, _consumers.size(), std::ref(_q), max_t, max_n );
_consumers.push_back(tp);
}
void run()
{
// join all producers
for ( int i = 0; i < _producers.size() ; i++ )
{
(*_producers[i]).join();
}
// join all consumers
for ( int i = 0; i < _consumers.size() ; i++ )
{
(*_consumers[i]).join();
}
}
private:
// Consumer and Producers threads
std::vector<std::thread * > _consumers;
std::vector<std::thread * > _producers;
// queue
SharedQueue<int> _q;
};
#if ENABLE_PYBIND
/*
* Define Python access
*/
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(ThreadPool, tp)
{
py::class_<ThreadPool>( tp, "ThreadPool")
.def(py::init())
.def("addProducer", &ThreadPool::addProducer, "Add a new Producer to the Pool")
.def("addConsumer", &ThreadPool::addConsumer, "Add a new Consumer to the Pool")
.def("run", &ThreadPool::run, "Run all registered Producers and Consumers");
}
#endif
#endif

View file

@ -83,8 +83,8 @@ void orderptr( int* a, int* b ) {
return;
}
int tmp = *a;
*a = *b;
*b = tmp;
int* tmp = a;
a = b;
b = tmp;
}
#endif

View file

@ -15,9 +15,9 @@
void sort( const char** A, const int size );
#if !USE_PTR
void order( const char* &a, const char* &b );
void order( const char* a, const char* b );
#else
void orderptr( const char* *a, const char* *b );
void orderptr( const char* &a, const char* &b );
#endif
@ -78,14 +78,34 @@ void sort( const char** A, const int size ) {
#if !USE_PTR
order( A[ i ], A[ j ] );
#else
orderptr( &A[ i ], &A[ j ] );
orderptr( A[ i ], A[ j ] );
#endif
}
}
}
#if !USE_PTR
void order( const char* &a, const char* &b ) {
void order( const char* a, const char* b ) {
#if VERBOSE
std::cout << "Order: " << a << std::endl;
#endif
// b is greater or equal to a, do nothing
if ( strcmp(a, b) > 0) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = a;
a = b;
b = tmp;
}
#else
void orderptr( const char* &a, const char* &b ) {
#if VERBOSE
std::cout << "Order: " << a << ", " << b << std::endl;
#endif
@ -103,24 +123,4 @@ void order( const char* &a, const char* &b ) {
a = b;
b = tmp;
}
#else
void orderptr( const char* *a, const char* *b ) {
#if VERBOSE
std::cout << "Order: " << a << std::endl;
#endif
// b is greater or equal to a, do nothing
if ( strcmp(*a, *b) > 0) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = *a;
*a = *b;
*b = tmp;
}
#endif

View file

@ -3,14 +3,12 @@
*
* generalisation of type is possible
* => not writing an int implementation means always use the double
* => => even when two ints are supplied, they are generalised to doubles
* => => even when to ints are supplied, they are generalised to doubles
*/
#include <iostream>
int min( int a, int b );
double min( double a, int b );
double min( int a, double b );
//int min( int a, int b );
double min( double a, double b );
int min( int a[], int size);
@ -29,30 +27,15 @@ int main() {
std::cout << "min( 3.12345, 4 ) = " << min(3.12345, 4) << std::endl;
std::cout << "min( 4, 3.12345 ) = " << min(4, 3.12345) << std::endl;
return 0;
}
int min( int a, int b ) {
if ( a < b ) {
return a;
}
return b;
}
double min( double a, int b ) {
if ( a < b ) {
return a;
}
return b;
}
double min( int a, double b ) {
if ( a < b ) {
return a;
}
return b;
}
//int min( int a, int b ) {
// if ( a < b ) {
// return a;
// }
// return b;
//}
double min( double a, double b ) {
if ( a < b ) {
@ -70,5 +53,3 @@ int min( int a[], int size ) {
return m;
}
// Fix interchangeabiliy of doubles and ints

View file

@ -37,6 +37,7 @@ int main() {
double * nvec = multiply( ivec, Nx, mtx );
@ -48,17 +49,6 @@ int main() {
}
std::cout << "]" << std::endl;
delete nvec;
// print the matrix as it is stored in memory
std::cout << "In-Memory Matrix = ";
std::cout << "[" << std::endl;
for ( int i = 0; i < Nx*Y_SIZE; i++ ){
std::cout << mtx[0][i] << ", ";
}
std::cout << std::endl;
std::cout << "]" << std::endl;
}

View file

@ -4,6 +4,8 @@
void Stack::push(double c) {
if (full()) {
std::cout << "Stack::push() Error: stack is full" << std::endl ;
grow( DELTA );
}
s[count++] = c ;
@ -32,10 +34,7 @@ void Stack::init( int in_size ) {
}
void Stack::grow( int delta ) {
std::cout << "Growing by " << delta << std::endl;
int newbufsize = bufsize + delta;
double* newbuf = new double[ newbufsize ];
double* newbuf = new double[ bufsize + delta ];
// Copy elements
for ( int i=0; i < count ; i++ )
@ -48,8 +47,4 @@ void Stack::grow( int delta ) {
// Assign pointer
s = newbuf;
// Update bufsize
bufsize = newbufsize;
}

View file

@ -7,7 +7,7 @@
using namespace std ;
int main() {
Stack s(5) ;// initialize Stack
Stack s(10) ;// initialize Stack
// Write doubles into Stack
int i ;
@ -17,7 +17,7 @@ int main() {
}
// Count doubles in fifo
cout << s.nitems() << " values in Stack" << endl ;
cout << s.nitems() << " value in stack" << endl ;
cout << "Inspect the FIFO" << endl;
s.inspect();
@ -26,7 +26,7 @@ int main() {
// Read doubles back from fifo
while (!s.empty()) {
double val = s.pop() ;
cout << "popping value " << val << " from Stack" << endl ;
cout << "popping value " << val << " from stack" << endl ;
}
cout << "Inspect the FIFO" << endl;

View file

@ -4,8 +4,6 @@
#include "Button.hh"
#define DEFAULT_NUMBER_BUTTONS 12
class Dialer {
public:
@ -16,11 +14,7 @@ public:
Dialer(const Dialer& other) {
std::cout << "Dialer Copy Constructor " << this << std::endl ;
init(other.buttons_size);
// Copy the buttons
for ( int i = 0; i < other.buttons_size; i++ ) {
buttons[i] = other.buttons[i];
}
init();
}
~Dialer() {
std::cout << "Dialer Destructor " << this << std::endl ;
@ -31,16 +25,10 @@ private:
void init()
{
this->init( DEFAULT_NUMBER_BUTTONS );
}
void init( int num_buttons )
{
buttons_size = num_buttons;
buttons = new Button[buttons_size];
buttons = new Button[12];
}
Button* buttons ;
int buttons_size;
} ;

View file

@ -10,13 +10,7 @@ class Handset {
public:
Handset() { std::cout << "Handset Constructor " << this << std::endl ; }
Handset(const Handset& h) :
mouthpiece( h.mouthpiece ),
earpiece( h.earpiece ),
cable( h.cable )
{
std::cout << "Handset Copy Constructor " << this << std::endl ;
}
Handset(const Handset&) { std::cout << "Handset Copy Constructor " << this << std::endl ; }
~Handset() { std::cout << "Handset Destructor " << this << std::endl ; }
private:

View file

@ -9,12 +9,7 @@ class Housing {
public:
Housing() { std::cout << "Housing Constructor " << this << std::endl ; }
Housing(const Housing& h) :
chassis( h.chassis ),
shell( h.shell )
{
std::cout << "Housing Copy Constructor " << this << std::endl ;
}
Housing(const Housing&) { std::cout << "Housing Copy Constructor " << this << std::endl ; }
~Housing() { std::cout << "Housing Destructor " << this << std::endl ; }
private:

View file

@ -9,19 +9,9 @@
class Telephone {
public:
Telephone() {
std::cout << "Telephone Constructor " << this << std::endl;
}
Telephone(const Telephone& t ) :
cable( t.cable ),
housing( t.housing ),
dialer( t.dialer ),
handset( t.handset ) {
std::cout << "Telephone Copy Constructor " << this << std::endl;
}
~Telephone() {
std::cout << "Telephone Destructor " << this << std::endl;
}
Telephone() { std::cout << "Telephone Constructor " << this << std::endl ; }
Telephone(const Telephone& t ) { std::cout << "Telephone Copy Constructor " << this << std::endl ; }
~Telephone() { std::cout << "Telephone Destructor " << this << std::endl ; }
private:

View file

@ -3,14 +3,5 @@
int main(){
Telephone t;
std::cout << std::endl;
std::cout << "Cloning the Telephone object" << std::endl;
std::cout << std::endl;
Telephone t2 = t;
std::cout << std::endl;
std::cout << "Destructing all objects" << std::endl;
std::cout << std::endl;
}

18
ex4.2/CaloCell.cc Normal file
View file

@ -0,0 +1,18 @@
//CaloGrid.cc
#include <iostream>
#include "CaloGrid.hh"
CaloCell* CaloGrid::cell(int x, int y ) {
if ( x > nx or x < 0 ) {
std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ;
return nullptr;
}
if ( y > ny or y < 0 ) {
std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ;
return nullptr;
}
return cells[ x*nx + y ] ;
}

View file

@ -13,10 +13,10 @@ class CaloCell
// No need for Constructor or Destructor
double getEnergy() const { return energy; }
void setEnergy( double new_energy ) { energy = new_energy ; }
bool setEnergy( double new_energy ) { return (energy = new_energy) ; }
int getId() const { return ID ; }
void setId( int new_id ) { ID = new_id ; }
bool setId( int new_id ) { return ( ID = new_id ) ; }
private:

View file

@ -3,16 +3,16 @@
#include "CaloGrid.hh"
CaloCell* CaloGrid::cell(int x, int y ) {
if ( x >= nx or x < 0 ) {
if ( x > nx or x < 0 ) {
std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ;
return nullptr;
}
if ( y >= ny or y < 0 ) {
if ( y > ny or y < 0 ) {
std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ;
return nullptr;
}
return &cells[ x*nx + y ] ;
return cells[ x * nx + y ] ;
}

View file

@ -25,9 +25,6 @@ class CaloGrid
}
CaloCell* cell(int x, int y);
const CaloCell* cell(int x, int y) const {
return const_cast<CaloGrid*>(this)->cell(x, y);
}
private:
@ -40,13 +37,6 @@ class CaloGrid
// contigious cells
cells = new CaloCell[ nx*ny ];
// set Cell Ids
for ( int i = 0; i < nx ; i++ ) {
for ( int j = 0; j < ny ; j++ ) {
cells[ i*ny+j ].setId( i*ny+j );
}
}
}
} ;
#endif

View file

@ -5,34 +5,5 @@
class Calorimeter
{
public:
Calorimeter( int nx, int ny, double x = 0, double y = 0, double z = 0) :
calogrid(nx, ny),
point(x, y, z)
{}
Calorimeter( const Calorimeter& other ) :
calogrid( other.calogrid ),
point( other.point )
{}
CaloGrid& grid() {
return calogrid;
}
const CaloGrid& grid() const {
return calogrid;
}
Point& position() {
return point;
}
const Point& position() const {
return point;
}
private:
Point point;
CaloGrid calogrid;
};
#endif

View file

@ -6,23 +6,22 @@
class Point
{
public:
Point( double x = 0, double y = 0, double z = 0) { init( x, y, z ) ; }
// No need for Constructor or Destructor
Point() { init(0, 0, 0) ; }
Point( double x, double y, double z ) { init( x, y, z ); }
int getX() const { return x ; }
void setX( double new_x ) { x = new_x ; }
bool setX( int new_x ) { return ( x = new_x ) ; }
int getY() const { return y ; }
void setY( double new_y ) { y = new_y ; }
bool setY( int new_y ) { return ( y = new_y ) ; }
int getZ() const { return z ; }
void setZ( double new_z ) { z = new_z ; }
bool setZ( int new_z ) { return ( z = new_z ) ; }
void setPoint( double x, double y, double z ) {
setX(x) ;
setY(y) ;
setZ(z) ;
void setPoint( double in_x, double in_y, double in_z ) {
x = in_x ;
y = in_y ;
z = in_z ;
}

View file

@ -1,12 +1,6 @@
#include "iostream"
#include "CaloCell.hh"
#include "CaloGrid.hh"
#include "Point.hh"
#include "Calorimeter.hh"
void printCellId( const CaloCell* cell ) {
std::cout << cell->getId() << std::endl;
}
int main() {
CaloCell cell(0, 1) ;
@ -16,19 +10,4 @@ int main() {
grid.cell(1, 1);
int nx = 3;
int ny = 4;
Calorimeter calo(nx, ny) ;
std::cout << "CellId at Start: " << calo.grid().cell(0,0)->getId() << std::endl;
std::cout << "CellId at End: " << calo.grid().cell(nx-1,ny-1)->getId() << std::endl;
std::cout << "CellId at Center: " ;
printCellId(calo.grid().cell(nx/2,ny/2));
const CaloGrid grid2(5,5);
grid2.cell(3,3);
}

View file

@ -1,7 +1,6 @@
//String.cc
#include "String.hh"
#include <iostream>
String operator+( const String& lhs, const String& rhs ) {
String result(lhs); // Copy lhs into res with constructor
result += rhs ; // Use operator+= function which is a member of the class
@ -29,65 +28,3 @@ String& String::operator+=( const String& a ) {
return *this ;
}
String& String::subString( int start, int stop ) {
/*
* Return a substring
*
* if stop is negative, interpret as len - stop
* if start is negative, set start = 0
*
* if start > stop, return reversed string
*/
bool reverse = 0;
int len = 0;
char * tmp;
// Higher than Range
if ( start > _len ) {
start = _len ;
}
if ( stop > _len ) {
stop = _len ;
}
// Lower than Range
if ( start < 0 ) {
start = _len + start%_len ;
}
if ( stop < 0 ) {
stop = _len + stop%_len ;
}
// Set length
len = stop - start;
if ( start > stop ) {
// Make sure len is positive
len = -1 * len ;
reverse = 1;
}
// allocate
tmp = new char[ len - 1];
tmp[len] = '\0';
for ( int i = 0 ; i < len ; i++ )
{
if ( reverse )
{
tmp[i] = _s[ start - i ];
}
else
{
tmp[i] = _s[ start + i ];
}
}
// make it a String again
String * result = new String(tmp);
delete tmp;
return *result;
}

View file

@ -12,8 +12,6 @@ public:
String& operator=( const String& a );
String& operator+=( const String& a );
String& subString( int start, int stop );
operator const char* const() { return data(); }
int length() const { return _len ; }
@ -34,6 +32,4 @@ private:
} ;
String operator+( const String& lhs, const String& rhs );
#endif

View file

@ -1,7 +1,6 @@
#include "String.hh"
#include <iostream>
#include <cstring>
#include "String.hh"
int main() {
String str("Blubaloo");
@ -33,10 +32,4 @@ int main() {
String c("Almost Empty");
std::cout << "String '" << c.data() << "' is " << strlen(c) << " chars long." << std::endl;
// Substring
std::cout << "First five characters: " << c.subString(0, 5) << std::endl;
std::cout << "First five characters reversed: " << c.subString(5, 0) << std::endl;
std::cout << "Last five characters: " << c.subString(-5, -1) << std::endl;
}

View file

@ -24,6 +24,9 @@ int main( int argc, char* argv[] ) {
const int bufsize = 100;
char buf[bufsize];
const int linebufsize = 10;
char linebuf[linebufsize];
// if EOF, fh returns false
while( fh ) {
fh.getline(buf, bufsize);
@ -33,14 +36,8 @@ int main( int argc, char* argv[] ) {
std::istringstream line(buf);
while( line ) {
std::string linebuf;
line >> linebuf;
if ( ! linebuf.empty() ) {
wordcount++ ;
}
if ( line.eof() )
{
@ -51,5 +48,5 @@ int main( int argc, char* argv[] ) {
}
std::cout << " " << linecount-1 << " " << wordcount << " " << charcount-1 << " " << argv[1] << std::endl;
std::cout << " " << linecount-1 << " " << wordcount-1 << " " << charcount-1 << " " << argv[1] << std::endl;
}

View file

@ -11,7 +11,6 @@ public:
Array(const Array& other) : _size(other._size) {
_arr = new T[other._size] ;
_default = other._default;
// Copy elements
for (int i=0 ; i<_size ; i++) {

View file

@ -11,7 +11,6 @@ public:
Array(const Array& other) : _size(other._size) {
_arr = new T[other._size] ;
_default = other._default ;
// Copy elements
for (int i=0 ; i<_size ; i++) {

View file

@ -8,23 +8,23 @@ template<class T>
class Stack {
// Interface
public:
Stack( int size ):
_s(size, 0)
{}
Stack( int in_size ) {
count = 0;
_s = new Array<T>(in_size, 0);
}
int nitems() { return count ; }
bool full() { return (count == _s.size()) ; }
bool full() { return (count == _s->size()) ; }
bool empty() { return (count == 0) ; }
void push(T c) {
if (full()) {
std::cout << "Stack::push() Error: stack is full" << std::endl ;
_s.resize( _s.size() + 10 );
_s->resize( _s->size() + 10 );
}
_s[count++] = c ;
_s[0][count++] = c ;
}
T pop() {
@ -33,20 +33,20 @@ class Stack {
return 0 ;
}
return _s[--count] ;
return _s[0][--count] ;
}
void inspect() {
for ( int i = nitems() - 1; i >= 0; i-- )
{
std::cout << i << ": " << _s[i] << std::endl;
std::cout << i << ": " << _s[0][i] << std::endl;
}
}
// Implementation
private:
Array<T> _s ;
int count = 0;
Array<T>* _s ;
int count ;
};
#endif

View file

@ -1,3 +0,0 @@
The timing is much longer for vectors than for a list.
However, I also get a segfault for the vector program.

View file

@ -1,4 +0,0 @@
real 0m0.028s
user 0m0.021s
sys 0m0.004s

View file

@ -4,7 +4,7 @@ int main() {
std::list<int> l;
// Fill List
for (int i = 0; i < 10000 ; i++) {
for (int i = 0; i < 10000000 ; i++) {
l.push_back(i);
}

View file

@ -13,4 +13,5 @@ int main() {
iter = l.erase( iter );
}
}
}

View file

@ -1,5 +0,0 @@
Segmentation fault (core dumped)
real 0m0.927s
user 0m0.349s
sys 0m0.007s

View file

@ -8,11 +8,18 @@ template<class T>
class Stack {
// Interface
public:
int nitems() { return _s.size() ; }
bool empty() { return (_s.size() == 0) ; }
Stack() {
_s = new std::deque<T>();
}
~Stack() {
delete _s;
}
int nitems() { return _s->size() ; }
bool empty() { return (_s->size() == 0) ; }
void push(T c) {
_s.push_back(c);
_s->push_back(c);
}
T pop() {
@ -21,15 +28,15 @@ class Stack {
return 0 ;
}
T tmp = _s.back();
_s.pop_back();
T tmp = _s->back();
_s->pop_back();
return tmp;
}
void inspect() {
for ( auto iter = _s.begin(); iter != _s.end() ; iter++ )
for ( auto iter = _s->begin(); iter != _s->end() ; iter++ )
{
std::cout << *iter << std::endl;
}
@ -37,7 +44,7 @@ class Stack {
// Implementation
private:
std::deque<T> _s ;
std::deque<T>* _s ;
};
#endif

View file

@ -36,7 +36,7 @@ public:
private:
string _name ;
double _salary ;
set<Employee*> _subordinates ;// subordinates is an unordered collection of unique people so set is usefull enough
set<Employee*> _subordinates ;// subordinates is an unordered collection so set is usefull enough
} ;

View file

@ -36,7 +36,7 @@ public:
private:
string _name ;
double _salary ;
set<Employee*> _subordinates ;// subordinates is an unordered collection of unique people so set is usefull enough
set<Employee*> _subordinates ;// subordinates is an unordered collection so set is usefull enough
} ;

View file

@ -10,15 +10,15 @@ void populate_directory( set<Employee*>& directory ) {
Manager* jo = new Manager("Jo", 5);
Manager* frank = new Manager("Frank", 6);
stan->addSubordinate( *wouter );
stan->addSubordinate( *ivo );
(*stan).addSubordinate( *wouter );
(*stan).addSubordinate( *ivo );
directory.insert( stan );
directory.insert( wouter );
// This does not give a problem because stan is also of type Employee
frank->addSubordinate( *stan );
frank->addSubordinate( *jo );
(*frank).addSubordinate( *stan );
(*frank).addSubordinate( *jo );
directory.insert( wouter );
directory.insert( ivo );

View file

@ -9,7 +9,7 @@ class Circle: public Shape {
public:
// Constructor, destructor
Circle( double radius ) : _radius(radius) {} ;
Circle(int radius) : _radius(radius) {} ;
virtual ~Circle() {} ;
// Implementation of abstract interface
@ -18,6 +18,6 @@ public:
virtual const char* shapeName() const { return "Circle"; }
private:
double _radius ;
int _radius ;
} ;
#endif

View file

@ -1,82 +0,0 @@
#include <thread>
#include <string>
#include <iostream>
#include <chrono>
#include <vector>
#include <functional>
/*
* Compile with 'g++ -pthread a.cpp'
*
* Modifying an object
*/
using namespace std;
/*
* This method will push the addition of the last two elements on the vector.
*/
void manipulate(vector<double>& v) {
int sleep = 2;
for ( int i = 0; i < 5; i++ )
{
double a = 0;
// Only use the last two elements
int j = 0;
for ( auto rit = v.rbegin() ; rit != v.rend(); ++rit )
{
if ( j > 1 )
{
break;
}
a += *rit;
++j;
}
v.push_back( a);
std::cout << "Updated Vector" << std::endl;
this_thread::sleep_for(std::chrono::seconds(sleep));
}
}
void display(vector<double>& v) {
int sleep = 1;
for ( int i = 0; i < 10; i++ )
{
std::cout << "Vector is now:" << std::endl;
std::cout << "{";
for ( int i = 0; i < v.size() ; i++ ) {
std::cout << v[i] << ", ";
}
std::cout << "}" << std::endl;
this_thread::sleep_for(std::chrono::seconds(sleep));
}
}
struct my_f {
vector<double>& v;
my_f(vector<double>& vv): v(vv) {};
void my_function() { manipulate(v); };
};
int main() {
vector<double> v { 1.1, 1.1 };
thread t {display, ref(v)};
my_f f{v};
thread t2( &my_f::my_function, std::ref(f) );
// Run the threads
t.join();
t2.join();
return 0;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1 +0,0 @@
<html><head><meta http-equiv="&#39;Content-Type&#39;" content="&#39;text/html;charset=utf-8&#39;" /><title>1920 CDS: Advanced Programming (KW2 V)</title></head><body><table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td colspan=3>&nbsp;</td></tr><tr><td valign="top" width="100%"><font class="title"><strong>1920 CDS: Advanced Programming (KW2 V)</strong></font><br><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Advanced-Programming.pdf" />1. Advanced-Programming</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises0.pdf" />2. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Introduction.pdf" />3. Introduction</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter1-reduced.pdf" />4. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises1-reduced.pdf" />5. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter2.pdf" />6. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises2.pdf" />7. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter3.pdf" />8. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises3.pdf" />9. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter4.pdf" />10. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises4.pdf" />11. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter5.pdf" />12. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises5.pdf" />13. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter6.pdf" />14. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises6.pdf" />15. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter7.pdf" />16. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises7.pdf" />17. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter8.pdf" />18. Course material</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises8.pdf" />19. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="Chapter9-concurrency.pdf" />20. Chapter9-concurrency</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercises91.pdf" />21. exercises</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="chapter10-bindings.pdf" />22. chapter10-bindings</a></p><p class='d2l' style=' margin-left: 40px'><a target="_blank" href="exercise10.pdf" />23. exercise</a></p></td></tr></table></body></html>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.