38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Factor
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Factor
		
	
	
		
			Executable File
		
	
| ! Copyright (C) 2005, 2006 Alex Chapman, Daniel Ehrenberg
 | |
| ! See http;//factorcode.org/license.txt for BSD license
 | |
| USING: kernel sequences math sequences.private strings ;
 | |
| IN: circular
 | |
| 
 | |
| ! a circular sequence wraps another sequence, but begins at an
 | |
| ! arbitrary element in the underlying sequence.
 | |
| TUPLE: circular seq start ;
 | |
| 
 | |
| : <circular> ( seq -- circular )
 | |
|     0 circular construct-boa ;
 | |
| 
 | |
| : circular-wrap ( n circular -- n circular )
 | |
|     [ circular-start + ] keep
 | |
|     [ circular-seq length rem ] keep ; inline
 | |
| 
 | |
| M: circular length circular-seq length ;
 | |
| 
 | |
| M: circular virtual@ circular-wrap circular-seq ;
 | |
| 
 | |
| M: circular nth virtual@ nth ;
 | |
| 
 | |
| M: circular set-nth virtual@ set-nth ;
 | |
| 
 | |
| : change-circular-start ( n circular -- )
 | |
|     #! change start to (start + n) mod length
 | |
|     circular-wrap set-circular-start ;
 | |
| 
 | |
| : push-circular ( elt circular -- )
 | |
|     [ set-first ] keep 1 swap change-circular-start ;
 | |
| 
 | |
| : <circular-string> ( n -- circular )
 | |
|     0 <string> <circular> ;
 | |
| 
 | |
| M: circular virtual-seq circular-seq ;
 | |
| 
 | |
| INSTANCE: circular virtual-sequence
 |