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