File indexing completed on 2024-05-05 05:44:23
0001 /* 0002 This file is part of KCachegrind. 0003 0004 SPDX-FileCopyrightText: 2004-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-only 0007 */ 0008 0009 0010 #include "subcost.h" 0011 #include "tracedata.h" 0012 0013 #include <QString> 0014 0015 0016 //--------------------------------------------------- 0017 // SubCost 0018 0019 bool SubCost::set(const char** ps) 0020 { 0021 const char* s = *ps; 0022 if (!s || (*s < '0') || (*s > '9')) return false; 0023 0024 v = *s - '0'; 0025 s++; 0026 while(*s >= '0' && *s <= '9') { 0027 v = 10* v + (*s-'0'); 0028 s++; 0029 } 0030 while(*s == ' ') s++; 0031 *ps = s; 0032 0033 return true; 0034 } 0035 0036 QString SubCost::pretty(char sep) const 0037 { 0038 unsigned long long n = v; 0039 0040 if (n==0) return QStringLiteral("0"); 0041 0042 int i = 0; 0043 QString res; 0044 0045 while (n) { 0046 if ((i>0) && !(i%3)) res = sep + res; 0047 i++; 0048 res = QChar('0'+int(n%10)) + res; 0049 n /= 10; 0050 } 0051 return res; 0052 } 0053 0054 0055 0056 0057 // HighestCostList 0058 0059 HighestCostList::HighestCostList() 0060 { 0061 _costType = nullptr; 0062 clear(1); 0063 } 0064 0065 void HighestCostList::clear(int maxSize) 0066 { 0067 _maxSize = maxSize; 0068 _count = 0; 0069 _item.resize(maxSize); 0070 _cost.resize(maxSize); 0071 } 0072 0073 void HighestCostList::addCost(ProfileCostArray* c, SubCost cost) 0074 { 0075 int i; 0076 0077 if (_maxSize == 0) return; 0078 0079 _count++; 0080 if (_count > _maxSize) { 0081 if (_cost[_maxSize-1] >= cost) return; 0082 i = _maxSize-1; 0083 } 0084 else i = _count-1; 0085 0086 for(; i>0; i--) { 0087 if (_cost[i-1] >= cost) break; 0088 else { 0089 _cost[i] = _cost[i-1]; 0090 _item[i] = _item[i-1]; 0091 } 0092 } 0093 _cost[i] = cost; 0094 _item[i] = c; 0095 } 0096 0097