Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of pymite by
vm/seglist.h@0:65f1469d6bfb, 2013-03-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 02 11:54:20 2013 +0000
- Revision:
- 0:65f1469d6bfb
first commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| va009039 | 0:65f1469d6bfb | 1 | /* |
| va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
| va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
| va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
| va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
| va009039 | 0:65f1469d6bfb | 6 | */ |
| va009039 | 0:65f1469d6bfb | 7 | |
| va009039 | 0:65f1469d6bfb | 8 | |
| va009039 | 0:65f1469d6bfb | 9 | #ifndef __SEGLIST_H__ |
| va009039 | 0:65f1469d6bfb | 10 | #define __SEGLIST_H__ |
| va009039 | 0:65f1469d6bfb | 11 | |
| va009039 | 0:65f1469d6bfb | 12 | |
| va009039 | 0:65f1469d6bfb | 13 | /** |
| va009039 | 0:65f1469d6bfb | 14 | * \file |
| va009039 | 0:65f1469d6bfb | 15 | * \brief Segmented List data structure |
| va009039 | 0:65f1469d6bfb | 16 | * |
| va009039 | 0:65f1469d6bfb | 17 | * A seglist is a linked list of segments. |
| va009039 | 0:65f1469d6bfb | 18 | * A segment is an array of ptrs to objects |
| va009039 | 0:65f1469d6bfb | 19 | * (with a pointer to the next segment). |
| va009039 | 0:65f1469d6bfb | 20 | * Seglists are used to implement Lists and Dicts. |
| va009039 | 0:65f1469d6bfb | 21 | * |
| va009039 | 0:65f1469d6bfb | 22 | * This implementation of Seglist is straight. |
| va009039 | 0:65f1469d6bfb | 23 | * That is, the next pointer in the final segment |
| va009039 | 0:65f1469d6bfb | 24 | * contains C_NULL. |
| va009039 | 0:65f1469d6bfb | 25 | * |
| va009039 | 0:65f1469d6bfb | 26 | * This implementation of Seglist is dense. |
| va009039 | 0:65f1469d6bfb | 27 | * That is, there are no gaps in a segment. |
| va009039 | 0:65f1469d6bfb | 28 | * All entries point to an object, except entries |
| va009039 | 0:65f1469d6bfb | 29 | * that are beyond the index of the last item. |
| va009039 | 0:65f1469d6bfb | 30 | */ |
| va009039 | 0:65f1469d6bfb | 31 | |
| va009039 | 0:65f1469d6bfb | 32 | |
| va009039 | 0:65f1469d6bfb | 33 | /** Defines the length of the object array in a segment */ |
| va009039 | 0:65f1469d6bfb | 34 | #define SEGLIST_OBJS_PER_SEG 8 |
| va009039 | 0:65f1469d6bfb | 35 | |
| va009039 | 0:65f1469d6bfb | 36 | |
| va009039 | 0:65f1469d6bfb | 37 | /** Segment - an array of ptrs to objs */ |
| va009039 | 0:65f1469d6bfb | 38 | typedef struct Segment_s |
| va009039 | 0:65f1469d6bfb | 39 | { |
| va009039 | 0:65f1469d6bfb | 40 | /** object descriptor */ |
| va009039 | 0:65f1469d6bfb | 41 | PmObjDesc_t od; |
| va009039 | 0:65f1469d6bfb | 42 | /** array of ptrs to objs */ |
| va009039 | 0:65f1469d6bfb | 43 | pPmObj_t s_val[SEGLIST_OBJS_PER_SEG]; |
| va009039 | 0:65f1469d6bfb | 44 | /** ptr to next segment */ |
| va009039 | 0:65f1469d6bfb | 45 | struct Segment_s *next; |
| va009039 | 0:65f1469d6bfb | 46 | } Segment_t, |
| va009039 | 0:65f1469d6bfb | 47 | *pSegment_t; |
| va009039 | 0:65f1469d6bfb | 48 | |
| va009039 | 0:65f1469d6bfb | 49 | |
| va009039 | 0:65f1469d6bfb | 50 | /** Seglist - linked list of segments with current index info */ |
| va009039 | 0:65f1469d6bfb | 51 | typedef struct Seglist_s |
| va009039 | 0:65f1469d6bfb | 52 | { |
| va009039 | 0:65f1469d6bfb | 53 | /** object descriptor */ |
| va009039 | 0:65f1469d6bfb | 54 | PmObjDesc_t od; |
| va009039 | 0:65f1469d6bfb | 55 | /** index of (one past) last obj in last segment */ |
| va009039 | 0:65f1469d6bfb | 56 | int16_t sl_length; |
| va009039 | 0:65f1469d6bfb | 57 | /** ptr to first segment in list */ |
| va009039 | 0:65f1469d6bfb | 58 | pSegment_t sl_rootseg; |
| va009039 | 0:65f1469d6bfb | 59 | /** ptr to last segment */ |
| va009039 | 0:65f1469d6bfb | 60 | pSegment_t sl_lastseg; |
| va009039 | 0:65f1469d6bfb | 61 | } Seglist_t, |
| va009039 | 0:65f1469d6bfb | 62 | *pSeglist_t; |
| va009039 | 0:65f1469d6bfb | 63 | |
| va009039 | 0:65f1469d6bfb | 64 | |
| va009039 | 0:65f1469d6bfb | 65 | /** |
| va009039 | 0:65f1469d6bfb | 66 | * Puts the new object at the end of the list. |
| va009039 | 0:65f1469d6bfb | 67 | * This is intended for the List type where |
| va009039 | 0:65f1469d6bfb | 68 | * the List index matches the order of the Seglist index. |
| va009039 | 0:65f1469d6bfb | 69 | * Makes room if necessary by adding new segments. |
| va009039 | 0:65f1469d6bfb | 70 | * |
| va009039 | 0:65f1469d6bfb | 71 | * @param pseglist Ptr to seglist |
| va009039 | 0:65f1469d6bfb | 72 | * @param pobj Pointer to object to append |
| va009039 | 0:65f1469d6bfb | 73 | * @return Return status |
| va009039 | 0:65f1469d6bfb | 74 | */ |
| va009039 | 0:65f1469d6bfb | 75 | PmReturn_t seglist_appendItem(pSeglist_t pseglist, pPmObj_t pobj); |
| va009039 | 0:65f1469d6bfb | 76 | |
| va009039 | 0:65f1469d6bfb | 77 | /** |
| va009039 | 0:65f1469d6bfb | 78 | * Clears the the seglist by unlinking the root segment. |
| va009039 | 0:65f1469d6bfb | 79 | * |
| va009039 | 0:65f1469d6bfb | 80 | * @param pseglist Ptr to seglist to empty |
| va009039 | 0:65f1469d6bfb | 81 | */ |
| va009039 | 0:65f1469d6bfb | 82 | PmReturn_t seglist_clear(pSeglist_t pseglist); |
| va009039 | 0:65f1469d6bfb | 83 | |
| va009039 | 0:65f1469d6bfb | 84 | /** |
| va009039 | 0:65f1469d6bfb | 85 | * Finds the first obj equal to pobj in the seglist. |
| va009039 | 0:65f1469d6bfb | 86 | * Starts searching the list at the given segnum and indx. |
| va009039 | 0:65f1469d6bfb | 87 | * |
| va009039 | 0:65f1469d6bfb | 88 | * @param pseglist The seglist to search |
| va009039 | 0:65f1469d6bfb | 89 | * @param pobj The object to match |
| va009039 | 0:65f1469d6bfb | 90 | * @param r_index Return arg; the index of where to start the search. |
| va009039 | 0:65f1469d6bfb | 91 | * If a match is found, return the index by reference. |
| va009039 | 0:65f1469d6bfb | 92 | * If no match is found, this value is undefined. |
| va009039 | 0:65f1469d6bfb | 93 | * @return Return status; PM_RET_OK means a matching object |
| va009039 | 0:65f1469d6bfb | 94 | * was found. PM_RET_ERR otherwise. |
| va009039 | 0:65f1469d6bfb | 95 | */ |
| va009039 | 0:65f1469d6bfb | 96 | PmReturn_t seglist_findEqual(pSeglist_t pseglist, |
| va009039 | 0:65f1469d6bfb | 97 | pPmObj_t pobj, int16_t *r_index); |
| va009039 | 0:65f1469d6bfb | 98 | |
| va009039 | 0:65f1469d6bfb | 99 | /** |
| va009039 | 0:65f1469d6bfb | 100 | * Gets the item in the seglist at the given coordinates. |
| va009039 | 0:65f1469d6bfb | 101 | * The segment number and the index within the segment |
| va009039 | 0:65f1469d6bfb | 102 | * are the coordinates of the object to get. |
| va009039 | 0:65f1469d6bfb | 103 | * |
| va009039 | 0:65f1469d6bfb | 104 | * @param pseglist Ptr to seglist to scan |
| va009039 | 0:65f1469d6bfb | 105 | * @param index Index of item to get |
| va009039 | 0:65f1469d6bfb | 106 | * @param r_pobj Return arg; Ptr to object at the index |
| va009039 | 0:65f1469d6bfb | 107 | * @return Return status; PM_RET_OK if object found. |
| va009039 | 0:65f1469d6bfb | 108 | * PM_RET_ERR otherwise. |
| va009039 | 0:65f1469d6bfb | 109 | */ |
| va009039 | 0:65f1469d6bfb | 110 | PmReturn_t seglist_getItem(pSeglist_t pseglist, |
| va009039 | 0:65f1469d6bfb | 111 | int16_t index, pPmObj_t *r_pobj); |
| va009039 | 0:65f1469d6bfb | 112 | |
| va009039 | 0:65f1469d6bfb | 113 | /** |
| va009039 | 0:65f1469d6bfb | 114 | * Allocates a new empty seglist |
| va009039 | 0:65f1469d6bfb | 115 | * |
| va009039 | 0:65f1469d6bfb | 116 | * @param r_pseglist return; Address of ptr to new seglist |
| va009039 | 0:65f1469d6bfb | 117 | * @return Return status |
| va009039 | 0:65f1469d6bfb | 118 | */ |
| va009039 | 0:65f1469d6bfb | 119 | PmReturn_t seglist_new(pSeglist_t *r_pseglist); |
| va009039 | 0:65f1469d6bfb | 120 | |
| va009039 | 0:65f1469d6bfb | 121 | |
| va009039 | 0:65f1469d6bfb | 122 | /** |
| va009039 | 0:65f1469d6bfb | 123 | * Puts the item in the next available slot in the first available segment. |
| va009039 | 0:65f1469d6bfb | 124 | * This is intended for the Dict type where |
| va009039 | 0:65f1469d6bfb | 125 | * the Seglist index is insignificant. |
| va009039 | 0:65f1469d6bfb | 126 | * Pushing an object assures it will be found early |
| va009039 | 0:65f1469d6bfb | 127 | * during a call to seglist_findEqual(). |
| va009039 | 0:65f1469d6bfb | 128 | * |
| va009039 | 0:65f1469d6bfb | 129 | * @param pseglist Ptr to seglist in which object is placed. |
| va009039 | 0:65f1469d6bfb | 130 | * @param pobj Ptr to object which is inserted. |
| va009039 | 0:65f1469d6bfb | 131 | * @param index Index into seglist before which item is inserted |
| va009039 | 0:65f1469d6bfb | 132 | * @return Return status; PM_RET_OK if the item was inserted. |
| va009039 | 0:65f1469d6bfb | 133 | * Any error condition comes from heap_getChunk. |
| va009039 | 0:65f1469d6bfb | 134 | */ |
| va009039 | 0:65f1469d6bfb | 135 | PmReturn_t seglist_insertItem(pSeglist_t pseglist, |
| va009039 | 0:65f1469d6bfb | 136 | pPmObj_t pobj, int16_t index); |
| va009039 | 0:65f1469d6bfb | 137 | |
| va009039 | 0:65f1469d6bfb | 138 | /** |
| va009039 | 0:65f1469d6bfb | 139 | * Puts the item in the designated slot and segment. |
| va009039 | 0:65f1469d6bfb | 140 | * This is intended to be used after seglist_findEqual() |
| va009039 | 0:65f1469d6bfb | 141 | * returns the proper indeces. |
| va009039 | 0:65f1469d6bfb | 142 | * |
| va009039 | 0:65f1469d6bfb | 143 | * @param pseglist Ptr to seglist in which object is placed. |
| va009039 | 0:65f1469d6bfb | 144 | * @param pobj Ptr to object which is set. |
| va009039 | 0:65f1469d6bfb | 145 | * @param index Index into seglist of where to put object. |
| va009039 | 0:65f1469d6bfb | 146 | * @return Return status; PM_RET_OK if object is set. |
| va009039 | 0:65f1469d6bfb | 147 | * PM_RET_ERR otherwise. |
| va009039 | 0:65f1469d6bfb | 148 | */ |
| va009039 | 0:65f1469d6bfb | 149 | PmReturn_t seglist_setItem(pSeglist_t pseglist, pPmObj_t pobj, int16_t index); |
| va009039 | 0:65f1469d6bfb | 150 | |
| va009039 | 0:65f1469d6bfb | 151 | /** |
| va009039 | 0:65f1469d6bfb | 152 | * Removes the item at the given index. |
| va009039 | 0:65f1469d6bfb | 153 | * |
| va009039 | 0:65f1469d6bfb | 154 | * @param pseglist Ptr to seglist in which object is removed. |
| va009039 | 0:65f1469d6bfb | 155 | * @param index Index into seglist of where to put object. |
| va009039 | 0:65f1469d6bfb | 156 | * @return Return status |
| va009039 | 0:65f1469d6bfb | 157 | */ |
| va009039 | 0:65f1469d6bfb | 158 | PmReturn_t seglist_removeItem(pSeglist_t pseglist, uint16_t index); |
| va009039 | 0:65f1469d6bfb | 159 | |
| va009039 | 0:65f1469d6bfb | 160 | #endif /* __SEGLIST_H__ */ |
