⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
字号:
/**************************************************************************\MODULE: vectorSUMMARY:Macros are defined providing template-like classes for dynamic-sizedarrays.The macro NTL_vector_decl(T,vec_T) declares a class vec_T, whoseimplementation can be instantiated with NTL_vector_impl(T,vec_T).  It ispresumed that the underlying type have a public default constructor, copyconstructor, assignment operator, and a destructor (this isnormally the case for most types).Note that the type T must be a type name (you'll need to makea typedef for the type if this is not the case).If the type T supports I/O operator << and >>, then vec_T can bemade to support these operators as well using NTL_io_vector_decl(T,vec_T) andNTL_io_vector_impl(T,vec_T).The same goes for equality operators == and != using NTL_eq_vector_decl(T,vec_T) and NTL_eq_vector_impl(T,vec_T).The declaration   vec_T v;creates a zero-length vector.  To grow this vector to length n,execute   v.SetLength(n)This causes space to be allocated for (at least) n elements, and alsocauses the delault constructor for T to be called to initialize theseelements.The current length of a vector is available as v.length().The i-th vector element (counting from 0) is accessed as v[i].  If themacro NTL_RANGE_CHECK is defined, code is emitted to test if 0 <= i <v.length().  This check is not performed by default.For old-time FORTRAN programmers, the i-th vector element (countingfrom 1) is accessed as v(i).Let n = v.length().  Calling v.SetLength(m) with m <= n sets thecurrent length of v to m (but does not call any destructors or freeany space).  Calling v.SetLength(m) with m > n will allocate space andinitialize as necessary, but will leave the values of the alreadyallocated elements unchanged (although their addresses may change).Initializations are performed using T's default constructor.v.MaxLength() is the largest value of n for which v.SetLength(n) was invoked,and is equal to the number of entries that have been initialized.v.SetMaxLength(n) will allocate space for andinitialize up to n elements, without changing v.length().When v's destructor is called, all constructed elements will bedestructed, and all space will be relinquished.Space is managed using malloc, realloc, and free.  When a vector isgrown, a bit more space may be allocated than was requested forefficiency reasons.Note that when a vector is grown, the space is reallocated usingrealloc, and thus the addresses of vector elements may change,possibly creating dangling references to vector elements.  One has tobe especially careful of this when using vectors passed as referenceparameters that may alias one another.Because realloc is used to grow a vector, the objects storedin a vector should be "relocatable"---that is, they shouldn't carewhat their actual address is, which may change over time.Most reasonable objects satisfy this constraint.\**************************************************************************/class vec_T {  public:     vec_T();  // initially length 0   vec_T(const vec_T& a);    // copy constructor;  currently, this is implemented by   // initializing elements using T's defaults constructor and    // copying elements from a using T's assigment operator.   vec_T& operator=(const vec_T& a);     // assignment...performs an element-wise assignment   // using T's assignment operator.   ~vec_T();       void SetLength(long n);     // set current length to n, growing vector if necessary   long length() const;   // current length   // SIZE INVARIANT: length()*sizeof(T) < 2^(NTL_BITS_PER_LONG-4)     T& operator[](long i);   const T& operator[](long i) const;   // indexing operation, starting from 0.   // The first version is applied to non-const vec_T,   // and returns a non-const reference to a T, while the second version   // is applied to a const vec_T and returns a const reference to a T.     T& operator()(long i);   const T& operator()(long i) const;   // indexing operation, starting from 1   // The first version is applied to non-const vec_T,   // and returns a non-const reference to a T, while the second version   // is applied to a const vec_T and returns a const reference to a T.     T* elts();   const T* elts() const;   // returns address of first vector element (or 0 if no space has   // been allocated for this vector).  If a vector potentially has   // length 0, it is safer to write v.elts() instead of &v[0].   // The first version is applied to non-const vec_T,   // and returns a non-const pointer to a T, while the second version   // is applied to a const vec_T and returns a const reference to a T.   // the remaining member functions are a bit esoteric (skip on first   // reading):   vec_T(INIT_SIZE_TYPE, long n);   // vec_T(INIT_SIZE, n) initializes with an intial length of n.   void kill();    // release space and set to length 0   void SetMaxLength(long n);    // allocates space and initializes up to n elements. Does not change   // current length   void FixLength(long n);   // sets length to n and prohibits all future length changes.   // FixLength may only be invoked immediately after the default   // construction or kill.   // The kill operation is also subsequently prohibited, and swap is   // allowed on fixed length vectors of the same length.   // FixLength is provided mainly to implement mat_T, to enforce   // the restriction that all rows have the same length.   long fixed() const;   // test if length has been fixed by FixLength().   long MaxLength() const;   // maximum length, i.e., number of allocated and initialized elements   T& RawGet(long i);   const T& RawGet(long i) const;   // indexing with no range checking   long position(const T& a) const;   // returns position of a in the vector, or -1 if it is not there         };   /**************************************************************************\                       Some utility routines\**************************************************************************/   void swap(vec_T& x, vec_T& y);// swaps x & y by swapping pointersvoid append(vec_T& v, const T& a); // appends a to the end of vvoid append(vec_T& v, const vec_T& w);// appends w to the end of v/**************************************************************************\                             Input/OutputThe I/O operators can be declared with NTL_io_vector_decl(T,vec_T), andimplemented using NTL_io_vector_impl(T,vec_T).  Elements are read and writtenusing the underlying I/O operators << and >> for T.The I/O format for a vector v with n elements is:   [v[0] v[1] ... v[n-1]]\**************************************************************************/istream& operator>>(istream&, vec_T&);  ostream& operator<<(ostream&, const vec_T&);  /**************************************************************************\                              Equality TestingThe equality testing operators == and != can be declaredwith NTL_eq_vector_decl(T,vec_T) and implemented with NTL_eq_vector_impl(T,vec_T).The tests are performed using the underlying operator == for T.\**************************************************************************/long operator==(const vec_T& a, const vec_T& b);  long operator!=(const vec_T& a, const vec_T& b);/**************************************************************************\                  Customized Constructors and Destructors Esoteric: skip on first readingWhen new elements in a vector need to be constructed, the routine   void BlockConstruct(T* p, long n);is called, whose default implementation invokes the defaultconstructor for T n times.  Likewise, when a vector is destroyed, theroutine   void BlockDestroy(T* p, long n);is called, whose default implementation invokes the default destructorfor T n times.Both of these default implementations can be overridden as follows.Instead of implementing vec_T with NTL_vector_impl(T,vec_T), implement itwith NTL_vector_impl_plain(T,vec_T), and then write your own BlockConstruct andBlockDestroy.For an example of this, see vec_ZZ_p.c.\**************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -