Skip to content

Commit 709ae16

Browse files
committed
Use module instead of remainder where needed.
1 parent 2e39925 commit 709ae16

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

Sources/LispKit/Base/Global.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
// limitations under the License.
1919
//
2020

21+
infix operator %%: MultiplicationPrecedence
22+
23+
func %%<T: BinaryInteger>(lhs: T, rhs: T) -> T {
24+
let rem = lhs % rhs
25+
return rem >= 0 ? rem : rem + rhs
26+
}
27+
2128
internal func log(_ str: String) {
2229
#if LOG
2330
print(str)

Sources/LispKit/Data/HashTable.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public final class HashTable: ManagedObject, CustomStringConvertible {
116116
for bucket in oldBuckets {
117117
var current = bucket
118118
while case .pair(.pair(let key, let value), let next) = current {
119-
let bid = self.hash(key) % capacity
119+
let bid = self.hash(key) %% capacity
120120
self.buckets[bid] = .pair(.pair(key, value), self.buckets[bid])
121121
current = next
122122
}
@@ -311,22 +311,22 @@ public final class HashTable: ManagedObject, CustomStringConvertible {
311311
}
312312

313313
public func get(_ key: Expr) -> Expr? {
314-
return self.get(self.hash(key) % self.buckets.count, key, self.eql)
314+
return self.get(self.hash(key) %% self.buckets.count, key, self.eql)
315315
}
316316

317317
@discardableResult public func set(key: Expr, mapsTo value: Expr) -> Bool {
318318
return self.remove(key: key) != nil && self.add(key: key, mapsTo: value)
319319
}
320320

321321
@discardableResult public func add(key: Expr, mapsTo value: Expr) -> Bool {
322-
return self.add(self.hash(key) % self.buckets.count, key, value)
322+
return self.add(self.hash(key) %% self.buckets.count, key, value)
323323
}
324324

325325
public func remove(key: Expr) -> Expr? {
326326
guard self.mutable else {
327327
return nil
328328
}
329-
return self.remove(self.hash(key) % self.buckets.count, key: key)
329+
return self.remove(self.hash(key) %% self.buckets.count, key: key)
330330
}
331331

332332
private func remove(_ bid: Int, key: Expr) -> Expr? {
@@ -355,7 +355,7 @@ public final class HashTable: ManagedObject, CustomStringConvertible {
355355
for bucket in map.buckets {
356356
var current = bucket
357357
while case .pair(.pair(let key, let value), let next) = current {
358-
let bid = self.hash(key) % self.buckets.count
358+
let bid = self.hash(key) %% self.buckets.count
359359
if self.get(bid, key, self.eql) == nil {
360360
self.add(bid, key, value)
361361
}

Sources/LispKit/Primitives/HashTableLibrary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public final class HashTableLibrary: NativeLibrary {
508508
private func hBuckets(_ expr: Expr, hval: Expr?) throws -> Expr {
509509
let map = try expr.asHashTable()
510510
if let hashValue = try hval?.asInt64() {
511-
return map.bucketList(Int(hashValue % Int64(map.bucketCount)))
511+
return map.bucketList(Int(hashValue %% Int64(map.bucketCount)))
512512
} else {
513513
return map.bucketList()
514514
}
@@ -521,15 +521,15 @@ public final class HashTableLibrary: NativeLibrary {
521521
if !key.isAtom || !value.isAtom {
522522
self.context.objects.manage(map)
523523
}
524-
map.add(Int(try hval.asInt64() % Int64(map.bucketCount)), key, value)
524+
map.add(Int(try hval.asInt64() %% Int64(map.bucketCount)), key, value)
525525
return .void
526526
}
527527

528528
private func hBucketRepl(_ expr: Expr, hval: Expr, bucket: Expr) throws -> Expr {
529529
guard case .table(let map) = expr else {
530530
throw RuntimeError.type(expr, expected: [.tableType])
531531
}
532-
map.replace(Int(try hval.asInt64() % Int64(map.bucketCount)), bucket)
532+
map.replace(Int(try hval.asInt64() %% Int64(map.bucketCount)), bucket)
533533
return .void
534534
}
535535
}

Sources/LispKit/Primitives/MathLibrary.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,7 @@ public final class MathLibrary: NativeLibrary {
15951595
}
15961596

15971597
private func fxModulo(_ x: Expr, _ y: Expr) throws -> Expr {
1598-
let rhs = try y.asInt64()
1599-
let res = try x.asInt64() % rhs
1600-
return .fixnum((res < 0) == (rhs < 0) ? res : res + rhs)
1598+
return .fixnum(try x.asInt64() %% y.asInt64())
16011599
}
16021600

16031601
private func fxAbs(_ x: Expr) throws -> Expr {

0 commit comments

Comments
 (0)