File: utils.rkt

package info (click to toggle)
racket 7.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,432 kB
  • sloc: ansic: 258,980; pascal: 59,975; sh: 33,650; asm: 13,558; lisp: 7,124; makefile: 3,329; cpp: 2,889; exp: 499; python: 274; xml: 11
file content (35 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#lang racket/base

(require (for-syntax racket/base racket/syntax syntax/strip-context)
         typed/racket/base
         (only-in ffi/unsafe
                  ctype-sizeof
                  _long
                  _ulong))

(provide (all-defined-out))

(define (unsigned-max type) (- (expt 2 (* 8 (ctype-sizeof type))) 1))
(define (signed-min type) (- (expt 2 (- (* 8 (ctype-sizeof type)) 1))))
(define (signed-max type) (- (expt 2 (- (* 8 (ctype-sizeof type)) 1)) 1))

(define _long-min (signed-min _long))
(define _long-max (signed-max _long))
(define _ulong-max (unsigned-max _ulong))

(define (_ulong? n) (and (exact-integer? n) (<= 0 n _ulong-max)))
(define (_long? n) (and (exact-integer? n) (<= _long-min n _long-max)))

(define-syntax (req/prov-uniform-collection stx)
  (syntax-case stx ()
    [(_ module collection type)
     (with-syntax ([require-it-name  (datum->syntax stx (gensym 'require-it))])
       (syntax/loc stx
         (begin
           (define-syntax (require-it-name stx1)
             (syntax-case stx1 ()
               [(require-it-name)
                (with-syntax ([(obj (... ...))  (replace-context #'require-it-name collection)])
                  #'(begin (require/typed module [obj  type] (... ...))
                           (provide obj (... ...))))]))
           (require-it-name))))]))