DataStructuresDemo.vala 8.5 KB

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