github.com/whatap/golib@v0.0.22/util/list/DoubleList.go (about) 1 package list 2 3 import ( 4 //"bytes" 5 "fmt" 6 "math" 7 "sort" 8 "strconv" 9 "sync" 10 11 "github.com/whatap/golib/io" 12 "github.com/whatap/golib/lang/value" 13 "github.com/whatap/golib/util/compare" 14 ) 15 16 type DoubleList struct { 17 size int 18 table []float64 19 lock sync.Mutex 20 } 21 22 func NewDoubleListDefault() *DoubleList { 23 o := new(DoubleList) 24 o.table = make([]float64, 0) 25 return o 26 } 27 28 func NewDoubleList(initialCapa int) *DoubleList { 29 o := new(DoubleList) 30 o.table = make([]float64, initialCapa) 31 return o 32 } 33 func (this *DoubleList) GetType() byte { 34 return ANYLIST_DOUBLE 35 } 36 37 func (this *DoubleList) ensure(minCapacity int) { 38 if minCapacity > len(this.table) { 39 if this.table == nil { 40 minCapacity = int(math.Min(float64(ANYLIST_DEFAULT_CAPACITY), float64(minCapacity))) 41 } 42 oldSize := len(this.table) 43 newSize := oldSize + (oldSize >> 1) 44 if newSize < minCapacity { 45 newSize = minCapacity 46 } 47 if newSize > ANYLIST_MAX_SIZE { 48 panic("too big size") 49 } 50 51 newTable := make([]float64, newSize) 52 copy(newTable, this.table) 53 this.table = newTable 54 } 55 } 56 57 func (this *DoubleList) get(i int) float64 { 58 if i >= this.size { 59 panic(fmt.Sprintln("Index out of bounds ", "Index: ", i, ", Size: ", this.size)) 60 } 61 return this.table[i] 62 } 63 64 func (this *DoubleList) set(i int, v float64) float64 { 65 if i >= this.size { 66 panic(fmt.Sprintln("Index out of bounds Index: ", i, ", Size: ", this.size)) 67 } 68 69 ov := this.table[i] 70 this.table[i] = v 71 return ov 72 } 73 74 func (this *DoubleList) remove(i int) float64 { 75 if i >= this.size { 76 panic(fmt.Sprintln("Index out of bounds Index: ", i, ", Size: ", this.size)) 77 } 78 ov := this.table[i] 79 80 movNum := this.size - i - 1 81 if movNum > 0 { 82 copy(this.table[i:i+movNum], this.table[i+1:i+movNum]) 83 //System.arraycopy(table, i + 1, table, i, movNum); 84 } 85 //table[--size] = 0; 86 last := this.size - 1 87 this.table[last] = 0 88 return ov 89 } 90 91 func (this *DoubleList) add(e float64) { 92 this.ensure(this.size + 1) 93 //table[size++] = e; 94 this.table[this.size] = e 95 this.size++ 96 } 97 98 func (this *DoubleList) AddAll(other *DoubleList) { 99 this.ensure(this.size + other.size) 100 for i := 0; i < other.size; i++ { 101 //this.table[this.size++] = other.table[i]; 102 this.table[this.size] = other.table[i] 103 this.size++ 104 } 105 } 106 107 func (this *DoubleList) AddAllArray(other []float64) { 108 this.ensure(this.size + len(other)) 109 n := len(other) 110 for i := 0; i < n; i++ { 111 //this.table[this.size++] = other[i]; 112 this.table[this.size] = other[i] 113 this.size++ 114 } 115 } 116 117 func (this *DoubleList) ToArray() []float64 { 118 newArray := make([]float64, this.size) 119 copy(newArray, this.table) 120 return newArray 121 } 122 123 func (this *DoubleList) ToString() string { 124 return fmt.Sprintf("%v", this.table) 125 } 126 127 func (this *DoubleList) Sort() { 128 if this.size > 0 { 129 //Arrays.sort(this.table, 0, size-1) 130 //sort.Ints(this.table) 131 } 132 } 133 134 func (this *DoubleList) SetInt(i, v int) { 135 this.set(i, float64(v)) 136 } 137 func (this *DoubleList) SetFloat(i int, v float32) { 138 this.set(i, float64(v)) 139 } 140 func (this *DoubleList) SetLong(i int, v int64) { 141 this.set(i, float64(v)) 142 } 143 func (this *DoubleList) SetDouble(i int, v float64) { 144 this.set(i, v) 145 } 146 func (this *DoubleList) SetString(i int, v string) { 147 n, err := strconv.ParseFloat(v, 32) 148 if err == nil { 149 this.set(i, float64(n)) 150 } else { 151 panic(" Parse error string(" + v + ") to float64") 152 } 153 154 } 155 156 func (this *DoubleList) AddInt(v int) { 157 this.add(float64(v)) 158 } 159 func (this *DoubleList) AddFloat(v float32) { 160 this.add(float64(v)) 161 } 162 func (this *DoubleList) AddLong(v int64) { 163 this.add(float64(v)) 164 } 165 func (this *DoubleList) AddDouble(v float64) { 166 this.add(v) 167 } 168 func (this *DoubleList) AddString(v string) { 169 170 n, err := strconv.ParseFloat(v, 64) 171 if err == nil { 172 this.add(n) 173 } else { 174 panic(" Parse error string(" + v + ") to float64") 175 } 176 } 177 178 func (this *DoubleList) GetInt(i int) int { 179 return int(this.get(i)) 180 } 181 func (this *DoubleList) GetFloat(i int) float32 { 182 return float32(this.get(i)) 183 } 184 func (this *DoubleList) GetDouble(i int) float64 { 185 return this.get(i) 186 } 187 func (this *DoubleList) GetLong(i int) int64 { 188 return int64(this.get(i)) 189 } 190 func (this *DoubleList) GetString(i int) string { 191 return strconv.FormatFloat(this.get(i), 'f', 6, 64) 192 //return fmt.Sprintf("%.6f", this.get(i)) 193 } 194 func (this *DoubleList) GetObject(i int) interface{} { 195 return nil 196 } 197 func (this *DoubleList) GetValue(i int) value.Value { 198 return value.NewDoubleValue(this.get(i)) 199 } 200 201 func (this *DoubleList) Write(out *io.DataOutputX) { 202 out.WriteInt3(int32(this.Size())) 203 for i := 0; i < this.size; i++ { 204 out.WriteDouble(this.get(i)) 205 } 206 } 207 func (this *DoubleList) Read(in *io.DataInputX) { 208 count := int(in.ReadInt3()) 209 for i := 0; i < count; i++ { 210 this.add(in.ReadDouble()) 211 } 212 } 213 214 func (this *DoubleList) Size() int { 215 return this.size 216 } 217 218 func (this *DoubleList) Sorting(asc bool) []int { 219 this.lock.Lock() 220 defer this.lock.Unlock() 221 222 table := make([]*DoubleListKeyVal, this.size) 223 for i := 0; i < this.size; i++ { 224 table[i] = &DoubleListKeyVal{i, this.get(i)} 225 } 226 227 c := func(o1, o2 *DoubleListKeyVal) bool { 228 if asc { 229 if compare.CompareToDouble(o1.value, o2.value) > 0 { 230 return false 231 } else { 232 return true 233 } 234 } else { 235 if compare.CompareToDouble(o2.value, o1.value) > 0 { 236 return false 237 } else { 238 return true 239 } 240 } 241 } 242 243 sort.Sort(DoubleListSortable{compare: c, data: table}) 244 245 out := make([]int, this.size) 246 for i := 0; i < this.size; i++ { 247 out[i] = table[i].key 248 } 249 return out 250 } 251 func (this *DoubleList) SortingAnyList(asc bool, child AnyList, childAsc bool) []int { 252 this.lock.Lock() 253 defer this.lock.Unlock() 254 255 table := make([]*DoubleListKeyVal, this.size) 256 for i := 0; i < this.size; i++ { 257 table[i] = &DoubleListKeyVal{i, this.get(i)} 258 } 259 260 c := func(o1, o2 *DoubleListKeyVal) bool { 261 var rt int 262 if asc { 263 rt = compare.CompareToDouble(o1.value, o2.value) 264 } else { 265 rt = compare.CompareToDouble(o2.value, o1.value) 266 } 267 268 if rt != 0 { 269 if rt > 0 { 270 return false 271 } else { 272 return true 273 } 274 } else { 275 rt = CompareChild(child, childAsc, o1.key, o2.key) 276 if rt > 0 { 277 return false 278 } else { 279 return true 280 } 281 } 282 } 283 284 sort.Sort(DoubleListSortable{compare: c, data: table}) 285 286 out := make([]int, this.size) 287 for i := 0; i < this.size; i++ { 288 out[i] = table[i].key 289 } 290 return out 291 } 292 func (this *DoubleList) Filtering(index []int) AnyList { 293 out := NewDoubleList(this.size) 294 sz := len(index) 295 for i := 0; i < sz; i++ { 296 out.add(this.get(index[i])) 297 } 298 return out 299 } 300 301 type DoubleListSortable struct { 302 compare func(a, b *DoubleListKeyVal) bool 303 data []*DoubleListKeyVal 304 } 305 306 func (this DoubleListSortable) Len() int { 307 return len(this.data) 308 } 309 310 func (this DoubleListSortable) Less(i, j int) bool { 311 return this.compare(this.data[i], this.data[j]) 312 } 313 314 func (this DoubleListSortable) Swap(i, j int) { 315 this.data[i], this.data[j] = this.data[j], this.data[i] 316 } 317 318 type DoubleListKeyVal struct { 319 key int 320 value float64 321 } 322 323 func DoubleListMain() { 324 list := NewDoubleList(30) 325 326 for i := 0; i < 30; i++ { 327 list.AddInt(i) 328 } 329 330 fmt.Println("\nlist") 331 for i := 0; i < list.Size(); i++ { 332 fmt.Printf("%d ", list.GetInt(i)) 333 } 334 335 fmt.Println("\nlist Sorting ASC") 336 ascList := list.Sorting(true) 337 338 for i := 0; i < len(ascList); i++ { 339 fmt.Printf("%d ", ascList[i]) 340 } 341 342 fmt.Println("\nlist Sorting DESC") 343 descList := list.Sorting(false) 344 345 for i := 0; i < len(descList); i++ { 346 fmt.Printf("%d ", descList[i]) 347 } 348 349 fmt.Println("\nlist Filtering") 350 var listFilter = make([]int, 30) 351 352 var fList = [30]int{3, 2, 1, 0, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 29, 28, 27, 26, 25, 24, 23, 22, 21} 353 354 for i := 0; i < len(fList); i++ { 355 listFilter[i] = fList[i] 356 } 357 aList := list.Filtering(listFilter) 358 359 for i := 0; i < aList.Size(); i++ { 360 fmt.Printf("%d ", aList.GetInt(i)) 361 } 362 363 }