gbonik commented on code in PR #11977:
URL: https://github.com/apache/tvm/pull/11977#discussion_r918260734


##########
src/node/object_path.cc:
##########
@@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tvm/node/object_path.h>
+#include <tvm/node/repr_printer.h>
+#include <tvm/runtime/memory.h>
+#include <tvm/runtime/registry.h>
+
+#include <algorithm>
+#include <cstring>
+
+using namespace tvm::runtime;
+
+namespace tvm {
+
+// ============== ObjectPathNode ==============
+
+ObjectPathNode::ObjectPathNode(ObjectPathNode* parent)
+    : parent_(GetRef<ObjectRef>(parent)), length_(parent == nullptr ? 1 : 
parent->length_ + 1) {}
+
+// --- GetParent ---
+
+ObjectPath ObjectPathNode::GetParent() const { return 
Downcast<ObjectPath>(parent_); }
+
+TVM_REGISTER_GLOBAL("node.ObjectPathGetParent").set_body_typed([](const 
ObjectPath& path) {
+  return path->GetParent();
+});
+
+// --- Length ---
+
+size_t ObjectPathNode::Length() const { return length_; }
+
+TVM_REGISTER_GLOBAL("node.ObjectPathLength").set_body_typed([](const 
ObjectPath& path) {
+  return static_cast<int64_t>(path->Length());
+});
+
+// --- GetPrefix ---
+
+ObjectPath ObjectPathNode::GetPrefix(size_t length) const {
+  if (length > Length()) {
+    throw std::out_of_range("Attempted to get a prefix longer than the path 
itself");
+  }
+
+  const ObjectPathNode* node = this;
+  size_t suffix_len = Length() - length;
+  for (size_t i = 0; i < suffix_len; ++i) {
+    node = node->ParentNode();
+  }
+
+  return GetRef<ObjectPath>(node);
+}
+
+TVM_REGISTER_GLOBAL("node.ObjectPathGetPrefix")
+    .set_body_typed([](const ObjectPath& path, int64_t length) {
+      if (length < 0) {
+        throw std::out_of_range("Prefix length can't be negative");
+      }
+      return path->GetPrefix(static_cast<size_t>(length));
+    });
+
+// --- IsPrefixOf ---
+
+bool ObjectPathNode::IsPrefixOf(const ObjectPath& other) const {
+  if (!other.defined()) {
+    return false;
+  }
+
+  size_t this_len = Length();
+  if (this_len > other->Length()) {
+    return false;
+  }
+  return this->PathsEqual(other->GetPrefix(this_len));
+}
+
+TVM_REGISTER_GLOBAL("node.ObjectPathIsPrefixOf")
+    .set_body_typed([](const ObjectPath& a, const ObjectPath& b) { return 
a->IsPrefixOf(b); });
+
+// --- Attr ---
+
+ObjectPath ObjectPathNode::Attr(const char* attr_key) {
+  if (attr_key != nullptr) {
+    return ObjectPath(make_object<AttributeAccessPathNode>(this, attr_key));
+  } else {
+    return ObjectPath(make_object<UnknownAttributeAccessPathNode>(this));
+  }
+}
+
+ObjectPath ObjectPathNode::Attr(String attr_key) {
+  if (attr_key.defined()) {
+    return ObjectPath(make_object<AttributeAccessPathNode>(this, attr_key));
+  } else {
+    return ObjectPath(make_object<UnknownAttributeAccessPathNode>(this));
+  }
+}

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to