Changeset 0751d4d2 for doc/generic_types
- Timestamp:
- Apr 17, 2017, 5:41:48 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- e2ef6bf
- Parents:
- bb91487
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/generic_types.tex
rbb91487 r0751d4d2 1176 1176 std::forward_list wrapped in std::stack interface 1177 1177 1178 template<typename T> void print(ostream & out, const T& x) { out << x; }1179 template<> void print<bool>(ostream & out, const bool& x) { out << (x ? "true" : "false"); }1180 template<> void print<char>(ostream & out, const char& x ) { out << "'" << x << "'"; }1181 template<typename R, typename S> ostream & operator<< (ostream& out, const pair<R, S>& x) {1178 template<typename T> void print(ostream & out, const T & x) { out << x; } 1179 template<> void print<bool>(ostream & out, const bool & x) { out << (x ? "true" : "false"); } 1180 template<> void print<char>(ostream & out, const char & x ) { out << "'" << x << "'"; } 1181 template<typename R, typename S> ostream & operator<< (ostream & out, const pair<R, S>& x) { 1182 1182 out << "["; print(out, x.first); out << ", "; print(out, x.second); return out << "]"; } 1183 template<typename T, typename... Args> void print(ostream & out, const T& arg, const Args&... rest) {1183 template<typename T, typename... Args> void print(ostream & out, const T & arg, const Args &... rest) { 1184 1184 out << arg; print(out, rest...); } 1185 1185 \end{lstlisting} … … 1220 1220 forall(otype T) struct stack_node { 1221 1221 T value; 1222 stack_node(T) * next;1222 stack_node(T) * next; 1223 1223 }; 1224 forall(otype T) void ?{}(stack(T) * s) { (&s->head){ 0 }; }1225 forall(otype T) void ?{}(stack(T) * s, stack(T) t) {1226 stack_node(T) ** crnt = &s->head;1227 for ( stack_node(T) * next = t.head; next; next = next->next ) {1228 *crnt = ((stack_node(T) *)malloc()){ next->value }; /***/1229 stack_node(T) * acrnt = *crnt;1224 forall(otype T) void ?{}(stack(T) * s) { (&s->head){ 0 }; } 1225 forall(otype T) void ?{}(stack(T) * s, stack(T) t) { 1226 stack_node(T) ** crnt = &s->head; 1227 for ( stack_node(T) * next = t.head; next; next = next->next ) { 1228 *crnt = ((stack_node(T) *)malloc()){ next->value }; /***/ 1229 stack_node(T) * acrnt = *crnt; 1230 1230 crnt = &acrnt->next; 1231 1231 } 1232 1232 *crnt = 0; 1233 1233 } 1234 forall(otype T) stack(T) ?=?(stack(T) * s, stack(T) t) {1234 forall(otype T) stack(T) ?=?(stack(T) * s, stack(T) t) { 1235 1235 if ( s->head == t.head ) return *s; 1236 1236 clear(s); … … 1238 1238 return *s; 1239 1239 } 1240 forall(otype T) void ^?{}(stack(T) * s) { clear(s); }1241 forall(otype T) _Bool empty(const stack(T) * s) { return s->head == 0; }1242 forall(otype T) void push(stack(T) * s, T value) {1243 s->head = ((stack_node(T) *)malloc()){ value, s->head }; /***/1244 } 1245 forall(otype T) T pop(stack(T) * s) {1246 stack_node(T) * n = s->head;1240 forall(otype T) void ^?{}(stack(T) * s) { clear(s); } 1241 forall(otype T) _Bool empty(const stack(T) * s) { return s->head == 0; } 1242 forall(otype T) void push(stack(T) * s, T value) { 1243 s->head = ((stack_node(T) *)malloc()){ value, s->head }; /***/ 1244 } 1245 forall(otype T) T pop(stack(T) * s) { 1246 stack_node(T) * n = s->head; 1247 1247 s->head = n->next; 1248 1248 T x = n->value; … … 1251 1251 return x; 1252 1252 } 1253 forall(otype T) void clear(stack(T) * s) {1254 for ( stack_node(T) * next = s->head; next; ) {1255 stack_node(T) * crnt = next;1253 forall(otype T) void clear(stack(T) * s) { 1254 for ( stack_node(T) * next = s->head; next; ) { 1255 stack_node(T) * crnt = next; 1256 1256 next = crnt->next; 1257 1257 delete(crnt); … … 1267 1267 struct node { 1268 1268 T value; 1269 node * next;1270 node( const T & v, node* n = nullptr ) : value(v), next(n) {}1269 node * next; 1270 node( const T & v, node * n = nullptr ) : value(v), next(n) {} 1271 1271 }; 1272 node * head;1272 node * head; 1273 1273 void copy(const stack<T>& o) { 1274 node ** crnt = &head;1275 for ( node * next = o.head;; next; next = next->next ) {1274 node ** crnt = &head; 1275 for ( node * next = o.head;; next; next = next->next ) { 1276 1276 *crnt = new node{ next->value }; /***/ 1277 1277 crnt = &(*crnt)->next; … … 1282 1282 stack() : head(nullptr) {} 1283 1283 stack(const stack<T>& o) { copy(o); } 1284 stack(stack<T> && o) : head(o.head) { o.head = nullptr; }1284 stack(stack<T> && o) : head(o.head) { o.head = nullptr; } 1285 1285 ~stack() { clear(); } 1286 stack & operator= (const stack<T>& o) {1286 stack & operator= (const stack<T>& o) { 1287 1287 if ( this == &o ) return *this; 1288 1288 clear(); … … 1290 1290 return *this; 1291 1291 } 1292 stack & operator= (stack<T>&& o) {1292 stack & operator= (stack<T> && o) { 1293 1293 if ( this == &o ) return *this; 1294 1294 head = o.head; … … 1297 1297 } 1298 1298 bool empty() const { return head == nullptr; } 1299 void push(const T & value) { head = new node{ value, head }; /***/ }1299 void push(const T & value) { head = new node{ value, head }; /***/ } 1300 1300 T pop() { 1301 node * n = head;1301 node * n = head; 1302 1302 head = n->next; 1303 1303 T x = std::move(n->value); … … 1306 1306 } 1307 1307 void clear() { 1308 for ( node * next = head; next; ) {1309 node * crnt = next;1308 for ( node * next = head; next; ) { 1309 node * crnt = next; 1310 1310 next = crnt->next; 1311 1311 delete crnt; … … 1320 1320 \begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt] 1321 1321 struct stack_node { 1322 void * value;1323 struct stack_node * next;1322 void * value; 1323 struct stack_node * next; 1324 1324 }; 1325 1325 struct stack new_stack() { return (struct stack){ NULL }; /***/ } 1326 void copy_stack(struct stack * s, const struct stack* t, void* (*copy)(const void*)) {1327 struct stack_node ** crnt = &s->head;1328 for ( struct stack_node * next = t->head; next; next = next->next ) {1326 void copy_stack(struct stack * s, const struct stack * t, void * (*copy)(const void *)) { 1327 struct stack_node ** crnt = &s->head; 1328 for ( struct stack_node * next = t->head; next; next = next->next ) { 1329 1329 *crnt = malloc(sizeof(struct stack_node)); /***/ 1330 1330 **crnt = (struct stack_node){ copy(next->value) }; /***/ … … 1333 1333 *crnt = 0; 1334 1334 } 1335 _Bool stack_empty(const struct stack * s) { return s->head == NULL; }1336 void push_stack(struct stack * s, void* value) {1337 struct stack_node * n = malloc(sizeof(struct stack_node)); /***/1335 _Bool stack_empty(const struct stack * s) { return s->head == NULL; } 1336 void push_stack(struct stack * s, void * value) { 1337 struct stack_node * n = malloc(sizeof(struct stack_node)); /***/ 1338 1338 *n = (struct stack_node){ value, s->head }; /***/ 1339 1339 s->head = n; 1340 1340 } 1341 void * pop_stack(struct stack* s) {1342 struct stack_node * n = s->head;1341 void * pop_stack(struct stack * s) { 1342 struct stack_node * n = s->head; 1343 1343 s->head = n->next; 1344 void * x = n->value;1344 void * x = n->value; 1345 1345 free(n); 1346 1346 return x; 1347 1347 } 1348 void clear_stack(struct stack * s, void (*free_el)(void*)) {1349 for ( struct stack_node * next = s->head; next; ) {1350 struct stack_node * crnt = next;1348 void clear_stack(struct stack * s, void (*free_el)(void *)) { 1349 for ( struct stack_node * next = s->head; next; ) { 1350 struct stack_node * crnt = next; 1351 1351 next = crnt->next; 1352 1352 free_el(crnt->value); … … 1360 1360 \CCV 1361 1361 \begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt] 1362 stack::node::node( const object & v, node* n ) : value( v.new_copy() ), next( n ) {}1363 void stack::copy(const stack & o) {1364 node ** crnt = &head;1365 for ( node * next = o.head; next; next = next->next ) {1362 stack::node::node( const object & v, node * n ) : value( v.new_copy() ), next( n ) {} 1363 void stack::copy(const stack & o) { 1364 node ** crnt = &head; 1365 for ( node * next = o.head; next; next = next->next ) { 1366 1366 *crnt = new node{ *next->value }; 1367 1367 crnt = &(*crnt)->next; … … 1370 1370 } 1371 1371 stack::stack() : head(nullptr) {} 1372 stack::stack(const stack & o) { copy(o); }1373 stack::stack(stack && o) : head(o.head) { o.head = nullptr; }1372 stack::stack(const stack & o) { copy(o); } 1373 stack::stack(stack && o) : head(o.head) { o.head = nullptr; } 1374 1374 stack::~stack() { clear(); } 1375 stack & stack::operator= (const stack& o) {1375 stack & stack::operator= (const stack & o) { 1376 1376 if ( this == &o ) return *this; 1377 1377 clear(); … … 1379 1379 return *this; 1380 1380 } 1381 stack & stack::operator= (stack&& o) {1381 stack & stack::operator= (stack && o) { 1382 1382 if ( this == &o ) return *this; 1383 1383 head = o.head; … … 1386 1386 } 1387 1387 bool stack::empty() const { return head == nullptr; } 1388 void stack::push(const object & value) { head = new node{ value, head }; /***/ }1388 void stack::push(const object & value) { head = new node{ value, head }; /***/ } 1389 1389 ptr<object> stack::pop() { 1390 node * n = head;1390 node * n = head; 1391 1391 head = n->next; 1392 1392 ptr<object> x = std::move(n->value); … … 1395 1395 } 1396 1396 void stack::clear() { 1397 for ( node * next = head; next; ) {1398 node * crnt = next;1397 for ( node * next = head; next; ) { 1398 node * crnt = next; 1399 1399 next = crnt->next; 1400 1400 delete crnt;
Note: See TracChangeset
for help on using the changeset viewer.