Переглянути джерело

Add UA_ExpandedNodeId_order and _hash

Julius Pfrommer 5 роки тому
батько
коміт
9e5f3b0d35
2 змінених файлів з 41 додано та 14 видалено
  1. 11 3
      include/open62541/types.h
  2. 30 11
      src/ua_types.c

+ 11 - 3
include/open62541/types.h

@@ -388,11 +388,19 @@ typedef struct {
     UA_UInt32 serverIndex;
 } UA_ExpandedNodeId;
 
-UA_Boolean UA_EXPORT UA_ExpandedNodeId_equal(const UA_ExpandedNodeId *n1,
-                                             const UA_ExpandedNodeId *n2);
-
 UA_EXPORT extern const UA_ExpandedNodeId UA_EXPANDEDNODEID_NULL;
 
+UA_Order UA_EXPORT
+UA_ExpandedNodeId_order(const UA_ExpandedNodeId *n1, const UA_ExpandedNodeId *n2);
+
+static UA_INLINE UA_Boolean
+UA_ExpandedNodeId_equal(const UA_ExpandedNodeId *n1, const UA_ExpandedNodeId *n2) {
+    return (UA_ExpandedNodeId_order(n1, n2) == UA_ORDER_EQ);
+}
+
+/* Returns a non-cryptographic hash for the NodeId */
+UA_UInt32 UA_EXPORT UA_ExpandedNodeId_hash(const UA_ExpandedNodeId *n);
+
 /** The following functions are shorthand for creating ExpandedNodeIds. */
 static UA_INLINE UA_ExpandedNodeId
 UA_EXPANDEDNODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {

+ 30 - 11
src/ua_types.c

@@ -354,17 +354,6 @@ UA_NodeId_order(const UA_NodeId *n1, const UA_NodeId *n2) {
     return UA_ORDER_EQ;
 }
 
-UA_Boolean
-UA_ExpandedNodeId_equal(const UA_ExpandedNodeId *n1, const UA_ExpandedNodeId *n2) {
-    if(n1 == NULL || n2 == NULL)
-        return false;
-    if(n1->serverIndex != n2->serverIndex)
-        return false;
-    if(!UA_String_equal(&n1->namespaceUri, &n2->namespaceUri))
-        return false;
-    return UA_NodeId_equal(&n1->nodeId, &n2->nodeId);
-}
-
 /* FNV non-cryptographic hash function. See
  * https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function */
 #define FNV_PRIME_32 16777619
@@ -408,6 +397,36 @@ ExpandedNodeId_copy(UA_ExpandedNodeId const *src, UA_ExpandedNodeId *dst,
     return retval;
 }
 
+UA_Order
+UA_ExpandedNodeId_order(const UA_ExpandedNodeId *n1,
+                        const UA_ExpandedNodeId *n2) {
+    if(n1->serverIndex > n2->serverIndex)
+        return UA_ORDER_MORE;
+    if(n1->serverIndex < n2->serverIndex)
+        return UA_ORDER_LESS;
+    if(n1->namespaceUri.length > 0) {
+        if(n1->namespaceUri.length > n2->namespaceUri.length)
+            return UA_ORDER_MORE;
+        if(n1->namespaceUri.length < n2->namespaceUri.length)
+            return UA_ORDER_LESS;
+        int cmp = strncmp((const char*)n1->namespaceUri.data,
+                          (const char*)n2->namespaceUri.data,
+                          n1->namespaceUri.length);
+        if(cmp < 0)
+            return UA_ORDER_LESS;
+        if(cmp > 0)
+            return UA_ORDER_MORE;
+    }
+    return UA_NodeId_order(&n1->nodeId, &n2->nodeId);
+}
+
+u32
+UA_ExpandedNodeId_hash(const UA_ExpandedNodeId *n) {
+    u32 h = UA_NodeId_hash(&n->nodeId);
+    h = fnv32(h, (const UA_Byte*)&n->serverIndex, 4);
+    return fnv32(h, n->namespaceUri.data, n->namespaceUri.length);
+}
+
 /* ExtensionObject */
 static void
 ExtensionObject_clear(UA_ExtensionObject *p, const UA_DataType *_) {