Sha256: 208761ab988739c8956f8bd210612c2977ecaaf2cef20dfbfc54c65434df0324

Contents?: true

Size: 602 Bytes

Versions: 103

Compression:

Stored size: 602 Bytes

Contents

require 'rubygems'
require 'ffi'

module LibC
  extend FFI::Library
  ffi_lib FFI::Library::LIBC
  callback :qsort_cmp, [ :pointer, :pointer ], :int
  attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :int
end

p = FFI::MemoryPointer.new(:int, 2)
p.put_array_of_int32(0, [ 2, 1 ])
puts "ptr=#{p.inspect}"
puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}"
LibC.qsort(p, 2, 4) do |p1, p2|
  i1 = p1.get_int32(0)
  i2 = p2.get_int32(0)
  puts "In block: comparing #{i1} and #{i2}"
  i1 < i2 ? -1 : i1 > i2 ? 1 : 0
end
puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}"

Version data entries

103 entries across 90 versions & 15 rubygems

Version Path
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/samples/qsort.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/samples/qsort.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/samples/qsort.rb
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/ffi-1.12.2/samples/qsort.rb
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.118 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.98 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.94 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.21 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.15 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.13 vendor/bundle/ruby/2.6.0/gems/ffi-1.12.2/samples/qsort.rb
cloudsmith-api-0.49.9 vendor/bundle/ruby/2.3.0/gems/ffi-1.12.2/samples/qsort.rb
ffi-1.12.2-x86-mingw32 samples/qsort.rb
ffi-1.12.2-x64-mingw32 samples/qsort.rb
ffi-1.12.2 samples/qsort.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/ffi-1.11.1/samples/qsort.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/ffi-1.11.1/samples/qsort.rb
ffi-1.12.1-x86-mingw32 samples/qsort.rb
ffi-1.12.1-x64-mingw32 samples/qsort.rb
ffi-1.12.1 samples/qsort.rb