Skip to content

Commit 47a4456

Browse files
FlyCloudCbobzhang
authored andcommitted
refactor(linked_hash_map): change type of prev and tail to Int
1 parent 6a43c4c commit 47a4456

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

builtin/linked_hash_map.mbt

+12-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
///|
1616
// Types
1717
priv struct Entry[K, V] {
18-
mut prev : Int?
18+
mut prev : Int
1919
mut next : Entry[K, V]?
2020
mut psl : Int
2121
hash : Int
@@ -47,7 +47,7 @@ struct Map[K, V] {
4747
mut capacity_mask : Int // capacity_mask = capacity - 1, used to find idx
4848
mut growAt : Int // threshold that triggers grow
4949
mut head : Entry[K, V]? // head of linked list
50-
mut tail : Int? // tail of linked list
50+
mut tail : Int // tail of linked list
5151
}
5252
5353
// Implementations
@@ -90,7 +90,7 @@ pub fn Map::new[K, V](capacity~ : Int = 8) -> Map[K, V] {
9090
growAt: calc_grow_threshold(capacity),
9191
entries: FixedArray::make(capacity, None),
9292
head: None,
93-
tail: None,
93+
tail: -1,
9494
}
9595
}
9696
@@ -165,8 +165,8 @@ fn Map::set_entry[K, V](
165165
) -> Unit {
166166
self.entries[new_idx] = Some(entry)
167167
match entry.next {
168-
None => self.tail = Some(new_idx)
169-
Some(next) => next.prev = Some(new_idx)
168+
None => self.tail = new_idx
169+
Some(next) => next.prev = new_idx
170170
}
171171
}
172172
@@ -347,24 +347,19 @@ fn Map::add_entry_to_tail[K, V](
347347
entry : Entry[K, V]
348348
) -> Unit {
349349
match self.tail {
350-
None => {
351-
self.head = Some(entry)
352-
self.tail = Some(idx)
353-
}
354-
Some(tail) => {
355-
self.entries[tail].unwrap().next = Some(entry)
356-
self.tail = Some(idx)
357-
}
350+
-1 => self.head = Some(entry)
351+
tail => self.entries[tail].unwrap().next = Some(entry)
358352
}
353+
self.tail = idx
359354
self.entries[idx] = Some(entry)
360355
self.size += 1
361356
}
362357
363358
///|
364359
fn Map::remove_entry[K, V](self : Map[K, V], entry : Entry[K, V]) -> Unit {
365360
match entry.prev {
366-
None => self.head = entry.next
367-
Some(idx) => self.entries[idx].unwrap().next = entry.next
361+
-1 => self.head = entry.next
362+
idx => self.entries[idx].unwrap().next = entry.next
368363
}
369364
match entry.next {
370365
None => self.tail = entry.prev
@@ -395,7 +390,7 @@ fn Map::grow[K : Hash + Eq, V](self : Map[K, V]) -> Unit {
395390
self.growAt = calc_grow_threshold(self.capacity)
396391
self.size = 0
397392
self.head = None
398-
self.tail = None
393+
self.tail = -1
399394
loop old_head {
400395
Some({ next, key, value, .. }) => {
401396
self.set(key, value)
@@ -491,7 +486,7 @@ pub fn Map::clear[K, V](self : Map[K, V]) -> Unit {
491486
self.entries.fill(None)
492487
self.size = 0
493488
self.head = None
494-
self.tail = None
489+
self.tail = -1
495490
}
496491
497492
///|

builtin/linked_hash_map_wbtest.mbt

+10-11
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ test "clear" {
191191
assert_eq!(m.size, 0)
192192
assert_eq!(m.capacity, 8)
193193
assert_eq!(m.head, None)
194-
assert_eq!(m.tail, None)
194+
assert_eq!(m.tail, -1)
195195
for i in 0..<m.capacity {
196196
assert_eq!(m.entries[i], None)
197197
}
@@ -406,7 +406,7 @@ test "clear" {
406406
assert_eq!(m.size, 0)
407407
assert_eq!(m.capacity, 8)
408408
assert_eq!(m.head, None)
409-
assert_eq!(m.tail, None)
409+
assert_eq!(m.tail, -1)
410410
for i in 0..<m.capacity {
411411
assert_eq!(m.entries[i], None)
412412
}
@@ -454,7 +454,7 @@ test "new" {
454454
assert_eq!(m.size, 0)
455455
assert_eq!(m.capacity, 8)
456456
assert_eq!(m.head, None)
457-
assert_eq!(m.tail, None)
457+
assert_eq!(m.tail, -1)
458458
for i in 0..<m.capacity {
459459
assert_eq!(m.entries[i], None)
460460
}
@@ -547,7 +547,7 @@ test "clear" {
547547
assert_eq!(m.size, 0)
548548
assert_eq!(m.capacity, 8)
549549
assert_eq!(m.head, None)
550-
assert_eq!(m.tail, None)
550+
assert_eq!(m.tail, -1)
551551
for i in 0..<m.capacity {
552552
assert_eq!(m.entries[i], None)
553553
}
@@ -640,7 +640,7 @@ test "clear" {
640640
assert_eq!(m.size, 0)
641641
assert_eq!(m.capacity, 8)
642642
assert_eq!(m.head, None)
643-
assert_eq!(m.tail, None)
643+
assert_eq!(m.tail, -1)
644644
for i in 0..<m.capacity {
645645
assert_eq!(m.entries[i], None)
646646
}
@@ -793,7 +793,7 @@ test "remove_entry_head" {
793793
// This should ensure the head is updated correctly
794794
assert_false!(map.head is None)
795795
guard map.head is Some(head)
796-
assert_eq!(head.prev, None)
796+
assert_eq!(head.prev, -1)
797797
assert_true!(head.next is None)
798798
assert_eq!(head.psl, 0)
799799
assert_eq!(head.hash, (2).hash())
@@ -808,11 +808,10 @@ test "remove_entry_tail" {
808808
map.set(2, 2)
809809
map.remove(2)
810810
// This should ensure the tail is updated correctly
811-
assert_false!(map.tail is None)
812-
guard map.tail is Some(tail_idx)
813-
assert_false!(map.entries[tail_idx] is None)
814-
guard map.entries[tail_idx] is Some(tail)
815-
assert_eq!(tail.prev, None)
811+
assert_false!(map.tail is -1)
812+
assert_false!(map.entries[map.tail] is None)
813+
guard map.entries[map.tail] is Some(tail)
814+
assert_eq!(tail.prev, -1)
816815
assert_true!(tail.next is None)
817816
assert_eq!(tail.psl, 0)
818817
assert_eq!(tail.hash, (1).hash())

0 commit comments

Comments
 (0)