2323#include < cassert>
2424#include < cstddef>
2525#include < memory>
26+ #include < optional>
2627#include < string>
2728#include < unordered_map>
2829#include < unordered_set>
@@ -51,21 +52,30 @@ namespace graphar::builder {
5152 */
5253class Vertex {
5354 public:
54- Vertex () : empty_( true ) {}
55+ Vertex () = default ;
5556
5657 /* *
5758 * @brief Initialize the vertex with a given id.
5859 *
5960 * @param id The id of the vertex.
6061 */
61- explicit Vertex (IdType id) : id_(id), empty_( false ) {}
62+ explicit Vertex (IdType id) : id_(id) {}
6263
6364 /* *
6465 * @brief Get id of the vertex.
6566 *
67+ * The id is absent until explicitly set or assigned by VerticesBuilder.
68+ *
6669 * @return The id of the vertex.
6770 */
68- IdType GetId () const noexcept { return id_; }
71+ IdType GetId () const { return id_.value (); }
72+
73+ /* *
74+ * @brief Check if the vertex id has been initialized.
75+ *
76+ * @return true/false.
77+ */
78+ bool HasId () const noexcept { return id_.has_value (); }
6979
7080 /* *
7181 * @brief Set id of the vertex.
@@ -75,11 +85,11 @@ class Vertex {
7585 void SetId (IdType id) { id_ = id; }
7686
7787 /* *
78- * @brief Check if the vertex is empty .
88+ * @brief Check if the vertex contains no property payload .
7989 *
8090 * @return true/false.
8191 */
82- bool Empty () const noexcept { return empty_ ; }
92+ bool Empty () const noexcept { return properties_. empty () ; }
8393
8494 /* *
8595 * @brief Add a property to the vertex.
@@ -89,7 +99,6 @@ class Vertex {
8999 */
90100 // TODO(@acezen): Enable the property to be a vector(list).
91101 void AddProperty (const std::string& name, const std::any& val) {
92- empty_ = false ;
93102 properties_[name] = val;
94103 }
95104
@@ -100,7 +109,6 @@ class Vertex {
100109 AddProperty (name, val);
101110 return ;
102111 }
103- empty_ = false ;
104112 if (cardinalities_.find (name) != cardinalities_.end ()) {
105113 if (cardinalities_[name] != cardinality) {
106114 throw std::runtime_error (" Cardinality mismatch for property: " + name);
@@ -211,8 +219,7 @@ class Vertex {
211219 }
212220
213221 private:
214- IdType id_;
215- bool empty_;
222+ std::optional<IdType> id_;
216223 std::unordered_map<std::string, std::any> properties_;
217224 std::unordered_map<std::string, Cardinality> cardinalities_;
218225};
0 commit comments