DataStructuresDemo.vala 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using Astralis;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. /**
  5. * DataStructuresDemo Example
  6. *
  7. * Demonstrates various Invercargill data structures.
  8. * Shows how to use Series, Vector, RingBuffer, and other structures
  9. * with the Astralis web framework.
  10. */
  11. // Root handler
  12. class RootEndpoint : Object, Endpoint {
  13. public string route { get { return "/"; } }
  14. public Method[] methods { owned get { return { Method.GET }; } }
  15. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  16. return new HttpStringResult("""Data Structures Demo
  17. This example demonstrates various Invercargill data structures:
  18. Endpoints:
  19. GET /series - Series operations
  20. GET /vector - Vector operations
  21. GET /ring-buffer - RingBuffer operations
  22. GET /immutable-buffer - ImmutableBuffer operations
  23. GET /wrap-operations - Wrap utility functions
  24. GET /combined-operations - Combined operations
  25. """);
  26. }
  27. }
  28. // Series operations
  29. class SeriesEndpoint : Object, Endpoint {
  30. public string route { get { return "/series"; } }
  31. public Method[] methods { owned get { return { Method.GET }; } }
  32. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  33. var series_array = new string[3];
  34. series_array[0] = "First";
  35. series_array[1] = "Second";
  36. series_array[2] = "Third";
  37. var parts = new Series<string>();
  38. parts.add_start("Series contents: ");
  39. foreach (var item in series_array) {
  40. parts.add_start(item + " ");
  41. }
  42. var count = series_array.length;
  43. parts.add_start("\nCount: " + count.to_string());
  44. var result = parts.to_immutable_buffer()
  45. .aggregate<string>("", (acc, s) => acc + s);
  46. return new HttpStringResult(result);
  47. }
  48. }
  49. // Vector operations
  50. class VectorEndpoint : Object, Endpoint {
  51. public string route { get { return "/vector"; } }
  52. public Method[] methods { owned get { return { Method.GET }; } }
  53. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  54. var numbers = new int[5];
  55. numbers[0] = 1;
  56. numbers[1] = 2;
  57. numbers[2] = 3;
  58. numbers[3] = 4;
  59. numbers[4] = 5;
  60. var vector = Wrap.array<int>(numbers).as_enumerable().to_vector();
  61. var count = vector.count();
  62. var first = vector.first_or_default(n => true);
  63. var last = vector.last_or_default(n => true);
  64. var result = @"Vector operations:
  65. Count: $count
  66. First: $first
  67. Last: $last
  68. Elements: 1, 2, 3, 4, 5";
  69. return new HttpStringResult(result);
  70. }
  71. }
  72. // RingBuffer operations
  73. class RingBufferEndpoint : Object, Endpoint {
  74. public string route { get { return "/ring-buffer"; } }
  75. public Method[] methods { owned get { return { Method.GET }; } }
  76. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  77. var buffer_array = new string[5];
  78. buffer_array[0] = "Item 1";
  79. buffer_array[1] = "Item 2";
  80. buffer_array[2] = "Item 3";
  81. buffer_array[3] = "Item 4";
  82. buffer_array[4] = "Item 5";
  83. var buffer = Wrap.array<string>(buffer_array).as_enumerable().to_ring_buffer(3);
  84. var count = buffer.count();
  85. var result = @"RingBuffer operations:
  86. Size: 3
  87. Count: $count
  88. Note: RingBuffer automatically overwrites old items when full";
  89. return new HttpStringResult(result);
  90. }
  91. }
  92. // ImmutableBuffer operations
  93. class ImmutableBufferEndpoint : Object, Endpoint {
  94. public string route { get { return "/immutable-buffer"; } }
  95. public Method[] methods { owned get { return { Method.GET }; } }
  96. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  97. var items_array = new string[4];
  98. items_array[0] = "Apple";
  99. items_array[1] = "Banana";
  100. items_array[2] = "Cherry";
  101. items_array[3] = "Date";
  102. var buffer = Wrap.array<string>(items_array).as_enumerable().to_immutable_buffer();
  103. var count = buffer.count();
  104. var parts = new Series<string>();
  105. parts.add_start("ImmutableBuffer contents: ");
  106. buffer.iterate((item) => {
  107. parts.add_start(item + " ");
  108. });
  109. parts.add_start("\nCount: " + count.to_string());
  110. var result = parts.to_immutable_buffer()
  111. .aggregate<string>("", (acc, s) => acc + s);
  112. return new HttpStringResult(result);
  113. }
  114. }
  115. // Wrap utility functions
  116. class WrapOperationsEndpoint : Object, Endpoint {
  117. public string route { get { return "/wrap-operations"; } }
  118. public Method[] methods { owned get { return { Method.GET }; } }
  119. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  120. var numbers = new int[5];
  121. numbers[0] = 1;
  122. numbers[1] = 2;
  123. numbers[2] = 3;
  124. numbers[3] = 4;
  125. numbers[4] = 5;
  126. var strings = new string[5];
  127. strings[0] = "one";
  128. strings[1] = "two";
  129. strings[2] = "three";
  130. strings[3] = "four";
  131. strings[4] = "five";
  132. var num_count = Wrap.array<int>(numbers).as_enumerable().count();
  133. var str_count = Wrap.array<string>(strings).as_enumerable().count();
  134. var result = @"Wrap operations:
  135. Array of ints count: $num_count
  136. Array of strings count: $str_count
  137. Numbers: 1, 2, 3, 4, 5
  138. Strings: one, two, three, four, five";
  139. return new HttpStringResult(result);
  140. }
  141. }
  142. // Combined operations
  143. class CombinedOperationsEndpoint : Object, Endpoint {
  144. public string route { get { return "/combined-operations"; } }
  145. public Method[] methods { owned get { return { Method.GET }; } }
  146. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  147. var numbers1 = new int[3];
  148. numbers1[0] = 1;
  149. numbers1[1] = 2;
  150. numbers1[2] = 3;
  151. var numbers2 = new int[3];
  152. numbers2[0] = 4;
  153. numbers2[1] = 5;
  154. numbers2[2] = 6;
  155. var concatenated = Wrap.array<int>(numbers1).as_enumerable()
  156. .concat(Wrap.array<int>(numbers2).as_enumerable());
  157. var sum = concatenated.aggregate<int>(0, (acc, n) => acc + n);
  158. var avg = (double)sum / concatenated.count();
  159. var filtered = concatenated.where(n => n > 2);
  160. var sorted = filtered.order_by<int>(n => n);
  161. var parts = new Series<string>();
  162. parts.add_start("Combined operations:\n");
  163. parts.add_start("Original arrays: [1,2,3] and [4,5,6]\n");
  164. parts.add_start("Concatenated: ");
  165. parts.add_start(enumerable_int_to_string(concatenated));
  166. parts.add_start("\n");
  167. parts.add_start(@"Sum: $sum\n");
  168. parts.add_start(@"Average: $avg\n");
  169. parts.add_start("Filtered (>2): ");
  170. parts.add_start(enumerable_int_to_string(filtered));
  171. parts.add_start("\n");
  172. parts.add_start("Sorted: ");
  173. parts.add_start(enumerable_int_to_string(sorted));
  174. var result = parts.to_immutable_buffer()
  175. .aggregate<string>("", (acc, s) => acc + s);
  176. return new HttpStringResult(result);
  177. }
  178. }
  179. void main() {
  180. var router = new EndpointRouter()
  181. .add_endpoint(new RootEndpoint())
  182. .add_endpoint(new SeriesEndpoint())
  183. .add_endpoint(new VectorEndpoint())
  184. .add_endpoint(new RingBufferEndpoint())
  185. .add_endpoint(new ImmutableBufferEndpoint())
  186. .add_endpoint(new WrapOperationsEndpoint())
  187. .add_endpoint(new CombinedOperationsEndpoint());
  188. var pipeline = new Pipeline()
  189. .add_component(router);
  190. var server = new Server(8086, pipeline);
  191. print("Data Structures Demo Server running on port 8086\n");
  192. print("Try these endpoints:\n");
  193. print(" - http://localhost:8086/\n");
  194. print(" - http://localhost:8086/series\n");
  195. print(" - http://localhost:8086/vector\n");
  196. print(" - http://localhost:8086/ring-buffer\n");
  197. print(" - http://localhost:8086/immutable-buffer\n");
  198. print(" - http://localhost:8086/wrap-operations\n");
  199. print(" - http://localhost:8086/combined-operations\n");
  200. server.run();
  201. }
  202. // Helper functions
  203. string enumerable_int_to_string(Enumerable<int> enumerable) {
  204. return enumerable.to_immutable_buffer()
  205. .aggregate<string>("[", (acc, n) => {
  206. if (acc == "[") return acc + n.to_string();
  207. return acc + ", " + n.to_string();
  208. }) + "]";
  209. }