/* method: "get_obj(i)->Object\n Returns the sub-object specified by the passed in integer. Only supported by groups." */ static PyObject* Shape_get_obj(Object& self, int index){ if (index < 0 || self.GetObjectCount() <= index){ throw IndexError("Invalid object index"); } return get_holder(self.GetObject(index)); }
static void set( T &x, size_t i, V const &v ) { if( i < x.size() ) { typename T::iterator iter = x.begin(); std::advance( iter, i ); *iter = v; } else IndexError(); }
static void del( T &x, size_t i ) { if( i < x.size() ) { typename T::iterator iter = x.begin(); std::advance( iter, i ); x.erase( iter ); } else IndexError(); }
static int RemoveRange(ElementType *SC,size_t start, size_t end) { size_t i; if (SC == NULL) return NullPtrError("RemoveRange"); if (SC->count == 0) return 0; if (end > SC->count) end = SC->count; if (start == end) return 0; if (start >= SC->count) return IndexError(SC,start,"RemoveRange"); if (SC->DestructorFn) { for (i=start; i<end; i++) { SC->DestructorFn(SC->contents[i]); SC->Allocator->free(SC->contents[i]); } } else { for (i=start; i<end; i++) { SC->Allocator->free(SC->contents[i]); } } if (end < SC->count) memmove(SC->contents+start, SC->contents+end, (SC->count-end)*sizeof(char *)); SC->count -= end - start; return 1; }
static V &get( T &x, size_t i ) { if( i >= x.size() ) { IndexError(); } typename T::iterator iter = x.begin(); std::advance( iter, i ); return *iter; }
Path::Shared PathsCollection::nextPath() { if (mCurrentPath > mPaths.size()) { throw IndexError("PathsCollection::nextPath " "no paths are available"); } mCurrentPath++; return make_shared<Path>( mSourceNode, mDestinationNode, mPaths.at(mCurrentPath - 1)); }
rune String::operator[](int idx) const { if (!s_len) { throw IndexError(); } if (idx < 0) { idx += s_len; if (idx < 0) { throw IndexError(); } } if ((size_t)idx > s_len) { throw IndexError(); } if ((size_t)idx == s_len) { return 0; } rune tmp[s_len + 1]; utf8_decode(s_data, tmp, s_len + 1); return tmp[idx]; }
/** * @brief Adjust the axis index to the correct axis index. The axis index * can be negative, which means, the index is counted from the back. * The returned axis index lies in the interval [0, nd). On error, an * IndexError is thrown. */ inline intptr_t adjust_axis_index(intptr_t const nd, intptr_t axis) { if(axis < 0) { axis += nd; } if(axis < 0) { std::stringstream ss; ss << "The axis value \""<< axis <<"\" specifies an axis < 0!"; throw IndexError(ss.str()); } else if(axis >= nd) { std::stringstream ss; ss << "The axis value \""<< axis <<"\" must be smaller than the " << "dimensionality of the histogram, i.e. smaller than " << nd <<"!"; throw IndexError(ss.str()); } return axis; }
static int InsertIn(ElementType *source, size_t idx, ElementType *newData) { size_t newCount,i,j,siz; CHAR_TYPE **p,**oldcontents; if (source == NULL || newData == NULL) { return NullPtrError("InsertIn"); } if (source->Flags & CONTAINER_READONLY) return ReadOnlyError(source,"InsertIn"); if (idx > source->count) { return IndexError(source,idx,"InsertIn"); } newCount = source->count + newData->count; if (newData->count == 0) return 1; if (newCount == 0) return 1; if (newCount >= (source->capacity-1)) { int r = ResizeTo(source,1+newCount+newCount/4); if (r <= 0) return r; } p = source->contents; siz = source->capacity*sizeof(CHAR_TYPE *); oldcontents = source->Allocator->malloc(siz); if (oldcontents == NULL) { return NoMemoryError(source,"InsertIn"); } memset(oldcontents,0,siz); memcpy(oldcontents,p,sizeof(char *)*source->count); if (idx < source->count) { memmove(p+(idx+newData->count), p+idx, (source->count-idx)*sizeof(char *)); } for (i=idx,j=0; i<idx+newData->count;i++,j++) { source->contents[i] = DuplicateString(newData,newData->contents[j],"InsertIn"); if (source->contents[i] == NULL) { source->Allocator->free(source->contents); source->contents = oldcontents; return NoMemoryError(source,"InsertIn"); } } source->Allocator->free(oldcontents); source->timestamp++; source->count = newCount; return 1; }
double ReactorNet::sensitivity(size_t k, size_t p) { if (!m_init) { initialize(); } if (p >= m_sens_params.size()) { throw IndexError("ReactorNet::sensitivity", "m_sens_params", p, m_sens_params.size()-1); } double denom = m_integ->solution(k); if (denom == 0.0) { denom = SmallNumber; } return m_integ->sensitivity(k, p) / denom; }
static CHAR_TYPE *GetElement(const ElementType *SC,size_t idx) { if (SC == NULL) { NullPtrError("GetElement"); return NULL; } if (SC->Flags & CONTAINER_READONLY) { ReadOnlyError(SC,"GetElement"); return NULL; } if (idx >=SC->count) { IndexError(SC,idx,"GetElement"); return NULL; } return SC->contents[idx]; }
DataArchive *DataManager::findArchive(ObjectID const &id, std::string const &path_str, DataArchive const *d) const { ArchiveMap::const_iterator idx = _archive_map.find(id); DataArchive *archive = 0; if (idx != _archive_map.end()) { assert(idx->second < _archives.size()); archive = _archives[idx->second]; } if (archive == 0 || archive == d) { std::string msg = path_str; if (msg=="") { msg = "human-readable path unavailable"; } msg = "path not found (" + msg + ")" + id.str() + "\n"; CSPLOG(ERROR, ARCHIVE) << "DataManager::findArchive() : " << msg; throw IndexError(msg.c_str()); } return archive; }
static int InsertAt(ElementType *SC,size_t idx,const CHAR_TYPE *newval) { CHAR_TYPE *p; if (SC == NULL) { return NullPtrError("InsertAt"); } if (newval == NULL) { return BadArgError(SC,"InsertAt"); } if (SC->Flags & CONTAINER_READONLY) { return ReadOnlyError(SC,"InsertAt"); } if (idx >= SC->count) { return IndexError(SC,idx,"InsertAt"); } if ((SC->count+1) >= SC->capacity) { int r = Resize(SC); if (r <= 0) return r; } p = DuplicateString(SC,newval,"InsertAt"); if (p == NULL) { return NoMemoryError(SC,"InsertAt"); } if (idx == 0) { if (SC->count > 0) memmove(SC->contents+1,SC->contents,SC->count*sizeof(CHAR_TYPE *)); SC->contents[0] = p; } else if (idx == SC->count) { SC->contents[idx] = p; } else if (idx < SC->count) { memmove(SC->contents+idx+1,SC->contents+idx,(SC->count-idx+1)*sizeof(CHAR_TYPE *)); SC->contents[idx] = p; } SC->timestamp++; ++SC->count; return 1; }
/* [email protected] (gerome) proposed calling DuplicateString. Good suggestion */ static int ReplaceAt(ElementType *SC,size_t idx,CHAR_TYPE *newval) { CHAR_TYPE *r; if (SC == NULL) { return NullPtrError("ReplaceAt"); } if (SC->Flags & CONTAINER_READONLY) { return ReadOnlyError(SC,"ReplaceAt"); } if (idx >= SC->count) { return IndexError(SC,idx,"ReplaceAt"); } SC->Allocator->free(SC->contents[idx]); r = DuplicateString(SC,newval,(char *)"ReplaceAt"); if (r == NULL) { return NoMemoryError(SC,"ReplaceAt"); } SC->contents[idx] = r; SC->timestamp++; return 1; }
static int RemoveAt(ElementType *SC,size_t idx) { if (SC == NULL) { return NullPtrError("RemoveAt"); } if (idx >= SC->count ) return IndexError(SC,idx,"RemoveAt"); if (SC->Flags & CONTAINER_READONLY) return ReadOnlyError(SC,"RemoveAt"); /* Test for remove of an empty collection */ if (SC->count == 0) return 0; if (SC->DestructorFn) SC->DestructorFn(SC->contents[idx]); SC->Allocator->free(SC->contents[idx]); if (idx < (SC->count-1)) { memmove(SC->contents+idx,SC->contents+idx+1,(SC->count-idx)*sizeof(char *)); } SC->contents[SC->count-1]=NULL; SC->timestamp++; --SC->count; return 1; }
template< class T > T Correlation< T >::operator () (const vector < T > & a, const vector < T > & b) const { static char F_Name[] = "template< class T > T Correlation< T >::operator () (const vector < T > & a, const vector < T > & b) const"; if(a.size() != b.size()) { TKVPairList kvlist; kvlist.push_back(TKVPair("Size", a.size())); kvlist.push_back(TKVPair("Size", b.size())); throw IndexError(F_Name, kvlist); } T av_a, av_b, d_a, d_b, sp(0), sa(0), sb(0); av_a = average(a); av_b = average(b); for(unsigned i=0; i<a.size(); i++) { d_a = a[i] - av_a; d_b = b[i] - av_b; sp += d_a * d_b; sa += SQR(d_a); sb += SQR(d_b); } return sp / sqrt(sa * sb); }
void Phase::checkElementIndex(size_t m) const { if (m >= m_mm) { throw IndexError("checkElementIndex", "elements", m, m_mm-1); } }
void Transport::checkSpeciesIndex(size_t k) const { if (k >= m_nsp) { throw IndexError("checkSpeciesIndex", "species", k, m_nsp-1); } }
void Kinetics::checkPhaseIndex(size_t m) const { if (m >= nPhases()) { throw IndexError("checkPhaseIndex", "phase", m, nPhases()-1); } }
void Kinetics::checkReactionIndex(size_t i) const { if (i >= m_ii) { throw IndexError("checkReactionIndex", "reactions", i, m_ii-1); } }
inline permutation operator*(const permutation & b) const{ if (N != b.N) IndexError("Permutations have different dimensions"); permutation res(N); for (int i=0; i<N; i++) res[i] = b[p[i]]; return res; }
void py_set(int i, int pi){ if (i<0) i+= size(); if (i<0 || i>=size()) IndexError("permutation: index out of range"); p[i]=pi; }
int py_get(int i) const{ if (i<0) i+= size(); if (i<0 || i>=size()) IndexError("permutation: index out of range"); return p[i]; }
void Phase::checkSpeciesIndex(size_t k) const { if (k >= m_kk) { throw IndexError("checkSpeciesIndex", "species", k, m_kk-1); } }
/* ** BTreeItems_seek ** ** Find the ith position in the BTreeItems. ** ** Arguments: self The BTree ** i the index to seek to, in 0 .. len(self)-1, or in ** -len(self) .. -1, as for indexing a Python sequence. ** ** ** Returns 0 if successful, -1 on failure to seek (like out-of-bounds). ** Upon successful return, index i is at offset self->currentoffset in bucket ** self->currentbucket. */ static int BTreeItems_seek(BTreeItems *self, Py_ssize_t i) { int delta, pseudoindex, currentoffset; Bucket *b, *currentbucket; int error; pseudoindex = self->pseudoindex; currentoffset = self->currentoffset; currentbucket = self->currentbucket; if (currentbucket == NULL) goto no_match; delta = i - pseudoindex; while (delta > 0) { /* move right */ int max; /* Want to move right delta positions; the most we can move right in * this bucket is currentbucket->len - currentoffset - 1 positions. */ PER_USE_OR_RETURN(currentbucket, -1); max = currentbucket->len - currentoffset - 1; b = currentbucket->next; PER_UNUSE(currentbucket); if (delta <= max) { currentoffset += delta; pseudoindex += delta; if (currentbucket == self->lastbucket && currentoffset > self->last) goto no_match; break; } /* Move to start of next bucket. */ if (currentbucket == self->lastbucket || b == NULL) goto no_match; currentbucket = b; pseudoindex += max + 1; delta -= max + 1; currentoffset = 0; } while (delta < 0) { /* move left */ int status; /* Want to move left -delta positions; the most we can move left in * this bucket is currentoffset positions. */ if ((-delta) <= currentoffset) { currentoffset += delta; pseudoindex += delta; if (currentbucket == self->firstbucket && currentoffset < self->first) goto no_match; break; } /* Move to end of previous bucket. */ if (currentbucket == self->firstbucket) goto no_match; status = PreviousBucket(¤tbucket, self->firstbucket); if (status == 0) goto no_match; else if (status < 0) return -1; pseudoindex -= currentoffset + 1; delta += currentoffset + 1; PER_USE_OR_RETURN(currentbucket, -1); currentoffset = currentbucket->len - 1; PER_UNUSE(currentbucket); } assert(pseudoindex == i); /* Alas, the user may have mutated the bucket since the last time we * were called, and if they deleted stuff, we may be pointing into * trash memory now. */ PER_USE_OR_RETURN(currentbucket, -1); error = currentoffset < 0 || currentoffset >= currentbucket->len; PER_UNUSE(currentbucket); if (error) { PyErr_SetString(PyExc_RuntimeError, "the bucket being iterated changed size"); return -1; } Py_INCREF(currentbucket); Py_DECREF(self->currentbucket); self->currentbucket = currentbucket; self->currentoffset = currentoffset; self->pseudoindex = pseudoindex; return 0; no_match: IndexError(i); return -1; }
void Kinetics::checkReactionIndex(size_t i) const { if (i >= nReactions()) { throw IndexError("checkReactionIndex", "reactions", i, nReactions()-1); } }