//唯一化 template <typename T> int List<T>::deduplicate() { if (_size < 2) return 0;//平凡列表无重复 int oldSize = _size; Posi(T) p = first(); Rank r=1;//初始,从首节点开始 while (trailer != (p = p->succ)) { Posi(T) q = find(p->data, r, p);//在p的r个(真)前驱中,查找与之雷同者 q ? remove(q) : r++;//若确实存在,删除;否则秩递增 }//assert: 循环过程中的任意时刻,p的所有前驱互不相同 return oldSize - _Size; //变化量 }
//对列表中起始于位置p的连续n个元素做选择排序,valid(p) && rank(p) + n<= size template <typename T> void List<T>::selectionSort(Posi(T) p, int n) Posi(T) head = p->pred; Posi(T) tail = p;//待排序区间(head, tail) for (int i = 0, i < n; i++) tail = tail->succ; //head tail可能是头尾哨兵 while (1 < n) //反复从非平凡的待排序区间内找出最大者,并移动至有序区间前段 { insertBefore (tail, remove(selectMax(head->succ,n))); tail = tail->pred;//待排序区间缩进一个 n--;//少一个要排序的元素啦 }
//对列表中起始于位置p连续n个元素做插入排序,valid(p) && rank(p) + n <=size template<typename T> void List<T>::insertionSort(Posi(T) p, int n) { for (int r = 0; r < n; r++) { insertAfter( search(p->data, r, p),p->data);//查找,插入 p = p->succ; remove(p->pred);//转向下一节点 }//n次迭代,每次O(r+1) }//辅助空间O(1)
bool Map::Generate_Vertices() { HRESULT hresult = 0; int start_x = 0; int start_z = 0; int end_x = length; int end_z = width; float u_increment = 1.0f / (float)cell_length; float v_increment = 1.0f / (float)cell_width; IDirect3DVertexBuffer9* temp_buffer = 0; mesh->GetVertexBuffer(&temp_buffer); Terrain_Vertex* v; temp_buffer->Lock(0, 0, (void**)&v, 0); int i = 0; for (int z = start_z; z <= end_z; z = z + cell_spacing) //### { int j = 0; for (int x = start_x; x <= end_x; x = x + cell_spacing) { int element = (i * vertex_length) + j; D3DXVECTOR3 Posi((float)x, height_float_vector[element], (float)z); D3DXVECTOR3 Norm(-N[element].x, -N[element].y, N[element].z); v[element] = Terrain_Vertex( Posi, Norm, (float)j * u_increment * 5, (float)i * v_increment * 5, (float)j * u_increment * 5, (float)i * v_increment * 5, (float)j * u_increment * 5, (float)i * v_increment * 5, (float)j * u_increment, (float)i * v_increment, (float)j * u_increment, (float)i * v_increment); j++; } i++; } temp_buffer->Unlock(); return true; }