Class: XArray

Inherits:
ValueAdd show all
Defined in:
lib/primitive_wrapper.rb

Overview

need to figure out how left and right things work …

OK this is cool,  things on the right if more than one go in one array
... or a non-array if just one item

Constant Summary collapse

LAM_ISORT =
lambda {|a,b| a.inspect <=> b.inspect }
LAM_SSORT =
lambda {|a,b| a.to_s <=> b.to_s }

Instance Method Summary collapse

Methods inherited from ValueAdd

bestow_methods, capture_base_methods

Methods inherited from Value

#!=, #==, #ensure_valid, #freeze, freeze_raise?, ignore_on_freeze, #inspect, #prim_value, raise_on_freeze, #replace, #to_s, #to_wrapper, #type, #type_of?, #unwrap, #val, #val=, #wrapped?, #~

Constructor Details

#initialize(obj = []) ⇒ XArray

Returns a new instance of XArray.



1155
1156
1157
1158
1159
# File 'lib/primitive_wrapper.rb', line 1155

def initialize(obj=[])
  obj = obj.prim_value
  ensure_valid(obj)
  @value = obj
end

Instance Method Details

#[](*list) ⇒ Object



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
# File 'lib/primitive_wrapper.rb', line 1178

def [](*list)
  list = PrimitiveWrapper::get_list(list,size)
  if list.empty?
    return nil
  elsif list.count==1
    if list.first == :all
      return @value
    elsif list.first.type_of? Range  # get_list may pick this appart
      rtn = []
      list.first.to_wrapper.re_range(size).each do |idx|
        rtn.push @value[idx]
      end
      return rtn  
    else
      return @value[list.first]
    end
  end
  rtn = []
  # for now keep this here until you can justify that it is not needed
  list.each do |ii|   # I don't need to do this do I ???  get_list should untangle things
    if ii.type_of? Range
      ii.to_wrapper.re_range(size).each do |idx|
        rtn.push @value[idx]
      end
    elsif ii.type_of? Array
      ii.each do |idx|
        rtn.push @value[idx]
      end
    else
      rtn.push @value[ii]
    end
  end
  return rtn
end

#[]=(*list) ⇒ Object



1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/primitive_wrapper.rb', line 1212

def []=(*list)
  data = list.pop
  list = PrimitiveWrapper::get_list(list,size)
  return nil if list.empty?
  if list.count==1
    if list.first.respond_to? :each
      list = list.first
      if list.type_of? Range
        list = list.to_xr.re_range(size)
      end
    end
  end
  if list.count==1
    if list.first == :all
      PrimitiveWrapper.copy(@value, data, :all) 
    end
    idxs = list.first
    if (idxs.respond_to? :each)
      if idxs.type_of? Range
        idxs = idxs.to_xr.re_range(size)
      end        
      idxs.each do |idx|
        @value[idx] = data
      end
    else
      @value[idxs] = data
    end 
  else
    if data.type_of? Array
      data = PrimitiveWrapper::get_list(data,size)
    end
    PrimitiveWrapper.copy(@value, data, list)
  end
end

#delete_at(*index_list) ⇒ Object



1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/primitive_wrapper.rb', line 1246

def delete_at(*index_list)
  return nil if index_list.empty?
  rtn = []
  if (index_list.count==1)
    if index_list.first==:all
      rtn = @value.dup
      @value.empty!
      return rtn
    else
      ii = index_list.first
      rtn = self[ii]
      if ii.type_of? Range
        ii = ii.to_xr
        ii.re_range!(size)
        ii.reorder!
        ii.reverse_each do |idx|
          @value.delete_at(idx)
        end
        return rtn
      end
    end
  end
  rtn = self[*index_list]
  list = PrimitiveWrapper::get_list(index_list,size)
  list = list & list  # remove duplicates
  list.sort.reverse.each do |idx|
    if idx.type_of? Range
      t_idx = idx.reorder
    else
      t_idx = idx
    end
    @value.delete_at(t_idx)
  end
  return rtn
end

#include?(*list) ⇒ Boolean

replace with list of stuff

Returns:

  • (Boolean)


1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
# File 'lib/primitive_wrapper.rb', line 1281

def include?(*list)  # replace with list of stuff
  if list.count==1
    if list.first.respond_to? :each
      list = list.first
    end
  end
  list.each do |item|
    return false unless @value.include? item
  end
  true
end

#include_any?(*list) ⇒ Boolean

replace with list of stuff

Returns:

  • (Boolean)


1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
# File 'lib/primitive_wrapper.rb', line 1292

def include_any?(*list)  # replace with list of stuff
  if list.count==1
    if list.first.respond_to? :each
      list = list.first
    end
  end
  list.each do |item|
    return true if @value.include? item
  end
  false
end

#isortObject



1166
1167
1168
# File 'lib/primitive_wrapper.rb', line 1166

def isort 
  @value.sort &LAM_ISORT
end

#isort!Object



1169
1170
1171
# File 'lib/primitive_wrapper.rb', line 1169

def isort!
  @value.sort! &LAM_ISORT
end

#ssortObject



1172
1173
1174
# File 'lib/primitive_wrapper.rb', line 1172

def ssort
  @value.sort &LAM_SSORT
end

#ssort!Object



1175
1176
1177
# File 'lib/primitive_wrapper.rb', line 1175

def ssort!
  @value.sort! &LAM_SSORT
end

#valid_type(prm) ⇒ Object



1161
1162
1163
1164
1165
# File 'lib/primitive_wrapper.rb', line 1161

def valid_type(prm)
  return true if prm.kind_of? Array
  return true if prm.kind_of? XArray
  false
end