less 0.8.13 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/README.md +10 -1
  2. data/Rakefile +21 -3
  3. data/VERSION +1 -1
  4. data/bin/lessc +0 -8
  5. data/less.gemspec +138 -15
  6. data/lib/less.rb +67 -11
  7. data/lib/less/command.rb +24 -25
  8. data/lib/less/engine.rb +36 -140
  9. data/lib/less/engine/builder.rb +8 -0
  10. data/lib/less/engine/less.tt +374 -0
  11. data/lib/less/engine/nodes.rb +8 -0
  12. data/lib/less/engine/nodes/element.rb +150 -0
  13. data/lib/less/engine/nodes/entity.rb +73 -0
  14. data/lib/less/engine/nodes/function.rb +82 -0
  15. data/lib/less/engine/nodes/literal.rb +135 -0
  16. data/lib/less/engine/nodes/property.rb +112 -0
  17. data/lib/less/engine/nodes/selector.rb +39 -0
  18. data/lib/less/engine/parser.rb +3860 -0
  19. data/lib/vendor/treetop/.gitignore +7 -0
  20. data/lib/vendor/treetop/LICENSE +19 -0
  21. data/lib/vendor/treetop/README +164 -0
  22. data/lib/vendor/treetop/Rakefile +19 -0
  23. data/lib/vendor/treetop/benchmark/seqpar.gnuplot +15 -0
  24. data/lib/vendor/treetop/benchmark/seqpar.treetop +16 -0
  25. data/lib/vendor/treetop/benchmark/seqpar_benchmark.rb +107 -0
  26. data/lib/vendor/treetop/bin/tt +28 -0
  27. data/lib/vendor/treetop/lib/treetop.rb +8 -0
  28. data/lib/vendor/treetop/lib/treetop/bootstrap_gen_1_metagrammar.rb +45 -0
  29. data/lib/vendor/treetop/lib/treetop/compiler.rb +6 -0
  30. data/lib/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb +42 -0
  31. data/lib/vendor/treetop/lib/treetop/compiler/lexical_address_space.rb +17 -0
  32. data/lib/vendor/treetop/lib/treetop/compiler/metagrammar.rb +3097 -0
  33. data/lib/vendor/treetop/lib/treetop/compiler/metagrammar.treetop +408 -0
  34. data/lib/vendor/treetop/lib/treetop/compiler/node_classes.rb +19 -0
  35. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/anything_symbol.rb +18 -0
  36. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/atomic_expression.rb +14 -0
  37. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/character_class.rb +23 -0
  38. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/choice.rb +31 -0
  39. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
  40. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/grammar.rb +28 -0
  41. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
  42. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/nonterminal.rb +13 -0
  43. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/optional.rb +19 -0
  44. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
  45. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_expression.rb +146 -0
  46. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
  47. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/predicate.rb +45 -0
  48. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/repetition.rb +55 -0
  49. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/sequence.rb +68 -0
  50. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/terminal.rb +20 -0
  51. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
  52. data/lib/vendor/treetop/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
  53. data/lib/vendor/treetop/lib/treetop/compiler/ruby_builder.rb +113 -0
  54. data/lib/vendor/treetop/lib/treetop/ruby_extensions.rb +2 -0
  55. data/lib/vendor/treetop/lib/treetop/ruby_extensions/string.rb +42 -0
  56. data/lib/vendor/treetop/lib/treetop/runtime.rb +5 -0
  57. data/lib/vendor/treetop/lib/treetop/runtime/compiled_parser.rb +109 -0
  58. data/lib/vendor/treetop/lib/treetop/runtime/interval_skip_list.rb +4 -0
  59. data/lib/vendor/treetop/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
  60. data/lib/vendor/treetop/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
  61. data/lib/vendor/treetop/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
  62. data/lib/vendor/treetop/lib/treetop/runtime/syntax_node.rb +90 -0
  63. data/lib/vendor/treetop/lib/treetop/runtime/terminal_parse_failure.rb +16 -0
  64. data/lib/vendor/treetop/lib/treetop/runtime/terminal_syntax_node.rb +17 -0
  65. data/lib/vendor/treetop/lib/treetop/version.rb +9 -0
  66. data/lib/vendor/treetop/spec/compiler/and_predicate_spec.rb +36 -0
  67. data/lib/vendor/treetop/spec/compiler/anything_symbol_spec.rb +44 -0
  68. data/lib/vendor/treetop/spec/compiler/character_class_spec.rb +247 -0
  69. data/lib/vendor/treetop/spec/compiler/choice_spec.rb +80 -0
  70. data/lib/vendor/treetop/spec/compiler/circular_compilation_spec.rb +28 -0
  71. data/lib/vendor/treetop/spec/compiler/failure_propagation_functional_spec.rb +21 -0
  72. data/lib/vendor/treetop/spec/compiler/grammar_compiler_spec.rb +84 -0
  73. data/lib/vendor/treetop/spec/compiler/grammar_spec.rb +41 -0
  74. data/lib/vendor/treetop/spec/compiler/nonterminal_symbol_spec.rb +40 -0
  75. data/lib/vendor/treetop/spec/compiler/not_predicate_spec.rb +38 -0
  76. data/lib/vendor/treetop/spec/compiler/one_or_more_spec.rb +35 -0
  77. data/lib/vendor/treetop/spec/compiler/optional_spec.rb +37 -0
  78. data/lib/vendor/treetop/spec/compiler/parenthesized_expression_spec.rb +19 -0
  79. data/lib/vendor/treetop/spec/compiler/parsing_rule_spec.rb +32 -0
  80. data/lib/vendor/treetop/spec/compiler/sequence_spec.rb +115 -0
  81. data/lib/vendor/treetop/spec/compiler/terminal_spec.rb +81 -0
  82. data/lib/vendor/treetop/spec/compiler/terminal_symbol_spec.rb +37 -0
  83. data/lib/vendor/treetop/spec/compiler/test_grammar.treetop +7 -0
  84. data/lib/vendor/treetop/spec/compiler/test_grammar.tt +7 -0
  85. data/lib/vendor/treetop/spec/compiler/test_grammar_do.treetop +7 -0
  86. data/lib/vendor/treetop/spec/compiler/zero_or_more_spec.rb +56 -0
  87. data/lib/vendor/treetop/spec/composition/a.treetop +11 -0
  88. data/lib/vendor/treetop/spec/composition/b.treetop +11 -0
  89. data/lib/vendor/treetop/spec/composition/c.treetop +10 -0
  90. data/lib/vendor/treetop/spec/composition/d.treetop +10 -0
  91. data/lib/vendor/treetop/spec/composition/f.treetop +17 -0
  92. data/lib/vendor/treetop/spec/composition/grammar_composition_spec.rb +40 -0
  93. data/lib/vendor/treetop/spec/composition/subfolder/e_includes_c.treetop +15 -0
  94. data/lib/vendor/treetop/spec/ruby_extensions/string_spec.rb +32 -0
  95. data/lib/vendor/treetop/spec/runtime/compiled_parser_spec.rb +101 -0
  96. data/lib/vendor/treetop/spec/runtime/interval_skip_list/delete_spec.rb +147 -0
  97. data/lib/vendor/treetop/spec/runtime/interval_skip_list/expire_range_spec.rb +349 -0
  98. data/lib/vendor/treetop/spec/runtime/interval_skip_list/insert_and_delete_node.rb +385 -0
  99. data/lib/vendor/treetop/spec/runtime/interval_skip_list/insert_spec.rb +660 -0
  100. data/lib/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.graffle +6175 -0
  101. data/lib/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.rb +58 -0
  102. data/lib/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture.rb +23 -0
  103. data/lib/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture_spec.rb +164 -0
  104. data/lib/vendor/treetop/spec/runtime/interval_skip_list/spec_helper.rb +84 -0
  105. data/lib/vendor/treetop/spec/runtime/syntax_node_spec.rb +68 -0
  106. data/lib/vendor/treetop/spec/spec_helper.rb +106 -0
  107. data/lib/vendor/treetop/spec/spec_suite.rb +4 -0
  108. data/lib/vendor/treetop/treetop.gemspec +17 -0
  109. data/spec/command_spec.rb +2 -6
  110. data/spec/css/accessors-1.0.css +18 -0
  111. data/spec/css/big-1.0.css +3768 -0
  112. data/spec/css/comments-1.0.css +9 -0
  113. data/spec/css/css-1.0.css +40 -0
  114. data/spec/css/functions-1.0.css +6 -0
  115. data/spec/css/import-1.0.css +11 -0
  116. data/spec/css/mixins-1.0.css +28 -0
  117. data/spec/css/operations-1.0.css +28 -0
  118. data/spec/css/rulesets-1.0.css +17 -0
  119. data/spec/css/scope-1.0.css +14 -0
  120. data/spec/css/strings-1.0.css +12 -0
  121. data/spec/css/variables-1.0.css +6 -0
  122. data/spec/css/whitespace-1.0.css +9 -0
  123. data/spec/engine_spec.rb +66 -18
  124. data/spec/less/accessors-1.0.less +20 -0
  125. data/spec/less/big-1.0.less +4810 -0
  126. data/spec/less/colors-1.0.less +0 -0
  127. data/spec/less/comments-1.0.less +46 -0
  128. data/spec/less/css-1.0.less +84 -0
  129. data/spec/less/exceptions/mixed-units-error.less +3 -0
  130. data/spec/less/exceptions/name-error-1.0.less +3 -0
  131. data/spec/less/exceptions/syntax-error-1.0.less +3 -0
  132. data/spec/less/functions-1.0.less +6 -0
  133. data/spec/less/import-1.0.less +7 -0
  134. data/spec/less/import/import-test-a.less +2 -0
  135. data/spec/less/import/import-test-b.less +8 -0
  136. data/spec/less/import/import-test-c.less +5 -0
  137. data/spec/less/mixins-1.0.less +43 -0
  138. data/spec/less/operations-1.0.less +39 -0
  139. data/spec/less/rulesets-1.0.less +30 -0
  140. data/spec/less/scope-1.0.less +33 -0
  141. data/spec/less/strings-1.0.less +14 -0
  142. data/spec/less/variables-1.0.less +18 -0
  143. data/spec/less/whitespace-1.0.less +21 -0
  144. data/spec/spec.css +79 -24
  145. data/spec/spec.less +2 -3
  146. data/spec/spec_helper.rb +4 -1
  147. metadata +136 -13
  148. data/lib/less/tree.rb +0 -82
  149. data/spec/css/less-0.8.10.css +0 -30
  150. data/spec/css/less-0.8.11.css +0 -31
  151. data/spec/css/less-0.8.12.css +0 -28
  152. data/spec/css/less-0.8.5.css +0 -24
  153. data/spec/css/less-0.8.6.css +0 -24
  154. data/spec/css/less-0.8.7.css +0 -24
  155. data/spec/css/less-0.8.8.css +0 -25
  156. data/spec/tree_spec.rb +0 -5
@@ -0,0 +1,68 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Sequence < ParsingExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+ begin_comment(self)
7
+ use_vars :result, :start_index, :accumulator
8
+ compile_sequence_elements(sequence_elements)
9
+ builder.if__ "#{accumulator_var}.last" do
10
+ assign_result "instantiate_node(#{node_class_name},input, #{start_index_var}...index, #{accumulator_var})"
11
+ extend_result sequence_element_accessor_module_name if sequence_element_accessor_module_name
12
+ extend_result_with_inline_module
13
+ end
14
+ builder.else_ do
15
+ reset_index
16
+ assign_failure start_index_var
17
+ end
18
+ end_comment(self)
19
+ end
20
+
21
+ def node_class_name
22
+ node_class_declarations.node_class_name || 'SyntaxNode'
23
+ end
24
+
25
+ def compile_sequence_elements(elements)
26
+ obtain_new_subexpression_address
27
+ elements.first.compile(subexpression_address, builder)
28
+ accumulate_subexpression_result
29
+ if elements.size > 1
30
+ builder.if_ subexpression_success? do
31
+ compile_sequence_elements(elements[1..-1])
32
+ end
33
+ end
34
+ end
35
+
36
+ def sequence_element_accessor_module
37
+ @sequence_element_accessor_module ||= SequenceElementAccessorModule.new(sequence_elements)
38
+ end
39
+
40
+ def sequence_element_accessor_module_name
41
+ sequence_element_accessor_module.module_name
42
+ end
43
+ end
44
+
45
+ class SequenceElementAccessorModule
46
+ include InlineModuleMixin
47
+ attr_reader :sequence_elements
48
+
49
+ def initialize(sequence_elements)
50
+ @sequence_elements = sequence_elements
51
+ end
52
+
53
+ def compile(index, builder, rule)
54
+ super
55
+ builder.module_declaration(module_name) do
56
+ sequence_elements.each_with_index do |element, index|
57
+ if element.label_name
58
+ builder.method_declaration(element.label_name) do
59
+ builder << "elements[#{index}]"
60
+ end
61
+ builder.newline unless index == sequence_elements.size - 1
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Terminal < AtomicExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+ string_length = eval(text_value).length
7
+
8
+ builder.if__ "has_terminal?(#{text_value}, false, index)" do
9
+ assign_result "instantiate_node(#{node_class_name},input, index...(index + #{string_length}))"
10
+ extend_result_with_inline_module
11
+ builder << "@index += #{string_length}"
12
+ end
13
+ builder.else_ do
14
+ builder << "terminal_parse_failure(#{text_value})"
15
+ assign_result 'nil'
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Treetop
2
+ module Compiler
3
+ class TransientPrefix < ParsingExpression
4
+ def compile(address, builder, parent_expression)
5
+ parent_expression.prefixed_expression.compile(address, builder)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Treetop
2
+ module Compiler
3
+ class TreetopFile < Runtime::SyntaxNode
4
+ def compile
5
+ (elements.map {|elt| elt.compile}).join
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,113 @@
1
+ module Treetop
2
+ module Compiler
3
+ class RubyBuilder
4
+
5
+ attr_reader :level, :address_space, :ruby
6
+
7
+ def initialize
8
+ @level = 0
9
+ @address_space = LexicalAddressSpace.new
10
+ @ruby = ""
11
+ end
12
+
13
+ def <<(ruby_line)
14
+ return if ruby_line.blank?
15
+ ruby << ruby_line.tabto(level) << "\n"
16
+ end
17
+
18
+ def newline
19
+ ruby << "\n"
20
+ end
21
+
22
+ def indented(depth = 2)
23
+ self.in(depth)
24
+ yield
25
+ self.out(depth)
26
+ end
27
+
28
+ def class_declaration(name, &block)
29
+ self << "class #{name}"
30
+ indented(&block)
31
+ self << "end"
32
+ end
33
+
34
+ def module_declaration(name, &block)
35
+ self << "module #{name}"
36
+ indented(&block)
37
+ self << "end"
38
+ end
39
+
40
+ def method_declaration(name, &block)
41
+ self << "def #{name}"
42
+ indented(&block)
43
+ self << "end"
44
+ end
45
+
46
+ def assign(left, right)
47
+ if left.instance_of? Array
48
+ self << "#{left.join(', ')} = #{right.join(', ')}"
49
+ else
50
+ self << "#{left} = #{right}"
51
+ end
52
+ end
53
+
54
+ def extend(var, module_name)
55
+ self << "#{var}.extend(#{module_name})"
56
+ end
57
+
58
+ def accumulate(left, right)
59
+ self << "#{left} << #{right}"
60
+ end
61
+
62
+ def if__(condition, &block)
63
+ self << "if #{condition}"
64
+ indented(&block)
65
+ end
66
+
67
+ def if_(condition, &block)
68
+ if__(condition, &block)
69
+ self << 'end'
70
+ end
71
+
72
+ def else_(&block)
73
+ self << 'else'
74
+ indented(&block)
75
+ self << 'end'
76
+ end
77
+
78
+ def loop(&block)
79
+ self << 'loop do'
80
+ indented(&block)
81
+ self << 'end'
82
+ end
83
+
84
+ def break
85
+ self << 'break'
86
+ end
87
+
88
+ def in(depth = 2)
89
+ @level += depth
90
+ self
91
+ end
92
+
93
+ def out(depth = 2)
94
+ @level -= depth
95
+ self
96
+ end
97
+
98
+ def next_address
99
+ address_space.next_address
100
+ end
101
+
102
+ def reset_addresses
103
+ address_space.reset_addresses
104
+ end
105
+
106
+ private
107
+
108
+ def indent
109
+ " " * level
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,2 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/ruby_extensions/string"
@@ -0,0 +1,42 @@
1
+ class String
2
+ def column_of(index)
3
+ return 1 if index == 0
4
+ newline_index = rindex("\n", index - 1)
5
+ if newline_index
6
+ index - newline_index
7
+ else
8
+ index + 1
9
+ end
10
+ end
11
+
12
+ def line_of(index)
13
+ self[0...index].count("\n") + 1
14
+ end
15
+
16
+ unless method_defined?(:blank?)
17
+ def blank?
18
+ self == ""
19
+ end
20
+ end
21
+
22
+ # The following methods are lifted from Facets 2.0.2
23
+ def tabto(n)
24
+ if self =~ /^( *)\S/
25
+ indent(n - $1.length)
26
+ else
27
+ self
28
+ end
29
+ end
30
+
31
+ def indent(n)
32
+ if n >= 0
33
+ gsub(/^/, ' ' * n)
34
+ else
35
+ gsub(/^ {0,#{-n}}/, "")
36
+ end
37
+ end
38
+
39
+ def treetop_camelize
40
+ to_s.gsub(/\/(.?)/){ "::" + $1.upcase }.gsub(/(^|_)(.)/){ $2.upcase }
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/runtime/compiled_parser"
3
+ require "#{dir}/runtime/syntax_node"
4
+ require "#{dir}/runtime/terminal_parse_failure"
5
+ require "#{dir}/runtime/interval_skip_list"
@@ -0,0 +1,109 @@
1
+ module Treetop
2
+ module Runtime
3
+ class CompiledParser
4
+ include Treetop::Runtime
5
+
6
+ attr_reader :input, :index, :max_terminal_failure_index
7
+ attr_writer :root
8
+ attr_accessor :consume_all_input
9
+ alias :consume_all_input? :consume_all_input
10
+
11
+ def initialize
12
+ self.consume_all_input = true
13
+ end
14
+
15
+ def parse(input, options = {})
16
+ prepare_to_parse(input)
17
+ @index = options[:index] if options[:index]
18
+ result = send("_nt_#{root}")
19
+ return nil if (consume_all_input? && index != input.size)
20
+ return result
21
+ end
22
+
23
+ def failure_index
24
+ max_terminal_failure_index
25
+ end
26
+
27
+ def failure_line
28
+ @terminal_failures && input.line_of(failure_index)
29
+ end
30
+
31
+ def failure_column
32
+ @terminal_failures && input.column_of(failure_index)
33
+ end
34
+
35
+ def failure_reason
36
+ return nil unless (tf = terminal_failures) && tf.size > 0
37
+ "Expected " +
38
+ (tf.size == 1 ?
39
+ tf[0].expected_string :
40
+ "one of #{tf.map{|f| f.expected_string}.uniq*', '}"
41
+ ) +
42
+ " at line #{failure_line}, column #{failure_column} (byte #{failure_index+1})" +
43
+ " after #{input[index...failure_index]}"
44
+ end
45
+
46
+ def terminal_failures
47
+ @terminal_failures.map! {|tf_ary| TerminalParseFailure.new(*tf_ary) }
48
+ end
49
+
50
+
51
+ protected
52
+
53
+ attr_reader :node_cache, :input_length
54
+ attr_writer :index
55
+
56
+ def prepare_to_parse(input)
57
+ @input = input
58
+ @input_length = input.length
59
+ reset_index
60
+ @node_cache = Hash.new {|hash, key| hash[key] = Hash.new}
61
+ @regexps = {}
62
+ @terminal_failures = []
63
+ @max_terminal_failure_index = 0
64
+ end
65
+
66
+ def reset_index
67
+ @index = 0
68
+ end
69
+
70
+ def parse_anything(node_class = SyntaxNode, inline_module = nil)
71
+ if index < input.length
72
+ result = instantiate_node(node_class,input, index...(index + 1))
73
+ result.extend(inline_module) if inline_module
74
+ @index += 1
75
+ result
76
+ else
77
+ terminal_parse_failure("any character")
78
+ end
79
+ end
80
+
81
+ def instantiate_node(node_type,*args)
82
+ if node_type.respond_to? :new
83
+ node_type.new(*args)
84
+ else
85
+ SyntaxNode.new(*args).extend(node_type)
86
+ end
87
+ end
88
+
89
+ def has_terminal?(terminal, regex, index)
90
+ if regex
91
+ rx = @regexps[terminal] ||= Regexp.new(terminal)
92
+ input.index(rx, index) == index
93
+ else
94
+ input[index] == terminal[0] && input.index(terminal, index) == index
95
+ end
96
+ end
97
+
98
+ def terminal_parse_failure(expected_string)
99
+ return nil if index < max_terminal_failure_index
100
+ if index > max_terminal_failure_index
101
+ @max_terminal_failure_index = index
102
+ @terminal_failures = []
103
+ end
104
+ @terminal_failures << [index, expected_string]
105
+ return nil
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,4 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/interval_skip_list/interval_skip_list.rb"
3
+ require "#{dir}/interval_skip_list/head_node.rb"
4
+ require "#{dir}/interval_skip_list/node.rb"
@@ -0,0 +1,15 @@
1
+ class IntervalSkipList
2
+ class HeadNode
3
+ attr_reader :height, :forward, :forward_markers
4
+
5
+ def initialize(height)
6
+ @height = height
7
+ @forward = Array.new(height, nil)
8
+ @forward_markers = Array.new(height) {|i| []}
9
+ end
10
+
11
+ def top_level
12
+ height - 1
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,200 @@
1
+ class IntervalSkipList
2
+ attr_reader :probability
3
+
4
+ def initialize
5
+ @head = HeadNode.new(max_height)
6
+ @ranges = {}
7
+ @probability = 0.5
8
+ end
9
+
10
+ def max_height
11
+ 3
12
+ end
13
+
14
+ def empty?
15
+ head.forward[0].nil?
16
+ end
17
+
18
+ def expire(range, length_change)
19
+ expired_markers, first_node_after_range = overlapping(range)
20
+ expired_markers.each { |marker| delete(marker) }
21
+ first_node_after_range.propagate_length_change(length_change)
22
+ end
23
+
24
+ def overlapping(range)
25
+ markers, first_node = containing_with_node(range.first)
26
+
27
+ cur_node = first_node
28
+ begin
29
+ markers.concat(cur_node.forward_markers.flatten)
30
+ cur_node = cur_node.forward[0]
31
+ end while cur_node.key < range.last
32
+
33
+ return markers.uniq, cur_node
34
+ end
35
+
36
+ def containing(n)
37
+ containing_with_node(n).first
38
+ end
39
+
40
+ def insert(range, marker)
41
+ ranges[marker] = range
42
+ first_node = insert_node(range.first)
43
+ first_node.endpoint_of.push(marker)
44
+ last_node = insert_node(range.last)
45
+ last_node.endpoint_of.push(marker)
46
+
47
+ cur_node = first_node
48
+ cur_level = first_node.top_level
49
+ while next_node_at_level_inside_range?(cur_node, cur_level, range)
50
+ while can_ascend_from?(cur_node, cur_level) && next_node_at_level_inside_range?(cur_node, cur_level + 1, range)
51
+ cur_level += 1
52
+ end
53
+ cur_node = mark_forward_path_at_level(cur_node, cur_level, marker)
54
+ end
55
+
56
+ while node_inside_range?(cur_node, range)
57
+ while can_descend_from?(cur_level) && next_node_at_level_outside_range?(cur_node, cur_level, range)
58
+ cur_level -= 1
59
+ end
60
+ cur_node = mark_forward_path_at_level(cur_node, cur_level, marker)
61
+ end
62
+ end
63
+
64
+ def delete(marker)
65
+ range = ranges[marker]
66
+ path_to_first_node = make_path
67
+ first_node = find(range.first, path_to_first_node)
68
+
69
+ cur_node = first_node
70
+ cur_level = first_node.top_level
71
+ while next_node_at_level_inside_range?(cur_node, cur_level, range)
72
+ while can_ascend_from?(cur_node, cur_level) && next_node_at_level_inside_range?(cur_node, cur_level + 1, range)
73
+ cur_level += 1
74
+ end
75
+ cur_node = unmark_forward_path_at_level(cur_node, cur_level, marker)
76
+ end
77
+
78
+ while node_inside_range?(cur_node, range)
79
+ while can_descend_from?(cur_level) && next_node_at_level_outside_range?(cur_node, cur_level, range)
80
+ cur_level -= 1
81
+ end
82
+ cur_node = unmark_forward_path_at_level(cur_node, cur_level, marker)
83
+ end
84
+ last_node = cur_node
85
+
86
+ first_node.endpoint_of.delete(marker)
87
+ if first_node.endpoint_of.empty?
88
+ first_node.delete(path_to_first_node)
89
+ end
90
+
91
+ last_node.endpoint_of.delete(marker)
92
+ if last_node.endpoint_of.empty?
93
+ path_to_last_node = make_path
94
+ find(range.last, path_to_last_node)
95
+ last_node.delete(path_to_last_node)
96
+ end
97
+ end
98
+
99
+ protected
100
+ attr_reader :head, :ranges
101
+
102
+ def insert_node(key)
103
+ path = make_path
104
+ found_node = find(key, path)
105
+ if found_node && found_node.key == key
106
+ return found_node
107
+ else
108
+ return Node.new(key, next_node_height, path)
109
+ end
110
+ end
111
+
112
+ def containing_with_node(n)
113
+ containing = []
114
+ cur_node = head
115
+ (max_height - 1).downto(0) do |cur_level|
116
+ while (next_node = cur_node.forward[cur_level]) && next_node.key <= n
117
+ cur_node = next_node
118
+ if cur_node.key == n
119
+ return containing + (cur_node.markers - cur_node.endpoint_of), cur_node
120
+ end
121
+ end
122
+ containing.concat(cur_node.forward_markers[cur_level])
123
+ end
124
+
125
+ return containing, cur_node
126
+ end
127
+
128
+ def delete_node(key)
129
+ path = make_path
130
+ found_node = find(key, path)
131
+ found_node.delete(path) if found_node.key == key
132
+ end
133
+
134
+ def find(key, path)
135
+ cur_node = head
136
+ (max_height - 1).downto(0) do |cur_level|
137
+ while (next_node = cur_node.forward[cur_level]) && next_node.key < key
138
+ cur_node = next_node
139
+ end
140
+ path[cur_level] = cur_node
141
+ end
142
+ cur_node.forward[0]
143
+ end
144
+
145
+ def make_path
146
+ Array.new(max_height, nil)
147
+ end
148
+
149
+ def next_node_height
150
+ height = 1
151
+ while rand < probability && height < max_height
152
+ height += 1
153
+ end
154
+ height
155
+ end
156
+
157
+ def can_ascend_from?(node, level)
158
+ level < node.top_level
159
+ end
160
+
161
+ def can_descend_from?(level)
162
+ level > 0
163
+ end
164
+
165
+ def node_inside_range?(node, range)
166
+ node.key < range.last
167
+ end
168
+
169
+ def next_node_at_level_inside_range?(node, level, range)
170
+ node.forward[level] && node.forward[level].key <= range.last
171
+ end
172
+
173
+ def next_node_at_level_outside_range?(node, level, range)
174
+ (node.forward[level].nil? || node.forward[level].key > range.last)
175
+ end
176
+
177
+ def mark_forward_path_at_level(node, level, marker)
178
+ node.forward_markers[level].push(marker)
179
+ next_node = node.forward[level]
180
+ next_node.markers.push(marker)
181
+ node = next_node
182
+ end
183
+
184
+ def unmark_forward_path_at_level(node, level, marker)
185
+ node.forward_markers[level].delete(marker)
186
+ next_node = node.forward[level]
187
+ next_node.markers.delete(marker)
188
+ node = next_node
189
+ end
190
+
191
+ def nodes
192
+ nodes = []
193
+ cur_node = head.forward[0]
194
+ until cur_node.nil?
195
+ nodes << cur_node
196
+ cur_node = cur_node.forward[0]
197
+ end
198
+ nodes
199
+ end
200
+ end