Class: Sequence

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/sequence.rb,
lib/sequence/io.rb,
lib/sequence/file.rb,
lib/sequence/list.rb,
lib/sequence/ofhash.rb,
lib/sequence/subseq.rb,
lib/sequence/indexed.rb,
lib/sequence/usedata.rb,
lib/sequence/version.rb,
lib/sequence/buffered.rb,
lib/sequence/circular.rb,
lib/sequence/position.rb,
lib/sequence/reversed.rb,
lib/sequence/shifting.rb,
lib/sequence/arraylike.rb,
lib/sequence/singleitem.rb,
lib/sequence/stringlike.rb,
lib/sequence/weakrefset.rb,
lib/sequence/ofobjectivars.rb,
lib/sequence/ofobjectmethods.rb

Overview

WeakRefSet implements an unordered collection of weak references to objects. These references don’t prevent garbage collection on these objects. As these objects are thrown away so does their entry in a WeakRefSet. Immmediate objects are not handled by this class (and wouldn’t be useful).

Direct Known Subclasses

Buffered, Circular, File, IO, List, Reversed, Shifting, SingleItem, SubSeq, UseData

Defined Under Namespace

Modules: ArrayLike, StringLike Classes: Buffered, Circular, File, IO, Indexed, List, OfArray, OfHash, OfObjectIvars, OfObjectMethods, OfString, Position, Reversed, Shifting, SingleItem, SubSeq, UseData, WeakRefSet

Constant Summary collapse

SubSequence =
SubSeq
VERSION =
'0.2.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSequence

Returns a new instance of Sequence.



41
# File 'lib/sequence.rb', line 41

def initialize; abstract end

Instance Attribute Details

#last_matchObject

Returns the value of attribute last_match.



240
241
242
# File 'lib/sequence.rb', line 240

def last_match
  @last_match
end

#maxmatchlen(backwards) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/sequence.rb', line 245

def maxmatchlen(backwards)
  size=self.size

  list=[ _default_maxmatchlen,
         backwards ? pos : size-pos%size
  ]
  list.push @maxmatchlen if defined? @maxmatchlen
  list.min
end

Class Method Details

.[](*x) ⇒ Object



61
# File 'lib/sequence.rb', line 61

def [](*x) new(*x) end

Instance Method Details

#+(other) ⇒ Object

Returns a new #position increased by len (positive or negative).



464
465
466
467
468
469
470
# File 'lib/sequence.rb', line 464

def +(other)
  if ::Sequence===other
    List[self, other]
  else
    position(pos+other)
  end
end

#-(other) ⇒ Object

if passed an integer arg, return a new position decreased by len. if passed a position, return the distance (number or elements) from other (a #position) to self. This can be +, -, or 0.



456
457
458
459
460
461
462
# File 'lib/sequence.rb', line 456

def -(other)
  if position?(other)
    pos-other.pos
  else
    position(pos-other)
  end
end

#<<(x) ⇒ Object



639
# File 'lib/sequence.rb', line 639

def <<(x) push x; return self end

#[](*a) ⇒ Object



598
# File 'lib/sequence.rb', line 598

def [](*a) slice(*a) end

#[]=(*a) ⇒ Object



616
# File 'lib/sequence.rb', line 616

def []=(*a) modify(*a) end

#_adjust_pos_on_change(pos, first, oldsize, newsize) ⇒ Object



679
680
681
682
683
684
685
686
687
688
# File 'lib/sequence.rb', line 679

def _adjust_pos_on_change pos,first,oldsize,newsize
#      assert newsize != oldsize
  if pos>=first+oldsize
    oldsize.zero? and pos==first and return pos
    pos+newsize-oldsize
  elsif pos>first
    first
  else pos
  end
end

#_default_maxmatchlenObject



243
# File 'lib/sequence.rb', line 243

def _default_maxmatchlen; 1024 end

#_delete_position(p) ⇒ Object

Delete p from the list of children (from #position). Should only be used by child Position.



706
707
708
# File 'lib/sequence.rb', line 706

def _delete_position(p) # :nodoc:
  @change_listeners.delete(p)
end

#_normalize_pos(pos, size = self.size) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
# File 'lib/sequence.rb', line 548

def _normalize_pos(pos,size=self.size)
  if pos<0 
    pos+=size
    pos<0 and pos=0
  elsif pos>size
    pos=size
  end

  assert((0..size)===pos)
  pos
end

#_parse_slice_args(*args) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/sequence.rb', line 560

def _parse_slice_args(*args)
  asize=args.size
  assert !closed?
  size=self.size
  case r=args.first
    when Range
      asize==1 or raise ArgumentError
      first,last=r.first,r.last
      first=_normalize_pos(first,size)
      last=_normalize_pos(last,size)
      len=last-first
      r.exclude_end? or len+=1
    when Integer
      asize<=2 or raise ArgumentError
      first=_normalize_pos(r,size)
      len=args[1] || (only1=1)
    when nil
      asize==0 or raise ArgumentError
      first=nil
      len=only1=1
    else raise ArgumentError
  end
  return first,len,only1
end

#_pos=(p) ⇒ Object



344
345
346
# File 'lib/sequence.rb', line 344

def _pos=(p)
  abstract
end

#all_dataObject



123
124
125
126
127
128
# File 'lib/sequence.rb', line 123

def all_data
  holding_position{|posi|
    posi.begin!
    posi.read!
  }
end

#append(stuff) ⇒ Object

push/unshift in stringlike/arraylike



643
644
645
646
# File 'lib/sequence.rb', line 643

def append stuff
  insert(size, stuff)
  self
end

#beginObject

Return a new #position for the beginning.



481
482
483
# File 'lib/sequence.rb', line 481

def begin
  position(0)
end

#begin!Object

go to beginning



490
491
492
# File 'lib/sequence.rb', line 490

def begin!
  self._pos=0
end

#check(pat) ⇒ Object



207
# File 'lib/sequence.rb', line 207

def check(pat)           holding{scan(pat)}    end

#check_until(pat) ⇒ Object



211
# File 'lib/sequence.rb', line 211

def check_until(pat)     holding{scan_until(pat)}    end

#checkback(pat) ⇒ Object



215
# File 'lib/sequence.rb', line 215

def checkback(pat)       holding{scanback(pat)}    end

#checkback_until(pat) ⇒ Object



219
# File 'lib/sequence.rb', line 219

def checkback_until(pat) holding{scanback_until(pat)} end

#closeObject

Close the sequence. This will also close/invalidate every child position or derived sequence.



429
430
431
432
433
434
435
436
# File 'lib/sequence.rb', line 429

def close
  defined? @change_listeners and @change_listeners.each { |p|
    Sequence===p and p.close
  }
  # this should make just about any operation fail
  instance_variables.each { |v| remove_instance_variable(v) }
  nil
end

#closed?Boolean

Is this sequence closed?

Returns:

  • (Boolean)


438
439
440
# File 'lib/sequence.rb', line 438

def closed?
  instance_variables.empty?
end

#delete(*args) ⇒ Object

index|range, len



618
619
620
621
# File 'lib/sequence.rb', line 618

def delete(*args) #index|range, len
  modify( *args<<new_data)
  nil
end

#eachObject

Performs each just to make this class Enumerable. self is returned (or the break value if the code does a break).



712
713
714
715
716
717
718
719
# File 'lib/sequence.rb', line 712

def each # :yield: value
  holding {
    begin!
    until eof?
      yield read1
    end or self
  }
end

#empty?Boolean

is there any data in the sequence?

Returns:

  • (Boolean)


534
535
536
# File 'lib/sequence.rb', line 534

def empty?
  size==0
end

#endObject

Return a new #position for the end.



485
486
487
# File 'lib/sequence.rb', line 485

def end
  position(size)
end

#end!Object

go to end



494
495
496
# File 'lib/sequence.rb', line 494

def end!
  self._pos=size
end

#eof?Boolean

are we at past the end of the sequence data, with no more data ever to arrive?

Returns:

  • (Boolean)


529
530
531
# File 'lib/sequence.rb', line 529

def eof?
  abstract
end

#exist?(pat) ⇒ Boolean

Returns:

  • (Boolean)


212
# File 'lib/sequence.rb', line 212

def exist?(pat)          holding{skip_until(pat)}    end

#existback?(pat) ⇒ Boolean

Returns:

  • (Boolean)


220
# File 'lib/sequence.rb', line 220

def existback?(pat)      holding{skipback_until(pat) }end

#firstObject

return first element of data



539
540
541
# File 'lib/sequence.rb', line 539

def first
  slice 0
end

#goto(p) ⇒ Object

go to an absolute position; identical to #pos=



340
341
342
# File 'lib/sequence.rb', line 340

def goto p
  self.pos= p
end

#holdingObject

hold current position while executing a block. The block is passed the current sequence as its parameter. you can move the position around or call methods like read that do it, but after the block returns, the position is reset to the original location. The return value is the result of the block.



259
260
261
262
263
264
265
266
# File 'lib/sequence.rb', line 259

def holding
  oldpos=pos
  begin
    yield self
  ensure
    self.pos=oldpos
  end
end

#holding!(&block) ⇒ Object

like #holding, but block is instance_eval’d in the sequence.



280
281
282
283
284
285
286
287
# File 'lib/sequence.rb', line 280

def holding! &block
  oldpos=pos
  begin
    instance_eval self, &block
  ensure
    self.pos=oldpos
  end
end

#holding?Boolean

like #holding, but position is reset only if block returns false or nil (or raises an exception).

Returns:

  • (Boolean)


270
271
272
273
274
275
276
277
# File 'lib/sequence.rb', line 270

def holding?
  oldpos=pos
  begin
    result=yield self
  ensure
    (self.pos=oldpos) unless result
  end
end

#holding_positionObject



290
291
292
293
294
295
296
297
298
# File 'lib/sequence.rb', line 290

def holding_position
  pos=position
  begin
    result=yield self
  ensure
    self.position=pos
    pos.close
  end
end

#holding_position!(&block) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'lib/sequence.rb', line 310

def holding_position! &block
  pos=position
  begin
    result=instance_eval self,&block
  ensure
    self.position=pos
    pos.close
  end
end

#holding_position?Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
306
307
308
# File 'lib/sequence.rb', line 300

def holding_position?
  pos=position
  begin
    result=yield self
  ensure
    self.position=pos unless result
    pos.close
  end
end

#insert(index, replacedata) ⇒ Object



623
624
625
# File 'lib/sequence.rb', line 623

def insert index, replacedata
  modify index,0, replacedata
end

#lastObject

return last element of data



544
545
546
# File 'lib/sequence.rb', line 544

def last
  slice( -1)
end

#lengthObject



526
# File 'lib/sequence.rb', line 526

def length; size end

#match?(pat) ⇒ Boolean

Returns:

  • (Boolean)


208
# File 'lib/sequence.rb', line 208

def match?(pat)          holding{skip(pat)}    end

#matchback?(pat) ⇒ Boolean

Returns:

  • (Boolean)


216
# File 'lib/sequence.rb', line 216

def matchback?(pat)      holding{skipback(pat)}    end

#modify(*args) ⇒ Object

Similar to #slice except data is written. index and len have the same meaning as they do in #slice. len elements are deleted and replacedata is inserted. replacedata is a single item if len is ommitted and 1st param is Fixnum



613
614
615
# File 'lib/sequence.rb', line 613

def modify(*args) #index|range, len, replacedata
  abstract
end

#more_data?Boolean

is there any more data after the position?

Returns:

  • (Boolean)


511
512
513
514
# File 'lib/sequence.rb', line 511

def more_data?
  #!eof?
  (size-pos).nonzero?
end

#move(len) ⇒ Object

move position len elements, relative to the current position. A negative len will go in reverse. The (positive) amount actually moved is returned (<len if reached beginning/end).



350
351
352
353
354
355
356
# File 'lib/sequence.rb', line 350

def move(len)
  oldpos=pos
  newpos=oldpos+len
  newpos<0 and newpos=0
  goto newpos
  return (pos-oldpos).abs
end

#move!(reverse = false) ⇒ Object

move to end of the remaining elements. reverse=true to move to beginning instead of end The amount moved is returned.



360
361
362
# File 'lib/sequence.rb', line 360

def move!(reverse=false)
  reverse ? begin! : end!
end

#nearbegin(len, at = pos) ⇒ Object


is position within len elements of the beginning?



500
501
502
# File 'lib/sequence.rb', line 500

def nearbegin(len,at=pos)
  at<=len
end

#nearend(len, at = pos) ⇒ Object


is position within len elements of the end?



506
507
508
# File 'lib/sequence.rb', line 506

def nearend(len,at=pos)
  at+len>=size
end

#new(seq) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sequence.rb', line 48

def new(seq)
  case seq
    when File,IO,Array,String,Enumerable
      seq.to_sequence
    else
      if seq.respond_to? :to_str
        seq.to_str.to_sequence
      else
        raise ArgumentError
      end
  end
end

#notify_change(*args) ⇒ Object

seq, first, oldsize, newsize



697
698
699
700
701
702
# File 'lib/sequence.rb', line 697

def notify_change *args   #seq, first, oldsize, newsize
  args[0]=self
  defined? @change_listeners and @change_listeners.each{|obj|
    obj.change_notification(*args)
  }
end

#on_change_notify(obj) ⇒ Object



690
691
692
693
694
695
# File 'lib/sequence.rb', line 690

def on_change_notify obj
  Symbol===obj and raise ArgumentError
  obj.respond_to? :change_notification or raise ArgumentError
  @change_listeners||=WeakRefSet[]
  @change_listeners<<obj
end

#original__newObject



46
# File 'lib/sequence.rb', line 46

alias original__new new

#overwrite(index, replacedata) ⇒ Object



627
628
629
# File 'lib/sequence.rb', line 627

def overwrite index, replacedata
  modify index,replacedata.size, replacedata
end

#pop(count = nil) ⇒ Object



631
632
633
# File 'lib/sequence.rb', line 631

def pop count=nil
  slice!(count ? -count...size : -1)
end

#posObject

number of elements from the beginning (0 is at the beginning).



322
323
324
# File 'lib/sequence.rb', line 322

def pos()
    abstract
end

#pos=(p) ⇒ Object

Set #pos to be p. When p is negative, it is set from the end.



334
335
336
337
# File 'lib/sequence.rb', line 334

def pos=(p)
    position?(p) and p=p.pos unless Integer===p
    self._pos=_normalize_pos p
end

#pos?(p) ⇒ Boolean

this checks to see if p is a valid numeric position.

Returns:

  • (Boolean)


328
329
330
331
# File 'lib/sequence.rb', line 328

def pos?(p)
  sz=size
  (-sz..sz)===p
end

#position(_pos = pos) ⇒ Object

#position returns a Sequence::Position to represent a location within this sequence. The argument allows you to specify a numeric location for the position; default is currrent position. If the element that a Position is anchored to is deleted, that Position may become invalid or have an unknown behavior.



392
393
394
# File 'lib/sequence.rb', line 392

def position(_pos=pos)
  Position.new(self,_pos)
end

#position=(p) ⇒ Object

Set the position to a Position p (from #position).



396
397
398
399
400
# File 'lib/sequence.rb', line 396

def position=(p)
  self.pos = p.pos
  self.prop(nil,p.prop)
  p
end

#position?(p) ⇒ Boolean

this queries whether a particular #position p is valid (is a child or self).

numeric positions and also be tested

Returns:

  • (Boolean)


404
405
406
407
408
409
410
# File 'lib/sequence.rb', line 404

def position?(p)
  case p
  when Integer; (-size..size)===p
  when Position; equal? p.data
  else equal? p
  end
end

#predObject

Return a new #position for previous location or nil if we are at the beginning.



477
478
479
# File 'lib/sequence.rb', line 477

def pred
  self-1 unless pos.zero?
end

#prepend(stuff) ⇒ Object



648
649
650
651
# File 'lib/sequence.rb', line 648

def prepend stuff
  insert(0, stuff)
  self
end

#prop(name = nil, *value) ⇒ Object

Get (if no value) and set properties. Normally, name should be a symbol. If name is nil, it wil get/set using a hash representing all of the properties.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/sequence.rb', line 367

def prop(name=nil,*value) # :args: (name[,value])
  if name.nil?
    if value.size.zero?
      defined?(@prop) &&@prop&&@prop.clone
    else
      if (value = value[0]).nil?
        defined?(@prop) &&@prop&&remove_instance_variable(:@prop)
      else
        (@prop||={}).replace(value)
      end
    end
  else
    if value.size.zero?
      defined?(@prop) &&@prop&&@prop[name]
    else
      (@prop||={})[name] = value[0]
    end
  end
end

#read(len) ⇒ Object

attempt to read up to len elements. the position is left just after the data read. #read may return less than the whole requested amount if less data than requested in len is available. This can happen at end of file or if more data is simply unavailable currently (ie with a Sequence::IO). Don’t assume that getting less than you requested means you’re at end of file; use #eof? to test for that instead.



92
93
94
# File 'lib/sequence.rb', line 92

def read(len)
  abstract
end

#read!(reverse = false) ⇒ Object

read the remaining elements. if reverse, read everything behind position



115
116
117
118
119
120
121
# File 'lib/sequence.rb', line 115

def read!(reverse=false)
  if reverse
    readback pos
  else
    read rest_size
  end
end

#read1Object

read next element or nil if eof and advance position



67
68
69
# File 'lib/sequence.rb', line 67

def read1
  (read 1)[0]
end

#readahead(len) ⇒ Object

like read, but position is left alone.



97
98
99
# File 'lib/sequence.rb', line 97

def readahead(len)
  holding{read(len)}
end

#readahead1Object

read element after the pos or nil if eof, leaving position alone



77
78
79
# File 'lib/sequence.rb', line 77

def readahead1
  slice pos
end

#readback(len) ⇒ Object

read data behind the current position, leaving position just before the data read.



108
109
110
111
# File 'lib/sequence.rb', line 108

def readback(len)
  len>pos and len=pos
  readahead move( -len )
end

#readback1Object

read previous element or nil if start of input and move position back



72
73
74
# File 'lib/sequence.rb', line 72

def readback1
  (readback 1)[0]
end

#readbehind(len) ⇒ Object

read data behind the current position, leaving position unchanged



102
103
104
105
# File 'lib/sequence.rb', line 102

def readbehind(len)
  len>pos and len=pos
  read move( -len)
end

#readbehind1Object

read element before the pos or nil if start of input, leaving position alone



82
83
84
# File 'lib/sequence.rb', line 82

def readbehind1
  slice pos-1 unless pos.zero?
end

#rest_sizeObject



325
# File 'lib/sequence.rb', line 325

def rest_size; size - pos end

#reversedObject

make a new sequence that reverses the order of data. reversed and parent sequence share data.



423
424
425
# File 'lib/sequence.rb', line 423

def reversed
  Reversed.new self
end

#shift(count = nil) ⇒ Object



635
636
637
# File 'lib/sequence.rb', line 635

def shift count=nil
  slice!(count ? 0...count : 0 )
end

#sizeObject

Returns the number of elements.



523
524
525
# File 'lib/sequence.rb', line 523

def size
  abstract
end

#skip(pat) ⇒ Object

def scan(pat)

  abstract
end

def scan_until(pat)
  abstract
end
def scanback(pat)
  abstract
end

def scanback_until(pat)
  abstract
end

def match(pat)
  abstract
end

def matchback(pat)
  abstract
end


206
# File 'lib/sequence.rb', line 206

def skip(pat)            match=  scan(pat) and match.length    end

#skip_literal(lits) ⇒ Object Also known as: skip_literals



222
223
224
225
# File 'lib/sequence.rb', line 222

def skip_literal(lits)
  sz=lits.size
  lits==readahead(sz) and move sz
end

#skip_until(pat) ⇒ Object



210
# File 'lib/sequence.rb', line 210

def skip_until(pat)      match=  scan_until(pat) and match.length    end

#skip_until_literal(lits) ⇒ Object Also known as: skip_until_literals



228
229
230
231
232
233
234
235
236
237
# File 'lib/sequence.rb', line 228

def skip_until_literal(lits)
  sz=lits.size
  first=lits[0]
  holding?{
  until eof?
    skip_until(first)
    lits==readahead(sz) and break pos
  end
  }
end

#skipback(pat) ⇒ Object



214
# File 'lib/sequence.rb', line 214

def skipback(pat)        match=  scanback(pat) and match.length    end

#skipback_until(pat) ⇒ Object



218
# File 'lib/sequence.rb', line 218

def skipback_until(pat)  match=  scanback_until(pat) and match.length end

#slice(*args) ⇒ Object

Provides random access for the sequence like what is in Array/String. index can be nil (start at the current location) or a numeric (for #pos=) or a range. len can be nil (get a single element) or the number of elements to #read (positive or negative). The sequence’s position is left alone.



590
591
592
593
594
595
596
597
# File 'lib/sequence.rb', line 590

def slice(*args) #index|range=nil,len=nil
  first,len,only1=_parse_slice_args( *args)
  pos==first and first=nil
  holding {
    self.pos = first if first
    only1 ? read1 : read(len)
  }
end

#slice!(*args) ⇒ Object

Like #slice except the element(s) are deleted.



602
603
604
605
606
607
# File 'lib/sequence.rb', line 602

def slice!(*args) #index|range, len
  first,len,only1=_parse_slice_args( *args)
  result=slice(first,len)
  delete(first,len)
  only1 ? result.first : result
end

#slice1(idx) ⇒ Object



599
# File 'lib/sequence.rb', line 599

def slice1(idx) slice(idx) end

#slice1!(idx) ⇒ Object



608
# File 'lib/sequence.rb', line 608

def slice1!(idx) slice!(idx) end

#subseq(*args) ⇒ Object

make a new sequence out of a subrange of current sequence data. the subseq and parent seq share data, so changes in one will be reflected in the other.



415
416
417
418
419
# File 'lib/sequence.rb', line 415

def subseq(*args)
  assert !closed?
  first,len,only1=_parse_slice_args(*args)
  SubSeq.new(self,first,len)
end

#succObject

Return a new #position for next location or nil if we are at the end.



473
474
475
# File 'lib/sequence.rb', line 473

def succ
  self+1 unless eof?
end

#to_sequenceObject



42
# File 'lib/sequence.rb', line 42

def to_sequence; self end

#was_data?Boolean

has any data been seen so far, or are we still at the beginning?

Returns:

  • (Boolean)


517
518
519
# File 'lib/sequence.rb', line 517

def was_data?
  pos.nonzero?
end

#write(data) ⇒ Object



653
654
655
656
657
658
# File 'lib/sequence.rb', line 653

def write(data)
  assert oldpos=pos
  writeahead(data)
  assert oldpos==pos
  move data.size
end

#writeahead(data) ⇒ Object

Raises:

  • (ArgumentError)


667
668
669
670
671
# File 'lib/sequence.rb', line 667

def writeahead(data)
  raise ArgumentError, "attempted overwrite at end of #{self}" if data.size>rest_size
     overwrite(pos,data)
  data.size
end

#writeback(data) ⇒ Object



660
661
662
663
664
665
# File 'lib/sequence.rb', line 660

def writeback(data)
  assert oldpos=pos
  writebehind(data)
  assert oldpos==pos
  move( -data.size)
end

#writebehind(data) ⇒ Object

Raises:

  • (ArgumentError)


673
674
675
676
677
# File 'lib/sequence.rb', line 673

def writebehind(data)
  raise ArgumentError, "attempted overwrite at begin of #{self}" if data.size>pos
     overwrite(pos-data.size,data)
  data.size
end