void sort_four_numbers(_ulong a[]) { if(a[0]>a[1]) swap_values(a+0,a+1); if(a[2]>a[3]) swap_values(a+2,a+3); if(a[0]>a[2]) swap_values(a+0,a+2); if(a[1]>a[3]) swap_values(a+1,a+3); if(a[1]>a[2]) swap_values(a+1,a+2); return; }
void iterate_node_2(node_t *node) { int action; action = get_action_2(node); switch (action) { case 0: invert_value(node); swap_values(node, node->link_0); break; case 1: invert_value(node); swap_values(node, node->link_1); break; case 2: invert_value(node); swap_values(node, node->link_2); break; case 3: connect_nodes(node, node->link_0, node->link_1); break; case 4: connect_nodes(node, node->link_1, node->link_2); break; case 5: connect_nodes(node, node->link_2, node->link_0); break; case 6: swap_nodes(node, node->link_0); break; case 7: swap_nodes(node, node->link_1); break; } /* node->value = 0; node->value = 1; invert_value(node); connect_nodes(node, node->link_0, node->link_1); swap_values(node, node->link_0); swap_values(node, node->link_1); node->link_0->value = node->value; node->link_0->value = 0; node->link_0->value = 1; node->link_0->value = inverted_value(node); node->link_1->value = node->value; node->link_1->value = 0; node->link_1->value = 1; node->link_1->value = inverted_value(node); swap_nodes(node, node->link_0); swap_nodes(node, node->link_1); */ }
// this function is called when we need to detect next option entry in argv // return value: // true - next option entry found, retval not used // false - no more options, retval - reason (-1 no more options, 1 - argument processed as default // option with code 1) static bool detect_next_option(int argc, char* const argv[], const getopt_param& param, int& retval) { if (!is_option(argv[optind])) { // if optind points to non-option - then check it... if (is_finish(argv[optind])) { // is it terminate sequence ('--')? optind++; if(!param.posix && !param.process_all) swap_values(argc, (char**)argv, optind); retval = -1; return false; } else if (param.process_all) {// processing all elements optarg = (char*)argv[optind]; optind++; retval = 1; return false; } else if (param.posix) { retval = -1; return false; } else { int next_ind = look_for_option(argc, argv, optind); assert(next_ind != optind); // should not be same if (next_ind > 0) { add_to_swap(optind, next_ind - optind); // marking elements for swap /* NOTE: in swap_list stored indexes of elements which are not detected as options and not detected as arguments (if argv[x] is detected as option - stsrts with '-', it doesn't added to this list). later, when no more element may be detected, these stored elements are moved to end of argv vector */ optind = next_ind; } else {// no more options found swap_values(argc, (char**)argv, optind); retval = -1; return false; } } if (is_finish(argv[optind])) { optind++; if (!param.posix && !param.process_all) swap_values(argc, (char**)argv, optind); retval = -1; return false; } } return true; }
int main() { int i {1}; int j {2}; std::cout << i << ", " << j << std::endl; swap_values(i, j); std::cout << i << ", " << j << std::endl; double a {3.1}; double b {5.2}; std::cout << a << ", " << b << std::endl; swap_values(a, b); std::cout << a << ", " << b << std::endl; return 0; }
/* * Given a heap and an index, sift_up checks to see if the value * at that index needs to be "sifted upward" in the heap, to * preserve the heap properties. Specifically, a value needs to * be moved up in the heap if it is less than its parent value. * (This is just the "order" property; the "shape" property is * not affected by sifting a value up.) */ void sift_up(float_heap *pHeap, int index) { int parent_index = PARENT(index); /* If the index to sift up is the root, we are done. */ if (index == 0) return; assert(parent_index >= 0); assert(parent_index != index); /* Parent of index 0 = 0... that's bad. */ /* If the specified value is smaller than its parent value then * we have to swap the value and its parent. */ if (pHeap->values[index] < pHeap->values[parent_index]) { /* Swap the value with its parent value. */ swap_values(pHeap, index, parent_index); /* If we haven't gotten to the root, we might have to * sift up again. */ if (parent_index != 0) sift_up(pHeap, parent_index); } }
void main(void) { int one = 1, two = 2; swap_values(&one, &two); printf("one contains %d two contains %d\n", one, two); }
// Destructive sort !! int * insert_sort(int * arr, int size) { int i, j; for(i = 1; i < size; ++i) for(j = 0; j < i; ++j) if(arr[i] < arr[j]) swap_values(arr, i, j); return arr; }
void main(void) { int a = 1, b = 2; printf("Original values a %d b %d\n", a, b); swap_values(a, b); printf("Swapped values a %d b %d\n", a, b); }
void image_input_command_t::redo() { node_.clips()[proxy_level_].swap( old_clip_); node_.create_reader( proxy_level_); node_.set_frame( app().document().composition().frame()); if( proxy_level_ == 0) { swap_values(); node_.update_widgets(); } node_.notify(); undo::command_t::redo(); }
int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex) { if (!argc || !argv) return -1; getopt_param param = scan_param(optstring ? optstring : ""); if (optind >= argc) { if (!param.posix && !param.process_all) swap_values(argc, (char**)argv, optind); return -1; } optarg = 0; if (!nextchar) {// we are in begin of argument... no previous iterations on this arg int retval; if (!detect_next_option(argc, argv, param, retval)) return retval; const char* str = argv[optind]; if (is_long_option(str)) { int _longindex; if (!longindex) // if no longindex specified - create own temp... longindex = &_longindex; return get_long_option(argc, argv, param, longopts, longindex); } else if (is_short_option(str)) { return get_long_short_option(argc, argv, param, longopts, longindex); } } return get_short_option(argc, argv, param); }
int getopt(int argc, char* const argv[], const char *optstring) { if (!argc || !argv || !optstring) return -1; getopt_param param = scan_param(optstring ? optstring : ""); if (optind >= argc) { if (!param.posix && !param.process_all) swap_values(argc, (char**)argv, optind); return -1; } optarg = 0; if (!nextchar) {// we are in begin of argument... no previous iterations on this arg int retval; if (!detect_next_option(argc, argv, param, retval)) return retval; nextchar = short_option_begin(argv[optind]); } return get_short_option(argc, argv, param); }
void iterate_node_3(node_t *node) { int action; action = get_action_3(node); switch (action) { case 0: node->link_1->value = node->value; case 9: connect_nodes(node, node->link_0, node->link_1); swap_nodes(node, node->link_2); break; case 1: node->link_2->value = node->value; case 10: connect_nodes(node, node->link_1, node->link_2); swap_nodes(node, node->link_0); break; case 2: node->link_0->value = node->value; case 11: connect_nodes(node, node->link_2, node->link_0); swap_nodes(node, node->link_1); break; case 3: disconnect_nodes(node, node->link_0); case 12: swap_nodes(node, node->link_0); break; case 4: disconnect_nodes(node, node->link_1); case 13: swap_nodes(node, node->link_1); break; case 5: disconnect_nodes(node, node->link_2); case 14: swap_nodes(node, node->link_2); break; case 6: case 15: swap_values(node, node->link_0); break; case 7: swap_values(node, node->link_1); break; case 8: swap_values(node, node->link_2); break; } /* node->value = 0; node->value = 1; invert_value(node); disconnect_nodes(node, node->link_0); disconnect_nodes(node, node->link_1); disconnect_nodes(node, node->link_2); connect_nodes(node, node->link_0, node->link_1); connect_nodes(node, node->link_1, node->link_2); connect_nodes(node, node->link_2, node->link_0); swap_values(node, node->link_0); swap_values(node, node->link_1); swap_values(node, node->link_2); node->link_0->value = node->value; node->link_0->value = 0; node->link_0->value = 1; node->link_0->value = inverted_value(node); node->link_1->value = node->value; node->link_1->value = 0; node->link_1->value = 1; node->link_1->value = inverted_value(node); node->link_2->value = node->value; node->link_2->value = 0; node->link_2->value = 1; node->link_2->value = inverted_value(node); swap_nodes(node, node->link_0); swap_nodes(node, node->link_1); swap_nodes(node, node->link_2); */ }
/* * Given a heap and an index, sift_down checks to see if the value * at that index needs to be "sifted downward" in the heap, to * preserve the heap properties. Specifically, a value needs to * be moved down in the heap if it is greater than either of its * children's values. (This is the "order" property.) In order * to preserve the "shape" property of heaps, the value is swapped * with the *smaller* of its two child values. * * If a value only has a left child, then only the left child is * examined for the swap. * * If a value has both left and right children, it is possible * that one child may be larger than the value, while the other is * smaller than the value. Since we swap with the smallest child * value, we preserve the heap properties even in that situation. */ void sift_down(float_heap *pHeap, int index) { assert(pHeap != NULL); assert(index < pHeap->num_values); int left_child = LEFT_CHILD(index); int right_child = RIGHT_CHILD(index); if (left_child >= pHeap->num_values) { /* If the left child's index is past the end of the heap * then this value has no children. We're done. */ return; } if (right_child >= pHeap->num_values) { /* Only have a left child. */ if (pHeap->values[left_child] < pHeap->values[index]) { /* Left child value is smaller. Swap this value and the * left child value. */ swap_values(pHeap, index, left_child); /* Don't need to call sift_down again, because if this * node only has a left child, we are at the bottom of * the heap. */ } } else { /* This value has a left and right child. */ float left_val = pHeap->values[left_child]; float right_val = pHeap->values[right_child]; int swap_child; if (left_val < pHeap->values[index] || right_val < pHeap->values[index]) { /* Need to swap this node with one of its children. Pick * the smaller of the two children, since this is a min-heap * and that will preserve the heap properties. */ if (left_val < right_val) swap_child = left_child; else swap_child = right_child; /* Do the swap, then call sift_down again, in case we aren't * at the bottom of the heap yet. */ swap_values(pHeap, index, swap_child); sift_down(pHeap, swap_child); } } }