-
Notifications
You must be signed in to change notification settings - Fork 28
[210_10] 实现 bag 构造器、访问器与谓词 #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
jetjinser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后面的还没看,先改成用 hash-table 存 entry 吧
goldfish/srfi/srfi-113.scm
Outdated
| (comparator bag-element-comparator)) | ||
|
|
||
| (define (check-bag obj) | ||
| (if (not (bag? obj)) (type-error "not a bag" obj))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (if (not (bag? obj)) (type-error "not a bag" obj))) | |
| (when (not (bag? obj)) (type-error "not a bag" obj))) |
goldfish/srfi/srfi-113.scm
Outdated
| (define (bag-entry element count) | ||
| (cons element count)) | ||
|
|
||
| (define (bag-entry-element entry) | ||
| (car entry)) | ||
|
|
||
| (define (bag-entry-count entry) | ||
| (cdr entry)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bag-entry 如果需要这些 proc 的话,也用 record,不要这么写,这里相当于手动构造 record 了。
但是没必要另外构造这个,entries 用一个 hash-table 就行。
另外如果这么写,也写得复杂了一些
| (define (bag-entry element count) | |
| (cons element count)) | |
| (define (bag-entry-element entry) | |
| (car entry)) | |
| (define (bag-entry-count entry) | |
| (cdr entry)) | |
| (define (bag-entry element count) | |
| (cons element count)) | |
| (define bag-entry-element car) | |
| (define bag-entry-count cdr) |
goldfish/srfi/srfi-113.scm
Outdated
| (if (not (and (exact-integer? count) (>= count 0))) | ||
| (type-error "bag-increment!" count)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (if (not (and (exact-integer? count) (>= count 0))) | |
| (type-error "bag-increment!" count)) | |
| (unless (and (exact-integer? count) (>= count 0)) | |
| (type-error "bag-increment!" count)) |
goldfish/srfi/srfi-113.scm
Outdated
| (%make-bag entries comparator) | ||
| bag? | ||
| (entries bag-entries set-bag-entries!) | ||
| (comparator bag-element-comparator)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (comparator bag-element-comparator)) | |
| (comparator bag-comparator)) |
保持简单些吧,不然有可能会带来 bag-element 的 comparator field 的混淆。
goldfish/srfi/srfi-113.scm
Outdated
| (let* ((eq (comparator-equality-predicate (bag-element-comparator bag))) | ||
| (entries (bag-entries bag))) | ||
| (let loop ((rest entries)) | ||
| (cond | ||
| ((null? rest) | ||
| (set-bag-entries! bag (cons (bag-entry element count) entries))) | ||
| (else | ||
| (let ((entry (car rest))) | ||
| (if (eq (bag-entry-element entry) element) | ||
| (set-cdr! entry (+ count (bag-entry-count entry))) | ||
| (loop (cdr rest))))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为 entries 是个 alist,可以用 assq 实现,但更好的方式还是 hash-table-xxx 的操作
goldfish/srfi/srfi-113.scm
Outdated
| (define (bag-decrement! bag element count) | ||
| (check-bag bag) | ||
| (if (not (and (exact-integer? count) (>= count 0))) | ||
| (type-error "bag-decrement!" count)) | ||
| (if (= count 0) | ||
| bag | ||
| (let* ((eq (comparator-equality-predicate (bag-element-comparator bag))) | ||
| (entries (bag-entries bag))) | ||
| (let loop ((prev #f) (rest entries)) | ||
| (cond | ||
| ((null? rest) bag) | ||
| (else | ||
| (let ((entry (car rest))) | ||
| (if (eq (bag-entry-element entry) element) | ||
| (let ((new-count (- (bag-entry-count entry) count))) | ||
| (if (> new-count 0) | ||
| (set-cdr! entry new-count) | ||
| (if prev | ||
| (set-cdr! prev (cdr rest)) | ||
| (set-bag-entries! bag (cdr rest)))) | ||
| bag) | ||
| (loop entry (cdr rest)))))))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
goldfish/srfi/srfi-113.scm
Outdated
| (define (bag-entry-count entry) | ||
| entry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个没有意义,let 的时候命名清楚 entry-count 就行
goldfish/srfi/srfi-113.scm
Outdated
| (when (> entry 0) | ||
| (let ((new-count (- (bag-entry-count entry) count))) | ||
| (if (> new-count 0) | ||
| (hash-table-set! entries element new-count) | ||
| (hash-table-delete! entries element)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接判断 entry > count 就行了吧
tests/goldfish/liii/bag-test.scm
Outdated
| (check-true (not (not (member 1 b-list)))) | ||
| (check-true (not (not (member 2 b-list)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
两次 not 没有意义
list 直接 check 内容吧
jetjinser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| (check-false (not (member 1 b-list))) | ||
| (check-false (not (member 2 b-list))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(check-true (member 1 b-list))
(check-true (member 2 b-list))
想到确实不能直接检查 b-list 内容,因为 bag 不保证顺序,这么检查是对的。
No description provided.