Skip to content

Commit 3c850c7

Browse files
committed
move to head
1 parent 12f325d commit 3c850c7

File tree

12 files changed

+490
-355
lines changed

12 files changed

+490
-355
lines changed

ch1/1_BinarySearch/main.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,10 @@
22
#include <vector>
33
#include <fstream>
44
#include <algorithm>
5+
#include "../head/BinarySearch.h"
56

67
using namespace std;
78

8-
/**
9-
* Returns the index of the specified key in the specified array.
10-
*
11-
* @param a the array of integers, must be sorted in ascending order
12-
* @param key the search key
13-
* @return index of key in array {@code a} if present; {@code -1} otherwise
14-
*/
15-
int indexOf(vector<int> &a, int key) {
16-
int lo = 0, hi = a.size() - 1;
17-
while (lo <= hi) {
18-
// Key is in a[lo..hi] or not present.
19-
int mid = lo + (hi - lo) / 2;
20-
if (key < a[mid]) hi = mid - 1;
21-
else if (key > a[mid]) lo = mid + 1;
22-
else return mid;
23-
}
24-
return -1;
25-
}
26-
279
int main() {
2810
// TODO: change to relative path
2911
ifstream input("/home/ace/AceDev/C++/algorithm/ch1/data/tinyW.txt");
@@ -35,7 +17,7 @@ int main() {
3517
sort(whitelist.begin(), whitelist.end());
3618
ifstream check("/home/ace/AceDev/C++/algorithm/ch1/data/tinyT.txt");
3719
while (check >> t) {
38-
int idx = indexOf(whitelist, t);
20+
int idx = BinarySearch::indexOf(whitelist, t);
3921
if (idx == -1)
4022
cout << t << endl;
4123
}

ch1/5_Knuth/main.cpp

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
1-
#include <iostream>
2-
#include <vector>
1+
#include <string>
32
#include <fstream>
3+
#include <iostream>
44
#include <iomanip>
5-
#include <algorithm>
5+
#include "../head/Knuth.h"
66

77
using namespace std;
88

9-
std::random_device rd;
10-
std::mt19937 gen(rd());
11-
12-
/**
13-
* Rearranges an array of objects in uniformly random order
14-
* (under the assumption that {@code Math.random()} generates independent
15-
* and uniformly distributed numbers between 0 and 1).
16-
* @param a the array to be shuffled
17-
*/
18-
template<typename T>
19-
void shuffle_(vector<T> &a) {
20-
int n = a.size();
21-
for (int i = 0; i < n; i++) {
22-
// choose index uniformly in [0, i]
23-
uniform_int_distribution<> dis(0, i);
24-
int r = dis(gen);
25-
swap(a[r], a[i]);
26-
}
27-
}
28-
29-
template<typename T>
30-
void shuffleAlternate_(vector<T> &a) {
31-
int n = a.size();
32-
for (int i = 0; i < n; i++) {
33-
// choose index uniformly in [i, n-1]
34-
uniform_int_distribution<> dis(0, n - i);
35-
int r = i + dis(gen);
36-
swap(a[r], a[i]);
37-
}
38-
}
39-
409
/**
4110
* Reads in a sequence of text files specified as the first command-line
4211
* arguments, concatenates them, and writes the results to the file
@@ -52,9 +21,9 @@ int main() {
5221
while (file >> tmp) {
5322
a.push_back(tmp);
5423
}
55-
shuffle_(a);
24+
Knuth::shuffle(a);
5625
for (int i = 0; i < a.size(); ++i) {
57-
cout << setw(6)<<left<<a[i];
26+
cout << setw(6) << left << a[i];
5827
if ((i + 1) % 13 == 0)
5928
cout << endl;
6029
}

ch1/6_Counter/main.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,11 @@
11
#include <iostream>
22
#include <vector>
3-
#include <iomanip>
43
#include <algorithm>
4+
#include "../head/Counter.h"
55

66
using namespace std;
77

88

9-
class Counter {
10-
public:
11-
/**
12-
* Initializes a new counter starting at 0, with the given id.
13-
* @param id the name of the counter
14-
*/
15-
Counter(string id) : name(id) {}
16-
17-
/**
18-
* Increments the counter by 1.
19-
*/
20-
void increment() {
21-
count++;
22-
}
23-
24-
/**
25-
* Returns the current value of this counter.
26-
*
27-
* @return the current value of this counter
28-
*/
29-
int tally() {
30-
return count;
31-
}
32-
33-
/**
34-
* Returns a string representation of this counter.
35-
*
36-
* @return a string representation of this counter
37-
*/
38-
string toString() {
39-
return to_string(count) + " " + name;
40-
}
41-
42-
bool operator<(const Counter &rhs) {
43-
return (this->count < rhs.count);
44-
}
45-
46-
private:
47-
const string name;
48-
int count = 0;
49-
};
50-
519
const int seed = 100;
5210

5311
int main() {
@@ -65,5 +23,5 @@ int main() {
6523
}
6624
// print results
6725
for (int i = 0; i < n; ++i)
68-
cout << hits[i].toString() << endl;
26+
cout << hits[i] << endl;
6927
}

ch1/7_StaticSETofInts/main.cpp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
11
#include <iostream>
2-
#include <vector>
3-
#include <iomanip>
4-
#include <algorithm>
2+
#include "../head/StaticSetofInts.h"
53

64
using namespace std;
75

8-
class StaticSETofInts {
9-
public:
10-
/**
11-
* Initializes a set of integers specified by the integer array.
12-
* @param keys the array of integers
13-
* @throws IllegalArgumentException if the array contains duplicate integers
14-
*/
15-
StaticSETofInts(vector<int> keys) : a(keys) {
16-
sort(a.begin(), a.end());
17-
for (int i = 1; i < a.size(); ++i) {
18-
if (a[i] == a[i - 1])
19-
throw runtime_error("Argument arrays contains duplicate keys.");
20-
}
21-
}
22-
23-
/**
24-
* Returns either the index of the search key in the sorted array
25-
* (if the key is in the set) or -1 (if the key is not in the set).
26-
* @param key the search key
27-
* @return the number of keys in this set less than the key (if the key is in the set)
28-
* or -1 (if the key is not in the set).
29-
*/
30-
int rank(int key) {
31-
int lo = 0, hi = a.size() - 1;
32-
while (lo <= hi) {
33-
auto mid = lo + (hi - lo) / 2;
34-
if (key < a[mid]) hi = mid - 1;
35-
else if (key > a[mid]) lo = mid + 1;
36-
else return mid;
37-
}
38-
return -1;
39-
}
40-
41-
/**
42-
* Is the key in this set of integers?
43-
* @param key the search key
44-
* @return true if the set of integers contains the key; false otherwise
45-
*/
46-
bool contains(int key) {
47-
return rank(key) != -1;
48-
}
49-
50-
private:
51-
vector<int> a;
52-
};
536

547
int main() {
558
StaticSETofInts *s = new StaticSETofInts({4, 2, 3, 6, 5});

ch1/8_Whitelist/main.cpp

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,16 @@
11
#include <iostream>
2-
#include <vector>
3-
#include <iomanip>
42
#include <fstream>
5-
#include <algorithm>
3+
#include "../head/StaticSetofInts.h"
64

75
using namespace std;
86

9-
class StaticSETofInts {
10-
public:
11-
/**
12-
* Initializes a set of integers specified by the integer array.
13-
* @param keys the array of integers
14-
* @throws IllegalArgumentException if the array contains duplicate integers
15-
*/
16-
StaticSETofInts(vector<int> keys) : a(keys) {
17-
sort(a.begin(), a.end());
18-
for (int i = 1; i < a.size(); ++i) {
19-
if (a[i] == a[i - 1])
20-
throw runtime_error("Argument arrays contains duplicate keys.");
21-
}
22-
}
23-
24-
/**
25-
* Returns either the index of the search key in the sorted array
26-
* (if the key is in the set) or -1 (if the key is not in the set).
27-
* @param key the search key
28-
* @return the number of keys in this set less than the key (if the key is in the set)
29-
* or -1 (if the key is not in the set).
30-
*/
31-
int rank(int key) {
32-
int lo = 0, hi = a.size() - 1;
33-
while (lo <= hi) {
34-
auto mid = lo + (hi - lo) / 2;
35-
if (key < a[mid]) hi = mid - 1;
36-
else if (key > a[mid]) lo = mid + 1;
37-
else return mid;
38-
}
39-
return -1;
40-
}
41-
42-
/**
43-
* Is the key in this set of integers?
44-
* @param key the search key
45-
* @return true if the set of integers contains the key; false otherwise
46-
*/
47-
bool contains(int key) {
48-
return rank(key) != -1;
49-
}
50-
51-
private:
52-
vector<int> a;
53-
};
54-
7+
/**
8+
* Reads in a sequence of integers from the whitelist file, specified as
9+
* a command-line argument. Reads in integers from standard input and
10+
* prints to standard output those integers that are not in the file.
11+
*
12+
* @param args the command-line arguments
13+
*/
5514
int main() {
5615
ifstream in("/home/ace/AceDev/C++/algorithm/ch1/data/tinyW.txt");
5716
int tmp;

0 commit comments

Comments
 (0)