list.py

Go to the documentation of this file.
00001 # This file is Copyright 2009, 2010 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 # 
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING in this directory.
00012 
00013 ## @file
00014 #  @copybrief list
00015 
00016 ## @package list
00017 #  @brief Provides PyMite's list module.
00018 #
00019 # Notes:
00020 # - index(l, o) does not offer start and stop arguments.
00021 
00022 
00023 def append(l, o):
00024     """__NATIVE__
00025     pPmObj_t pl;
00026     PmReturn_t retval = PM_RET_OK;
00027 
00028     /* Raise TypeError if it's not a list or wrong number of args, */
00029     pl = NATIVE_GET_LOCAL(0);
00030     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00031     {
00032         PM_RAISE(retval, PM_RET_EX_TYPE);
00033         return retval;
00034     }
00035 
00036     /* Append the object to the list */
00037     retval = list_append(pl, NATIVE_GET_LOCAL(1));
00038 
00039     NATIVE_SET_TOS(PM_NONE);
00040 
00041     return retval;
00042     """
00043     pass
00044 
00045 
00046 def count(l, v):
00047     c = 0
00048     for o in l:
00049         if o == v:
00050             c = c + 1
00051     return c
00052 
00053 
00054 def extend(l, s):
00055     for i in s:
00056         append(l, i)
00057 
00058 
00059 def index(l, o):
00060     """__NATIVE__
00061     pPmObj_t pl;
00062     pPmObj_t po;
00063     pPmObj_t pi;
00064     PmReturn_t retval = PM_RET_OK;
00065     uint16_t i;
00066 
00067     /* Raise TypeError if it's not a list or wrong number of args, */
00068     pl = NATIVE_GET_LOCAL(0);
00069     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00070     {
00071         PM_RAISE(retval, PM_RET_EX_TYPE);
00072         return retval;
00073     }
00074 
00075     /* Get the index of the object in the list */
00076     po = NATIVE_GET_LOCAL(1);
00077     retval = list_index(pl, po, &i);
00078 
00079     if (retval == PM_RET_EX_VAL)
00080     {
00081         PM_RAISE(retval, PM_RET_EX_VAL);
00082         return retval;
00083     }
00084 
00085     int_new((int32_t)i, &pi);
00086     NATIVE_SET_TOS(pi);
00087 
00088     return retval;
00089     """
00090     pass
00091 
00092 
00093 def insert(l, i, o):
00094     """__NATIVE__
00095     pPmObj_t pl;
00096     pPmObj_t pi;
00097     pPmObj_t po;
00098     PmReturn_t retval = PM_RET_OK;
00099     uint16_t i;
00100 
00101     /*
00102      * Raise TypeError if wrong number of args, first arg is not a list, or
00103      * second arg is not an int
00104      */
00105     pl = NATIVE_GET_LOCAL(0);
00106     pi = NATIVE_GET_LOCAL(1);
00107     po = NATIVE_GET_LOCAL(2);
00108     if ((NATIVE_GET_NUM_ARGS() != 3)
00109         || (OBJ_GET_TYPE(pl) != OBJ_TYPE_LST)
00110         || (OBJ_GET_TYPE(pi) != OBJ_TYPE_INT) )
00111     {
00112         PM_RAISE(retval, PM_RET_EX_TYPE);
00113         return retval;
00114     }
00115 
00116     /* Insert the object before the given index */
00117     i = (uint16_t)((pPmInt_t)pi)->val;
00118     retval = list_insert(pl, i, po);
00119 
00120     if (retval != PM_RET_OK)
00121     {
00122         PM_RAISE(retval, PM_RET_EX_SYS);
00123     }
00124 
00125     NATIVE_SET_TOS(PM_NONE);
00126 
00127     return retval;
00128     """
00129     pass
00130 
00131 
00132 def pop(l, i):
00133     """__NATIVE__
00134     pPmObj_t pl;
00135     pPmObj_t pi;
00136     pPmObj_t po;
00137     PmReturn_t retval = PM_RET_OK;
00138     int16_t i;
00139 
00140     /*
00141      * Raise TypeError if first arg is not a list o second arg is not an int
00142      * or there are the wrong number of arguments
00143      */
00144     pl = NATIVE_GET_LOCAL(0);
00145     if (OBJ_GET_TYPE(pl) != OBJ_TYPE_LST)
00146     {
00147         PM_RAISE(retval, PM_RET_EX_TYPE);
00148         return retval;
00149     }
00150 
00151     pi = NATIVE_GET_LOCAL(1);
00152     if (NATIVE_GET_NUM_ARGS() == 2)
00153     {
00154         if (OBJ_GET_TYPE(pi) != OBJ_TYPE_INT)
00155         {
00156             PM_RAISE(retval, PM_RET_EX_TYPE);
00157             return retval;
00158         }
00159         i = (uint16_t)((pPmInt_t)pi)->val;
00160     }
00161     else
00162     {
00163         i = -1;
00164     }
00165     if ((NATIVE_GET_NUM_ARGS() < 1) || (NATIVE_GET_NUM_ARGS() > 2))
00166     {
00167         PM_RAISE(retval, PM_RET_EX_TYPE);
00168         return retval;
00169     }
00170 
00171     /* Get the object at the given index */
00172     retval = list_getItem(pl, i, &po);
00173     PM_RETURN_IF_ERROR(retval);
00174 
00175     /* Return the object to the caller */
00176     NATIVE_SET_TOS(po);
00177 
00178     /* Remove the object from the given index */
00179     retval = list_delItem(pl, i);
00180     PM_RETURN_IF_ERROR(retval);
00181 
00182     return retval;
00183     """
00184     pass
00185 
00186 
00187 def remove(l, v):
00188     """__NATIVE__
00189     pPmObj_t pl;
00190     pPmObj_t pv;
00191     PmReturn_t retval = PM_RET_OK;
00192 
00193     /* Raise TypeError if it's not a list or wrong number of args, */
00194     pl = NATIVE_GET_LOCAL(0);
00195     if ((OBJ_GET_TYPE(pl) != OBJ_TYPE_LST) || (NATIVE_GET_NUM_ARGS() != 2))
00196     {
00197         PM_RAISE(retval, PM_RET_EX_TYPE);
00198         return retval;
00199     }
00200 
00201     /* Remove the value from the list */
00202     pv = NATIVE_GET_LOCAL(1);
00203     retval = list_remove(pl, pv);
00204     if (retval != PM_RET_OK)
00205     {
00206         PM_RAISE(retval, retval);
00207     }
00208 
00209     NATIVE_SET_TOS(PM_NONE);
00210 
00211     return retval;
00212     """
00213     pass
00214 
00215 
00216 
00217 
00218 # TODO:
00219 # L.reverse() -- reverse *IN PLACE*
00220 # L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
00221 #    cmp(x, y) -> -1, 0, 1
00222 
00223 # :mode=c:

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9