Class: NMap

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/nmap.rb

Overview

numerical grids

Defined Under Namespace

Modules: Util Classes: Header

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#int_list_of, #int_of, #string_of

Constructor Details

#initialize(path, *argv) ⇒ NMap

Returns a new instance of NMap.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nmap.rb', line 93

def initialize path, *argv 
  @path = string_of path

  @na_type = argv.shift
  @shape = argv

  @na_type = int_of @na_type if @na_type 
  @shape = int_list_of @shape if @shape

  @lockfile = @path + '.lock'
  
  locked do
    init! unless test ?s, @path
    load_header!
    load!
  end

  @closed = false

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



91
92
93
# File 'lib/nmap.rb', line 91

def closed
  @closed
end

#headerObject (readonly)

Returns the value of attribute header.



89
90
91
# File 'lib/nmap.rb', line 89

def header
  @header
end

#lockfileObject (readonly)

Returns the value of attribute lockfile.



88
89
90
# File 'lib/nmap.rb', line 88

def lockfile
  @lockfile
end

#naObject (readonly)

Returns the value of attribute na.



90
91
92
# File 'lib/nmap.rb', line 90

def na
  @na
end

#na_typeObject (readonly)

Returns the value of attribute na_type.



86
87
88
# File 'lib/nmap.rb', line 86

def na_type
  @na_type
end

#pathObject (readonly)

Returns the value of attribute path.



85
86
87
# File 'lib/nmap.rb', line 85

def path
  @path
end

#shapeObject (readonly)

Returns the value of attribute shape.



87
88
89
# File 'lib/nmap.rb', line 87

def shape
  @shape
end

Class Method Details

.init(*a, &b) ⇒ Object



168
# File 'lib/nmap.rb', line 168

def init(*a, &b) new(*a, &b) end

Instance Method Details

#closeObject



158
159
160
161
162
163
164
165
# File 'lib/nmap.rb', line 158

def close
  return if @closed
  sync
  @mmap.munmap
  @mmap = nil
  @na = nil
  @closed = true
end

#init!Object

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
# File 'lib/nmap.rb', line 121

def init!
  raise ArgumentError, "no na_type" unless @na_type
  raise ArgumentError, "no shape" unless @shape
  sizeof_type = NArray::new(@na_type, 1).to_s.size
  size = @shape.inject(1){|product, dim| product *= dim}
  pos = sizeof_type * size
  File.unlink @path rescue nil
  open(@path, File::CREAT|File::EXCL|File::RDWR){|f| f.truncate pos}
  Header::init @path + '.nmap', @na_type, @shape
end

#load!Object



138
139
140
141
# File 'lib/nmap.rb', line 138

def load!
  @mmap = ::Mmap::new @path, "rw", Mmap::MAP_SHARED
  @na = ::NArray::str @mmap, @na_type, *@shape
end

#load_header!Object



132
133
134
135
136
# File 'lib/nmap.rb', line 132

def load_header!
  @header = Header::new @path + '.nmap' 
  @na_type = @header.na_type
  @shape = @header.shape
end

#lockedObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/nmap.rb', line 143

def locked
  f = open @lockfile, 'a+'
  f.flock File::LOCK_EX
  yield
ensure
  if f
    f.flock File::LOCK_UN rescue nil
    f.close rescue nil
  end
end

#syncObject



154
155
156
# File 'lib/nmap.rb', line 154

def sync
  @mmap.msync
end