github.com/whatap/golib@v0.0.22/util/list/StringList.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 StringList struct { 17 size int 18 table []string 19 lock sync.Mutex 20 } 21 22 func NewStringListDefault() *StringList { 23 o := new(StringList) 24 o.table = make([]string, 0) 25 return o 26 } 27 28 func NewStringList(initialCapa int) *StringList { 29 o := new(StringList) 30 o.table = make([]string, initialCapa) 31 return o 32 } 33 34 func (this *StringList) GetType() byte { 35 return ANYLIST_STRING 36 } 37 38 func (this *StringList) 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([]string, newSize) 53 copy(newTable, this.table) 54 this.table = newTable 55 } 56 } 57 58 func (this *StringList) get(i int) string { 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 *StringList) set(i int, v string) string { 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 *StringList) remove(i int) string { 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] = "" 89 return ov 90 } 91 92 func (this *StringList) add(e string) { 93 this.ensure(this.size + 1) 94 //table[size++] = e; 95 this.table[this.size] = e 96 this.size++ 97 } 98 99 func (this *StringList) AddAll(other *StringList) { 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 *StringList) AddAllArray(other []string) { 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 *StringList) ToArray() []string { 119 newArray := make([]string, this.size) 120 copy(newArray, this.table) 121 return newArray 122 } 123 124 func (this *StringList) ToString() string { 125 return fmt.Sprintf("%v", this.table) 126 } 127 128 func (this *StringList) 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 *StringList) SetInt(i, v int) { 136 this.set(i, strconv.Itoa(v)) 137 } 138 func (this *StringList) SetFloat(i int, v float32) { 139 this.set(i, strconv.FormatFloat(float64(v), 'f', 6, 32)) 140 //this.set(i, fmt.Sprintf("%.6f", this.get(i))) 141 } 142 func (this *StringList) SetLong(i int, v int64) { 143 this.set(i, strconv.FormatInt(v, 10)) 144 } 145 func (this *StringList) SetDouble(i int, v float64) { 146 this.set(i, strconv.FormatFloat(v, 'f', 6, 64)) 147 //this.set(i, fmt.Sprintf("%.6f", this.get(i))) 148 } 149 func (this *StringList) SetString(i int, v string) { 150 this.set(i, v) 151 } 152 153 func (this *StringList) AddInt(v int) { 154 this.add(strconv.Itoa(v)) 155 } 156 func (this *StringList) AddFloat(v float32) { 157 this.add(strconv.FormatFloat(float64(v), 'f', 6, 32)) 158 //this.add(fmt.Sprintf("%.6f", this.get(i))) 159 } 160 func (this *StringList) AddLong(v int64) { 161 this.add(strconv.FormatInt(v, 10)) 162 } 163 func (this *StringList) AddDouble(v float64) { 164 this.add(strconv.FormatFloat(float64(v), 'f', 6, 64)) 165 //this.add(i, fmt.Sprintf("%.6f", this.get(i))) 166 } 167 func (this *StringList) AddString(v string) { 168 this.add(v) 169 } 170 171 func (this *StringList) GetInt(i int) int { 172 n, err := strconv.Atoi(this.get(i)) 173 if err == nil { 174 return n 175 } else { 176 panic(" Parse error string(" + this.get(i) + ") to int") 177 } 178 } 179 func (this *StringList) GetFloat(i int) float32 { 180 n, err := strconv.ParseFloat(this.get(i), 32) 181 if err == nil { 182 return float32(n) 183 } else { 184 panic(" Parse error string(" + this.get(i) + ") to float32") 185 } 186 } 187 func (this *StringList) GetDouble(i int) float64 { 188 n, err := strconv.ParseFloat(this.get(i), 64) 189 if err == nil { 190 return n 191 } else { 192 panic(" Parse error string(" + this.get(i) + ") to float64") 193 } 194 } 195 func (this *StringList) GetLong(i int) int64 { 196 n, err := strconv.ParseInt(this.get(i), 10, 64) 197 if err == nil { 198 return n 199 } else { 200 panic(" Parse error string(" + this.get(i) + ") to int64") 201 } 202 } 203 func (this *StringList) GetString(i int) string { 204 return this.get(i) 205 } 206 func (this *StringList) GetObject(i int) interface{} { 207 return nil 208 } 209 func (this *StringList) GetValue(i int) value.Value { 210 return value.NewTextValue(this.get(i)) 211 } 212 213 func (this *StringList) Write(out *io.DataOutputX) { 214 out.WriteInt3(int32(this.Size())) 215 for i := 0; i < this.size; i++ { 216 out.WriteText(this.get(i)) 217 } 218 } 219 func (this *StringList) Read(in *io.DataInputX) { 220 count := int(in.ReadInt3()) 221 for i := 0; i < count; i++ { 222 this.add(in.ReadText()) 223 } 224 } 225 226 func (this *StringList) Size() int { 227 return this.size 228 } 229 230 func (this *StringList) Sorting(asc bool) []int { 231 this.lock.Lock() 232 defer this.lock.Unlock() 233 234 table := make([]*StringListKeyVal, this.size) 235 for i := 0; i < this.size; i++ { 236 table[i] = &StringListKeyVal{i, this.get(i)} 237 } 238 239 c := func(o1, o2 *StringListKeyVal) bool { 240 if asc { 241 if compare.CompareToString(o1.value, o2.value) > 0 { 242 return false 243 } else { 244 return true 245 } 246 } else { 247 if compare.CompareToString(o2.value, o1.value) > 0 { 248 return false 249 } else { 250 return true 251 } 252 } 253 } 254 255 sort.Sort(StringListSortable{compare: c, data: table}) 256 257 out := make([]int, this.size) 258 for i := 0; i < this.size; i++ { 259 out[i] = table[i].key 260 } 261 return out 262 } 263 func (this *StringList) SortingAnyList(asc bool, child AnyList, childAsc bool) []int { 264 this.lock.Lock() 265 defer this.lock.Unlock() 266 267 table := make([]*StringListKeyVal, this.size) 268 for i := 0; i < this.size; i++ { 269 table[i] = &StringListKeyVal{i, this.get(i)} 270 } 271 272 c := func(o1, o2 *StringListKeyVal) bool { 273 var rt int 274 if asc { 275 rt = compare.CompareToString(o1.value, o2.value) 276 } else { 277 rt = compare.CompareToString(o2.value, o1.value) 278 } 279 280 if rt != 0 { 281 if rt > 0 { 282 return false 283 } else { 284 return true 285 } 286 } else { 287 rt = CompareChild(child, childAsc, o1.key, o2.key) 288 if rt > 0 { 289 return false 290 } else { 291 return true 292 } 293 } 294 } 295 296 sort.Sort(StringListSortable{compare: c, data: table}) 297 298 out := make([]int, this.size) 299 for i := 0; i < this.size; i++ { 300 out[i] = table[i].key 301 } 302 return out 303 } 304 func (this *StringList) Filtering(index []int) AnyList { 305 out := NewStringList(this.size) 306 sz := len(index) 307 for i := 0; i < sz; i++ { 308 out.add(this.get(index[i])) 309 } 310 return out 311 } 312 313 type StringListSortable struct { 314 compare func(a, b *StringListKeyVal) bool 315 data []*StringListKeyVal 316 } 317 318 func (this StringListSortable) Len() int { 319 return len(this.data) 320 } 321 322 func (this StringListSortable) Less(i, j int) bool { 323 return this.compare(this.data[i], this.data[j]) 324 } 325 326 func (this StringListSortable) Swap(i, j int) { 327 this.data[i], this.data[j] = this.data[j], this.data[i] 328 } 329 330 type StringListKeyVal struct { 331 key int 332 value string 333 } 334 335 func StringListMain() { 336 list := NewStringList(30) 337 338 for i := 0; i < 30; i++ { 339 list.AddInt(i) 340 } 341 342 fmt.Println("\nlist") 343 for i := 0; i < list.Size(); i++ { 344 fmt.Printf("%d ", list.GetInt(i)) 345 } 346 347 fmt.Println("\nlist Sorting ASC") 348 ascList := list.Sorting(true) 349 350 for i := 0; i < len(ascList); i++ { 351 fmt.Printf("%d ", ascList[i]) 352 } 353 354 fmt.Println("\nlist Sorting DESC") 355 descList := list.Sorting(false) 356 357 for i := 0; i < len(descList); i++ { 358 fmt.Printf("%d ", descList[i]) 359 } 360 361 fmt.Println("\nlist Filtering") 362 var listFilter = make([]int, 30) 363 364 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} 365 366 for i := 0; i < len(fList); i++ { 367 listFilter[i] = fList[i] 368 } 369 aList := list.Filtering(listFilter) 370 371 for i := 0; i < aList.Size(); i++ { 372 fmt.Printf("%d ", aList.GetInt(i)) 373 } 374 375 }