Category talk:Wren-llist: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
(→‎Source code: Added copy() methods.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(5 intermediate revisions by the same user not shown)
Line 13:
For various reasons (efficient indexing, smaller memory footprint, more cache friendly) dynamic arrays tend to be preferred to linked lists nowadays and it is not expected that this module will see much use outside of RC particularly as it is written entirely in Wren whereas the built-in List class is written mostly in C. Consequently, only limited effort has been made to optimize the implementation.
===Source code===
<langsyntaxhighlight ecmascriptlang="wren">/* Module "llist.wren" */
 
/* Node is a building block for a singly linked list. As such it consists of two fields:
Line 87:
isEmpty { _count == 0 }
 
// Adds an element at the tail of the current instance and returns it.
add(d) {
var node = Node.new(d)
Line 101:
}
_tail = node
return d
}
 
// Adds a sequence of elements at the tail of the current instance and returns them.
addAll(a) {
if (!(a is Sequence)) Fiber.abort("Argument must be a Sequence.")
for (e in a) add(e)
return a
}
 
// Inserts an element at the head of the current instance and returns it.
prepend(d) { insert_(0, d) }
 
// Inserts a sequence of elements at the head of the current instance and returns them.
prependAll(a) {
if (!(a is Sequence)) Fiber.abort("Argument must be a Sequence.")
Line 120 ⟶ 122:
i = i + 1
}
return a
}
 
Line 273 ⟶ 276:
// Exchanges the elements at indices 'i' and 'j' of the current instance.
exchange(i, j) {
if (i == j) return
var t = this[i]
this[i] = this[j]
Line 526 ⟶ 530:
isEmpty { _count == 0 }
 
// Adds an element at the tail of the current instance and returns it.
add(d) {
var node = DNode.new(d)
Line 542 ⟶ 546:
}
_tail = node
return d
}
 
// Adds a sequence of elements at the tail of the current instance and returns them.
addAll(a) {
if (!(a is Sequence)) Fiber.abort("Argument must be a Sequence.")
for (e in a) add(e)
return a
}
 
// Inserts an element at the head of the current instance and returns it.
prepend(d) { insert_(0, d) }
 
// Inserts a sequence of elements at the head of the current instance and returns them.
prependAll(a) {
if (!(a is Sequence)) Fiber.abort("Argument must be a Sequence.")
Line 561 ⟶ 567:
i = i + 1
}
return a
}
 
Line 734 ⟶ 741:
// Exchanges the elements at indices 'i' and 'j' of the current instance.
exchange(i, j) {
if (i == j) return
var t = this[i]
this[i] = this[j]
Line 973 ⟶ 981:
// Returns the string representation of the current instance.
toString { "[" + toList.join(" <-> ") +"]" }
}</syntaxhighlight>
}
 
// Type aliases for classes in case of any name clashes with other modules.
var LList_Node = Node
var LList_LinkedList = LinkedList
var LList_DNode = DNode
var LList_DLinkedList = DLinkedList</lang>
9,476

edits