Skip to content

Commit 84edc67

Browse files
Added API annotations for internal functions
1 parent 5dc0341 commit 84edc67

File tree

9 files changed

+378
-66
lines changed

9 files changed

+378
-66
lines changed

src.primitives/algorithms/LinkedHashItems.js

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
primitives.common.LinkedHashItems = function () {
1+
/**
2+
* Creates linked hash list collection.
3+
* @returns {LinkedHashItems} Returns linked hash list structure
4+
*/
5+
primitives.common.LinkedHashItems = function () {
26
var segmentsHash = {},
37
nextKeys = {},
48
prevKeys = {},
59
startSegmentKey = null,
610
endSegmentKey = null;
711

12+
/**
13+
* Adds new item to collection
14+
* @param {string} key The new item key
15+
* @param {object} item The new item context object value
16+
*/
817
function add(key, item) {
918
if (segmentsHash.hasOwnProperty(key)) {
1019
throw "Duplicate segments are not supported!";
@@ -21,30 +30,70 @@
2130
endSegmentKey = key;
2231
}
2332

33+
/**
34+
* Checks if collection is empty
35+
*
36+
* @returns {boolean} Returns true if collection is empty
37+
*/
2438
function isEmpty() {
2539
return startSegmentKey == null;
2640
}
2741

42+
/**
43+
* Item context object
44+
*
45+
* @param {string} key The item's key
46+
* @returns {object} Returns context object
47+
*/
2848
function item(key) {
2949
return segmentsHash[key];
3050
}
3151

52+
/**
53+
* Gets next key
54+
*
55+
* @param {string} key The item key
56+
* @returns {string} Returns key of the next collection item
57+
*/
3258
function nextKey(key) {
3359
return nextKeys[key];
3460
}
3561

62+
/**
63+
* Gets previous key
64+
*
65+
* @param {string} key The item key
66+
* @returns {string} Returns key of the previous collection item
67+
*/
3668
function prevKey(key) {
3769
return prevKeys[key];
3870
}
3971

72+
/**
73+
* First collection item key
74+
*
75+
* @returns {string} Returns the key of the first item in the collection
76+
*/
4077
function startKey() {
4178
return startSegmentKey;
4279
}
4380

81+
/**
82+
* Last collection item key
83+
*
84+
* @returns {string} Returns key of the last item in the collection
85+
*/
4486
function endKey() {
4587
return endSegmentKey;
4688
}
4789

90+
/**
91+
* Adds new item to the head of the list
92+
*
93+
* @param {string} key The new item key
94+
* @param {object} item The new item context object value
95+
* @returns {string} Returns key of the last item in the collection
96+
*/
4897
function unshift(key, item) {
4998
if (segmentsHash.hasOwnProperty(key)) {
5099
throw "Duplicate segments are not supported!";
@@ -61,6 +110,13 @@
61110
startSegmentKey = key;
62111
}
63112

113+
/**
114+
* Inserts new item into the list after the given key
115+
*
116+
* @param {string} afterKey The key that the new element is placed after
117+
* @param {string} key The new item key
118+
* @param {object} item The new item context object value
119+
*/
64120
function insertAfter(afterKey, key, item) {
65121
if (segmentsHash.hasOwnProperty(key)) {
66122
throw "Duplicate segments are not supported!";
@@ -82,6 +138,13 @@
82138
}
83139
}
84140

141+
/**
142+
* Inserts new item into the list before the given key
143+
*
144+
* @param {string} beforeKey The key that the new element is placed before
145+
* @param {string} key The new item key
146+
* @param {object} item The new item context object value
147+
*/
85148
function insertBefore(beforeKey, key, item) {
86149
if (segmentsHash.hasOwnProperty(key)) {
87150
throw "Duplicate segments are not supported!";
@@ -97,6 +160,10 @@
97160
}
98161
}
99162

163+
/**
164+
* Removes item
165+
* @param {string} key The key of the item
166+
*/
100167
function remove(key) {
101168
var prevKey = prevKeys[key],
102169
nextKey = nextKeys[key];
@@ -118,6 +185,9 @@
118185
delete prevKeys[key];
119186
}
120187

188+
/**
189+
* Empties collection
190+
*/
121191
function empty() {
122192
segmentsHash = {};
123193
nextKeys = {};
@@ -152,20 +222,50 @@
152222
}
153223
}
154224

225+
/**
226+
* Appends one list to another
227+
*
228+
* @param {LinkedHashItems} list A list to append to the end of the current list
229+
*/
155230
function attach(list) {
156231
list.iterate(function (segment, key) {
157232
add(key, segment);
158233
});
159234
}
160235

236+
/**
237+
* Callback function for iterating list items
238+
*
239+
* @callback onLinkedHashItemsCallback
240+
* @param {object} item The item context object
241+
* @param {string} key The item key
242+
* @returns {boolean} Returns true to break the iteration process
243+
*/
244+
245+
/**
246+
* Loops items of the collection
247+
* @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items
248+
* @param {string} startKey The key to start iteration from
249+
* @param {string} endKey The key to end iteration at
250+
*/
161251
function iterate(onItem, startKey, endKey) {
162252
_iterate(true, onItem, startKey, endKey);
163253
}
164254

255+
/**
256+
* Loops items of the collection backward
257+
* @param {onLinkedHashItemsCallback} onItem Callback function for iterating collection items
258+
* @param {string} startKey The key to start iteration from
259+
* @param {string} endKey The key to end iteration at
260+
*/
165261
function iterateBack(onItem, startKey, endKey) {
166262
_iterate(false, onItem, startKey, endKey);
167263
}
168264

265+
/**
266+
* Validates internal data consistensy of the structure
267+
* @returns {boolean} Returns true if it pass validation
268+
*/
169269
function validate(info) {
170270
var key, prevKey, nextKey;
171271
for (key in segmentsHash) {
@@ -221,6 +321,11 @@
221321
return true;
222322
}
223323

324+
/**
325+
* Returns a regular javascript array of collection items
326+
*
327+
* @returns{object[]} Returns array containing items of the collection
328+
*/
224329
function toArray() {
225330
var result = [];
226331

src.primitives/algorithms/RMQ.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
primitives.common.RMQ = function (items) {
1+
/**
2+
* Creates range minimum query structure
3+
*
4+
* @params {number[]} items Collection of numbers
5+
* @returns {rmq} Returns range minimum query structure
6+
*/
7+
primitives.common.RMQ = function (items) {
28
var _lookup = [];
39
var _log2 = Math.log(2);
410

@@ -21,6 +27,12 @@
2127
}
2228
}
2329

30+
/**
31+
* Returns index of minimum item for the given range of items
32+
* @param {number} from The left margin index
33+
* @param {number} to The right margin index
34+
* @returns {number} Returns index of the minimum item
35+
*/
2436
function getRangeMinimumIndex(from, to) {
2537
var power = Math.floor(Math.log(to - from + 1) / _log2);
2638

@@ -31,6 +43,12 @@
3143
}
3244
}
3345

46+
/**
47+
* Return minimum value for the given range
48+
* @param {number} from The left index of the range
49+
* @param {number} to The right index of the range
50+
* @returns {number} Returns minimum value in the range
51+
*/
3452
function getRangeMinimum(from, to) {
3553
return items[getRangeMinimumIndex(from, to)];
3654
}

src.primitives/algorithms/SortedList.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Creates self-balancing binary search tree structure.
3+
* @returns {SortedList} Returns sorted list collection.
4+
*/
15
primitives.common.SortedList = function () {
26
var _rootNode = null;
37

@@ -117,6 +121,20 @@ primitives.common.SortedList = function () {
117121
}
118122
}
119123

124+
/**
125+
* Callback function to notify about duplicate values
126+
*
127+
* @callback onSortedListDuplicateCallback
128+
* @param {objct} context The context object of the duplicate value
129+
*/
130+
131+
/**
132+
* Adds value to sorted list collection
133+
* @param {number} value The value
134+
* @param {object} context The value context object
135+
* @param {object} thisArg The callback function invocation context
136+
* @param {onSortedListDuplicateCallback} onDuplicate Callback function for duplicates values notification
137+
*/
120138
function add(value, context, thisArg, onDuplicate) {
121139
if (_rootNode == null) {
122140
_rootNode = new Node(value, context);
@@ -211,6 +229,10 @@ primitives.common.SortedList = function () {
211229
toNode.context = fromNode.context;
212230
}
213231

232+
/**
233+
* Removes value from the sorted list
234+
* @param {number} value The removed value
235+
*/
214236
function remove(value) {
215237
var trace = [];
216238
var node = _rootNode;
@@ -273,6 +295,11 @@ primitives.common.SortedList = function () {
273295
}
274296
}
275297

298+
/**
299+
* Returns context object of the next value following the given one
300+
* @param {number} fromValue The value to start search from
301+
* @returns {object} Returns context object of the first value in sorted list greater than the start value.
302+
*/
276303
function nextContext(fromValue) {
277304
var result = null;
278305
loopForward(this, fromValue, function (value, context) {
@@ -282,6 +309,21 @@ primitives.common.SortedList = function () {
282309
return result;
283310
}
284311

312+
/**
313+
* Callback function for iterating values of the sorted list
314+
*
315+
* @callback onSortedListItemCallback
316+
* @param {number} value The value
317+
* @param {object} context The value context object
318+
* @returns {boolean} Returns true to break loop operation
319+
*/
320+
321+
/**
322+
* Loops sorted list values
323+
* @param {object} thisArg The callback function invocation context
324+
* @param {number} fromValue The start value to loop items of sorted list
325+
* @param {onSortedListItemCallback} onItem Callback function to iterate over sorted list values
326+
*/
285327
function loopForward(thisArg, fromValue, onItem) { //function onItem(value, context)
286328
if (onItem != null) {
287329
var trace = [];
@@ -317,6 +359,11 @@ primitives.common.SortedList = function () {
317359
}
318360
}
319361

362+
/**
363+
* Returns context object of the previous value preceding the given one
364+
* @param {number} fromValue The value to start search from
365+
* @returns {object} Returns context object of the first value in sorted list less than the start value.
366+
*/
320367
function previousContext(fromValue) {
321368
var result = null;
322369
loopBackward(this, fromValue, function (nextValue, context) {
@@ -326,6 +373,12 @@ primitives.common.SortedList = function () {
326373
return result;
327374
}
328375

376+
/**
377+
* Loops sorted list values backward
378+
* @param {object} thisArg The callback function invocation context
379+
* @param {number} fromValue The start value to loop items of sorted list
380+
* @param {onSortedListItemCallback} onItem Callback function to iterate over sorted list values
381+
*/
329382
function loopBackward(thisArg, fromValue, onItem) {
330383
if (onItem != null) {
331384
var trace = [];
@@ -388,6 +441,11 @@ primitives.common.SortedList = function () {
388441
return result;
389442
}
390443

444+
/**
445+
* Validate internal data consistency of the self-balancing binary search tree structure
446+
*
447+
* @returns {boolean} Returns true if structure pass validation
448+
*/
391449
function validate() {
392450
if (_rootNode != null) {
393451
var level = [_rootNode];

0 commit comments

Comments
 (0)