Skip to content

Commit ad55029

Browse files
committed
Adding src/casm and include/casm
1 parent 6268a5a commit ad55029

530 files changed

Lines changed: 166354 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/casm/BP_C++/BP_Comb.hh

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#ifndef BP_Comb_HH
2+
#define BP_Comb_HH
3+
4+
5+
#include "casm/BP_C++/BP_Vec.hh"
6+
#include <cstring>
7+
#include <sstream>
8+
#include <iostream>
9+
10+
////////////////////////////////////////////////////////////
11+
////////////////////////////////////////////////////////////
12+
/// Coordinate classes
13+
14+
namespace BP {
15+
16+
class BP_Comb {
17+
private:
18+
BP_Vec<bool> val_list;
19+
BP_Vec<unsigned int> pos_list;
20+
BP_Vec<unsigned int> fix_pos_list;
21+
BP_Vec<bool> fix_val_list;
22+
int curr_index;
23+
bool is_complete;
24+
int N;
25+
int K;
26+
unsigned long int count;
27+
28+
public:
29+
BP_Comb(int n, int k) {
30+
reset(n, k);
31+
};
32+
33+
void reset(int n, int k) {
34+
N = n;
35+
K = k;
36+
val_list = BP_Vec<bool>(N, 0);
37+
38+
fix_pos_list.erase();
39+
fix_val_list.erase();
40+
41+
pos_list.clear();
42+
for(int i = 0; i < K; i++) {
43+
pos_list.add(i);
44+
val_list[i] = 1;
45+
}
46+
47+
curr_index = K - 1;
48+
49+
is_complete = false;
50+
count = 0;
51+
};
52+
53+
double total_combs() {
54+
double tot = 1;
55+
unsigned long int i;
56+
unsigned long int j = K;
57+
for(i = N; i > N - K; i--) {
58+
tot *= i;
59+
}
60+
61+
for(i = K; i > 0; i--) {
62+
tot /= i;
63+
}
64+
65+
return tot;
66+
67+
};
68+
69+
bool get(int i) const {
70+
return val_list[i];
71+
};
72+
73+
bool operator[](int i) const {
74+
return val_list[i];
75+
};
76+
77+
std::string get_bit_string() const {
78+
std::stringstream ss;
79+
for(int i = 0; i < size(); i++)
80+
ss << val_list[i];
81+
return ss.str();
82+
}
83+
84+
BP_Vec<bool> get_all() const {
85+
return val_list;
86+
};
87+
88+
void increment() {
89+
//cout << "begin increment()" << endl;
90+
91+
bool ok = 1;
92+
do {
93+
//cout << " here b1" << endl;
94+
95+
int curr_index = K - 1;
96+
if(pos_list[curr_index] == N - 1) {
97+
curr_index--;
98+
if(curr_index == -1) {
99+
is_complete = 1;
100+
//cout << "finish increment() 1a" << endl;
101+
return;
102+
}
103+
104+
bool cont = 1;
105+
106+
//cout << " here b2" << endl;
107+
//cout << " curr_index: " << curr_index << endl;
108+
109+
do {
110+
if(pos_list[curr_index] < pos_list[curr_index + 1] - 1) {
111+
cont = 0;
112+
}
113+
else {
114+
curr_index--;
115+
if(curr_index == -1) {
116+
is_complete = 1;
117+
//cout << "finish increment() 1" << endl;
118+
return;
119+
}
120+
}
121+
122+
}
123+
while(cont == 1);
124+
125+
//cout << " curr_index: " << curr_index << endl;
126+
for(int i = curr_index; i < pos_list.size(); i++) {
127+
//cout << "i: " << i << endl;
128+
val_list[ pos_list[ i]] = 0;
129+
}
130+
//cout << " here b3" << endl;
131+
int new_start = pos_list[curr_index] + 1;
132+
//cout << " new_start: " << new_start << endl;
133+
for(int i = 0; i < pos_list.size() - curr_index; i++) {
134+
//cout << " here b3 i: "<< i << endl;
135+
pos_list[ curr_index + i] = new_start + i;
136+
//cout << " here b3 pos_list: "<< pos_list[ curr_index+ i] << endl;
137+
val_list[ pos_list[ curr_index + i]] = 1;
138+
}
139+
//cout << " here b4" << endl;
140+
141+
//count++;
142+
curr_index = K - 1;
143+
//cout << " here b5" << endl;
144+
145+
}
146+
else {
147+
//cout << " here a" << endl;
148+
val_list[ pos_list[ curr_index]] = 0;
149+
pos_list[curr_index]++;
150+
val_list[ pos_list[ curr_index]] = 1;
151+
//count++;
152+
//cout << " here a2" << endl;
153+
154+
}
155+
156+
//cout << " here c" << endl;
157+
158+
ok = 1;
159+
for(int i = 0; i < fix_pos_list.size(); i++) {
160+
if(val_list[ fix_pos_list[i]] != fix_val_list[i]) {
161+
ok = 0;
162+
continue;
163+
}
164+
}
165+
166+
//cout << " here d" << endl;
167+
168+
}
169+
while(ok == 0);
170+
count++;
171+
172+
//cout << "finish increment() 2" << endl;
173+
174+
};
175+
176+
int get_N() const {
177+
return N;
178+
};
179+
int get_K() const {
180+
return K;
181+
};
182+
int size() const {
183+
return N;
184+
};
185+
unsigned long int get_count() const {
186+
return count;
187+
};
188+
189+
bool complete() const {
190+
return is_complete;
191+
};
192+
193+
void fix(int i1, bool i2) {
194+
fix_pos_list.add(i1);
195+
fix_val_list.add(i2);
196+
};
197+
198+
void unfix(int i1) {
199+
for(int i = 0; i < fix_pos_list.size(); i++) {
200+
if(fix_pos_list[i] == i1) {
201+
fix_pos_list.remove(i);
202+
fix_val_list.remove(i);
203+
}
204+
}
205+
};
206+
207+
void unfix_all() {
208+
fix_pos_list.erase();
209+
fix_val_list.erase();
210+
};
211+
212+
friend std::ostream &operator<<(std::ostream &outstream, const BP_Comb &c) {
213+
for(int i = 0; i < c.size(); i++)
214+
outstream << c[i];
215+
return outstream;
216+
};
217+
};
218+
219+
220+
}
221+
222+
#endif // BP_Comb_HH

include/casm/BP_C++/BP_Dir.hh

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#ifndef BP_Dir_HH
2+
#define BP_Dir_HH
3+
4+
#include <iostream>
5+
#include <istream>
6+
#include <fstream>
7+
#include <string>
8+
#include <cstdio>
9+
#include <sstream>
10+
#include "casm/BP_C++/BP_Parse.hh"
11+
#include "casm/BP_C++/BP_basic.hh"
12+
#include <errno.h>
13+
#include <dirent.h>
14+
#include <sys/stat.h>
15+
#include <unistd.h>
16+
17+
namespace BP {
18+
19+
/// \brief BP_Dir, a class for managing directories and files
20+
/// - list, create, copy, rename (move), delete directories
21+
/// - list, copy, rename (move), delete files
22+
/// - use absolute or relative filenames
23+
///
24+
class BP_Dir {
25+
private:
26+
std::string _name;
27+
std::string _abs_name;
28+
std::string _joint;
29+
30+
BP_Vec<std::string> files_list;
31+
BP_Vec<std::string> dirs_list;
32+
33+
public:
34+
35+
// // Initialize with current directory:
36+
// BP_Dir dir;
37+
//
38+
// // Get the current directory (just directory name):
39+
// cout << "The current directory: " << dir.name() << endl;
40+
//
41+
// // Get the current directory (include full path):
42+
// cout << "The current directory: " << dir.abs_name() << endl;
43+
//
44+
//
45+
// // get a list of files or directories in current directory:
46+
// BP_Vec<std::string> files = dir.files();
47+
// BP_Vec<std::string> dirs = dir.dirs();
48+
//
49+
// // or just use directly:
50+
// cout << "There are " << dir.files().size() << " files in this directory" << endl;
51+
//
52+
// // Change the current directory (of the BP_Dir object):
53+
// dir.cd( dir.dirs()[0]);
54+
//
55+
// // or directly:
56+
// dir.cd( "the/path/other_directory");
57+
//
58+
// // to be more general you can do this:
59+
// dir.cd( "the" + dir.joint() + "path" + dir.joint() + "other_directory");
60+
//
61+
// // Write a file in the new current directory using BP_Write:
62+
// BP_Write outfile(dir.abs_name("new_file.txt"));
63+
// outfile.newfile();
64+
// outfile << "This is a new file" << endl;
65+
66+
67+
68+
BP_Dir();
69+
70+
std::string name() const {
71+
return _name;
72+
};
73+
std::string abs_name() const {
74+
return _abs_name;
75+
};
76+
77+
void refresh();
78+
bool cd(std::string s);
79+
bool mkdir(std::string s);
80+
bool rmdir(std::string s);
81+
bool rmdir(BP_Vec<std::string> s);
82+
bool recurs_rmdir(std::string s);
83+
bool recurs_rmdir(BP_Vec<std::string> s);
84+
bool rm(std::string s);
85+
bool rm(BP_Vec<std::string> s);
86+
bool rename(std::string s1, std::string s2);
87+
bool copy(std::string s1, std::string s2);
88+
bool copydir(std::string s1, std::string s2);
89+
bool cat(std::string s1, std::string s2, std::string s3);
90+
91+
bool print(std::string s, std::ostream &sout) const;
92+
const BP_Vec<std::string> &files() const {
93+
return files_list;
94+
};
95+
const BP_Vec<std::string> &dirs() const {
96+
return dirs_list;
97+
};
98+
bool is_file(std::string s) const;
99+
bool is_dir(std::string s) const;
100+
bool exists(std::string s) const;
101+
102+
void set_joint(std::string s) {
103+
_joint = s;
104+
};
105+
std::string joint() const {
106+
return _joint;
107+
};
108+
std::string without_path(std::string s) const;
109+
110+
bool is_absolute_path(std::string s) const;
111+
void make_absolute_path(std::string &s) const;
112+
std::string abs_name(std::string s) const;
113+
114+
115+
private:
116+
bool generate_files_dirs();
117+
bool remove_file(std::string s);
118+
bool remove_dir(std::string s);
119+
bool recurs_remove_dir(std::string s);
120+
bool change_directory(std::string s);
121+
122+
};
123+
124+
125+
}
126+
127+
#endif // BP_Dir_HH
128+

0 commit comments

Comments
 (0)