Guest User

Untitled

a guest
Nov 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. void explicit_euler(T period, T time_step, !тутдолжнабытьфункция);
  2.  
  3. std::vector<vector_type> quadratic(const std::vector<object_type *>& objects);
  4. std::vector<vector_type> finit_element(const std::vector<object_type *>& objects);
  5.  
  6. void simulate (QString state_directory_name,
  7. T period,
  8. T time_step,
  9. time_method time = time_method::explicit_euler,
  10. force_method force = force_method::quadratic);
  11.  
  12. enum class time_method { explicit_euler };
  13. enum class force_method { quadratic, finit_element };
  14. using system_type = gravity::system<T, dim>;
  15. using object_type = gravity::object<T, dim>;
  16. using vector_type = geometry::vector<T, dim>;
  17.  
  18. template<typename T, size_t dim>
  19. void model<T, dim>::simulate (QString state_directory_name,
  20. T period, T time_step,
  21. time_method time,
  22. force_method force)
  23. {
  24. std::function<std::vector<vector_type>(const std::vector<object_type *>&)> method;
  25. switch(force)
  26. {
  27. case force_method::quadratic:
  28. {
  29. method = quadratic;
  30. break;
  31. }
  32. case force_method::finit_element:
  33. {
  34. method = finit_element;
  35. break;
  36. }
  37. }
  38.  
  39. switch(time)
  40. {
  41. case time_method::explicit_euler:
  42. {
  43. explicit_euler(period, time_step, method);
  44. break;
  45. }
  46. }
  47. }
  48.  
  49. std::vector<vector_type> quadratic(const std::vector<object_type *>& objects);
  50. std::vector<vector_type> finit_element(const std::vector<object_type *>& objects);
  51.  
  52. std::function<std::vector<vector_type>(const std::vector<object_type *>&)>
  53.  
  54. #include<vector>
  55. #include<array>
  56. #include<functional>
  57.  
  58. using vector_type = std::array<int,3>;
  59. using object_type = vector_type;
  60.  
  61. std::vector<vector_type> quadratic(const std::vector<object_type*>& objects){
  62. return {std::array<int,3>{1,2,3}};
  63. }
  64.  
  65. std::vector<vector_type> finit_element(const std::vector<object_type*>& objects){
  66. return {};
  67. }
  68. volatile static int v = 0;
  69. auto foo(){
  70. std::function<std::vector<vector_type>(const std::vector<object_type*>&)> fn;
  71. if(v == 0)
  72. fn = quadratic;
  73. else
  74. fn = finit_element;
  75. return 0;
  76. }
  77.  
  78. #include<vector>
  79. #include<array>
  80. #include<functional>
  81. #include<iostream>
  82. class MyInt{
  83. int v_;
  84. public:
  85. MyInt(int v): v_{v} { }
  86.  
  87. // Функция - член
  88. void setV(int v) { v_ = v; }
  89. int getV()const {return v_;}
  90.  
  91. // Статическая функция
  92. static int static_setV(MyInt& self, int v) {
  93. return self.v_ = v; // Обратите внимание, есть доступ к private - полю
  94. // Кроме того, для шаблонных типов доступны все соответствующие типы шаблона
  95. }
  96. };
  97.  
  98. // Свободная функция
  99. void free_setV(MyInt& i, int v){
  100. i.setV(v); // Нет доступа к private-полю, если не объявлена дружественной
  101. }
  102.  
  103. int main (){
  104. std::function<void(MyInt&, int)> set_value_fn;
  105. MyInt i {0};
  106. std::cout << i.getV() <<std::endl;
  107.  
  108. set_value_fn = MyInt::static_setV;
  109. set_value_fn(i, 1);
  110. std::cout << i.getV() <<std::endl;
  111.  
  112. set_value_fn = free_setV;
  113. set_value_fn(i, 2);
  114. std::cout << i.getV() <<std::endl;
  115.  
  116. set_value_fn = &MyInt::setV;
  117. set_value_fn(i, 3);
  118. std::cout << i.getV() <<std::endl;
  119.  
  120. return 0;
  121. }
  122.  
  123. template<typename T, size_t dim>
  124. void model<T, dim>::simulate (QString state_directory_name,
  125. T period, T time_step,
  126. time_method time,
  127. force_method force)
  128. {
  129. std::function<std::vector<vector_type>(const model<T, dim>&, const std::vector<object_type *>&)> method;
  130. switch(force)
  131. {
  132. case force_method::quadratic:
  133. {
  134. method = &model<T,dim>::quadratic;
  135. break;
  136. }
  137. case force_method::finit_element:
  138. {
  139. method = &model<T,dim>::finit_element;
  140. break;
  141. }
  142. }
  143.  
  144. switch(time)
  145. {
  146. case time_method::explicit_euler:
  147. {
  148. explicit_euler(period, time_step, method);
  149. break;
  150. }
  151. }
  152. }
  153.  
  154. std::vector<vector_type> quadratic(const std::vector<object_type *>& objects) const;
  155. std::vector<vector_type> finit_element(const std::vector<object_type *>& objects) const;
  156.  
  157. void explicit_euler(T period, T time_step,
  158. std::function<std::vector<vector_type>(const model<T, dim>&, const std::vector<object_type *>&)> method);
  159.  
  160. method(*this, objects);
Add Comment
Please, Sign In to add comment