DataStructuresDemo.vala 8.0 KB

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