The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

137 lines
4.1 KiB

2 months ago
  1. // a sketch of what the new API might look like
  2. #include "yaml-cpp/yaml.h"
  3. #include <iostream>
  4. int main() {
  5. {
  6. // test.yaml
  7. // - foo
  8. // - primes: [2, 3, 5, 7, 11]
  9. // odds: [1, 3, 5, 7, 9, 11]
  10. // - [x, y]
  11. // move-like semantics
  12. YAML::Value root = YAML::Parse("test.yaml");
  13. std::cout << root[0].as<std::string>(); // "foo"
  14. std::cout << str(root[0]); // "foo", shorthand?
  15. std::cout << root[1]["primes"][3].as<int>(); // "7"
  16. std::cout << root[1]["odds"][6].as<int>(); // throws?
  17. root[2].push_back(5);
  18. root[3] = "Hello, World";
  19. root[0].reset();
  20. root[0]["key"] = "value";
  21. std::cout << root;
  22. // # not sure about formatting
  23. // - {key: value}
  24. // - primes: [2, 3, 5, 7, 11]
  25. // odds: [1, 3, 5, 7, 9, 11]
  26. // - [x, y, 5]
  27. // - Hello, World
  28. }
  29. {
  30. // for all copy-like commands, think of python's "name/value" semantics
  31. YAML::Value root = "Hello"; // Hello
  32. root = YAML::Sequence(); // []
  33. root[0] = 0; // [0]
  34. root[2] = "two"; // [0, ~, two] # forces root[1] to be initialized to null
  35. YAML::Value other = root; // both point to the same thing
  36. other[0] = 5; // now root[0] is 0 also
  37. other.push_back(root); // &1 [5, ~, two, *1]
  38. other[3][0] = 0; // &1 [0, ~, two, *1] # since it's a true alias
  39. other.push_back(Copy(root)); // &1 [0, ~, two, *1, &2 [0, ~, two, *2]]
  40. other[4][0] = 5; // &1 [0, ~, two, *1, &2 [5, ~, two, *2]] # they're
  41. // really different
  42. }
  43. {
  44. YAML::Value node; // ~
  45. node[0] = 1; // [1] # auto-construct a sequence
  46. node["key"] = 5; // {0: 1, key: 5} # auto-turn it into a map
  47. node.push_back(10); // error, can't turn a map into a sequence
  48. node.erase("key"); // {0: 1} # still a map, even if we remove the key that
  49. // caused the problem
  50. node = "Hello"; // Hello # assignment overwrites everything, so it's now
  51. // just a plain scalar
  52. }
  53. {
  54. YAML::Value map; // ~
  55. map[3] = 1; // {3: 1} # auto-constructs a map, *not* a sequence
  56. YAML::Value seq; // ~
  57. seq = YAML::Sequence(); // []
  58. seq[3] = 1; // [~, ~, ~, 1]
  59. }
  60. {
  61. YAML::Value node; // ~
  62. node[0] = node; // &1 [*1] # fun stuff
  63. }
  64. {
  65. YAML::Value node;
  66. YAML::Value subnode =
  67. node["key"]; // 'subnode' is not instantiated ('node' is still null)
  68. subnode = "value"; // {key: value} # now it is
  69. YAML::Value subnode2 = node["key2"];
  70. node["key3"] = subnode2; // subnode2 is still not instantiated, but
  71. // node["key3"] is "pseudo" aliased to it
  72. subnode2 = "monkey"; // {key: value, key2: &1 monkey, key3: *1} # bam! it
  73. // instantiates both
  74. }
  75. {
  76. YAML::Value seq = YAML::Sequence();
  77. seq[0] = "zero"; // [zero]
  78. seq[1] = seq[0]; // [&1 zero, *1]
  79. seq[0] = seq[1]; // [&1 zero, *1] # no-op (they both alias the same thing,
  80. // so setting them equal is nothing)
  81. Is(seq[0], seq[1]); // true
  82. seq[1] = "one"; // [&1 one, *1]
  83. UnAlias(seq[1]); // [one, one]
  84. Is(seq[0], seq[1]); // false
  85. }
  86. {
  87. YAML::Value root;
  88. root.push_back("zero");
  89. root.push_back("one");
  90. root.push_back("two");
  91. YAML::Value two = root[2];
  92. root = "scalar"; // 'two' is still "two", even though 'root' is "scalar"
  93. // (the sequence effectively no longer exists)
  94. // Note: in all likelihood, the memory for nodes "zero" and "one" is still
  95. // allocated. How can it go away? Weak pointers?
  96. }
  97. {
  98. YAML::Value root; // ~
  99. root[0] = root; // &1 [*1]
  100. root[0] = 5; // [5]
  101. }
  102. {
  103. YAML::Value root;
  104. YAML::Value key;
  105. key["key"] = "value";
  106. root[key] = key; // &1 {key: value}: *1
  107. }
  108. {
  109. YAML::Value root;
  110. root[0] = "hi";
  111. root[1][0] = "bye";
  112. root[1][1] = root; // &1 [hi, [bye, *1]] # root
  113. YAML::Value sub = root[1]; // &1 [bye, [hi, *1]] # sub
  114. root = "gone"; // [bye, gone] # sub
  115. }
  116. return 0;
  117. }