summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTomohiro <gpuppur@gmail.com>2024-02-09 06:18:52 +0900
committerGitHub <noreply@github.com>2024-02-08 22:18:52 +0100
commitbefb383ac8f033be94ca83845b1a26e9feeb3306 (patch)
tree7e89083b5677d89dd0f8b623f21c3633b154faaa
parent4b67cccf5097cc5d2a592bf78ae2746cc3ee8959 (diff)
downloadNim-befb383ac8f033be94ca83845b1a26e9feeb3306.tar.gz
fixes #23275; Add `==` for Deque (#23276)
-rw-r--r--lib/pure/collections/deques.nim29
-rw-r--r--tests/stdlib/tdeques.nim49
2 files changed, 77 insertions, 1 deletions
diff --git a/lib/pure/collections/deques.nim b/lib/pure/collections/deques.nim
index b07138e84..a329f523a 100644
--- a/lib/pure/collections/deques.nim
+++ b/lib/pure/collections/deques.nim
@@ -50,7 +50,7 @@ runnableExamples:
 
 import std/private/since
 
-import std/math
+import std/[hashes, math]
 
 type
   Deque*[T] = object
@@ -455,3 +455,30 @@ proc `$`*[T](deq: Deque[T]): string =
     if result.len > 1: result.add(", ")
     result.addQuoted(x)
   result.add("]")
+
+func `==`*[T](deq1, deq2: Deque[T]): bool =
+  ## The `==` operator for Deque.
+  ## Returns `true` if both deques contains the same values in the same order.
+  runnableExamples:
+    var a, b = initDeque[int]()
+    a.addFirst(2)
+    a.addFirst(1)
+    b.addLast(1)
+    b.addLast(2)
+    doAssert a == b
+
+  if deq1.count != deq2.count:
+    return false
+
+  for i in 0 ..< deq1.count:
+    if deq1.data[(deq1.head + i) and deq1.mask] != deq2.data[(deq2.head + i) and deq2.mask]:
+      return false
+
+  true
+
+func hash*[T](deq: Deque[T]): Hash =
+  ## Hashing of Deque.
+  var h: Hash = 0
+  for x in deq:
+    h = h !& hash(x)
+  !$h
diff --git a/tests/stdlib/tdeques.nim b/tests/stdlib/tdeques.nim
index 49072b150..39ff996d1 100644
--- a/tests/stdlib/tdeques.nim
+++ b/tests/stdlib/tdeques.nim
@@ -189,6 +189,55 @@ proc main() =
     a.shrink(fromFirst = 0, fromLast = 1)
     doAssert $a == "[10, 20, 30]"
 
+  block:
+    var a, b: Deque[int]
+    for i in 1 .. 256:
+      a.addLast(i)
+    for i in 1 .. 255:
+      a.popLast
+    b.addLast(1)
+    doAssert a == b
+
+  block:
+    # Issue 23275
+    # Test `==`.
+    block:
+      var a, b = initDeque[int]()
+      doAssert a == b
+      doAssert a.hash == b.hash
+      a.addFirst(1)
+      doAssert a != b
+      doAssert a.hash != b.hash
+      b.addLast(1)
+      doAssert a == b
+      doAssert a.hash == b.hash
+      a.popFirst
+      b.popLast
+      doAssert a == b
+      doAssert a.hash == b.hash
+      a.addLast 2
+      doAssert a != b
+      doAssert a.hash != b.hash
+      b.addFirst 2
+      doAssert a == b
+      doAssert a.hash == b.hash
+
+    block:
+      var a, b = initDeque[int]()
+      for i in countDown(100, 1):
+        a.addFirst(i)
+      for i in 1..100:
+        b.addLast(i)
+      doAssert a == b
+      for i in 1..99:
+        a.popLast
+      let a1 = [1].toDeque
+      doAssert a == a1
+      doAssert a.hash == a1.hash
+      var c = initDeque[int]()
+      c.addLast(1)
+      doAssert a == c
+      doAssert a.hash == c.hash
 
 static: main()
 main()