BViewState::BViewState(BMallocIO* stream, bool endianSwap) { _Init(); stream->Read(&fViewMode, sizeof(uint32)); stream->Read(&fLastIconMode, sizeof(uint32)); stream->Read(&fListOrigin, sizeof(BPoint)); stream->Read(&fIconOrigin, sizeof(BPoint)); stream->Read(&fPrimarySortAttr, sizeof(uint32)); stream->Read(&fPrimarySortType, sizeof(uint32)); stream->Read(&fSecondarySortAttr, sizeof(uint32)); stream->Read(&fSecondarySortType, sizeof(uint32)); stream->Read(&fReverseSort, sizeof(bool)); stream->Read(&fIconSize, sizeof(uint32)); stream->Read(&fLastIconSize, sizeof(uint32)); if (endianSwap) { PRINT(("endian swapping view state\n")); fViewMode = B_SWAP_INT32(fViewMode); fLastIconMode = B_SWAP_INT32(fLastIconMode); fIconSize = B_SWAP_INT32(fIconSize); fLastIconSize = B_SWAP_INT32(fLastIconSize); swap_data(B_POINT_TYPE, &fListOrigin, sizeof(fListOrigin), B_SWAP_ALWAYS); swap_data(B_POINT_TYPE, &fIconOrigin, sizeof(fIconOrigin), B_SWAP_ALWAYS); fPrimarySortAttr = B_SWAP_INT32(fPrimarySortAttr); fSecondarySortAttr = B_SWAP_INT32(fSecondarySortAttr); fPrimarySortType = B_SWAP_INT32(fPrimarySortType); fSecondarySortType = B_SWAP_INT32(fSecondarySortType); } _StorePreviousState(); _Sanitize(this, true); }
void list_char_sort(struct _list_char *list) { if (list->data_count <= 1) return; struct _list_char_data *data1, *data2; int i = 2; do { int t = i; while (t != 1) { int k = t / 2; data1 = &list->data[k - 1]; data2 = &list->data[t - 1]; if (strcmp(data1->string, data2->string) >= 0) break; swap_data(list, k - 1, t - 1); t = k; } i++; } while (i <= list->data_count); i = list->data_count - 1; do { swap_data(list, i, 0); int t = 1; while (t != 0) { int k = 2 * t; if (k > i) break; if (k < i) { data1 = &list->data[k]; data2 = &list->data[k - 1]; if (strcmp(data1->string, data2->string) > 0) k++; } data1 = &list->data[t - 1]; if (strcmp(data1->string, data2->string) >= 0) break; swap_data(list, k - 1, t - 1); t = k; } i--; } while (i >= 1); }
BViewState::BViewState(BMallocIO *stream, bool endianSwap) { stream->Read(&fViewMode, sizeof(uint32)); stream->Read(&fLastIconMode, sizeof(uint32)); stream->Read(&fListOrigin, sizeof(BPoint)); stream->Read(&fIconOrigin, sizeof(BPoint)); stream->Read(&fPrimarySortAttr, sizeof(uint32)); stream->Read(&fPrimarySortType, sizeof(uint32)); stream->Read(&fSecondarySortAttr, sizeof(uint32)); stream->Read(&fSecondarySortType, sizeof(uint32)); stream->Read(&fReverseSort, sizeof(bool)); if (endianSwap) { PRINT(("endian swapping view state\n")); fViewMode = B_SWAP_INT32(fViewMode); fLastIconMode = B_SWAP_INT32(fLastIconMode); swap_data(B_POINT_TYPE, &fListOrigin, sizeof(fListOrigin), B_SWAP_ALWAYS); swap_data(B_POINT_TYPE, &fIconOrigin, sizeof(fIconOrigin), B_SWAP_ALWAYS); fPrimarySortAttr = B_SWAP_INT32(fPrimarySortAttr); fSecondarySortAttr = B_SWAP_INT32(fSecondarySortAttr); fPrimarySortType = B_SWAP_INT32(fPrimarySortType); fSecondarySortType = B_SWAP_INT32(fSecondarySortType); } fStateNeedsSaving = false; }
status_t PackageItem::ParseData(uint8 *buffer, BFile *file, uint64 originalSize, bool *done) { status_t ret = B_OK; if (!memcmp(buffer, "FiMF", 5)) { parser_debug(" Found file data.\n"); uint64 compressed, original; fPackage->Read(&compressed, 8); swap_data(B_UINT64_TYPE, &compressed, sizeof(uint64), B_SWAP_BENDIAN_TO_HOST); fPackage->Read(&original, 8); swap_data(B_UINT64_TYPE, &original, sizeof(uint64), B_SWAP_BENDIAN_TO_HOST); parser_debug(" Still good... (%llu : %llu)\n", original, originalSize); if (original != originalSize) { parser_debug(" File size mismatch\n"); return B_ERROR; // File size mismatch } parser_debug(" Still good...\n"); if (fPackage->Read(buffer, 4) != 4) { parser_debug(" Read(buffer, 4) failed\n"); return B_ERROR; } parser_debug(" Still good...\n"); ret = inflate_file_to_file(fPackage, compressed, file, original); if (ret != B_OK) { parser_debug(" inflate_file_to_file failed\n"); return ret; } parser_debug(" File data inflation complete!\n"); } else if (!memcmp(buffer, padding, 7)) { *done = true; return ret; } else { parser_debug("_ParseData unknown tag\n"); ret = B_ERROR; } return ret; }
void TiffUintField::LoadByte(IFDEntry &entry, BPositionIO &io, swap_action swp) { // Make certain there is enough memory // before trying to do anything else fpByte = new uint8[entry.count]; if (!fpByte) { finitStatus = B_NO_MEMORY; return; } if (entry.count <= 4) { // If all of the byte values can fit into the // IFD entry value bytes memcpy(fpByte, entry.bytevals, entry.count); finitStatus = B_OK; } else { // entry.count > 4, use longval to find offset for byte data if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK) finitStatus = B_ERROR; else { ssize_t read; read = io.ReadAt(entry.longval, fpByte, entry.count); if (read != static_cast<ssize_t>(entry.count)) finitStatus = B_IO_ERROR; else finitStatus = B_OK; } } }
//1. build a small list and big list, combine. too complicate //2. like this, just swap value void partition(node_t* head, int value) { if (head == NULL) return; node_t* slow = head; node_t* fast = head; while (fast && slow) { while (slow != NULL) { if (slow->data < value) slow = slow->next; else break; } fast = slow; while (fast != NULL) { if (fast->data >= value) fast = fast->next; else break; } if (fast && slow) swap_data(slow, fast); } }
void change_queen(int* queens,int* conf,int num) { // num番目の列にあるクイーンを移動する // conf[i]:num番目の列にあるクイーンがi行目に移動したときの衝突数 int move = get_min(conf);//どの行に移動すると衝突数が小さくなるか判定 int temp,i; // move列目にあるクイーンを探す --> 列をtempに入れる temp=-1; for(i=0;i<nq;++i) { if(queens[i] == move) { temp = i; break; } } if (temp < 0) { // 移動先の行にクイーンが存在しない場合はリターン return; } swap_data(&queens[num],&queens[temp]);//num列目とtemp列目のクイーンの位置を交換 }
void bubble_sort(int a[],int num) { int i=0,j=0,tmp=0; for(j=0;j<num-1;j++) for(i=0;i<num-1-j;i++) if(a[i]>a[i+1]) swap_data(&a[i],&a[i+1]); }
void heap_sort(data_t A[], int n) { int active; build_heap(A, n); for (active=n-1; active>0; active--) { swap_data(A+0, A+active); sift_down(A, 0, active); } }
void sort_data(Student *st, int num, int order) { int i, j; void swap_data(Student *st1, Student *st2); for( i=0 ; i<num-1 ; i++ ) { for( j=i+1 ; j<num ; j++ ) { if( order == ID_ORDER ) { if( (st+i)->id > (st+j)->id ) swap_data(st+i, st+j); } else if( order == PT_ORDER ) { if( (st+i)->point < (st+j)->point ) swap_data(st+i, st+j); } } } }
// Swaps the byte order of source, no matter the byte order, and // copies the result to destination. void BBitmapStream::SwapHeader(const TranslatorBitmap* source, TranslatorBitmap* destination) { if (source == NULL || destination == NULL) return; memcpy(destination, source, sizeof(TranslatorBitmap)); swap_data(B_UINT32_TYPE, destination, sizeof(TranslatorBitmap), B_SWAP_ALWAYS); }
void test_swap_data(void) { int ia = 5; int ib = 3; if (ia > ib) { swap_data(&ia, &ib); } }
BBitmap *GetCicnFromResource(const char *theResource) { // Get application info app_info info; be_app->GetAppInfo(&info); BFile file(&info.ref, O_RDONLY); if (file.InitCheck()) return NULL; size_t size; cicn *icon; BResources res; status_t err; if ( (err = res.SetTo(&file)) != B_NO_ERROR ) return NULL; icon = (cicn *)res.FindResource('cicn', theResource, &size); if (!icon) return NULL; // Swap bytes if needed. We do this because the resources are currently // built on Macintosh BeOS if (B_HOST_IS_LENDIAN) { status_t retVal; retVal = swap_data(B_INT16_TYPE, &icon->width, sizeof(int16), B_SWAP_BENDIAN_TO_HOST); retVal = swap_data(B_INT16_TYPE, &icon->height, sizeof(int16), B_SWAP_BENDIAN_TO_HOST); } // Get cicn bounding rect BRect bounds(0, 0, icon->width-1, icon->height-1); // Load bitmap BBitmap *bitmap = new BBitmap(bounds, B_COLOR_8_BIT); ASSERT(bitmap); bitmap->SetBits(&icon->data, size - sizeof(int16)*2, 0, B_COLOR_8_BIT); return (bitmap); }
static void copy_frame(struct conversion_info *ci_ptr) { long start[5]; long count[5]; size_t nitems; message(MSG_INFO, "Inserting frame #%d\n", ci_ptr->frame_index); /* Actually read the data from the image file. */ nitems = fread(ci_ptr->frame_buffer, ci_ptr->frame_nbytes, 1, ci_ptr->img_fp); if (nitems != 1) { message(MSG_FATAL, "Read failed with error %d, return %d\n", errno, nitems); exit(-1); } /* Setup the starts and counts for the data block. */ start[DIM_T] = ci_ptr->frame_index - ci_ptr->frame_zero; start[DIM_X] = 0; start[DIM_Y] = 0; start[DIM_Z] = 0; start[DIM_W] = 0; count[DIM_T] = 1; count[DIM_X] = ci_ptr->dim_lengths[DIM_X]; count[DIM_Y] = ci_ptr->dim_lengths[DIM_Y]; count[DIM_Z] = ci_ptr->dim_lengths[DIM_Z]; count[DIM_W] = ci_ptr->dim_lengths[DIM_W]; /* Perform swapping if necessary. */ if (ci_ptr->swap_size != 0) { swap_data(ci_ptr->swap_size, ci_ptr->frame_nvoxels, ci_ptr->frame_buffer); } /* Scale the raw data into the final range. */ scale_data(ci_ptr->minc_type, ci_ptr->frame_nvoxels, ci_ptr->frame_buffer, COMBINED_SCALE_FACTOR(ci_ptr)); /* For now we perform no conversions on the data as it is stored. * This may be worth modifying in the future, to allow storage of * non-floating-point formats from a typical microPET file. */ ncvarput(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, MIimage), start, count, ci_ptr->frame_buffer); }
void partition(data_t A[], int n, data_t *pivot, int *first_eq, int *first_gt) { int next=0, fe=0, fg=n, outcome; while (next<fg) { if ((outcome = cmp(A+next, pivot)) < 0) { swap_data(A+fe, A+next); fe += 1; next += 1; } else if (outcome > 0) { fg -= 1; swap_data(A+next, A+fg); } else { next += 1; } } assert(0<=fe && fe<fg && fg<=n); *first_eq = fe; *first_gt = fg; return; }
void swap_bytes_unit(void *buff, unsigned int unit_num, short unit_size) { unsigned int i, j; unit_num--; // to last index. unsigned char *arr = (unsigned char *)buff; for (i = 0; i < unit_num; i++, unit_num--) { for (j = 0; j < unit_size; j++) swap_data(arr[i*unit_size +j], arr[unit_num*unit_size +j]); } }
int ICNSLoader::GetIcon(BPositionIO *target, int index) { if (index < 1 || index > fIconsCount || !fLoaded) return B_NO_TRANSLATOR; icns_image_t iconImage; memset(&iconImage, 0, sizeof(icns_image_t)); icns_type_t typeItem = *((icns_type_t*)fFormatList.ItemAt(index - 1)); int status = icns_get_image32_with_mask_from_family(fIconFamily, typeItem, &iconImage); if (status != 0) return B_NO_TRANSLATOR; TranslatorBitmap bitsHeader; bitsHeader.magic = B_TRANSLATOR_BITMAP; bitsHeader.bounds.left = 0; bitsHeader.bounds.top = 0; bitsHeader.bounds.right = iconImage.imageWidth - 1; bitsHeader.bounds.bottom = iconImage.imageHeight - 1; bitsHeader.rowBytes = sizeof(uint32) * iconImage.imageWidth; bitsHeader.colors = B_RGBA32; bitsHeader.dataSize = bitsHeader.rowBytes * iconImage.imageHeight; if (swap_data(B_UINT32_TYPE, &bitsHeader, sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) != B_OK) { icns_free_image(&iconImage); return B_NO_TRANSLATOR; } target->Write(&bitsHeader, sizeof(TranslatorBitmap)); uint8 *rowBuff = new uint8[iconImage.imageWidth * sizeof(uint32)]; for (uint32 i = 0; i < iconImage.imageHeight; i++) { uint8 *rowData = iconImage.imageData + (i * iconImage.imageWidth * sizeof(uint32)); uint8 *rowBuffPtr = rowBuff; for (uint32 j=0; j < iconImage.imageWidth; j++) { rowBuffPtr[0] = rowData[2]; rowBuffPtr[1] = rowData[1]; rowBuffPtr[2] = rowData[0]; rowBuffPtr[3] = rowData[3]; rowBuffPtr += sizeof(uint32); rowData += sizeof(uint32); } target->Write(rowBuff, iconImage.imageWidth * sizeof(uint32)); } delete rowBuff; icns_free_image(&iconImage); return B_OK; }
void quick_sort(void* Array, size_t size_of_array, size_t size_of_data, int (*compare_function)(const void*, const void*)) { if(size_of_array <= 1) return; else { int i; for(i = 0; i < size_of_array; i++) printf("%d ", *((int*)(Array + size_of_data * i))); printf("\n"); void* begin_iterator = Array; void* end_iterator = Array + (size_of_array - 1) * size_of_data; void* array_end = end_iterator; void* split_data = malloc(size_of_data); memcpy(split_data, begin_iterator, size_of_data); printf("begin = %d, end = %d, split = %d\n", *(int*)begin_iterator, *(int*)end_iterator, *(int*)split_data); while(begin_iterator < end_iterator) { while(compare_function(begin_iterator, split_data) < 0) begin_iterator += size_of_data; while(compare_function(end_iterator, split_data) > 0) end_iterator -= size_of_data; printf("begin = %d, end = %d\n", *(int*)begin_iterator, *(int*)end_iterator); if(begin_iterator < end_iterator) { printf("swap\n"); swap_data(begin_iterator, end_iterator, size_of_data); begin_iterator += size_of_data; end_iterator -= size_of_data; for(i = 0; i < size_of_array; i++) printf("%d ", *((int*)(Array) + i)); printf("\n"); } } free(split_data); printf("begin = %d, end = %d\n", *(int*)begin_iterator, *(int*)end_iterator); printf("---------------------------------\n"); if(Array < end_iterator + size_of_data) quick_sort(Array, (end_iterator - Array) / size_of_data + 1, size_of_data, compare_function); if(begin_iterator + size_of_data < Array + size_of_array * size_of_data) quick_sort(begin_iterator, (size_of_array - 1) - (begin_iterator - Array) / size_of_data, size_of_data, compare_function); } }
void straight_selection_sort(int a[], int num) { int i=0,j=0,index=0; for(i=0;i<num;i++) { index = i; for(j=i+1;j<num;j++) { if(a[j]<a[index])index = j; } swap_data(&a[i],&a[index]); } }
void heap_sort(int a[], int num) { int i = 0; for(i=(num/2-1);i>=0;i--) { heap_fix_down(a,i,num); } for(i=num-1;i>=1;i--) { swap_data(&a[i],&a[0]); heap_fix_down(a,0,i); } }
void TiffUintField::LoadShort(IFDEntry &entry, BPositionIO &io, swap_action swp) { // Make certain there is enough memory // before trying to do anything else fpShort = new uint16[entry.count]; if (!fpShort) { finitStatus = B_NO_MEMORY; return; } if (entry.count <= 2) { // If all of the byte values can fit into the // IFD entry value bytes memcpy(fpShort, entry.shortvals, entry.count * 2); finitStatus = B_OK; } else { // entry.count > 2, use longval to find offset for short data if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK) finitStatus = B_ERROR; else { ssize_t read; read = io.ReadAt(entry.longval, fpShort, entry.count * 2); if (read != static_cast<ssize_t>(entry.count) * 2) finitStatus = B_IO_ERROR; else finitStatus = B_OK; } } // If short values were successfully read in, swap them to // the correct byte order if (finitStatus == B_OK && swap_data(B_UINT16_TYPE, fpShort, entry.count * 2, swp) != B_OK) finitStatus = B_ERROR; }
void chenge_queen(int* queens,int* conf,int num) { // num番目の列にあるクイーンを移動する // conf[i]:num番目の列にあるクイーンがi行目に移動したときの衝突数 int move = get_min(conf);//どの行に移動すると衝突数が小さくなるか判定 int temp,i; // //ここにプログラムを書きくわえる // swap_data(&queens[num],&queens[temp]);//num列目とtemp列目のクイーンの位置を交換 }
void TiffUintField::LoadLong(IFDEntry &entry, BPositionIO &io, swap_action swp) { // Make certain there is enough memory // before trying to do anything else fpLong = new uint32[entry.count]; if (!fpLong) { finitStatus = B_NO_MEMORY; return; } if (entry.count == 1) { fpLong[0] = entry.longval; finitStatus = B_OK; } else { // entry.count > 1, use longval to find offset for long data if (swap_data(B_UINT32_TYPE, &entry.longval, 4, swp) != B_OK) finitStatus = B_ERROR; else { ssize_t read; read = io.ReadAt(entry.longval, fpLong, entry.count * 4); if (read != static_cast<ssize_t>(entry.count) * 4) finitStatus = B_IO_ERROR; else finitStatus = B_OK; } } // If long values were successfully read in, swap them to // the correct byte order if (finitStatus == B_OK && swap_data(B_UINT32_TYPE, fpLong, entry.count * 4, swp) != B_OK) finitStatus = B_ERROR; }
// --------------------------------------------------------------- // translate_from_bits_to_bits // // Convert the data in inSource from the Be Bitmap format ('bits') // to the format specified in outType (either bits or Base). // // Preconditions: // // Parameters: inSource, the bits data to translate // // amtread, the amount of data already read from // inSource // // read, pointer to the data already read from // inSource // // outType, the type of data to convert to // // outDestination, where the output is written to // // Postconditions: // // Returns: B_NO_TRANSLATOR, if the data is not in a supported // format // // B_ERROR, if there was an error allocating memory or some other // error // // B_OK, if successfully translated the data from the bits format // --------------------------------------------------------------- status_t BaseTranslator::translate_from_bits_to_bits(BPositionIO *inSource, uint32 outType, BPositionIO *outDestination) { TranslatorBitmap bitsHeader; bool bheaderonly = false, bdataonly = false; status_t result; result = identify_bits_header(inSource, NULL, &bitsHeader); if (result != B_OK) return result; // Translate B_TRANSLATOR_BITMAP to B_TRANSLATOR_BITMAP, easy enough :) if (outType == B_TRANSLATOR_BITMAP) { // write out bitsHeader (only if configured to) if (bheaderonly || (!bheaderonly && !bdataonly)) { if (swap_data(B_UINT32_TYPE, &bitsHeader, sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) != B_OK) return B_ERROR; if (outDestination->Write(&bitsHeader, sizeof(TranslatorBitmap)) != sizeof(TranslatorBitmap)) return B_ERROR; } // write out the data (only if configured to) if (bdataonly || (!bheaderonly && !bdataonly)) { uint8 buf[1024]; uint32 remaining = B_BENDIAN_TO_HOST_INT32(bitsHeader.dataSize); ssize_t rd, writ; rd = inSource->Read(buf, 1024); while (rd > 0) { writ = outDestination->Write(buf, rd); if (writ < 0) break; remaining -= static_cast<uint32>(writ); rd = inSource->Read(buf, min(1024, remaining)); } if (remaining > 0) return B_ERROR; else return B_OK; } else return B_OK; } else return B_NO_TRANSLATOR; }
void sift_down(data_t A[], int parent, int n) { int child; if ((child = 2*parent+1) < n) { /* there is at least one child to be checked */ if (child+1<n && cmp(A+child, A+child+1)<0) { /* the right child exists, and is larger */ child += 1; } if (cmp(A+parent, A+child)<0) { /* parent is smaller than larger child */ swap_data(A+parent, A+child); sift_down(A, child, n); } } }
void* thread_update_rrd(void* args) { data_collector_t *datac; rrd_update_args_t arg = *(rrd_update_args_t*)args; for(;;) { sleep(RRD_UPDATE_STEP-1); datac = swap_data(); sleep(1); update_rrd(arg.in_rrdfile, datac->last_up, &datac->data_in); // fprintf(stderr,"updata\n"); // if (arg.inout_traffic) // update_rrd(arg.out_rrdfile, datac->last_up, &datac->data_out); } return NULL; }
void init_queens(int* queens)//クイーンをランダムに配置。但し、同じ行に2つ以上のクイーンがあってはいけない { int i; // 重複がないように配列 queens の中身を初期化 for(i=0;i<nq;i++) { queens[i] = i; } // 0≦x<nq, 0≦y<nq でランダムに queens[x], queens[y] を並び替え for(i=0;i<nq;i++) { swap_data(&queens[random(nq)], &queens[random(nq)]); } }
void swap_elems(struct LList *list, int idx1, int idx2){ struct LNode* hold1 = list->root; struct LNode* hold2 = list->root; int count = 0; while(count < idx1){ hold1 = hold1->next; count++; } count = 0; while(count < idx2){ hold2 = hold2->next; count++; } swap_data(hold1->data, hold2->data); }
static siblings_idx sort_impl(Tree& t, siblings_idx s, DataSwap&& data_swap) noexcept { siblings_idx should = s; for (auto n : t.nodes(s) | t.with_children()) { ++should; siblings_idx c_sg = t.children_group(n); if (c_sg != should) { t.swap(c_sg, should); swap_data(t, c_sg, should, data_swap); should = sort_impl(t, should, data_swap); } else { should = sort_impl(t, c_sg, data_swap); } } return should; }
static void swap_sample_byte_order(void* buffer, uint32 format, size_t length) { type_code type = B_ANY_TYPE; switch (format) { case media_raw_audio_format::B_AUDIO_FLOAT: type = B_FLOAT_TYPE; break; case media_raw_audio_format::B_AUDIO_INT: type = B_INT32_TYPE; break; case media_raw_audio_format::B_AUDIO_SHORT: type = B_INT16_TYPE; break; case media_raw_audio_format::B_AUDIO_UCHAR: break; case media_raw_audio_format::B_AUDIO_CHAR: break; } if (type != B_ANY_TYPE) swap_data(type, buffer, length, B_SWAP_ALWAYS); }