11--[[ --
2- Array of homogenous objects.
2+ Vector of homogenous objects.
33
4- An array is usually a block of contiguous memory, divided into equal
4+ A vector is usually a block of contiguous memory, divided into equal
55 sized elements that can be indexed quickly.
66
7- Create a new array with:
7+ Create a new vector with:
88
9- > array = require "std.array "
10- > Array = array ()
11- > a = Array ("int", {0xdead, 0xbeef, 0xfeed})
9+ > vector = require "std.vector "
10+ > Vector = vector ()
11+ > a = Vector ("int", {0xdead, 0xbeef, 0xfeed})
1212 > =a[1], a[2], a[3], a[-3], a[-4]
1313 57005 48879 65261 57005 nil
1414
15- All the indices passed to array methods use 1-based counting.
15+ All the indices passed to vector methods use 1-based counting.
1616
1717 If the Lua alien module is installed, and the `type` argument passed
18- when cloning a new array object is suitable (i.e. the name of a numeric
19- C type that `alien.sizeof` understands), then the array contents are
18+ when cloning a new vector object is suitable (i.e. the name of a numeric
19+ C type that `alien.sizeof` understands), then the vector contents are
2020 managed in an `alien.buffer`.
2121
2222 If alien is not installed, or does not understand the `type` argument
2323 given when cloning, then a much slower (but API compatible) Lua table
2424 is transparently used to manage elements instead.
2525
26- In either case, `std.array ` provides a means for managing collections
27- of homogenous Lua objects with a vector -like, stack-like or queue-like
26+ In either case, `std.vector ` provides a means for managing collections
27+ of homogenous Lua objects with an array -like, stack-like or queue-like
2828 API.
2929
30- @classmod std.array
30+ @classmod std.vector
3131]]
3232
3333
@@ -67,18 +67,18 @@ local function sizeof (type)
6767end
6868
6969
70- --- Convert an array element index into a pointer.
71- -- @tparam std.array self an array
72- -- @int i[opt=1] an index into array
70+ --- Convert a vector element index into a pointer.
71+ -- @tparam std.vector self a vector
72+ -- @int i[opt=1] an index into vector
7373-- @treturn alien.buffer.pointer suitable for memmove or memset
7474local function topointer (self , i )
7575 i = i or 1
7676 return self .buffer :topointer ((i - 1 ) * self .size + 1 )
7777end
7878
7979
80- --- Fast zeroing of a contiguous block of array elements for `alien.buffer`s.
81- -- @tparam std.array self an array
80+ --- Fast zeroing of a contiguous block of vector elements for `alien.buffer`s.
81+ -- @tparam std.vector self a vector
8282-- @int from index of first element to zero out
8383-- @int n number of elements to zero out
8484local function setzero (self , from , n )
9292--[[ ================== ]] --
9393
9494
95- -- Initial array prototype object, plus any derived object containing
95+ -- Initial vector prototype object, plus any derived object containing
9696-- elements that don't fit in alien buffers use `core_functions` to
9797-- find object methods and `core_metatable` for metamethods.
9898
@@ -103,7 +103,7 @@ local core_functions = {
103103 --- Remove the right-most element.
104104 -- @function pop
105105 -- @return the right-most element
106- -- @usage removed = anarray :pop ()
106+ -- @usage removed = anvector :pop ()
107107 pop = function (self )
108108 self .length = math.max (self .length - 1 , 0 )
109109 return table.remove (self .buffer )
@@ -114,7 +114,7 @@ local core_functions = {
114114 -- @function push
115115 -- @param elem new element to be pushed
116116 -- @return elem
117- -- @usage added = anarray :push (anelement)
117+ -- @usage added = anvector :push (anelement)
118118 push = function (self , elem )
119119 local length = self .length + 1
120120 self .buffer [length ] = elem
@@ -126,8 +126,8 @@ local core_functions = {
126126 --- Change the number of elements allocated to be at least `n`.
127127 -- @function realloc
128128 -- @int n the number of elements required
129- -- @treturn std.array the array
130- -- @usage anarray = anarray :realloc (anarray .length)
129+ -- @treturn std.vector the vector
130+ -- @usage anvector = anvector :realloc (anvector .length)
131131 realloc = function (self , n )
132132 argcheck (" realloc" , 2 , " int" , n )
133133
@@ -146,10 +146,10 @@ local core_functions = {
146146 -- @int from index of first element to set
147147 -- @param v value to store
148148 -- @int n number of elements to set
149- -- @treturn std.array the array
150- -- @usage anarray :realloc (anarray .length):set (1, -1, anarray .length)
149+ -- @treturn std.vector the vector
150+ -- @usage anvector :realloc (anvector .length):set (1, -1, anvector .length)
151151 set = function (self , from , v , n )
152- argscheck (" set" , {" Array " , " int" , " any" , " int" },
152+ argscheck (" set" , {" Vector " , " int" , " any" , " int" },
153153 {self , from , v , n })
154154
155155 local length = self .length
@@ -164,22 +164,22 @@ local core_functions = {
164164 end ,
165165
166166
167- --- Shift the whole array to the left by removing the left-most element.
168- -- This makes the array 1 element shorter than it was before the shift.
167+ --- Shift the whole vector to the left by removing the left-most element.
168+ -- This makes the vector 1 element shorter than it was before the shift.
169169 -- @function shift
170170 -- @return the removed element.
171- -- @usage removed = anarray :shift ()
171+ -- @usage removed = anvector :shift ()
172172 shift = function (self )
173173 self .length = math.max (self .length - 1 , 0 )
174174 return table.remove (self .buffer , 1 )
175175 end ,
176176
177177
178- --- Shift the whole array to the right by inserting a new left-most element.
178+ --- Shift the whole vector to the right by inserting a new left-most element.
179179 -- @function unshift
180180 -- @param elem new element to be pushed
181181 -- @treturn elem
182- -- @usage added = anarray :unshift (anelement)
182+ -- @usage added = anvector :unshift (anelement)
183183 unshift = function (self , elem )
184184 self .length = self .length + 1
185185 table.insert (self .buffer , 1 , elem )
@@ -189,30 +189,30 @@ local core_functions = {
189189
190190
191191core_metatable = {
192- _type = " Array " ,
192+ _type = " Vector " ,
193193
194194
195- --- Instantiate a newly cloned array .
196- -- If not specified, `type` will be the same as the prototype array being
195+ --- Instantiate a newly cloned vector .
196+ -- If not specified, `type` will be the same as the prototype vector being
197197 -- cloned; otherwise, it can be any string. Only a type name accepted by
198198 -- `alien.sizeof` will use the fast `alien.buffer` managed memory buffer
199- -- for array contents; otherwise, a much slower Lua emulation is used.
199+ -- for vector contents; otherwise, a much slower Lua emulation is used.
200200 -- @function __call
201201 -- @string type element type name
202202 -- @tparam [opt] int|table init initial size or list of initial elements
203- -- @treturn std.array a new array object
203+ -- @treturn std.vector a new vector object
204204 -- @usage
205- -- local Array = require "std.array " {} -- not a typo!
206- -- local new = Array ("int", {1, 2, 3})
205+ -- local Vector = require "std.vector " {} -- not a typo!
206+ -- local new = Vector ("int", {1, 2, 3})
207207 __call = function (self , type , init )
208208 if _ARGCHECK then
209209 if init ~= nil then
210210 -- When called with 2 arguments:
211- argcheck (" Array " , 1 , " string" , type )
212- argcheck (" Array " , 2 , " int|table" , init )
211+ argcheck (" Vector " , 1 , " string" , type )
212+ argcheck (" Vector " , 2 , " int|table" , init )
213213 elseif type ~= nil then
214214 -- When called with 1 argument:
215- argcheck (" Array " , 1 , " int|string|table" , type )
215+ argcheck (" Vector " , 1 , " int|string|table" , type )
216216 end
217217 end
218218
@@ -222,7 +222,7 @@ core_metatable = {
222222 type = type or self .type
223223 init = init or self .length
224224
225- -- This will become the cloned array object.
225+ -- This will become the cloned vector object.
226226 local obj = {}
227227
228228 for k , v in pairs (self ) do
@@ -295,10 +295,10 @@ core_metatable = {
295295 end ,
296296
297297
298- --- Iterate consecutively over all elements with `ipairs (array )`.
298+ --- Iterate consecutively over all elements with `ipairs (vector )`.
299299 -- @function __ipairs
300300 -- @treturn function iterator function
301- -- @usage for index, anelement in ipairs (anarray ) do ... end
301+ -- @usage for index, anelement in ipairs (anvector ) do ... end
302302 __ipairs = function (self )
303303 local i , n = 0 , self .length
304304 return function ()
@@ -310,11 +310,11 @@ core_metatable = {
310310 end ,
311311
312312
313- --- Return the `n`th character in this array .
313+ --- Return the `n`th element in this vector .
314314 -- @function __index
315315 -- @int n 1-based index, or negative to index starting from the right
316316 -- @treturn string the element at index `n`
317- -- @usage rightmost = anarray[anarray .length]
317+ -- @usage rightmost = anvector[anvector .length]
318318 __index = function (self , n )
319319 argcheck (" __index" , 2 , " int|string" , n )
320320
@@ -329,19 +329,19 @@ core_metatable = {
329329 end ,
330330
331331
332- --- Set the `n`th element of this array to `elem`.
332+ --- Set the `n`th element of this vector to `elem`.
333333 -- @function __newindex
334334 -- @int n 1-based index
335335 -- @param elem value to store at index n
336- -- @treturn std.array the array
337- -- @usage anarray [1] = newvalue
336+ -- @treturn std.vector the vector
337+ -- @usage anvector [1] = newvalue
338338 __newindex = function (self , n , elem )
339339 argcheck (" __newindex" , 2 , " int" , n )
340340
341341 if typeof (n ) == " number" then
342342 local used = self .length
343343 if n == 0 or math.abs (n ) > used then
344- error (" array access " .. n .. " out of bounds: 0 < abs (n) <= " ..
344+ error (" vector access " .. n .. " out of bounds: 0 < abs (n) <= " ..
345345 tostring (self .length ), 2 )
346346 end
347347 if n < 0 then n = n + used + 1 end
@@ -353,26 +353,26 @@ core_metatable = {
353353 end ,
354354
355355
356- --- Return the number of elements in this array .
356+ --- Return the number of elements in this vector .
357357 --
358358 -- Beware that Lua 5.1 does not respect this metamethod; use
359- -- `array .length` if you care about portability.
359+ -- `vector .length` if you care about portability.
360360 -- @function __len
361361 -- @treturn int number of elements
362- -- @usage length = #anarray
362+ -- @usage length = #anvector
363363 __len = function (self )
364- argcheck (" __len" , 1 , " Array " , self )
364+ argcheck (" __len" , 1 , " Vector " , self )
365365
366366 return self .length
367367 end ,
368368
369369
370- --- Return a string representation of the contents of this array .
370+ --- Return a string representation of the contents of this vector .
371371 -- @function __tostring
372372 -- @treturn string string representation
373- -- @usage print (anarray )
373+ -- @usage print (anvector )
374374 __tostring = function (self )
375- argcheck (" __tostring" , 1 , " Array " , self )
375+ argcheck (" __tostring" , 1 , " Vector " , self )
376376
377377 local t = {}
378378 for i = 1 , self .length do
@@ -390,7 +390,7 @@ core_metatable = {
390390--[[ ===================== ]] --
391391
392392
393- -- Cloned array objects with elements managed by an alien buffer use
393+ -- Cloned vector objects with elements managed by an alien buffer use
394394-- `alien_functions` to find object methods and `alien_metatable`
395395-- for metamethods.
396396
@@ -438,7 +438,7 @@ local alien_functions = {
438438
439439
440440 set = function (self , from , v , n )
441- argscheck (" set" , {" Array " , " int" , " number" , " int" },
441+ argscheck (" set" , {" Vector " , " int" , " number" , " int" },
442442 {self , from , v , n })
443443
444444 local used = self .length
@@ -479,7 +479,7 @@ local alien_functions = {
479479
480480
481481alien_metatable = {
482- _type = " Array " ,
482+ _type = " Vector " ,
483483
484484 __ipairs = function (self )
485485 local i , n = 0 , self .length
@@ -511,7 +511,7 @@ alien_metatable = {
511511 if typeof (n ) == " number" then
512512 local used = self .length
513513 if n == 0 or math.abs (n ) > used then
514- error (" array access " .. n .. " out of bounds: 0 < n <= " .. tostring (self .length ), 2 )
514+ error (" vector access " .. n .. " out of bounds: 0 < n <= " .. tostring (self .length ), 2 )
515515 end
516516 if n < 0 then n = n + used + 1 end
517517 self .buffer :set ((n - 1 ) * self .size + 1 , elem , self .type )
@@ -534,36 +534,36 @@ alien_metatable = {
534534
535535
536536--- Return a function that dispatches to a virtual function table.
537- -- The __call metamethod ensures that cloned array objects are assigned
537+ -- The __call metamethod ensures that cloned vector objects are assigned
538538-- a metatable and method table optimised for the element storage method
539- -- (either alien buffer, or Lua table element containers), but the array
539+ -- (either alien buffer, or Lua table element containers), but the vector
540540-- prototype returned by this module needs to dispatch to the correct
541541-- function according to the element type at run-time, because we want
542542-- to support passing either object as an argument to a module function.
543543-- @string name method name to dispatch
544544-- @treturn function call `alien_function[name]` or -- `core_function[name]`
545- -- as appropriate to the element manager of array
545+ -- as appropriate to the element manager of vector
546546local function dispatch (name )
547- return function (array , ...)
548- argcheck (name , 1 , " Array " , array )
549- local vfns = array .size > 0 and alien_functions or core_functions
550- return vfns [name ] (array , ... )
547+ return function (vector , ...)
548+ argcheck (name , 1 , " Vector " , vector )
549+ local vfns = vector .size > 0 and alien_functions or core_functions
550+ return vfns [name ] (vector , ... )
551551 end
552552end
553553
554554
555555---- --
556- -- An efficient array of homogenous objects.
557- -- @table std.array
556+ -- An efficient vector of homogenous objects.
557+ -- @table std.vector
558558-- @int allocated number of allocated element slots, for `alien.buffer`
559559-- managed elements
560560-- @tfield alien.buffer|table buffer a block of indexable memory
561561-- @int length number of elements currently stored
562562-- @int size length of each stored element, or 0 when `alien.buffer` is
563- -- not managing this array
563+ -- not managing this vector
564564-- @string type type name for elements
565- local Array = Container {
566- _type = " Array " ,
565+ local Vector = Container {
566+ _type = " Vector " ,
567567
568568
569569 -- Prototype initial values.
@@ -593,4 +593,4 @@ local Array = Container {
593593}
594594
595595
596- return Array
596+ return Vector
0 commit comments